blob: 5b05ddbfa46fb04a35f23bf149e5cde28e98f02b [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äfbf49ea2013-10-11 14:21:31 +0300895static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
896{
897 struct drm_i915_private *dev_priv = dev->dev_private;
898 u32 reg = PIPEDSL(pipe);
899 u32 line1, line2;
900 u32 line_mask;
901
902 if (IS_GEN2(dev))
903 line_mask = DSL_LINEMASK_GEN2;
904 else
905 line_mask = DSL_LINEMASK_GEN3;
906
907 line1 = I915_READ(reg) & line_mask;
908 mdelay(5);
909 line2 = I915_READ(reg) & line_mask;
910
911 return line1 == line2;
912}
913
Keith Packardab7ad7f2010-10-03 00:33:06 -0700914/*
915 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300916 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700917 *
918 * After disabling a pipe, we can't wait for vblank in the usual way,
919 * spinning on the vblank interrupt status bit, since we won't actually
920 * see an interrupt when the pipe is disabled.
921 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700922 * On Gen4 and above:
923 * wait for the pipe register state bit to turn off
924 *
925 * Otherwise:
926 * wait for the display line value to settle (it usually
927 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100928 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700929 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300930static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700931{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300932 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700933 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300934 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
935 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700936
Keith Packardab7ad7f2010-10-03 00:33:06 -0700937 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200938 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700939
Keith Packardab7ad7f2010-10-03 00:33:06 -0700940 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100941 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
942 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200943 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700944 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700945 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300946 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200947 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700948 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800949}
950
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000951/*
952 * ibx_digital_port_connected - is the specified port connected?
953 * @dev_priv: i915 private structure
954 * @port: the port to test
955 *
956 * Returns true if @port is connected, false otherwise.
957 */
958bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
959 struct intel_digital_port *port)
960{
961 u32 bit;
962
Damien Lespiauc36346e2012-12-13 16:09:03 +0000963 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +0200964 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000965 case PORT_B:
966 bit = SDE_PORTB_HOTPLUG;
967 break;
968 case PORT_C:
969 bit = SDE_PORTC_HOTPLUG;
970 break;
971 case PORT_D:
972 bit = SDE_PORTD_HOTPLUG;
973 break;
974 default:
975 return true;
976 }
977 } else {
Robin Schroereba905b2014-05-18 02:24:50 +0200978 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000979 case PORT_B:
980 bit = SDE_PORTB_HOTPLUG_CPT;
981 break;
982 case PORT_C:
983 bit = SDE_PORTC_HOTPLUG_CPT;
984 break;
985 case PORT_D:
986 bit = SDE_PORTD_HOTPLUG_CPT;
987 break;
988 default:
989 return true;
990 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000991 }
992
993 return I915_READ(SDEISR) & bit;
994}
995
Jesse Barnesb24e7172011-01-04 15:09:30 -0800996static const char *state_string(bool enabled)
997{
998 return enabled ? "on" : "off";
999}
1000
1001/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001002void assert_pll(struct drm_i915_private *dev_priv,
1003 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001004{
1005 int reg;
1006 u32 val;
1007 bool cur_state;
1008
1009 reg = DPLL(pipe);
1010 val = I915_READ(reg);
1011 cur_state = !!(val & DPLL_VCO_ENABLE);
1012 WARN(cur_state != state,
1013 "PLL state assertion failure (expected %s, current %s)\n",
1014 state_string(state), state_string(cur_state));
1015}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001016
Jani Nikula23538ef2013-08-27 15:12:22 +03001017/* XXX: the dsi pll is shared between MIPI DSI ports */
1018static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1019{
1020 u32 val;
1021 bool cur_state;
1022
1023 mutex_lock(&dev_priv->dpio_lock);
1024 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1025 mutex_unlock(&dev_priv->dpio_lock);
1026
1027 cur_state = val & DSI_PLL_VCO_EN;
1028 WARN(cur_state != state,
1029 "DSI PLL state assertion failure (expected %s, current %s)\n",
1030 state_string(state), state_string(cur_state));
1031}
1032#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1033#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1034
Daniel Vetter55607e82013-06-16 21:42:39 +02001035struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001036intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001037{
Daniel Vettere2b78262013-06-07 23:10:03 +02001038 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1039
Daniel Vettera43f6e02013-06-07 23:10:32 +02001040 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001041 return NULL;
1042
Daniel Vettera43f6e02013-06-07 23:10:32 +02001043 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001044}
1045
Jesse Barnesb24e7172011-01-04 15:09:30 -08001046/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001047void assert_shared_dpll(struct drm_i915_private *dev_priv,
1048 struct intel_shared_dpll *pll,
1049 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001050{
Jesse Barnes040484a2011-01-03 12:14:26 -08001051 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001052 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001053
Chris Wilson92b27b02012-05-20 18:10:50 +01001054 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001055 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001056 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001057
Daniel Vetter53589012013-06-05 13:34:16 +02001058 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +01001059 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001060 "%s assertion failure (expected %s, current %s)\n",
1061 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001062}
Jesse Barnes040484a2011-01-03 12:14:26 -08001063
1064static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1065 enum pipe pipe, bool state)
1066{
1067 int reg;
1068 u32 val;
1069 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001070 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1071 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001072
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001073 if (HAS_DDI(dev_priv->dev)) {
1074 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001075 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001076 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001077 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001078 } else {
1079 reg = FDI_TX_CTL(pipe);
1080 val = I915_READ(reg);
1081 cur_state = !!(val & FDI_TX_ENABLE);
1082 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001083 WARN(cur_state != state,
1084 "FDI TX state assertion failure (expected %s, current %s)\n",
1085 state_string(state), state_string(cur_state));
1086}
1087#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1088#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1089
1090static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1091 enum pipe pipe, bool state)
1092{
1093 int reg;
1094 u32 val;
1095 bool cur_state;
1096
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001097 reg = FDI_RX_CTL(pipe);
1098 val = I915_READ(reg);
1099 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001100 WARN(cur_state != state,
1101 "FDI RX state assertion failure (expected %s, current %s)\n",
1102 state_string(state), state_string(cur_state));
1103}
1104#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1105#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1106
1107static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1108 enum pipe pipe)
1109{
1110 int reg;
1111 u32 val;
1112
1113 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001114 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001115 return;
1116
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001117 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001118 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001119 return;
1120
Jesse Barnes040484a2011-01-03 12:14:26 -08001121 reg = FDI_TX_CTL(pipe);
1122 val = I915_READ(reg);
1123 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1124}
1125
Daniel Vetter55607e82013-06-16 21:42:39 +02001126void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1127 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001128{
1129 int reg;
1130 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001131 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001132
1133 reg = FDI_RX_CTL(pipe);
1134 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001135 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1136 WARN(cur_state != state,
1137 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1138 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001139}
1140
Jesse Barnesea0760c2011-01-04 15:09:32 -08001141static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1142 enum pipe pipe)
1143{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001144 struct drm_device *dev = dev_priv->dev;
1145 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001146 u32 val;
1147 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001148 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001149
Jani Nikulabedd4db2014-08-22 15:04:13 +03001150 if (WARN_ON(HAS_DDI(dev)))
1151 return;
1152
1153 if (HAS_PCH_SPLIT(dev)) {
1154 u32 port_sel;
1155
Jesse Barnesea0760c2011-01-04 15:09:32 -08001156 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001157 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1158
1159 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1160 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1161 panel_pipe = PIPE_B;
1162 /* XXX: else fix for eDP */
1163 } else if (IS_VALLEYVIEW(dev)) {
1164 /* presumably write lock depends on pipe, not port select */
1165 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1166 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001167 } else {
1168 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001169 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1170 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001171 }
1172
1173 val = I915_READ(pp_reg);
1174 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001175 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001176 locked = false;
1177
Jesse Barnesea0760c2011-01-04 15:09:32 -08001178 WARN(panel_pipe == pipe && locked,
1179 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001180 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001181}
1182
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001183static void assert_cursor(struct drm_i915_private *dev_priv,
1184 enum pipe pipe, bool state)
1185{
1186 struct drm_device *dev = dev_priv->dev;
1187 bool cur_state;
1188
Paulo Zanonid9d82082014-02-27 16:30:56 -03001189 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001190 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001191 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001192 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001193
1194 WARN(cur_state != state,
1195 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1196 pipe_name(pipe), state_string(state), state_string(cur_state));
1197}
1198#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1199#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1200
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001201void assert_pipe(struct drm_i915_private *dev_priv,
1202 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001203{
1204 int reg;
1205 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001206 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001207 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1208 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001209
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001210 /* if we need the pipe quirk it must be always on */
1211 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1212 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001213 state = true;
1214
Imre Deakda7e29b2014-02-18 00:02:02 +02001215 if (!intel_display_power_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001216 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001217 cur_state = false;
1218 } else {
1219 reg = PIPECONF(cpu_transcoder);
1220 val = I915_READ(reg);
1221 cur_state = !!(val & PIPECONF_ENABLE);
1222 }
1223
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001224 WARN(cur_state != state,
1225 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001226 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001227}
1228
Chris Wilson931872f2012-01-16 23:01:13 +00001229static void assert_plane(struct drm_i915_private *dev_priv,
1230 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001231{
1232 int reg;
1233 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001234 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001235
1236 reg = DSPCNTR(plane);
1237 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001238 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1239 WARN(cur_state != state,
1240 "plane %c assertion failure (expected %s, current %s)\n",
1241 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001242}
1243
Chris Wilson931872f2012-01-16 23:01:13 +00001244#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1245#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1246
Jesse Barnesb24e7172011-01-04 15:09:30 -08001247static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1248 enum pipe pipe)
1249{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001250 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001251 int reg, i;
1252 u32 val;
1253 int cur_pipe;
1254
Ville Syrjälä653e1022013-06-04 13:49:05 +03001255 /* Primary planes are fixed to pipes on gen4+ */
1256 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001257 reg = DSPCNTR(pipe);
1258 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001259 WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001260 "plane %c assertion failure, should be disabled but not\n",
1261 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001262 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001263 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001264
Jesse Barnesb24e7172011-01-04 15:09:30 -08001265 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001266 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001267 reg = DSPCNTR(i);
1268 val = I915_READ(reg);
1269 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1270 DISPPLANE_SEL_PIPE_SHIFT;
1271 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001272 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1273 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001274 }
1275}
1276
Jesse Barnes19332d72013-03-28 09:55:38 -07001277static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1278 enum pipe pipe)
1279{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001280 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001281 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001282 u32 val;
1283
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001284 if (IS_VALLEYVIEW(dev)) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001285 for_each_sprite(pipe, sprite) {
1286 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001287 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001288 WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001289 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001290 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001291 }
1292 } else if (INTEL_INFO(dev)->gen >= 7) {
1293 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001294 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001295 WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001296 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001297 plane_name(pipe), pipe_name(pipe));
1298 } else if (INTEL_INFO(dev)->gen >= 5) {
1299 reg = DVSCNTR(pipe);
1300 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001301 WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001302 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1303 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001304 }
1305}
1306
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001307static void assert_vblank_disabled(struct drm_crtc *crtc)
1308{
1309 if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1310 drm_crtc_vblank_put(crtc);
1311}
1312
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001313static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001314{
1315 u32 val;
1316 bool enabled;
1317
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001318 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001319
Jesse Barnes92f25842011-01-04 15:09:34 -08001320 val = I915_READ(PCH_DREF_CONTROL);
1321 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1322 DREF_SUPERSPREAD_SOURCE_MASK));
1323 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1324}
1325
Daniel Vetterab9412b2013-05-03 11:49:46 +02001326static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1327 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001328{
1329 int reg;
1330 u32 val;
1331 bool enabled;
1332
Daniel Vetterab9412b2013-05-03 11:49:46 +02001333 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001334 val = I915_READ(reg);
1335 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001336 WARN(enabled,
1337 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1338 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001339}
1340
Keith Packard4e634382011-08-06 10:39:45 -07001341static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1342 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001343{
1344 if ((val & DP_PORT_EN) == 0)
1345 return false;
1346
1347 if (HAS_PCH_CPT(dev_priv->dev)) {
1348 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1349 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1350 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1351 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001352 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1353 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1354 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001355 } else {
1356 if ((val & DP_PIPE_MASK) != (pipe << 30))
1357 return false;
1358 }
1359 return true;
1360}
1361
Keith Packard1519b992011-08-06 10:35:34 -07001362static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1363 enum pipe pipe, u32 val)
1364{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001365 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001366 return false;
1367
1368 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001369 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001370 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001371 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1372 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1373 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001374 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001375 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001376 return false;
1377 }
1378 return true;
1379}
1380
1381static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1382 enum pipe pipe, u32 val)
1383{
1384 if ((val & LVDS_PORT_EN) == 0)
1385 return false;
1386
1387 if (HAS_PCH_CPT(dev_priv->dev)) {
1388 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1389 return false;
1390 } else {
1391 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1392 return false;
1393 }
1394 return true;
1395}
1396
1397static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1398 enum pipe pipe, u32 val)
1399{
1400 if ((val & ADPA_DAC_ENABLE) == 0)
1401 return false;
1402 if (HAS_PCH_CPT(dev_priv->dev)) {
1403 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1404 return false;
1405 } else {
1406 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1407 return false;
1408 }
1409 return true;
1410}
1411
Jesse Barnes291906f2011-02-02 12:28:03 -08001412static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001413 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001414{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001415 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001416 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001417 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001418 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001419
Daniel Vetter75c5da22012-09-10 21:58:29 +02001420 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1421 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001422 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001423}
1424
1425static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1426 enum pipe pipe, int reg)
1427{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001428 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001429 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001430 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001431 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001432
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001433 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001434 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001435 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001436}
1437
1438static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1439 enum pipe pipe)
1440{
1441 int reg;
1442 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001443
Keith Packardf0575e92011-07-25 22:12:43 -07001444 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1445 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1446 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001447
1448 reg = PCH_ADPA;
1449 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001450 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001451 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001452 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001453
1454 reg = PCH_LVDS;
1455 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001456 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001457 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001458 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001459
Paulo Zanonie2debe92013-02-18 19:00:27 -03001460 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1461 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1462 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001463}
1464
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001465static void intel_init_dpio(struct drm_device *dev)
1466{
1467 struct drm_i915_private *dev_priv = dev->dev_private;
1468
1469 if (!IS_VALLEYVIEW(dev))
1470 return;
1471
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001472 /*
1473 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1474 * CHV x1 PHY (DP/HDMI D)
1475 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1476 */
1477 if (IS_CHERRYVIEW(dev)) {
1478 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1479 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1480 } else {
1481 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1482 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001483}
1484
Daniel Vetter426115c2013-07-11 22:13:42 +02001485static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001486{
Daniel Vetter426115c2013-07-11 22:13:42 +02001487 struct drm_device *dev = crtc->base.dev;
1488 struct drm_i915_private *dev_priv = dev->dev_private;
1489 int reg = DPLL(crtc->pipe);
1490 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001491
Daniel Vetter426115c2013-07-11 22:13:42 +02001492 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001493
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001494 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001495 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1496
1497 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001498 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001499 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001500
Daniel Vetter426115c2013-07-11 22:13:42 +02001501 I915_WRITE(reg, dpll);
1502 POSTING_READ(reg);
1503 udelay(150);
1504
1505 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1506 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1507
1508 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1509 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001510
1511 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001512 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001513 POSTING_READ(reg);
1514 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001515 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001516 POSTING_READ(reg);
1517 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001518 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001519 POSTING_READ(reg);
1520 udelay(150); /* wait for warmup */
1521}
1522
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001523static void chv_enable_pll(struct intel_crtc *crtc)
1524{
1525 struct drm_device *dev = crtc->base.dev;
1526 struct drm_i915_private *dev_priv = dev->dev_private;
1527 int pipe = crtc->pipe;
1528 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001529 u32 tmp;
1530
1531 assert_pipe_disabled(dev_priv, crtc->pipe);
1532
1533 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1534
1535 mutex_lock(&dev_priv->dpio_lock);
1536
1537 /* Enable back the 10bit clock to display controller */
1538 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1539 tmp |= DPIO_DCLKP_EN;
1540 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1541
1542 /*
1543 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1544 */
1545 udelay(1);
1546
1547 /* Enable PLL */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001548 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001549
1550 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001551 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001552 DRM_ERROR("PLL %d failed to lock\n", pipe);
1553
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001554 /* not sure when this should be written */
1555 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1556 POSTING_READ(DPLL_MD(pipe));
1557
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001558 mutex_unlock(&dev_priv->dpio_lock);
1559}
1560
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001561static int intel_num_dvo_pipes(struct drm_device *dev)
1562{
1563 struct intel_crtc *crtc;
1564 int count = 0;
1565
1566 for_each_intel_crtc(dev, crtc)
1567 count += crtc->active &&
1568 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO);
1569
1570 return count;
1571}
1572
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001573static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001574{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001575 struct drm_device *dev = crtc->base.dev;
1576 struct drm_i915_private *dev_priv = dev->dev_private;
1577 int reg = DPLL(crtc->pipe);
1578 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001579
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001580 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001581
1582 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001583 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001584
1585 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001586 if (IS_MOBILE(dev) && !IS_I830(dev))
1587 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001588
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001589 /* Enable DVO 2x clock on both PLLs if necessary */
1590 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1591 /*
1592 * It appears to be important that we don't enable this
1593 * for the current pipe before otherwise configuring the
1594 * PLL. No idea how this should be handled if multiple
1595 * DVO outputs are enabled simultaneosly.
1596 */
1597 dpll |= DPLL_DVO_2X_MODE;
1598 I915_WRITE(DPLL(!crtc->pipe),
1599 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1600 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001601
1602 /* Wait for the clocks to stabilize. */
1603 POSTING_READ(reg);
1604 udelay(150);
1605
1606 if (INTEL_INFO(dev)->gen >= 4) {
1607 I915_WRITE(DPLL_MD(crtc->pipe),
1608 crtc->config.dpll_hw_state.dpll_md);
1609 } else {
1610 /* The pixel multiplier can only be updated once the
1611 * DPLL is enabled and the clocks are stable.
1612 *
1613 * So write it again.
1614 */
1615 I915_WRITE(reg, dpll);
1616 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001617
1618 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001619 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001620 POSTING_READ(reg);
1621 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001622 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001623 POSTING_READ(reg);
1624 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001625 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001626 POSTING_READ(reg);
1627 udelay(150); /* wait for warmup */
1628}
1629
1630/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001631 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001632 * @dev_priv: i915 private structure
1633 * @pipe: pipe PLL to disable
1634 *
1635 * Disable the PLL for @pipe, making sure the pipe is off first.
1636 *
1637 * Note! This is for pre-ILK only.
1638 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001639static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001640{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001641 struct drm_device *dev = crtc->base.dev;
1642 struct drm_i915_private *dev_priv = dev->dev_private;
1643 enum pipe pipe = crtc->pipe;
1644
1645 /* Disable DVO 2x clock on both PLLs if necessary */
1646 if (IS_I830(dev) &&
1647 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO) &&
1648 intel_num_dvo_pipes(dev) == 1) {
1649 I915_WRITE(DPLL(PIPE_B),
1650 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1651 I915_WRITE(DPLL(PIPE_A),
1652 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1653 }
1654
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001655 /* Don't disable pipe or pipe PLLs if needed */
1656 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1657 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001658 return;
1659
1660 /* Make sure the pipe isn't still relying on us */
1661 assert_pipe_disabled(dev_priv, pipe);
1662
Daniel Vetter50b44a42013-06-05 13:34:33 +02001663 I915_WRITE(DPLL(pipe), 0);
1664 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001665}
1666
Jesse Barnesf6071162013-10-01 10:41:38 -07001667static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1668{
1669 u32 val = 0;
1670
1671 /* Make sure the pipe isn't still relying on us */
1672 assert_pipe_disabled(dev_priv, pipe);
1673
Imre Deake5cbfbf2014-01-09 17:08:16 +02001674 /*
1675 * Leave integrated clock source and reference clock enabled for pipe B.
1676 * The latter is needed for VGA hotplug / manual detection.
1677 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001678 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001679 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001680 I915_WRITE(DPLL(pipe), val);
1681 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001682
1683}
1684
1685static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1686{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001687 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001688 u32 val;
1689
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001690 /* Make sure the pipe isn't still relying on us */
1691 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001692
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001693 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001694 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001695 if (pipe != PIPE_A)
1696 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1697 I915_WRITE(DPLL(pipe), val);
1698 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001699
1700 mutex_lock(&dev_priv->dpio_lock);
1701
1702 /* Disable 10bit clock to display controller */
1703 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1704 val &= ~DPIO_DCLKP_EN;
1705 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1706
Ville Syrjälä61407f62014-05-27 16:32:55 +03001707 /* disable left/right clock distribution */
1708 if (pipe != PIPE_B) {
1709 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1710 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1711 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1712 } else {
1713 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1714 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1715 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1716 }
1717
Ville Syrjäläd7520482014-04-09 13:28:59 +03001718 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001719}
1720
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001721void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1722 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001723{
1724 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001725 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001726
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001727 switch (dport->port) {
1728 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001729 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001730 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001731 break;
1732 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001733 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001734 dpll_reg = DPLL(0);
1735 break;
1736 case PORT_D:
1737 port_mask = DPLL_PORTD_READY_MASK;
1738 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001739 break;
1740 default:
1741 BUG();
1742 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001743
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001744 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001745 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001746 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001747}
1748
Daniel Vetterb14b1052014-04-24 23:55:13 +02001749static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1750{
1751 struct drm_device *dev = crtc->base.dev;
1752 struct drm_i915_private *dev_priv = dev->dev_private;
1753 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1754
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001755 if (WARN_ON(pll == NULL))
1756 return;
1757
Daniel Vetterb14b1052014-04-24 23:55:13 +02001758 WARN_ON(!pll->refcount);
1759 if (pll->active == 0) {
1760 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1761 WARN_ON(pll->on);
1762 assert_shared_dpll_disabled(dev_priv, pll);
1763
1764 pll->mode_set(dev_priv, pll);
1765 }
1766}
1767
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001768/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001769 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001770 * @dev_priv: i915 private structure
1771 * @pipe: pipe PLL to enable
1772 *
1773 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1774 * drives the transcoder clock.
1775 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001776static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001777{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001778 struct drm_device *dev = crtc->base.dev;
1779 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001780 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001781
Daniel Vetter87a875b2013-06-05 13:34:19 +02001782 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001783 return;
1784
1785 if (WARN_ON(pll->refcount == 0))
1786 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001787
Damien Lespiau74dd6922014-07-29 18:06:17 +01001788 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001789 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001790 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001791
Daniel Vettercdbd2312013-06-05 13:34:03 +02001792 if (pll->active++) {
1793 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001794 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001795 return;
1796 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001797 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001798
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001799 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1800
Daniel Vetter46edb022013-06-05 13:34:12 +02001801 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001802 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001803 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001804}
1805
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001806static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001807{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001808 struct drm_device *dev = crtc->base.dev;
1809 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001810 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001811
Jesse Barnes92f25842011-01-04 15:09:34 -08001812 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001813 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001814 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001815 return;
1816
Chris Wilson48da64a2012-05-13 20:16:12 +01001817 if (WARN_ON(pll->refcount == 0))
1818 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001819
Daniel Vetter46edb022013-06-05 13:34:12 +02001820 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1821 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001822 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001823
Chris Wilson48da64a2012-05-13 20:16:12 +01001824 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001825 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001826 return;
1827 }
1828
Daniel Vettere9d69442013-06-05 13:34:15 +02001829 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001830 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001831 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001832 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001833
Daniel Vetter46edb022013-06-05 13:34:12 +02001834 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001835 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001836 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001837
1838 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001839}
1840
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001841static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1842 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001843{
Daniel Vetter23670b322012-11-01 09:15:30 +01001844 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001845 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001846 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001847 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001848
1849 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001850 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001851
1852 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001853 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001854 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001855
1856 /* FDI must be feeding us bits for PCH ports */
1857 assert_fdi_tx_enabled(dev_priv, pipe);
1858 assert_fdi_rx_enabled(dev_priv, pipe);
1859
Daniel Vetter23670b322012-11-01 09:15:30 +01001860 if (HAS_PCH_CPT(dev)) {
1861 /* Workaround: Set the timing override bit before enabling the
1862 * pch transcoder. */
1863 reg = TRANS_CHICKEN2(pipe);
1864 val = I915_READ(reg);
1865 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1866 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001867 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001868
Daniel Vetterab9412b2013-05-03 11:49:46 +02001869 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001870 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001871 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001872
1873 if (HAS_PCH_IBX(dev_priv->dev)) {
1874 /*
1875 * make the BPC in transcoder be consistent with
1876 * that in pipeconf reg.
1877 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001878 val &= ~PIPECONF_BPC_MASK;
1879 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001880 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001881
1882 val &= ~TRANS_INTERLACE_MASK;
1883 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001884 if (HAS_PCH_IBX(dev_priv->dev) &&
1885 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1886 val |= TRANS_LEGACY_INTERLACED_ILK;
1887 else
1888 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001889 else
1890 val |= TRANS_PROGRESSIVE;
1891
Jesse Barnes040484a2011-01-03 12:14:26 -08001892 I915_WRITE(reg, val | TRANS_ENABLE);
1893 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001894 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001895}
1896
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001897static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001898 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001899{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001900 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001901
1902 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001903 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001904
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001905 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001906 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001907 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001908
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001909 /* Workaround: set timing override bit. */
1910 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001911 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001912 I915_WRITE(_TRANSA_CHICKEN2, val);
1913
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001914 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001915 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001916
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001917 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1918 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001919 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001920 else
1921 val |= TRANS_PROGRESSIVE;
1922
Daniel Vetterab9412b2013-05-03 11:49:46 +02001923 I915_WRITE(LPT_TRANSCONF, val);
1924 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001925 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001926}
1927
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001928static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1929 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001930{
Daniel Vetter23670b322012-11-01 09:15:30 +01001931 struct drm_device *dev = dev_priv->dev;
1932 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001933
1934 /* FDI relies on the transcoder */
1935 assert_fdi_tx_disabled(dev_priv, pipe);
1936 assert_fdi_rx_disabled(dev_priv, pipe);
1937
Jesse Barnes291906f2011-02-02 12:28:03 -08001938 /* Ports must be off as well */
1939 assert_pch_ports_disabled(dev_priv, pipe);
1940
Daniel Vetterab9412b2013-05-03 11:49:46 +02001941 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001942 val = I915_READ(reg);
1943 val &= ~TRANS_ENABLE;
1944 I915_WRITE(reg, val);
1945 /* wait for PCH transcoder off, transcoder state */
1946 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001947 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001948
1949 if (!HAS_PCH_IBX(dev)) {
1950 /* Workaround: Clear the timing override chicken bit again. */
1951 reg = TRANS_CHICKEN2(pipe);
1952 val = I915_READ(reg);
1953 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1954 I915_WRITE(reg, val);
1955 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001956}
1957
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001958static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001959{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001960 u32 val;
1961
Daniel Vetterab9412b2013-05-03 11:49:46 +02001962 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001963 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001964 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001965 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001966 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001967 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001968
1969 /* Workaround: clear timing override bit. */
1970 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001971 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001972 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001973}
1974
1975/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001976 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001977 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001978 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001979 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001980 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001981 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001982static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001983{
Paulo Zanoni03722642014-01-17 13:51:09 -02001984 struct drm_device *dev = crtc->base.dev;
1985 struct drm_i915_private *dev_priv = dev->dev_private;
1986 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001987 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1988 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001989 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001990 int reg;
1991 u32 val;
1992
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001993 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001994 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001995 assert_sprites_disabled(dev_priv, pipe);
1996
Paulo Zanoni681e5812012-12-06 11:12:38 -02001997 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001998 pch_transcoder = TRANSCODER_A;
1999 else
2000 pch_transcoder = pipe;
2001
Jesse Barnesb24e7172011-01-04 15:09:30 -08002002 /*
2003 * A pipe without a PLL won't actually be able to drive bits from
2004 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2005 * need the check.
2006 */
2007 if (!HAS_PCH_SPLIT(dev_priv->dev))
Paulo Zanonifbf32182014-01-17 13:51:11 -02002008 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002009 assert_dsi_pll_enabled(dev_priv);
2010 else
2011 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002012 else {
Paulo Zanoni30421c42014-01-17 13:51:10 -02002013 if (crtc->config.has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002014 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002015 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002016 assert_fdi_tx_pll_enabled(dev_priv,
2017 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002018 }
2019 /* FIXME: assert CPU port conditions for SNB+ */
2020 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002021
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002022 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002023 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002024 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002025 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2026 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002027 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002028 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002029
2030 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002031 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002032}
2033
2034/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002035 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002036 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002037 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002038 * Disable the pipe of @crtc, making sure that various hardware
2039 * specific requirements are met, if applicable, e.g. plane
2040 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002041 *
2042 * Will wait until the pipe has shut down before returning.
2043 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002044static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002045{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002046 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2047 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2048 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002049 int reg;
2050 u32 val;
2051
2052 /*
2053 * Make sure planes won't keep trying to pump pixels to us,
2054 * or we might hang the display.
2055 */
2056 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002057 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002058 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002059
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002060 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002061 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002062 if ((val & PIPECONF_ENABLE) == 0)
2063 return;
2064
Ville Syrjälä67adc642014-08-15 01:21:57 +03002065 /*
2066 * Double wide has implications for planes
2067 * so best keep it disabled when not needed.
2068 */
2069 if (crtc->config.double_wide)
2070 val &= ~PIPECONF_DOUBLE_WIDE;
2071
2072 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002073 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2074 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002075 val &= ~PIPECONF_ENABLE;
2076
2077 I915_WRITE(reg, val);
2078 if ((val & PIPECONF_ENABLE) == 0)
2079 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002080}
2081
Keith Packardd74362c2011-07-28 14:47:14 -07002082/*
2083 * Plane regs are double buffered, going from enabled->disabled needs a
2084 * trigger in order to latch. The display address reg provides this.
2085 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002086void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2087 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002088{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002089 struct drm_device *dev = dev_priv->dev;
2090 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002091
2092 I915_WRITE(reg, I915_READ(reg));
2093 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002094}
2095
Jesse Barnesb24e7172011-01-04 15:09:30 -08002096/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002097 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002098 * @plane: plane to be enabled
2099 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002100 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002101 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002102 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002103static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2104 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002105{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002106 struct drm_device *dev = plane->dev;
2107 struct drm_i915_private *dev_priv = dev->dev_private;
2108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002109
2110 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002111 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002112
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002113 if (intel_crtc->primary_enabled)
2114 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002115
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002116 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002117
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002118 dev_priv->display.update_primary_plane(crtc, plane->fb,
2119 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002120
2121 /*
2122 * BDW signals flip done immediately if the plane
2123 * is disabled, even if the plane enable is already
2124 * armed to occur at the next vblank :(
2125 */
2126 if (IS_BROADWELL(dev))
2127 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002128}
2129
Jesse Barnesb24e7172011-01-04 15:09:30 -08002130/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002131 * intel_disable_primary_hw_plane - disable the primary hardware plane
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002132 * @plane: plane to be disabled
2133 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002134 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002135 * Disable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002136 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002137static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2138 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002139{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002140 struct drm_device *dev = plane->dev;
2141 struct drm_i915_private *dev_priv = dev->dev_private;
2142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2143
2144 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002145
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002146 if (!intel_crtc->primary_enabled)
2147 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002148
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002149 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002150
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002151 dev_priv->display.update_primary_plane(crtc, plane->fb,
2152 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002153}
2154
Chris Wilson693db182013-03-05 14:52:39 +00002155static bool need_vtd_wa(struct drm_device *dev)
2156{
2157#ifdef CONFIG_INTEL_IOMMU
2158 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2159 return true;
2160#endif
2161 return false;
2162}
2163
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002164static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2165{
2166 int tile_height;
2167
2168 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2169 return ALIGN(height, tile_height);
2170}
2171
Chris Wilson127bd2a2010-07-23 23:32:05 +01002172int
Chris Wilson48b956c2010-09-14 12:50:34 +01002173intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002174 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002175 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002176{
Chris Wilsonce453d82011-02-21 14:43:56 +00002177 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002178 u32 alignment;
2179 int ret;
2180
Matt Roperebcdd392014-07-09 16:22:11 -07002181 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2182
Chris Wilson05394f32010-11-08 19:18:58 +00002183 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002184 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01002185 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2186 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002187 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002188 alignment = 4 * 1024;
2189 else
2190 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002191 break;
2192 case I915_TILING_X:
2193 /* pin() will align the object as required by fence */
2194 alignment = 0;
2195 break;
2196 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02002197 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002198 return -EINVAL;
2199 default:
2200 BUG();
2201 }
2202
Chris Wilson693db182013-03-05 14:52:39 +00002203 /* Note that the w/a also requires 64 PTE of padding following the
2204 * bo. We currently fill all unused PTE with the shadow page and so
2205 * we should always have valid PTE following the scanout preventing
2206 * the VT-d warning.
2207 */
2208 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2209 alignment = 256 * 1024;
2210
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002211 /*
2212 * Global gtt pte registers are special registers which actually forward
2213 * writes to a chunk of system memory. Which means that there is no risk
2214 * that the register values disappear as soon as we call
2215 * intel_runtime_pm_put(), so it is correct to wrap only the
2216 * pin/unpin/fence and not more.
2217 */
2218 intel_runtime_pm_get(dev_priv);
2219
Chris Wilsonce453d82011-02-21 14:43:56 +00002220 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002221 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002222 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002223 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002224
2225 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2226 * fence, whereas 965+ only requires a fence if using
2227 * framebuffer compression. For simplicity, we always install
2228 * a fence as the cost is not that onerous.
2229 */
Chris Wilson06d98132012-04-17 15:31:24 +01002230 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002231 if (ret)
2232 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002233
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002234 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002235
Chris Wilsonce453d82011-02-21 14:43:56 +00002236 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002237 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002238 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002239
2240err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002241 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002242err_interruptible:
2243 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002244 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002245 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002246}
2247
Chris Wilson1690e1e2011-12-14 13:57:08 +01002248void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2249{
Matt Roperebcdd392014-07-09 16:22:11 -07002250 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2251
Chris Wilson1690e1e2011-12-14 13:57:08 +01002252 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002253 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002254}
2255
Daniel Vetterc2c75132012-07-05 12:17:30 +02002256/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2257 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002258unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2259 unsigned int tiling_mode,
2260 unsigned int cpp,
2261 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002262{
Chris Wilsonbc752862013-02-21 20:04:31 +00002263 if (tiling_mode != I915_TILING_NONE) {
2264 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002265
Chris Wilsonbc752862013-02-21 20:04:31 +00002266 tile_rows = *y / 8;
2267 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002268
Chris Wilsonbc752862013-02-21 20:04:31 +00002269 tiles = *x / (512/cpp);
2270 *x %= 512/cpp;
2271
2272 return tile_rows * pitch * 8 + tiles * 4096;
2273 } else {
2274 unsigned int offset;
2275
2276 offset = *y * pitch + *x * cpp;
2277 *y = 0;
2278 *x = (offset & 4095) / cpp;
2279 return offset & -4096;
2280 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002281}
2282
Jesse Barnes46f297f2014-03-07 08:57:48 -08002283int intel_format_to_fourcc(int format)
2284{
2285 switch (format) {
2286 case DISPPLANE_8BPP:
2287 return DRM_FORMAT_C8;
2288 case DISPPLANE_BGRX555:
2289 return DRM_FORMAT_XRGB1555;
2290 case DISPPLANE_BGRX565:
2291 return DRM_FORMAT_RGB565;
2292 default:
2293 case DISPPLANE_BGRX888:
2294 return DRM_FORMAT_XRGB8888;
2295 case DISPPLANE_RGBX888:
2296 return DRM_FORMAT_XBGR8888;
2297 case DISPPLANE_BGRX101010:
2298 return DRM_FORMAT_XRGB2101010;
2299 case DISPPLANE_RGBX101010:
2300 return DRM_FORMAT_XBGR2101010;
2301 }
2302}
2303
Jesse Barnes484b41d2014-03-07 08:57:55 -08002304static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -08002305 struct intel_plane_config *plane_config)
2306{
2307 struct drm_device *dev = crtc->base.dev;
2308 struct drm_i915_gem_object *obj = NULL;
2309 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2310 u32 base = plane_config->base;
2311
Chris Wilsonff2652e2014-03-10 08:07:02 +00002312 if (plane_config->size == 0)
2313 return false;
2314
Jesse Barnes46f297f2014-03-07 08:57:48 -08002315 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2316 plane_config->size);
2317 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002318 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002319
2320 if (plane_config->tiled) {
2321 obj->tiling_mode = I915_TILING_X;
Dave Airlie66e514c2014-04-03 07:51:54 +10002322 obj->stride = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002323 }
2324
Dave Airlie66e514c2014-04-03 07:51:54 +10002325 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2326 mode_cmd.width = crtc->base.primary->fb->width;
2327 mode_cmd.height = crtc->base.primary->fb->height;
2328 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002329
2330 mutex_lock(&dev->struct_mutex);
2331
Dave Airlie66e514c2014-04-03 07:51:54 +10002332 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002333 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002334 DRM_DEBUG_KMS("intel fb init failed\n");
2335 goto out_unref_obj;
2336 }
2337
Daniel Vettera071fa02014-06-18 23:28:09 +02002338 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002339 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002340
2341 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2342 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002343
2344out_unref_obj:
2345 drm_gem_object_unreference(&obj->base);
2346 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002347 return false;
2348}
2349
2350static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2351 struct intel_plane_config *plane_config)
2352{
2353 struct drm_device *dev = intel_crtc->base.dev;
2354 struct drm_crtc *c;
2355 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002356 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002357
Dave Airlie66e514c2014-04-03 07:51:54 +10002358 if (!intel_crtc->base.primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002359 return;
2360
2361 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2362 return;
2363
Dave Airlie66e514c2014-04-03 07:51:54 +10002364 kfree(intel_crtc->base.primary->fb);
2365 intel_crtc->base.primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002366
2367 /*
2368 * Failed to alloc the obj, check to see if we should share
2369 * an fb with another CRTC instead
2370 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002371 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002372 i = to_intel_crtc(c);
2373
2374 if (c == &intel_crtc->base)
2375 continue;
2376
Matt Roper2ff8fde2014-07-08 07:50:07 -07002377 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002378 continue;
2379
Matt Roper2ff8fde2014-07-08 07:50:07 -07002380 obj = intel_fb_obj(c->primary->fb);
2381 if (obj == NULL)
2382 continue;
2383
2384 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Dave Airlie66e514c2014-04-03 07:51:54 +10002385 drm_framebuffer_reference(c->primary->fb);
2386 intel_crtc->base.primary->fb = c->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002387 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002388 break;
2389 }
2390 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002391}
2392
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002393static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2394 struct drm_framebuffer *fb,
2395 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002396{
2397 struct drm_device *dev = crtc->dev;
2398 struct drm_i915_private *dev_priv = dev->dev_private;
2399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002400 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002401 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002402 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002403 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002404 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302405 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002406
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002407 if (!intel_crtc->primary_enabled) {
2408 I915_WRITE(reg, 0);
2409 if (INTEL_INFO(dev)->gen >= 4)
2410 I915_WRITE(DSPSURF(plane), 0);
2411 else
2412 I915_WRITE(DSPADDR(plane), 0);
2413 POSTING_READ(reg);
2414 return;
2415 }
2416
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002417 obj = intel_fb_obj(fb);
2418 if (WARN_ON(obj == NULL))
2419 return;
2420
2421 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2422
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002423 dspcntr = DISPPLANE_GAMMA_ENABLE;
2424
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002425 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002426
2427 if (INTEL_INFO(dev)->gen < 4) {
2428 if (intel_crtc->pipe == PIPE_B)
2429 dspcntr |= DISPPLANE_SEL_PIPE_B;
2430
2431 /* pipesrc and dspsize control the size that is scaled from,
2432 * which should always be the user's requested size.
2433 */
2434 I915_WRITE(DSPSIZE(plane),
2435 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2436 (intel_crtc->config.pipe_src_w - 1));
2437 I915_WRITE(DSPPOS(plane), 0);
2438 }
2439
Ville Syrjälä57779d02012-10-31 17:50:14 +02002440 switch (fb->pixel_format) {
2441 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002442 dspcntr |= DISPPLANE_8BPP;
2443 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002444 case DRM_FORMAT_XRGB1555:
2445 case DRM_FORMAT_ARGB1555:
2446 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002447 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002448 case DRM_FORMAT_RGB565:
2449 dspcntr |= DISPPLANE_BGRX565;
2450 break;
2451 case DRM_FORMAT_XRGB8888:
2452 case DRM_FORMAT_ARGB8888:
2453 dspcntr |= DISPPLANE_BGRX888;
2454 break;
2455 case DRM_FORMAT_XBGR8888:
2456 case DRM_FORMAT_ABGR8888:
2457 dspcntr |= DISPPLANE_RGBX888;
2458 break;
2459 case DRM_FORMAT_XRGB2101010:
2460 case DRM_FORMAT_ARGB2101010:
2461 dspcntr |= DISPPLANE_BGRX101010;
2462 break;
2463 case DRM_FORMAT_XBGR2101010:
2464 case DRM_FORMAT_ABGR2101010:
2465 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002466 break;
2467 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002468 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002469 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002470
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002471 if (INTEL_INFO(dev)->gen >= 4 &&
2472 obj->tiling_mode != I915_TILING_NONE)
2473 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002474
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002475 if (IS_G4X(dev))
2476 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2477
Ville Syrjäläb98971272014-08-27 16:51:22 +03002478 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002479
Daniel Vetterc2c75132012-07-05 12:17:30 +02002480 if (INTEL_INFO(dev)->gen >= 4) {
2481 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002482 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002483 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002484 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002485 linear_offset -= intel_crtc->dspaddr_offset;
2486 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002487 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002488 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002489
Sonika Jindal48404c12014-08-22 14:06:04 +05302490 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2491 dspcntr |= DISPPLANE_ROTATE_180;
2492
2493 x += (intel_crtc->config.pipe_src_w - 1);
2494 y += (intel_crtc->config.pipe_src_h - 1);
2495
2496 /* Finding the last pixel of the last line of the display
2497 data and adding to linear_offset*/
2498 linear_offset +=
2499 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2500 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2501 }
2502
2503 I915_WRITE(reg, dspcntr);
2504
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002505 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2506 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2507 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002508 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002509 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002510 I915_WRITE(DSPSURF(plane),
2511 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002512 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002513 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002514 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002515 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002516 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002517}
2518
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002519static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2520 struct drm_framebuffer *fb,
2521 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002522{
2523 struct drm_device *dev = crtc->dev;
2524 struct drm_i915_private *dev_priv = dev->dev_private;
2525 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002526 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002527 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002528 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002529 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002530 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302531 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002532
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002533 if (!intel_crtc->primary_enabled) {
2534 I915_WRITE(reg, 0);
2535 I915_WRITE(DSPSURF(plane), 0);
2536 POSTING_READ(reg);
2537 return;
2538 }
2539
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002540 obj = intel_fb_obj(fb);
2541 if (WARN_ON(obj == NULL))
2542 return;
2543
2544 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2545
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002546 dspcntr = DISPPLANE_GAMMA_ENABLE;
2547
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002548 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002549
2550 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2551 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2552
Ville Syrjälä57779d02012-10-31 17:50:14 +02002553 switch (fb->pixel_format) {
2554 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002555 dspcntr |= DISPPLANE_8BPP;
2556 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002557 case DRM_FORMAT_RGB565:
2558 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002559 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002560 case DRM_FORMAT_XRGB8888:
2561 case DRM_FORMAT_ARGB8888:
2562 dspcntr |= DISPPLANE_BGRX888;
2563 break;
2564 case DRM_FORMAT_XBGR8888:
2565 case DRM_FORMAT_ABGR8888:
2566 dspcntr |= DISPPLANE_RGBX888;
2567 break;
2568 case DRM_FORMAT_XRGB2101010:
2569 case DRM_FORMAT_ARGB2101010:
2570 dspcntr |= DISPPLANE_BGRX101010;
2571 break;
2572 case DRM_FORMAT_XBGR2101010:
2573 case DRM_FORMAT_ABGR2101010:
2574 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002575 break;
2576 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002577 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002578 }
2579
2580 if (obj->tiling_mode != I915_TILING_NONE)
2581 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002582
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002583 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002584 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002585
Ville Syrjäläb98971272014-08-27 16:51:22 +03002586 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002587 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002588 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002589 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002590 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002591 linear_offset -= intel_crtc->dspaddr_offset;
Sonika Jindal48404c12014-08-22 14:06:04 +05302592 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2593 dspcntr |= DISPPLANE_ROTATE_180;
2594
2595 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2596 x += (intel_crtc->config.pipe_src_w - 1);
2597 y += (intel_crtc->config.pipe_src_h - 1);
2598
2599 /* Finding the last pixel of the last line of the display
2600 data and adding to linear_offset*/
2601 linear_offset +=
2602 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2603 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2604 }
2605 }
2606
2607 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002608
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002609 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2610 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2611 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002612 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002613 I915_WRITE(DSPSURF(plane),
2614 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002615 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002616 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2617 } else {
2618 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2619 I915_WRITE(DSPLINOFF(plane), linear_offset);
2620 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002621 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002622}
2623
2624/* Assume fb object is pinned & idle & fenced and just update base pointers */
2625static int
2626intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2627 int x, int y, enum mode_set_atomic state)
2628{
2629 struct drm_device *dev = crtc->dev;
2630 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002631
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002632 if (dev_priv->display.disable_fbc)
2633 dev_priv->display.disable_fbc(dev);
Daniel Vettercc365132014-06-18 13:59:13 +02002634 intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
Jesse Barnes81255562010-08-02 12:07:50 -07002635
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002636 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2637
2638 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07002639}
2640
Ville Syrjälä96a02912013-02-18 19:08:49 +02002641void intel_display_handle_reset(struct drm_device *dev)
2642{
2643 struct drm_i915_private *dev_priv = dev->dev_private;
2644 struct drm_crtc *crtc;
2645
2646 /*
2647 * Flips in the rings have been nuked by the reset,
2648 * so complete all pending flips so that user space
2649 * will get its events and not get stuck.
2650 *
2651 * Also update the base address of all primary
2652 * planes to the the last fb to make sure we're
2653 * showing the correct fb after a reset.
2654 *
2655 * Need to make two loops over the crtcs so that we
2656 * don't try to grab a crtc mutex before the
2657 * pending_flip_queue really got woken up.
2658 */
2659
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002660 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2662 enum plane plane = intel_crtc->plane;
2663
2664 intel_prepare_page_flip(dev, plane);
2665 intel_finish_page_flip_plane(dev, plane);
2666 }
2667
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002668 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2670
Rob Clark51fd3712013-11-19 12:10:12 -05002671 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002672 /*
2673 * FIXME: Once we have proper support for primary planes (and
2674 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002675 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002676 */
Matt Roperf4510a22014-04-01 15:22:40 -07002677 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002678 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002679 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002680 crtc->x,
2681 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05002682 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002683 }
2684}
2685
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002686static int
Chris Wilson14667a42012-04-03 17:58:35 +01002687intel_finish_fb(struct drm_framebuffer *old_fb)
2688{
Matt Roper2ff8fde2014-07-08 07:50:07 -07002689 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01002690 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2691 bool was_interruptible = dev_priv->mm.interruptible;
2692 int ret;
2693
Chris Wilson14667a42012-04-03 17:58:35 +01002694 /* Big Hammer, we also need to ensure that any pending
2695 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2696 * current scanout is retired before unpinning the old
2697 * framebuffer.
2698 *
2699 * This should only fail upon a hung GPU, in which case we
2700 * can safely continue.
2701 */
2702 dev_priv->mm.interruptible = false;
2703 ret = i915_gem_object_finish_gpu(obj);
2704 dev_priv->mm.interruptible = was_interruptible;
2705
2706 return ret;
2707}
2708
Chris Wilson7d5e3792014-03-04 13:15:08 +00002709static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2710{
2711 struct drm_device *dev = crtc->dev;
2712 struct drm_i915_private *dev_priv = dev->dev_private;
2713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002714 bool pending;
2715
2716 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2717 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2718 return false;
2719
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02002720 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002721 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02002722 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002723
2724 return pending;
2725}
2726
Gustavo Padovane30e8f72014-09-10 12:04:17 -03002727static void intel_update_pipe_size(struct intel_crtc *crtc)
2728{
2729 struct drm_device *dev = crtc->base.dev;
2730 struct drm_i915_private *dev_priv = dev->dev_private;
2731 const struct drm_display_mode *adjusted_mode;
2732
2733 if (!i915.fastboot)
2734 return;
2735
2736 /*
2737 * Update pipe size and adjust fitter if needed: the reason for this is
2738 * that in compute_mode_changes we check the native mode (not the pfit
2739 * mode) to see if we can flip rather than do a full mode set. In the
2740 * fastboot case, we'll flip, but if we don't update the pipesrc and
2741 * pfit state, we'll end up with a big fb scanned out into the wrong
2742 * sized surface.
2743 *
2744 * To fix this properly, we need to hoist the checks up into
2745 * compute_mode_changes (or above), check the actual pfit state and
2746 * whether the platform allows pfit disable with pipe active, and only
2747 * then update the pipesrc and pfit state, even on the flip path.
2748 */
2749
2750 adjusted_mode = &crtc->config.adjusted_mode;
2751
2752 I915_WRITE(PIPESRC(crtc->pipe),
2753 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2754 (adjusted_mode->crtc_vdisplay - 1));
2755 if (!crtc->config.pch_pfit.enabled &&
2756 (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) ||
2757 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))) {
2758 I915_WRITE(PF_CTL(crtc->pipe), 0);
2759 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
2760 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
2761 }
2762 crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2763 crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
2764}
2765
Chris Wilson14667a42012-04-03 17:58:35 +01002766static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002767intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002768 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002769{
2770 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002771 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02002773 enum pipe pipe = intel_crtc->pipe;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002774 struct drm_framebuffer *old_fb = crtc->primary->fb;
2775 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2776 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002777 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002778
Chris Wilson7d5e3792014-03-04 13:15:08 +00002779 if (intel_crtc_has_pending_flip(crtc)) {
2780 DRM_ERROR("pipe is still busy with an old pageflip\n");
2781 return -EBUSY;
2782 }
2783
Jesse Barnes79e53942008-11-07 14:24:08 -08002784 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002785 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002786 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002787 return 0;
2788 }
2789
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002790 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002791 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2792 plane_name(intel_crtc->plane),
2793 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002794 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002795 }
2796
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002797 mutex_lock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +02002798 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2799 if (ret == 0)
Matt Roper91565c852014-06-24 17:05:02 -07002800 i915_gem_track_fb(old_obj, obj,
Daniel Vettera071fa02014-06-18 23:28:09 +02002801 INTEL_FRONTBUFFER_PRIMARY(pipe));
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002802 mutex_unlock(&dev->struct_mutex);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002803 if (ret != 0) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002804 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002805 return ret;
2806 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002807
Gustavo Padovane30e8f72014-09-10 12:04:17 -03002808 intel_update_pipe_size(intel_crtc);
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002809
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002810 dev_priv->display.update_primary_plane(crtc, fb, x, y);
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002811
Daniel Vetterf99d7062014-06-19 16:01:59 +02002812 if (intel_crtc->active)
2813 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
2814
Matt Roperf4510a22014-04-01 15:22:40 -07002815 crtc->primary->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002816 crtc->x = x;
2817 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002818
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002819 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002820 if (intel_crtc->active && old_fb != fb)
2821 intel_wait_for_vblank(dev, intel_crtc->pipe);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002822 mutex_lock(&dev->struct_mutex);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002823 intel_unpin_fb_obj(old_obj);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002824 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002825 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002826
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002827 mutex_lock(&dev->struct_mutex);
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002828 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002829 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002830
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002831 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002832}
2833
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002834static void intel_fdi_normal_train(struct drm_crtc *crtc)
2835{
2836 struct drm_device *dev = crtc->dev;
2837 struct drm_i915_private *dev_priv = dev->dev_private;
2838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2839 int pipe = intel_crtc->pipe;
2840 u32 reg, temp;
2841
2842 /* enable normal train */
2843 reg = FDI_TX_CTL(pipe);
2844 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002845 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002846 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2847 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002848 } else {
2849 temp &= ~FDI_LINK_TRAIN_NONE;
2850 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002851 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002852 I915_WRITE(reg, temp);
2853
2854 reg = FDI_RX_CTL(pipe);
2855 temp = I915_READ(reg);
2856 if (HAS_PCH_CPT(dev)) {
2857 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2858 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2859 } else {
2860 temp &= ~FDI_LINK_TRAIN_NONE;
2861 temp |= FDI_LINK_TRAIN_NONE;
2862 }
2863 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2864
2865 /* wait one idle pattern time */
2866 POSTING_READ(reg);
2867 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002868
2869 /* IVB wants error correction enabled */
2870 if (IS_IVYBRIDGE(dev))
2871 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2872 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002873}
2874
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002875static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002876{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002877 return crtc->base.enabled && crtc->active &&
2878 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002879}
2880
Daniel Vetter01a415f2012-10-27 15:58:40 +02002881static void ivb_modeset_global_resources(struct drm_device *dev)
2882{
2883 struct drm_i915_private *dev_priv = dev->dev_private;
2884 struct intel_crtc *pipe_B_crtc =
2885 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2886 struct intel_crtc *pipe_C_crtc =
2887 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2888 uint32_t temp;
2889
Daniel Vetter1e833f42013-02-19 22:31:57 +01002890 /*
2891 * When everything is off disable fdi C so that we could enable fdi B
2892 * with all lanes. Note that we don't care about enabled pipes without
2893 * an enabled pch encoder.
2894 */
2895 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2896 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002897 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2898 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2899
2900 temp = I915_READ(SOUTH_CHICKEN1);
2901 temp &= ~FDI_BC_BIFURCATION_SELECT;
2902 DRM_DEBUG_KMS("disabling fdi C rx\n");
2903 I915_WRITE(SOUTH_CHICKEN1, temp);
2904 }
2905}
2906
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002907/* The FDI link training functions for ILK/Ibexpeak. */
2908static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2909{
2910 struct drm_device *dev = crtc->dev;
2911 struct drm_i915_private *dev_priv = dev->dev_private;
2912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2913 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002914 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002915
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03002916 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002917 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002918
Adam Jacksone1a44742010-06-25 15:32:14 -04002919 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2920 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002921 reg = FDI_RX_IMR(pipe);
2922 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002923 temp &= ~FDI_RX_SYMBOL_LOCK;
2924 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002925 I915_WRITE(reg, temp);
2926 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002927 udelay(150);
2928
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002929 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002930 reg = FDI_TX_CTL(pipe);
2931 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002932 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2933 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002934 temp &= ~FDI_LINK_TRAIN_NONE;
2935 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002936 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002937
Chris Wilson5eddb702010-09-11 13:48:45 +01002938 reg = FDI_RX_CTL(pipe);
2939 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002940 temp &= ~FDI_LINK_TRAIN_NONE;
2941 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002942 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2943
2944 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002945 udelay(150);
2946
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002947 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002948 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2949 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2950 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002951
Chris Wilson5eddb702010-09-11 13:48:45 +01002952 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002953 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002954 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002955 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2956
2957 if ((temp & FDI_RX_BIT_LOCK)) {
2958 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002959 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002960 break;
2961 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002962 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002963 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002964 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002965
2966 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002967 reg = FDI_TX_CTL(pipe);
2968 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002969 temp &= ~FDI_LINK_TRAIN_NONE;
2970 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002971 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002972
Chris Wilson5eddb702010-09-11 13:48:45 +01002973 reg = FDI_RX_CTL(pipe);
2974 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002975 temp &= ~FDI_LINK_TRAIN_NONE;
2976 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002977 I915_WRITE(reg, temp);
2978
2979 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002980 udelay(150);
2981
Chris Wilson5eddb702010-09-11 13:48:45 +01002982 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002983 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002984 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002985 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2986
2987 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002988 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002989 DRM_DEBUG_KMS("FDI train 2 done.\n");
2990 break;
2991 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002992 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002993 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002994 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002995
2996 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002997
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002998}
2999
Akshay Joshi0206e352011-08-16 15:34:10 -04003000static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003001 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3002 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3003 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3004 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3005};
3006
3007/* The FDI link training functions for SNB/Cougarpoint. */
3008static void gen6_fdi_link_train(struct drm_crtc *crtc)
3009{
3010 struct drm_device *dev = crtc->dev;
3011 struct drm_i915_private *dev_priv = dev->dev_private;
3012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3013 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003014 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003015
Adam Jacksone1a44742010-06-25 15:32:14 -04003016 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3017 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003018 reg = FDI_RX_IMR(pipe);
3019 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003020 temp &= ~FDI_RX_SYMBOL_LOCK;
3021 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003022 I915_WRITE(reg, temp);
3023
3024 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003025 udelay(150);
3026
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003027 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003028 reg = FDI_TX_CTL(pipe);
3029 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003030 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3031 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003032 temp &= ~FDI_LINK_TRAIN_NONE;
3033 temp |= FDI_LINK_TRAIN_PATTERN_1;
3034 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3035 /* SNB-B */
3036 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003037 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003038
Daniel Vetterd74cf322012-10-26 10:58:13 +02003039 I915_WRITE(FDI_RX_MISC(pipe),
3040 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3041
Chris Wilson5eddb702010-09-11 13:48:45 +01003042 reg = FDI_RX_CTL(pipe);
3043 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003044 if (HAS_PCH_CPT(dev)) {
3045 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3046 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3047 } else {
3048 temp &= ~FDI_LINK_TRAIN_NONE;
3049 temp |= FDI_LINK_TRAIN_PATTERN_1;
3050 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003051 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3052
3053 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003054 udelay(150);
3055
Akshay Joshi0206e352011-08-16 15:34:10 -04003056 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003057 reg = FDI_TX_CTL(pipe);
3058 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003059 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3060 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003061 I915_WRITE(reg, temp);
3062
3063 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003064 udelay(500);
3065
Sean Paulfa37d392012-03-02 12:53:39 -05003066 for (retry = 0; retry < 5; retry++) {
3067 reg = FDI_RX_IIR(pipe);
3068 temp = I915_READ(reg);
3069 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3070 if (temp & FDI_RX_BIT_LOCK) {
3071 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3072 DRM_DEBUG_KMS("FDI train 1 done.\n");
3073 break;
3074 }
3075 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003076 }
Sean Paulfa37d392012-03-02 12:53:39 -05003077 if (retry < 5)
3078 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003079 }
3080 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003081 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003082
3083 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003084 reg = FDI_TX_CTL(pipe);
3085 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003086 temp &= ~FDI_LINK_TRAIN_NONE;
3087 temp |= FDI_LINK_TRAIN_PATTERN_2;
3088 if (IS_GEN6(dev)) {
3089 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3090 /* SNB-B */
3091 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3092 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003093 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003094
Chris Wilson5eddb702010-09-11 13:48:45 +01003095 reg = FDI_RX_CTL(pipe);
3096 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003097 if (HAS_PCH_CPT(dev)) {
3098 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3099 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3100 } else {
3101 temp &= ~FDI_LINK_TRAIN_NONE;
3102 temp |= FDI_LINK_TRAIN_PATTERN_2;
3103 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003104 I915_WRITE(reg, temp);
3105
3106 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003107 udelay(150);
3108
Akshay Joshi0206e352011-08-16 15:34:10 -04003109 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003110 reg = FDI_TX_CTL(pipe);
3111 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003112 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3113 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003114 I915_WRITE(reg, temp);
3115
3116 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003117 udelay(500);
3118
Sean Paulfa37d392012-03-02 12:53:39 -05003119 for (retry = 0; retry < 5; retry++) {
3120 reg = FDI_RX_IIR(pipe);
3121 temp = I915_READ(reg);
3122 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3123 if (temp & FDI_RX_SYMBOL_LOCK) {
3124 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3125 DRM_DEBUG_KMS("FDI train 2 done.\n");
3126 break;
3127 }
3128 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003129 }
Sean Paulfa37d392012-03-02 12:53:39 -05003130 if (retry < 5)
3131 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003132 }
3133 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003134 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003135
3136 DRM_DEBUG_KMS("FDI train done.\n");
3137}
3138
Jesse Barnes357555c2011-04-28 15:09:55 -07003139/* Manual link training for Ivy Bridge A0 parts */
3140static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3141{
3142 struct drm_device *dev = crtc->dev;
3143 struct drm_i915_private *dev_priv = dev->dev_private;
3144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3145 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003146 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003147
3148 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3149 for train result */
3150 reg = FDI_RX_IMR(pipe);
3151 temp = I915_READ(reg);
3152 temp &= ~FDI_RX_SYMBOL_LOCK;
3153 temp &= ~FDI_RX_BIT_LOCK;
3154 I915_WRITE(reg, temp);
3155
3156 POSTING_READ(reg);
3157 udelay(150);
3158
Daniel Vetter01a415f2012-10-27 15:58:40 +02003159 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3160 I915_READ(FDI_RX_IIR(pipe)));
3161
Jesse Barnes139ccd32013-08-19 11:04:55 -07003162 /* Try each vswing and preemphasis setting twice before moving on */
3163 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3164 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003165 reg = FDI_TX_CTL(pipe);
3166 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003167 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3168 temp &= ~FDI_TX_ENABLE;
3169 I915_WRITE(reg, temp);
3170
3171 reg = FDI_RX_CTL(pipe);
3172 temp = I915_READ(reg);
3173 temp &= ~FDI_LINK_TRAIN_AUTO;
3174 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3175 temp &= ~FDI_RX_ENABLE;
3176 I915_WRITE(reg, temp);
3177
3178 /* enable CPU FDI TX and PCH FDI RX */
3179 reg = FDI_TX_CTL(pipe);
3180 temp = I915_READ(reg);
3181 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3182 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3183 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003184 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003185 temp |= snb_b_fdi_train_param[j/2];
3186 temp |= FDI_COMPOSITE_SYNC;
3187 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3188
3189 I915_WRITE(FDI_RX_MISC(pipe),
3190 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3191
3192 reg = FDI_RX_CTL(pipe);
3193 temp = I915_READ(reg);
3194 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3195 temp |= FDI_COMPOSITE_SYNC;
3196 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3197
3198 POSTING_READ(reg);
3199 udelay(1); /* should be 0.5us */
3200
3201 for (i = 0; i < 4; i++) {
3202 reg = FDI_RX_IIR(pipe);
3203 temp = I915_READ(reg);
3204 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3205
3206 if (temp & FDI_RX_BIT_LOCK ||
3207 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3208 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3209 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3210 i);
3211 break;
3212 }
3213 udelay(1); /* should be 0.5us */
3214 }
3215 if (i == 4) {
3216 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3217 continue;
3218 }
3219
3220 /* Train 2 */
3221 reg = FDI_TX_CTL(pipe);
3222 temp = I915_READ(reg);
3223 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3224 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3225 I915_WRITE(reg, temp);
3226
3227 reg = FDI_RX_CTL(pipe);
3228 temp = I915_READ(reg);
3229 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3230 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003231 I915_WRITE(reg, temp);
3232
3233 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003234 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003235
Jesse Barnes139ccd32013-08-19 11:04:55 -07003236 for (i = 0; i < 4; i++) {
3237 reg = FDI_RX_IIR(pipe);
3238 temp = I915_READ(reg);
3239 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003240
Jesse Barnes139ccd32013-08-19 11:04:55 -07003241 if (temp & FDI_RX_SYMBOL_LOCK ||
3242 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3243 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3244 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3245 i);
3246 goto train_done;
3247 }
3248 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003249 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003250 if (i == 4)
3251 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003252 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003253
Jesse Barnes139ccd32013-08-19 11:04:55 -07003254train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003255 DRM_DEBUG_KMS("FDI train done.\n");
3256}
3257
Daniel Vetter88cefb62012-08-12 19:27:14 +02003258static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003259{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003260 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003261 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003262 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003263 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003264
Jesse Barnesc64e3112010-09-10 11:27:03 -07003265
Jesse Barnes0e23b992010-09-10 11:10:00 -07003266 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003267 reg = FDI_RX_CTL(pipe);
3268 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003269 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3270 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003271 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003272 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3273
3274 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003275 udelay(200);
3276
3277 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003278 temp = I915_READ(reg);
3279 I915_WRITE(reg, temp | FDI_PCDCLK);
3280
3281 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003282 udelay(200);
3283
Paulo Zanoni20749732012-11-23 15:30:38 -02003284 /* Enable CPU FDI TX PLL, always on for Ironlake */
3285 reg = FDI_TX_CTL(pipe);
3286 temp = I915_READ(reg);
3287 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3288 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003289
Paulo Zanoni20749732012-11-23 15:30:38 -02003290 POSTING_READ(reg);
3291 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003292 }
3293}
3294
Daniel Vetter88cefb62012-08-12 19:27:14 +02003295static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3296{
3297 struct drm_device *dev = intel_crtc->base.dev;
3298 struct drm_i915_private *dev_priv = dev->dev_private;
3299 int pipe = intel_crtc->pipe;
3300 u32 reg, temp;
3301
3302 /* Switch from PCDclk to Rawclk */
3303 reg = FDI_RX_CTL(pipe);
3304 temp = I915_READ(reg);
3305 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3306
3307 /* Disable CPU FDI TX PLL */
3308 reg = FDI_TX_CTL(pipe);
3309 temp = I915_READ(reg);
3310 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3311
3312 POSTING_READ(reg);
3313 udelay(100);
3314
3315 reg = FDI_RX_CTL(pipe);
3316 temp = I915_READ(reg);
3317 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3318
3319 /* Wait for the clocks to turn off. */
3320 POSTING_READ(reg);
3321 udelay(100);
3322}
3323
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003324static void ironlake_fdi_disable(struct drm_crtc *crtc)
3325{
3326 struct drm_device *dev = crtc->dev;
3327 struct drm_i915_private *dev_priv = dev->dev_private;
3328 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3329 int pipe = intel_crtc->pipe;
3330 u32 reg, temp;
3331
3332 /* disable CPU FDI tx and PCH FDI rx */
3333 reg = FDI_TX_CTL(pipe);
3334 temp = I915_READ(reg);
3335 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3336 POSTING_READ(reg);
3337
3338 reg = FDI_RX_CTL(pipe);
3339 temp = I915_READ(reg);
3340 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003341 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003342 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3343
3344 POSTING_READ(reg);
3345 udelay(100);
3346
3347 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003348 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003349 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003350
3351 /* still set train pattern 1 */
3352 reg = FDI_TX_CTL(pipe);
3353 temp = I915_READ(reg);
3354 temp &= ~FDI_LINK_TRAIN_NONE;
3355 temp |= FDI_LINK_TRAIN_PATTERN_1;
3356 I915_WRITE(reg, temp);
3357
3358 reg = FDI_RX_CTL(pipe);
3359 temp = I915_READ(reg);
3360 if (HAS_PCH_CPT(dev)) {
3361 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3362 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3363 } else {
3364 temp &= ~FDI_LINK_TRAIN_NONE;
3365 temp |= FDI_LINK_TRAIN_PATTERN_1;
3366 }
3367 /* BPC in FDI rx is consistent with that in PIPECONF */
3368 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003369 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003370 I915_WRITE(reg, temp);
3371
3372 POSTING_READ(reg);
3373 udelay(100);
3374}
3375
Chris Wilson5dce5b932014-01-20 10:17:36 +00003376bool intel_has_pending_fb_unpin(struct drm_device *dev)
3377{
3378 struct intel_crtc *crtc;
3379
3380 /* Note that we don't need to be called with mode_config.lock here
3381 * as our list of CRTC objects is static for the lifetime of the
3382 * device and so cannot disappear as we iterate. Similarly, we can
3383 * happily treat the predicates as racy, atomic checks as userspace
3384 * cannot claim and pin a new fb without at least acquring the
3385 * struct_mutex and so serialising with us.
3386 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003387 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003388 if (atomic_read(&crtc->unpin_work_count) == 0)
3389 continue;
3390
3391 if (crtc->unpin_work)
3392 intel_wait_for_vblank(dev, crtc->pipe);
3393
3394 return true;
3395 }
3396
3397 return false;
3398}
3399
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003400static void page_flip_completed(struct intel_crtc *intel_crtc)
3401{
3402 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3403 struct intel_unpin_work *work = intel_crtc->unpin_work;
3404
3405 /* ensure that the unpin work is consistent wrt ->pending. */
3406 smp_rmb();
3407 intel_crtc->unpin_work = NULL;
3408
3409 if (work->event)
3410 drm_send_vblank_event(intel_crtc->base.dev,
3411 intel_crtc->pipe,
3412 work->event);
3413
3414 drm_crtc_vblank_put(&intel_crtc->base);
3415
3416 wake_up_all(&dev_priv->pending_flip_queue);
3417 queue_work(dev_priv->wq, &work->work);
3418
3419 trace_i915_flip_complete(intel_crtc->plane,
3420 work->pending_flip_obj);
3421}
3422
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003423void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003424{
Chris Wilson0f911282012-04-17 10:05:38 +01003425 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003426 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003427
Daniel Vetter2c10d572012-12-20 21:24:07 +01003428 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003429 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3430 !intel_crtc_has_pending_flip(crtc),
3431 60*HZ) == 0)) {
3432 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003433
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003434 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003435 if (intel_crtc->unpin_work) {
3436 WARN_ONCE(1, "Removing stuck page flip\n");
3437 page_flip_completed(intel_crtc);
3438 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003439 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003440 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003441
Chris Wilson975d5682014-08-20 13:13:34 +01003442 if (crtc->primary->fb) {
3443 mutex_lock(&dev->struct_mutex);
3444 intel_finish_fb(crtc->primary->fb);
3445 mutex_unlock(&dev->struct_mutex);
3446 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003447}
3448
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003449/* Program iCLKIP clock to the desired frequency */
3450static void lpt_program_iclkip(struct drm_crtc *crtc)
3451{
3452 struct drm_device *dev = crtc->dev;
3453 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003454 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003455 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3456 u32 temp;
3457
Daniel Vetter09153002012-12-12 14:06:44 +01003458 mutex_lock(&dev_priv->dpio_lock);
3459
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003460 /* It is necessary to ungate the pixclk gate prior to programming
3461 * the divisors, and gate it back when it is done.
3462 */
3463 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3464
3465 /* Disable SSCCTL */
3466 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003467 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3468 SBI_SSCCTL_DISABLE,
3469 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003470
3471 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003472 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003473 auxdiv = 1;
3474 divsel = 0x41;
3475 phaseinc = 0x20;
3476 } else {
3477 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003478 * but the adjusted_mode->crtc_clock in in KHz. To get the
3479 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003480 * convert the virtual clock precision to KHz here for higher
3481 * precision.
3482 */
3483 u32 iclk_virtual_root_freq = 172800 * 1000;
3484 u32 iclk_pi_range = 64;
3485 u32 desired_divisor, msb_divisor_value, pi_value;
3486
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003487 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003488 msb_divisor_value = desired_divisor / iclk_pi_range;
3489 pi_value = desired_divisor % iclk_pi_range;
3490
3491 auxdiv = 0;
3492 divsel = msb_divisor_value - 2;
3493 phaseinc = pi_value;
3494 }
3495
3496 /* This should not happen with any sane values */
3497 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3498 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3499 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3500 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3501
3502 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 +03003503 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003504 auxdiv,
3505 divsel,
3506 phasedir,
3507 phaseinc);
3508
3509 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003510 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003511 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3512 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3513 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3514 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3515 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3516 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003517 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003518
3519 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003520 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003521 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3522 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003523 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003524
3525 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003526 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003527 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003528 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003529
3530 /* Wait for initialization time */
3531 udelay(24);
3532
3533 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003534
3535 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003536}
3537
Daniel Vetter275f01b22013-05-03 11:49:47 +02003538static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3539 enum pipe pch_transcoder)
3540{
3541 struct drm_device *dev = crtc->base.dev;
3542 struct drm_i915_private *dev_priv = dev->dev_private;
3543 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3544
3545 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3546 I915_READ(HTOTAL(cpu_transcoder)));
3547 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3548 I915_READ(HBLANK(cpu_transcoder)));
3549 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3550 I915_READ(HSYNC(cpu_transcoder)));
3551
3552 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3553 I915_READ(VTOTAL(cpu_transcoder)));
3554 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3555 I915_READ(VBLANK(cpu_transcoder)));
3556 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3557 I915_READ(VSYNC(cpu_transcoder)));
3558 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3559 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3560}
3561
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003562static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3563{
3564 struct drm_i915_private *dev_priv = dev->dev_private;
3565 uint32_t temp;
3566
3567 temp = I915_READ(SOUTH_CHICKEN1);
3568 if (temp & FDI_BC_BIFURCATION_SELECT)
3569 return;
3570
3571 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3572 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3573
3574 temp |= FDI_BC_BIFURCATION_SELECT;
3575 DRM_DEBUG_KMS("enabling fdi C rx\n");
3576 I915_WRITE(SOUTH_CHICKEN1, temp);
3577 POSTING_READ(SOUTH_CHICKEN1);
3578}
3579
3580static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3581{
3582 struct drm_device *dev = intel_crtc->base.dev;
3583 struct drm_i915_private *dev_priv = dev->dev_private;
3584
3585 switch (intel_crtc->pipe) {
3586 case PIPE_A:
3587 break;
3588 case PIPE_B:
3589 if (intel_crtc->config.fdi_lanes > 2)
3590 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3591 else
3592 cpt_enable_fdi_bc_bifurcation(dev);
3593
3594 break;
3595 case PIPE_C:
3596 cpt_enable_fdi_bc_bifurcation(dev);
3597
3598 break;
3599 default:
3600 BUG();
3601 }
3602}
3603
Jesse Barnesf67a5592011-01-05 10:31:48 -08003604/*
3605 * Enable PCH resources required for PCH ports:
3606 * - PCH PLLs
3607 * - FDI training & RX/TX
3608 * - update transcoder timings
3609 * - DP transcoding bits
3610 * - transcoder
3611 */
3612static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003613{
3614 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003615 struct drm_i915_private *dev_priv = dev->dev_private;
3616 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3617 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003618 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003619
Daniel Vetterab9412b2013-05-03 11:49:46 +02003620 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003621
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003622 if (IS_IVYBRIDGE(dev))
3623 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3624
Daniel Vettercd986ab2012-10-26 10:58:12 +02003625 /* Write the TU size bits before fdi link training, so that error
3626 * detection works. */
3627 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3628 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3629
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003630 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003631 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003632
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003633 /* We need to program the right clock selection before writing the pixel
3634 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003635 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003636 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003637
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003638 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003639 temp |= TRANS_DPLL_ENABLE(pipe);
3640 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003641 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003642 temp |= sel;
3643 else
3644 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003645 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003646 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003647
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003648 /* XXX: pch pll's can be enabled any time before we enable the PCH
3649 * transcoder, and we actually should do this to not upset any PCH
3650 * transcoder that already use the clock when we share it.
3651 *
3652 * Note that enable_shared_dpll tries to do the right thing, but
3653 * get_shared_dpll unconditionally resets the pll - we need that to have
3654 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02003655 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003656
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003657 /* set transcoder timing, panel must allow it */
3658 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003659 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003660
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003661 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003662
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003663 /* For PCH DP, enable TRANS_DP_CTL */
3664 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003665 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3666 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003667 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003668 reg = TRANS_DP_CTL(pipe);
3669 temp = I915_READ(reg);
3670 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003671 TRANS_DP_SYNC_MASK |
3672 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003673 temp |= (TRANS_DP_OUTPUT_ENABLE |
3674 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003675 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003676
3677 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003678 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003679 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003680 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003681
3682 switch (intel_trans_dp_port_sel(crtc)) {
3683 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003684 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003685 break;
3686 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003687 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003688 break;
3689 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003690 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003691 break;
3692 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003693 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003694 }
3695
Chris Wilson5eddb702010-09-11 13:48:45 +01003696 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003697 }
3698
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003699 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003700}
3701
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003702static void lpt_pch_enable(struct drm_crtc *crtc)
3703{
3704 struct drm_device *dev = crtc->dev;
3705 struct drm_i915_private *dev_priv = dev->dev_private;
3706 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003707 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003708
Daniel Vetterab9412b2013-05-03 11:49:46 +02003709 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003710
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003711 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003712
Paulo Zanoni0540e482012-10-31 18:12:40 -02003713 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003714 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003715
Paulo Zanoni937bb612012-10-31 18:12:47 -02003716 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003717}
3718
Daniel Vetter716c2e52014-06-25 22:02:02 +03003719void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003720{
Daniel Vettere2b78262013-06-07 23:10:03 +02003721 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003722
3723 if (pll == NULL)
3724 return;
3725
3726 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003727 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003728 return;
3729 }
3730
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003731 if (--pll->refcount == 0) {
3732 WARN_ON(pll->on);
3733 WARN_ON(pll->active);
3734 }
3735
Daniel Vettera43f6e02013-06-07 23:10:32 +02003736 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003737}
3738
Daniel Vetter716c2e52014-06-25 22:02:02 +03003739struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003740{
Daniel Vettere2b78262013-06-07 23:10:03 +02003741 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3742 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3743 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003744
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003745 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003746 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3747 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003748 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003749 }
3750
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003751 if (HAS_PCH_IBX(dev_priv->dev)) {
3752 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003753 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003754 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003755
Daniel Vetter46edb022013-06-05 13:34:12 +02003756 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3757 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003758
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003759 WARN_ON(pll->refcount);
3760
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003761 goto found;
3762 }
3763
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003764 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3765 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003766
3767 /* Only want to check enabled timings first */
3768 if (pll->refcount == 0)
3769 continue;
3770
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003771 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3772 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003773 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003774 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003775 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003776
3777 goto found;
3778 }
3779 }
3780
3781 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003782 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3783 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003784 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003785 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3786 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003787 goto found;
3788 }
3789 }
3790
3791 return NULL;
3792
3793found:
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003794 if (pll->refcount == 0)
3795 pll->hw_state = crtc->config.dpll_hw_state;
3796
Daniel Vettera43f6e02013-06-07 23:10:32 +02003797 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003798 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3799 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003800
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003801 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003802
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003803 return pll;
3804}
3805
Daniel Vettera1520312013-05-03 11:49:50 +02003806static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003807{
3808 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003809 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003810 u32 temp;
3811
3812 temp = I915_READ(dslreg);
3813 udelay(500);
3814 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003815 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003816 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003817 }
3818}
3819
Jesse Barnesb074cec2013-04-25 12:55:02 -07003820static void ironlake_pfit_enable(struct intel_crtc *crtc)
3821{
3822 struct drm_device *dev = crtc->base.dev;
3823 struct drm_i915_private *dev_priv = dev->dev_private;
3824 int pipe = crtc->pipe;
3825
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003826 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003827 /* Force use of hard-coded filter coefficients
3828 * as some pre-programmed values are broken,
3829 * e.g. x201.
3830 */
3831 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3832 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3833 PF_PIPE_SEL_IVB(pipe));
3834 else
3835 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3836 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3837 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003838 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003839}
3840
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003841static void intel_enable_planes(struct drm_crtc *crtc)
3842{
3843 struct drm_device *dev = crtc->dev;
3844 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003845 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003846 struct intel_plane *intel_plane;
3847
Matt Roperaf2b6532014-04-01 15:22:32 -07003848 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3849 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003850 if (intel_plane->pipe == pipe)
3851 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003852 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003853}
3854
3855static void intel_disable_planes(struct drm_crtc *crtc)
3856{
3857 struct drm_device *dev = crtc->dev;
3858 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003859 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003860 struct intel_plane *intel_plane;
3861
Matt Roperaf2b6532014-04-01 15:22:32 -07003862 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3863 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003864 if (intel_plane->pipe == pipe)
3865 intel_plane_disable(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003866 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003867}
3868
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003869void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003870{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003871 struct drm_device *dev = crtc->base.dev;
3872 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03003873
3874 if (!crtc->config.ips_enabled)
3875 return;
3876
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003877 /* We can only enable IPS after we enable a plane and wait for a vblank */
3878 intel_wait_for_vblank(dev, crtc->pipe);
3879
Paulo Zanonid77e4532013-09-24 13:52:55 -03003880 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003881 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003882 mutex_lock(&dev_priv->rps.hw_lock);
3883 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3884 mutex_unlock(&dev_priv->rps.hw_lock);
3885 /* Quoting Art Runyan: "its not safe to expect any particular
3886 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003887 * mailbox." Moreover, the mailbox may return a bogus state,
3888 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003889 */
3890 } else {
3891 I915_WRITE(IPS_CTL, IPS_ENABLE);
3892 /* The bit only becomes 1 in the next vblank, so this wait here
3893 * is essentially intel_wait_for_vblank. If we don't have this
3894 * and don't wait for vblanks until the end of crtc_enable, then
3895 * the HW state readout code will complain that the expected
3896 * IPS_CTL value is not the one we read. */
3897 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3898 DRM_ERROR("Timed out waiting for IPS enable\n");
3899 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003900}
3901
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003902void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003903{
3904 struct drm_device *dev = crtc->base.dev;
3905 struct drm_i915_private *dev_priv = dev->dev_private;
3906
3907 if (!crtc->config.ips_enabled)
3908 return;
3909
3910 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003911 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003912 mutex_lock(&dev_priv->rps.hw_lock);
3913 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3914 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003915 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3916 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3917 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08003918 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003919 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08003920 POSTING_READ(IPS_CTL);
3921 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003922
3923 /* We need to wait for a vblank before we can disable the plane. */
3924 intel_wait_for_vblank(dev, crtc->pipe);
3925}
3926
3927/** Loads the palette/gamma unit for the CRTC with the prepared values */
3928static void intel_crtc_load_lut(struct drm_crtc *crtc)
3929{
3930 struct drm_device *dev = crtc->dev;
3931 struct drm_i915_private *dev_priv = dev->dev_private;
3932 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3933 enum pipe pipe = intel_crtc->pipe;
3934 int palreg = PALETTE(pipe);
3935 int i;
3936 bool reenable_ips = false;
3937
3938 /* The clocks have to be on to load the palette. */
3939 if (!crtc->enabled || !intel_crtc->active)
3940 return;
3941
3942 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3943 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3944 assert_dsi_pll_enabled(dev_priv);
3945 else
3946 assert_pll_enabled(dev_priv, pipe);
3947 }
3948
3949 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05303950 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03003951 palreg = LGC_PALETTE(pipe);
3952
3953 /* Workaround : Do not read or write the pipe palette/gamma data while
3954 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3955 */
Paulo Zanoni41e6fc42014-01-08 17:26:31 -02003956 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03003957 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3958 GAMMA_MODE_MODE_SPLIT)) {
3959 hsw_disable_ips(intel_crtc);
3960 reenable_ips = true;
3961 }
3962
3963 for (i = 0; i < 256; i++) {
3964 I915_WRITE(palreg + 4 * i,
3965 (intel_crtc->lut_r[i] << 16) |
3966 (intel_crtc->lut_g[i] << 8) |
3967 intel_crtc->lut_b[i]);
3968 }
3969
3970 if (reenable_ips)
3971 hsw_enable_ips(intel_crtc);
3972}
3973
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003974static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3975{
3976 if (!enable && intel_crtc->overlay) {
3977 struct drm_device *dev = intel_crtc->base.dev;
3978 struct drm_i915_private *dev_priv = dev->dev_private;
3979
3980 mutex_lock(&dev->struct_mutex);
3981 dev_priv->mm.interruptible = false;
3982 (void) intel_overlay_switch_off(intel_crtc->overlay);
3983 dev_priv->mm.interruptible = true;
3984 mutex_unlock(&dev->struct_mutex);
3985 }
3986
3987 /* Let userspace switch the overlay on again. In most cases userspace
3988 * has to recompute where to put it anyway.
3989 */
3990}
3991
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003992static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003993{
3994 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3996 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003997
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03003998 intel_enable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003999 intel_enable_planes(crtc);
4000 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004001 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004002
4003 hsw_enable_ips(intel_crtc);
4004
4005 mutex_lock(&dev->struct_mutex);
4006 intel_update_fbc(dev);
4007 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004008
4009 /*
4010 * FIXME: Once we grow proper nuclear flip support out of this we need
4011 * to compute the mask of flip planes precisely. For the time being
4012 * consider this a flip from a NULL plane.
4013 */
4014 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004015}
4016
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004017static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004018{
4019 struct drm_device *dev = crtc->dev;
4020 struct drm_i915_private *dev_priv = dev->dev_private;
4021 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4022 int pipe = intel_crtc->pipe;
4023 int plane = intel_crtc->plane;
4024
4025 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004026
4027 if (dev_priv->fbc.plane == plane)
4028 intel_disable_fbc(dev);
4029
4030 hsw_disable_ips(intel_crtc);
4031
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004032 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004033 intel_crtc_update_cursor(crtc, false);
4034 intel_disable_planes(crtc);
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03004035 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004036
Daniel Vetterf99d7062014-06-19 16:01:59 +02004037 /*
4038 * FIXME: Once we grow proper nuclear flip support out of this we need
4039 * to compute the mask of flip planes precisely. For the time being
4040 * consider this a flip to a NULL plane.
4041 */
4042 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004043}
4044
Jesse Barnesf67a5592011-01-05 10:31:48 -08004045static void ironlake_crtc_enable(struct drm_crtc *crtc)
4046{
4047 struct drm_device *dev = crtc->dev;
4048 struct drm_i915_private *dev_priv = dev->dev_private;
4049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004050 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004051 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004052
Daniel Vetter08a48462012-07-02 11:43:47 +02004053 WARN_ON(!crtc->enabled);
4054
Jesse Barnesf67a5592011-01-05 10:31:48 -08004055 if (intel_crtc->active)
4056 return;
4057
Daniel Vetterb14b1052014-04-24 23:55:13 +02004058 if (intel_crtc->config.has_pch_encoder)
4059 intel_prepare_shared_dpll(intel_crtc);
4060
Daniel Vetter29407aa2014-04-24 23:55:08 +02004061 if (intel_crtc->config.has_dp_encoder)
4062 intel_dp_set_m_n(intel_crtc);
4063
4064 intel_set_pipe_timings(intel_crtc);
4065
4066 if (intel_crtc->config.has_pch_encoder) {
4067 intel_cpu_transcoder_set_m_n(intel_crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07004068 &intel_crtc->config.fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004069 }
4070
4071 ironlake_set_pipeconf(crtc);
4072
Jesse Barnesf67a5592011-01-05 10:31:48 -08004073 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004074
4075 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4076 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4077
Daniel Vetterf6736a12013-06-05 13:34:30 +02004078 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004079 if (encoder->pre_enable)
4080 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004081
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004082 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004083 /* Note: FDI PLL enabling _must_ be done before we enable the
4084 * cpu pipes, hence this is separate from all the other fdi/pch
4085 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004086 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004087 } else {
4088 assert_fdi_tx_disabled(dev_priv, pipe);
4089 assert_fdi_rx_disabled(dev_priv, pipe);
4090 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004091
Jesse Barnesb074cec2013-04-25 12:55:02 -07004092 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004093
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004094 /*
4095 * On ILK+ LUT must be loaded before the pipe is running but with
4096 * clocks enabled
4097 */
4098 intel_crtc_load_lut(crtc);
4099
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004100 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004101 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004102
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004103 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004104 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004105
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004106 for_each_encoder_on_crtc(dev, crtc, encoder)
4107 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004108
4109 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004110 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004111
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004112 assert_vblank_disabled(crtc);
4113 drm_crtc_vblank_on(crtc);
4114
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004115 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004116}
4117
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004118/* IPS only exists on ULT machines and is tied to pipe A. */
4119static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4120{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004121 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004122}
4123
Paulo Zanonie4916942013-09-20 16:21:19 -03004124/*
4125 * This implements the workaround described in the "notes" section of the mode
4126 * set sequence documentation. When going from no pipes or single pipe to
4127 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4128 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4129 */
4130static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4131{
4132 struct drm_device *dev = crtc->base.dev;
4133 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4134
4135 /* We want to get the other_active_crtc only if there's only 1 other
4136 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004137 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004138 if (!crtc_it->active || crtc_it == crtc)
4139 continue;
4140
4141 if (other_active_crtc)
4142 return;
4143
4144 other_active_crtc = crtc_it;
4145 }
4146 if (!other_active_crtc)
4147 return;
4148
4149 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4150 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4151}
4152
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004153static void haswell_crtc_enable(struct drm_crtc *crtc)
4154{
4155 struct drm_device *dev = crtc->dev;
4156 struct drm_i915_private *dev_priv = dev->dev_private;
4157 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4158 struct intel_encoder *encoder;
4159 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004160
4161 WARN_ON(!crtc->enabled);
4162
4163 if (intel_crtc->active)
4164 return;
4165
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004166 if (intel_crtc_to_shared_dpll(intel_crtc))
4167 intel_enable_shared_dpll(intel_crtc);
4168
Daniel Vetter229fca92014-04-24 23:55:09 +02004169 if (intel_crtc->config.has_dp_encoder)
4170 intel_dp_set_m_n(intel_crtc);
4171
4172 intel_set_pipe_timings(intel_crtc);
4173
4174 if (intel_crtc->config.has_pch_encoder) {
4175 intel_cpu_transcoder_set_m_n(intel_crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07004176 &intel_crtc->config.fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004177 }
4178
4179 haswell_set_pipeconf(crtc);
4180
4181 intel_set_pipe_csc(crtc);
4182
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004183 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004184
4185 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004186 for_each_encoder_on_crtc(dev, crtc, encoder)
4187 if (encoder->pre_enable)
4188 encoder->pre_enable(encoder);
4189
Imre Deak4fe94672014-06-25 22:01:49 +03004190 if (intel_crtc->config.has_pch_encoder) {
4191 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4192 dev_priv->display.fdi_link_train(crtc);
4193 }
4194
Paulo Zanoni1f544382012-10-24 11:32:00 -02004195 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004196
Jesse Barnesb074cec2013-04-25 12:55:02 -07004197 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004198
4199 /*
4200 * On ILK+ LUT must be loaded before the pipe is running but with
4201 * clocks enabled
4202 */
4203 intel_crtc_load_lut(crtc);
4204
Paulo Zanoni1f544382012-10-24 11:32:00 -02004205 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004206 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004207
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004208 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004209 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004210
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004211 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004212 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004213
Dave Airlie0e32b392014-05-02 14:02:48 +10004214 if (intel_crtc->config.dp_encoder_is_mst)
4215 intel_ddi_set_vc_payload_alloc(crtc, true);
4216
Jani Nikula8807e552013-08-30 19:40:32 +03004217 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004218 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004219 intel_opregion_notify_encoder(encoder, true);
4220 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004221
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004222 assert_vblank_disabled(crtc);
4223 drm_crtc_vblank_on(crtc);
4224
Paulo Zanonie4916942013-09-20 16:21:19 -03004225 /* If we change the relative order between pipe/planes enabling, we need
4226 * to change the workaround. */
4227 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004228 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004229}
4230
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004231static void ironlake_pfit_disable(struct intel_crtc *crtc)
4232{
4233 struct drm_device *dev = crtc->base.dev;
4234 struct drm_i915_private *dev_priv = dev->dev_private;
4235 int pipe = crtc->pipe;
4236
4237 /* To avoid upsetting the power well on haswell only disable the pfit if
4238 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01004239 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004240 I915_WRITE(PF_CTL(pipe), 0);
4241 I915_WRITE(PF_WIN_POS(pipe), 0);
4242 I915_WRITE(PF_WIN_SZ(pipe), 0);
4243 }
4244}
4245
Jesse Barnes6be4a602010-09-10 10:26:01 -07004246static void ironlake_crtc_disable(struct drm_crtc *crtc)
4247{
4248 struct drm_device *dev = crtc->dev;
4249 struct drm_i915_private *dev_priv = dev->dev_private;
4250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004251 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004252 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004253 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004254
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004255 if (!intel_crtc->active)
4256 return;
4257
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004258 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004259
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004260 drm_crtc_vblank_off(crtc);
4261 assert_vblank_disabled(crtc);
4262
Daniel Vetterea9d7582012-07-10 10:42:52 +02004263 for_each_encoder_on_crtc(dev, crtc, encoder)
4264 encoder->disable(encoder);
4265
Daniel Vetterd925c592013-06-05 13:34:04 +02004266 if (intel_crtc->config.has_pch_encoder)
4267 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4268
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004269 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004270
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004271 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004272
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004273 for_each_encoder_on_crtc(dev, crtc, encoder)
4274 if (encoder->post_disable)
4275 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004276
Daniel Vetterd925c592013-06-05 13:34:04 +02004277 if (intel_crtc->config.has_pch_encoder) {
4278 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004279
Daniel Vetterd925c592013-06-05 13:34:04 +02004280 ironlake_disable_pch_transcoder(dev_priv, pipe);
4281 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004282
Daniel Vetterd925c592013-06-05 13:34:04 +02004283 if (HAS_PCH_CPT(dev)) {
4284 /* disable TRANS_DP_CTL */
4285 reg = TRANS_DP_CTL(pipe);
4286 temp = I915_READ(reg);
4287 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4288 TRANS_DP_PORT_SEL_MASK);
4289 temp |= TRANS_DP_PORT_SEL_NONE;
4290 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004291
Daniel Vetterd925c592013-06-05 13:34:04 +02004292 /* disable DPLL_SEL */
4293 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004294 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004295 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004296 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004297
4298 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004299 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004300
4301 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004302 }
4303
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004304 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004305 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004306
4307 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004308 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004309 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004310}
4311
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004312static void haswell_crtc_disable(struct drm_crtc *crtc)
4313{
4314 struct drm_device *dev = crtc->dev;
4315 struct drm_i915_private *dev_priv = dev->dev_private;
4316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4317 struct intel_encoder *encoder;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004318 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004319
4320 if (!intel_crtc->active)
4321 return;
4322
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004323 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004324
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004325 drm_crtc_vblank_off(crtc);
4326 assert_vblank_disabled(crtc);
4327
Jani Nikula8807e552013-08-30 19:40:32 +03004328 for_each_encoder_on_crtc(dev, crtc, encoder) {
4329 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004330 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004331 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004332
Paulo Zanoni86642812013-04-12 17:57:57 -03004333 if (intel_crtc->config.has_pch_encoder)
4334 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004335 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004336
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03004337 if (intel_crtc->config.dp_encoder_is_mst)
4338 intel_ddi_set_vc_payload_alloc(crtc, false);
4339
Paulo Zanoniad80a812012-10-24 16:06:19 -02004340 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004341
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004342 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004343
Paulo Zanoni1f544382012-10-24 11:32:00 -02004344 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004345
Daniel Vetter88adfff2013-03-28 10:42:01 +01004346 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004347 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03004348 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004349 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004350 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004351
Imre Deak97b040a2014-06-25 22:01:50 +03004352 for_each_encoder_on_crtc(dev, crtc, encoder)
4353 if (encoder->post_disable)
4354 encoder->post_disable(encoder);
4355
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004356 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004357 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004358
4359 mutex_lock(&dev->struct_mutex);
4360 intel_update_fbc(dev);
4361 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004362
4363 if (intel_crtc_to_shared_dpll(intel_crtc))
4364 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004365}
4366
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004367static void ironlake_crtc_off(struct drm_crtc *crtc)
4368{
4369 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004370 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004371}
4372
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004373
Jesse Barnes2dd24552013-04-25 12:55:01 -07004374static void i9xx_pfit_enable(struct intel_crtc *crtc)
4375{
4376 struct drm_device *dev = crtc->base.dev;
4377 struct drm_i915_private *dev_priv = dev->dev_private;
4378 struct intel_crtc_config *pipe_config = &crtc->config;
4379
Daniel Vetter328d8e82013-05-08 10:36:31 +02004380 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004381 return;
4382
Daniel Vetterc0b03412013-05-28 12:05:54 +02004383 /*
4384 * The panel fitter should only be adjusted whilst the pipe is disabled,
4385 * according to register description and PRM.
4386 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004387 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4388 assert_pipe_disabled(dev_priv, crtc->pipe);
4389
Jesse Barnesb074cec2013-04-25 12:55:02 -07004390 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4391 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004392
4393 /* Border color in case we don't scale up to the full screen. Black by
4394 * default, change to something else for debugging. */
4395 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004396}
4397
Dave Airlied05410f2014-06-05 13:22:59 +10004398static enum intel_display_power_domain port_to_power_domain(enum port port)
4399{
4400 switch (port) {
4401 case PORT_A:
4402 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4403 case PORT_B:
4404 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4405 case PORT_C:
4406 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4407 case PORT_D:
4408 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4409 default:
4410 WARN_ON_ONCE(1);
4411 return POWER_DOMAIN_PORT_OTHER;
4412 }
4413}
4414
Imre Deak77d22dc2014-03-05 16:20:52 +02004415#define for_each_power_domain(domain, mask) \
4416 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4417 if ((1 << (domain)) & (mask))
4418
Imre Deak319be8a2014-03-04 19:22:57 +02004419enum intel_display_power_domain
4420intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004421{
Imre Deak319be8a2014-03-04 19:22:57 +02004422 struct drm_device *dev = intel_encoder->base.dev;
4423 struct intel_digital_port *intel_dig_port;
4424
4425 switch (intel_encoder->type) {
4426 case INTEL_OUTPUT_UNKNOWN:
4427 /* Only DDI platforms should ever use this output type */
4428 WARN_ON_ONCE(!HAS_DDI(dev));
4429 case INTEL_OUTPUT_DISPLAYPORT:
4430 case INTEL_OUTPUT_HDMI:
4431 case INTEL_OUTPUT_EDP:
4432 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10004433 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10004434 case INTEL_OUTPUT_DP_MST:
4435 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4436 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02004437 case INTEL_OUTPUT_ANALOG:
4438 return POWER_DOMAIN_PORT_CRT;
4439 case INTEL_OUTPUT_DSI:
4440 return POWER_DOMAIN_PORT_DSI;
4441 default:
4442 return POWER_DOMAIN_PORT_OTHER;
4443 }
4444}
4445
4446static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4447{
4448 struct drm_device *dev = crtc->dev;
4449 struct intel_encoder *intel_encoder;
4450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4451 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02004452 unsigned long mask;
4453 enum transcoder transcoder;
4454
4455 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4456
4457 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4458 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Daniel Vetterfabf6e52014-05-29 14:10:22 +02004459 if (intel_crtc->config.pch_pfit.enabled ||
4460 intel_crtc->config.pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02004461 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4462
Imre Deak319be8a2014-03-04 19:22:57 +02004463 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4464 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4465
Imre Deak77d22dc2014-03-05 16:20:52 +02004466 return mask;
4467}
4468
4469void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4470 bool enable)
4471{
4472 if (dev_priv->power_domains.init_power_on == enable)
4473 return;
4474
4475 if (enable)
4476 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4477 else
4478 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4479
4480 dev_priv->power_domains.init_power_on = enable;
4481}
4482
4483static void modeset_update_crtc_power_domains(struct drm_device *dev)
4484{
4485 struct drm_i915_private *dev_priv = dev->dev_private;
4486 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4487 struct intel_crtc *crtc;
4488
4489 /*
4490 * First get all needed power domains, then put all unneeded, to avoid
4491 * any unnecessary toggling of the power wells.
4492 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004493 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004494 enum intel_display_power_domain domain;
4495
4496 if (!crtc->base.enabled)
4497 continue;
4498
Imre Deak319be8a2014-03-04 19:22:57 +02004499 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004500
4501 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4502 intel_display_power_get(dev_priv, domain);
4503 }
4504
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004505 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004506 enum intel_display_power_domain domain;
4507
4508 for_each_power_domain(domain, crtc->enabled_power_domains)
4509 intel_display_power_put(dev_priv, domain);
4510
4511 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4512 }
4513
4514 intel_display_set_init_power(dev_priv, false);
4515}
4516
Ville Syrjälädfcab172014-06-13 13:37:47 +03004517/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004518static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004519{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004520 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004521
Jesse Barnes586f49d2013-11-04 16:06:59 -08004522 /* Obtain SKU information */
4523 mutex_lock(&dev_priv->dpio_lock);
4524 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4525 CCK_FUSE_HPLL_FREQ_MASK;
4526 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004527
Ville Syrjälädfcab172014-06-13 13:37:47 +03004528 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004529}
4530
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004531static void vlv_update_cdclk(struct drm_device *dev)
4532{
4533 struct drm_i915_private *dev_priv = dev->dev_private;
4534
4535 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
4536 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
4537 dev_priv->vlv_cdclk_freq);
4538
4539 /*
4540 * Program the gmbus_freq based on the cdclk frequency.
4541 * BSpec erroneously claims we should aim for 4MHz, but
4542 * in fact 1MHz is the correct frequency.
4543 */
4544 I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
4545}
4546
Jesse Barnes30a970c2013-11-04 13:48:12 -08004547/* Adjust CDclk dividers to allow high res or save power if possible */
4548static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4549{
4550 struct drm_i915_private *dev_priv = dev->dev_private;
4551 u32 val, cmd;
4552
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03004553 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02004554
Ville Syrjälädfcab172014-06-13 13:37:47 +03004555 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08004556 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03004557 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004558 cmd = 1;
4559 else
4560 cmd = 0;
4561
4562 mutex_lock(&dev_priv->rps.hw_lock);
4563 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4564 val &= ~DSPFREQGUAR_MASK;
4565 val |= (cmd << DSPFREQGUAR_SHIFT);
4566 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4567 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4568 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4569 50)) {
4570 DRM_ERROR("timed out waiting for CDclk change\n");
4571 }
4572 mutex_unlock(&dev_priv->rps.hw_lock);
4573
Ville Syrjälädfcab172014-06-13 13:37:47 +03004574 if (cdclk == 400000) {
Jesse Barnes30a970c2013-11-04 13:48:12 -08004575 u32 divider, vco;
4576
4577 vco = valleyview_get_vco(dev_priv);
Ville Syrjälädfcab172014-06-13 13:37:47 +03004578 divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004579
4580 mutex_lock(&dev_priv->dpio_lock);
4581 /* adjust cdclk divider */
4582 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03004583 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004584 val |= divider;
4585 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03004586
4587 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4588 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4589 50))
4590 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08004591 mutex_unlock(&dev_priv->dpio_lock);
4592 }
4593
4594 mutex_lock(&dev_priv->dpio_lock);
4595 /* adjust self-refresh exit latency value */
4596 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4597 val &= ~0x7f;
4598
4599 /*
4600 * For high bandwidth configs, we set a higher latency in the bunit
4601 * so that the core display fetch happens in time to avoid underruns.
4602 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03004603 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004604 val |= 4500 / 250; /* 4.5 usec */
4605 else
4606 val |= 3000 / 250; /* 3.0 usec */
4607 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4608 mutex_unlock(&dev_priv->dpio_lock);
4609
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004610 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004611}
4612
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004613static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4614{
4615 struct drm_i915_private *dev_priv = dev->dev_private;
4616 u32 val, cmd;
4617
4618 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4619
4620 switch (cdclk) {
4621 case 400000:
4622 cmd = 3;
4623 break;
4624 case 333333:
4625 case 320000:
4626 cmd = 2;
4627 break;
4628 case 266667:
4629 cmd = 1;
4630 break;
4631 case 200000:
4632 cmd = 0;
4633 break;
4634 default:
4635 WARN_ON(1);
4636 return;
4637 }
4638
4639 mutex_lock(&dev_priv->rps.hw_lock);
4640 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4641 val &= ~DSPFREQGUAR_MASK_CHV;
4642 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4643 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4644 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4645 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4646 50)) {
4647 DRM_ERROR("timed out waiting for CDclk change\n");
4648 }
4649 mutex_unlock(&dev_priv->rps.hw_lock);
4650
4651 vlv_update_cdclk(dev);
4652}
4653
Jesse Barnes30a970c2013-11-04 13:48:12 -08004654static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4655 int max_pixclk)
4656{
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004657 int vco = valleyview_get_vco(dev_priv);
4658 int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
4659
Ville Syrjäläd49a3402014-06-28 02:03:58 +03004660 /* FIXME: Punit isn't quite ready yet */
4661 if (IS_CHERRYVIEW(dev_priv->dev))
4662 return 400000;
4663
Jesse Barnes30a970c2013-11-04 13:48:12 -08004664 /*
4665 * Really only a few cases to deal with, as only 4 CDclks are supported:
4666 * 200MHz
4667 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004668 * 320/333MHz (depends on HPLL freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004669 * 400MHz
4670 * So we check to see whether we're above 90% of the lower bin and
4671 * adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004672 *
4673 * We seem to get an unstable or solid color picture at 200MHz.
4674 * Not sure what's wrong. For now use 200MHz only when all pipes
4675 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08004676 */
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004677 if (max_pixclk > freq_320*9/10)
Ville Syrjälädfcab172014-06-13 13:37:47 +03004678 return 400000;
4679 else if (max_pixclk > 266667*9/10)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004680 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004681 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03004682 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004683 else
4684 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004685}
4686
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004687/* compute the max pixel clock for new configuration */
4688static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004689{
4690 struct drm_device *dev = dev_priv->dev;
4691 struct intel_crtc *intel_crtc;
4692 int max_pixclk = 0;
4693
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004694 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004695 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004696 max_pixclk = max(max_pixclk,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004697 intel_crtc->new_config->adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004698 }
4699
4700 return max_pixclk;
4701}
4702
4703static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004704 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004705{
4706 struct drm_i915_private *dev_priv = dev->dev_private;
4707 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004708 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004709
Imre Deakd60c4472014-03-27 17:45:10 +02004710 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4711 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004712 return;
4713
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004714 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004715 for_each_intel_crtc(dev, intel_crtc)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004716 if (intel_crtc->base.enabled)
4717 *prepare_pipes |= (1 << intel_crtc->pipe);
4718}
4719
4720static void valleyview_modeset_global_resources(struct drm_device *dev)
4721{
4722 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004723 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004724 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4725
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004726 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
4727 if (IS_CHERRYVIEW(dev))
4728 cherryview_set_cdclk(dev, req_cdclk);
4729 else
4730 valleyview_set_cdclk(dev, req_cdclk);
4731 }
4732
Imre Deak77961eb2014-03-05 16:20:56 +02004733 modeset_update_crtc_power_domains(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004734}
4735
Jesse Barnes89b667f2013-04-18 14:51:36 -07004736static void valleyview_crtc_enable(struct drm_crtc *crtc)
4737{
4738 struct drm_device *dev = crtc->dev;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4740 struct intel_encoder *encoder;
4741 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03004742 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004743
4744 WARN_ON(!crtc->enabled);
4745
4746 if (intel_crtc->active)
4747 return;
4748
Shobhit Kumar8525a232014-06-25 12:20:39 +05304749 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4750
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03004751 if (!is_dsi) {
4752 if (IS_CHERRYVIEW(dev))
4753 chv_prepare_pll(intel_crtc);
4754 else
4755 vlv_prepare_pll(intel_crtc);
4756 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02004757
4758 if (intel_crtc->config.has_dp_encoder)
4759 intel_dp_set_m_n(intel_crtc);
4760
4761 intel_set_pipe_timings(intel_crtc);
4762
Daniel Vetter5b18e572014-04-24 23:55:06 +02004763 i9xx_set_pipeconf(intel_crtc);
4764
Jesse Barnes89b667f2013-04-18 14:51:36 -07004765 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004766
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004767 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4768
Jesse Barnes89b667f2013-04-18 14:51:36 -07004769 for_each_encoder_on_crtc(dev, crtc, encoder)
4770 if (encoder->pre_pll_enable)
4771 encoder->pre_pll_enable(encoder);
4772
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004773 if (!is_dsi) {
4774 if (IS_CHERRYVIEW(dev))
4775 chv_enable_pll(intel_crtc);
4776 else
4777 vlv_enable_pll(intel_crtc);
4778 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07004779
4780 for_each_encoder_on_crtc(dev, crtc, encoder)
4781 if (encoder->pre_enable)
4782 encoder->pre_enable(encoder);
4783
Jesse Barnes2dd24552013-04-25 12:55:01 -07004784 i9xx_pfit_enable(intel_crtc);
4785
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004786 intel_crtc_load_lut(crtc);
4787
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004788 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004789 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004790
Jani Nikula50049452013-07-30 12:20:32 +03004791 for_each_encoder_on_crtc(dev, crtc, encoder)
4792 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004793
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004794 assert_vblank_disabled(crtc);
4795 drm_crtc_vblank_on(crtc);
4796
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004797 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004798
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004799 /* Underruns don't raise interrupts, so check manually. */
4800 i9xx_check_fifo_underruns(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004801}
4802
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004803static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4804{
4805 struct drm_device *dev = crtc->base.dev;
4806 struct drm_i915_private *dev_priv = dev->dev_private;
4807
4808 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4809 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4810}
4811
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004812static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004813{
4814 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08004815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004816 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004817 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004818
Daniel Vetter08a48462012-07-02 11:43:47 +02004819 WARN_ON(!crtc->enabled);
4820
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004821 if (intel_crtc->active)
4822 return;
4823
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004824 i9xx_set_pll_dividers(intel_crtc);
4825
Daniel Vetter5b18e572014-04-24 23:55:06 +02004826 if (intel_crtc->config.has_dp_encoder)
4827 intel_dp_set_m_n(intel_crtc);
4828
4829 intel_set_pipe_timings(intel_crtc);
4830
Daniel Vetter5b18e572014-04-24 23:55:06 +02004831 i9xx_set_pipeconf(intel_crtc);
4832
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004833 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004834
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004835 if (!IS_GEN2(dev))
4836 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4837
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004838 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004839 if (encoder->pre_enable)
4840 encoder->pre_enable(encoder);
4841
Daniel Vetterf6736a12013-06-05 13:34:30 +02004842 i9xx_enable_pll(intel_crtc);
4843
Jesse Barnes2dd24552013-04-25 12:55:01 -07004844 i9xx_pfit_enable(intel_crtc);
4845
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004846 intel_crtc_load_lut(crtc);
4847
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004848 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004849 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004850
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004851 for_each_encoder_on_crtc(dev, crtc, encoder)
4852 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004853
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004854 assert_vblank_disabled(crtc);
4855 drm_crtc_vblank_on(crtc);
4856
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004857 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004858
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004859 /*
4860 * Gen2 reports pipe underruns whenever all planes are disabled.
4861 * So don't enable underrun reporting before at least some planes
4862 * are enabled.
4863 * FIXME: Need to fix the logic to work when we turn off all planes
4864 * but leave the pipe running.
4865 */
4866 if (IS_GEN2(dev))
4867 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4868
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004869 /* Underruns don't raise interrupts, so check manually. */
4870 i9xx_check_fifo_underruns(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004871}
4872
Daniel Vetter87476d62013-04-11 16:29:06 +02004873static void i9xx_pfit_disable(struct intel_crtc *crtc)
4874{
4875 struct drm_device *dev = crtc->base.dev;
4876 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004877
4878 if (!crtc->config.gmch_pfit.control)
4879 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004880
4881 assert_pipe_disabled(dev_priv, crtc->pipe);
4882
Daniel Vetter328d8e82013-05-08 10:36:31 +02004883 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4884 I915_READ(PFIT_CONTROL));
4885 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004886}
4887
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004888static void i9xx_crtc_disable(struct drm_crtc *crtc)
4889{
4890 struct drm_device *dev = crtc->dev;
4891 struct drm_i915_private *dev_priv = dev->dev_private;
4892 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004893 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004894 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004895
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004896 if (!intel_crtc->active)
4897 return;
4898
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004899 /*
4900 * Gen2 reports pipe underruns whenever all planes are disabled.
4901 * So diasble underrun reporting before all the planes get disabled.
4902 * FIXME: Need to fix the logic to work when we turn off all planes
4903 * but leave the pipe running.
4904 */
4905 if (IS_GEN2(dev))
4906 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4907
Imre Deak564ed192014-06-13 14:54:21 +03004908 /*
4909 * Vblank time updates from the shadow to live plane control register
4910 * are blocked if the memory self-refresh mode is active at that
4911 * moment. So to make sure the plane gets truly disabled, disable
4912 * first the self-refresh mode. The self-refresh enable bit in turn
4913 * will be checked/applied by the HW only at the next frame start
4914 * event which is after the vblank start event, so we need to have a
4915 * wait-for-vblank between disabling the plane and the pipe.
4916 */
4917 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004918 intel_crtc_disable_planes(crtc);
4919
Ville Syrjälä6304cd92014-04-25 13:30:12 +03004920 /*
4921 * On gen2 planes are double buffered but the pipe isn't, so we must
4922 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03004923 * We also need to wait on all gmch platforms because of the
4924 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03004925 */
Imre Deak564ed192014-06-13 14:54:21 +03004926 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03004927
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004928 drm_crtc_vblank_off(crtc);
4929 assert_vblank_disabled(crtc);
4930
4931 for_each_encoder_on_crtc(dev, crtc, encoder)
4932 encoder->disable(encoder);
4933
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004934 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004935
Daniel Vetter87476d62013-04-11 16:29:06 +02004936 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004937
Jesse Barnes89b667f2013-04-18 14:51:36 -07004938 for_each_encoder_on_crtc(dev, crtc, encoder)
4939 if (encoder->post_disable)
4940 encoder->post_disable(encoder);
4941
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03004942 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4943 if (IS_CHERRYVIEW(dev))
4944 chv_disable_pll(dev_priv, pipe);
4945 else if (IS_VALLEYVIEW(dev))
4946 vlv_disable_pll(dev_priv, pipe);
4947 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03004948 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03004949 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004950
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004951 if (!IS_GEN2(dev))
4952 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4953
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004954 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004955 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004956
Daniel Vetterefa96242014-04-24 23:55:02 +02004957 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004958 intel_update_fbc(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02004959 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004960}
4961
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004962static void i9xx_crtc_off(struct drm_crtc *crtc)
4963{
4964}
4965
Daniel Vetter976f8a22012-07-08 22:34:21 +02004966static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4967 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004968{
4969 struct drm_device *dev = crtc->dev;
4970 struct drm_i915_master_private *master_priv;
4971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4972 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004973
4974 if (!dev->primary->master)
4975 return;
4976
4977 master_priv = dev->primary->master->driver_priv;
4978 if (!master_priv->sarea_priv)
4979 return;
4980
Jesse Barnes79e53942008-11-07 14:24:08 -08004981 switch (pipe) {
4982 case 0:
4983 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4984 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4985 break;
4986 case 1:
4987 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4988 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4989 break;
4990 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004991 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004992 break;
4993 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004994}
4995
Borun Fub04c5bd2014-07-12 10:02:27 +05304996/* Master function to enable/disable CRTC and corresponding power wells */
4997void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004998{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004999 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005000 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005002 enum intel_display_power_domain domain;
5003 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005004
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005005 if (enable) {
5006 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005007 domains = get_crtc_power_domains(crtc);
5008 for_each_power_domain(domain, domains)
5009 intel_display_power_get(dev_priv, domain);
5010 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005011
5012 dev_priv->display.crtc_enable(crtc);
5013 }
5014 } else {
5015 if (intel_crtc->active) {
5016 dev_priv->display.crtc_disable(crtc);
5017
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005018 domains = intel_crtc->enabled_power_domains;
5019 for_each_power_domain(domain, domains)
5020 intel_display_power_put(dev_priv, domain);
5021 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005022 }
5023 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305024}
5025
5026/**
5027 * Sets the power management mode of the pipe and plane.
5028 */
5029void intel_crtc_update_dpms(struct drm_crtc *crtc)
5030{
5031 struct drm_device *dev = crtc->dev;
5032 struct intel_encoder *intel_encoder;
5033 bool enable = false;
5034
5035 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5036 enable |= intel_encoder->connectors_active;
5037
5038 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005039
5040 intel_crtc_update_sarea(crtc, enable);
5041}
5042
Daniel Vetter976f8a22012-07-08 22:34:21 +02005043static void intel_crtc_disable(struct drm_crtc *crtc)
5044{
5045 struct drm_device *dev = crtc->dev;
5046 struct drm_connector *connector;
5047 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2ff8fde2014-07-08 07:50:07 -07005048 struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
Daniel Vettera071fa02014-06-18 23:28:09 +02005049 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005050
5051 /* crtc should still be enabled when we disable it. */
5052 WARN_ON(!crtc->enabled);
5053
5054 dev_priv->display.crtc_disable(crtc);
5055 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005056 dev_priv->display.off(crtc);
5057
Matt Roperf4510a22014-04-01 15:22:40 -07005058 if (crtc->primary->fb) {
Chris Wilsoncdd59982010-09-08 16:30:16 +01005059 mutex_lock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +02005060 intel_unpin_fb_obj(old_obj);
5061 i915_gem_track_fb(old_obj, NULL,
5062 INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilsoncdd59982010-09-08 16:30:16 +01005063 mutex_unlock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07005064 crtc->primary->fb = NULL;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005065 }
5066
5067 /* Update computed state. */
5068 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5069 if (!connector->encoder || !connector->encoder->crtc)
5070 continue;
5071
5072 if (connector->encoder->crtc != crtc)
5073 continue;
5074
5075 connector->dpms = DRM_MODE_DPMS_OFF;
5076 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01005077 }
5078}
5079
Chris Wilsonea5b2132010-08-04 13:50:23 +01005080void intel_encoder_destroy(struct drm_encoder *encoder)
5081{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005082 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005083
Chris Wilsonea5b2132010-08-04 13:50:23 +01005084 drm_encoder_cleanup(encoder);
5085 kfree(intel_encoder);
5086}
5087
Damien Lespiau92373292013-08-08 22:28:57 +01005088/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005089 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5090 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01005091static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005092{
5093 if (mode == DRM_MODE_DPMS_ON) {
5094 encoder->connectors_active = true;
5095
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005096 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005097 } else {
5098 encoder->connectors_active = false;
5099
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005100 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005101 }
5102}
5103
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005104/* Cross check the actual hw state with our own modeset state tracking (and it's
5105 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02005106static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005107{
5108 if (connector->get_hw_state(connector)) {
5109 struct intel_encoder *encoder = connector->encoder;
5110 struct drm_crtc *crtc;
5111 bool encoder_enabled;
5112 enum pipe pipe;
5113
5114 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5115 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005116 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005117
Dave Airlie0e32b392014-05-02 14:02:48 +10005118 /* there is no real hw state for MST connectors */
5119 if (connector->mst_port)
5120 return;
5121
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005122 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5123 "wrong connector dpms state\n");
5124 WARN(connector->base.encoder != &encoder->base,
5125 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005126
Dave Airlie36cd7442014-05-02 13:44:18 +10005127 if (encoder) {
5128 WARN(!encoder->connectors_active,
5129 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005130
Dave Airlie36cd7442014-05-02 13:44:18 +10005131 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5132 WARN(!encoder_enabled, "encoder not enabled\n");
5133 if (WARN_ON(!encoder->base.crtc))
5134 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005135
Dave Airlie36cd7442014-05-02 13:44:18 +10005136 crtc = encoder->base.crtc;
5137
5138 WARN(!crtc->enabled, "crtc not enabled\n");
5139 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5140 WARN(pipe != to_intel_crtc(crtc)->pipe,
5141 "encoder active on the wrong pipe\n");
5142 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005143 }
5144}
5145
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005146/* Even simpler default implementation, if there's really no special case to
5147 * consider. */
5148void intel_connector_dpms(struct drm_connector *connector, int mode)
5149{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005150 /* All the simple cases only support two dpms states. */
5151 if (mode != DRM_MODE_DPMS_ON)
5152 mode = DRM_MODE_DPMS_OFF;
5153
5154 if (mode == connector->dpms)
5155 return;
5156
5157 connector->dpms = mode;
5158
5159 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01005160 if (connector->encoder)
5161 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005162
Daniel Vetterb9805142012-08-31 17:37:33 +02005163 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005164}
5165
Daniel Vetterf0947c32012-07-02 13:10:34 +02005166/* Simple connector->get_hw_state implementation for encoders that support only
5167 * one connector and no cloning and hence the encoder state determines the state
5168 * of the connector. */
5169bool intel_connector_get_hw_state(struct intel_connector *connector)
5170{
Daniel Vetter24929352012-07-02 20:28:59 +02005171 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005172 struct intel_encoder *encoder = connector->encoder;
5173
5174 return encoder->get_hw_state(encoder, &pipe);
5175}
5176
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005177static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5178 struct intel_crtc_config *pipe_config)
5179{
5180 struct drm_i915_private *dev_priv = dev->dev_private;
5181 struct intel_crtc *pipe_B_crtc =
5182 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5183
5184 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5185 pipe_name(pipe), pipe_config->fdi_lanes);
5186 if (pipe_config->fdi_lanes > 4) {
5187 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5188 pipe_name(pipe), pipe_config->fdi_lanes);
5189 return false;
5190 }
5191
Paulo Zanonibafb6552013-11-02 21:07:44 -07005192 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005193 if (pipe_config->fdi_lanes > 2) {
5194 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5195 pipe_config->fdi_lanes);
5196 return false;
5197 } else {
5198 return true;
5199 }
5200 }
5201
5202 if (INTEL_INFO(dev)->num_pipes == 2)
5203 return true;
5204
5205 /* Ivybridge 3 pipe is really complicated */
5206 switch (pipe) {
5207 case PIPE_A:
5208 return true;
5209 case PIPE_B:
5210 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5211 pipe_config->fdi_lanes > 2) {
5212 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5213 pipe_name(pipe), pipe_config->fdi_lanes);
5214 return false;
5215 }
5216 return true;
5217 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01005218 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005219 pipe_B_crtc->config.fdi_lanes <= 2) {
5220 if (pipe_config->fdi_lanes > 2) {
5221 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5222 pipe_name(pipe), pipe_config->fdi_lanes);
5223 return false;
5224 }
5225 } else {
5226 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5227 return false;
5228 }
5229 return true;
5230 default:
5231 BUG();
5232 }
5233}
5234
Daniel Vettere29c22c2013-02-21 00:00:16 +01005235#define RETRY 1
5236static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5237 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005238{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005239 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005240 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005241 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005242 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005243
Daniel Vettere29c22c2013-02-21 00:00:16 +01005244retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005245 /* FDI is a binary signal running at ~2.7GHz, encoding
5246 * each output octet as 10 bits. The actual frequency
5247 * is stored as a divider into a 100MHz clock, and the
5248 * mode pixel clock is stored in units of 1KHz.
5249 * Hence the bw of each lane in terms of the mode signal
5250 * is:
5251 */
5252 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5253
Damien Lespiau241bfc32013-09-25 16:45:37 +01005254 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005255
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005256 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005257 pipe_config->pipe_bpp);
5258
5259 pipe_config->fdi_lanes = lane;
5260
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005261 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005262 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005263
Daniel Vettere29c22c2013-02-21 00:00:16 +01005264 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5265 intel_crtc->pipe, pipe_config);
5266 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5267 pipe_config->pipe_bpp -= 2*3;
5268 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5269 pipe_config->pipe_bpp);
5270 needs_recompute = true;
5271 pipe_config->bw_constrained = true;
5272
5273 goto retry;
5274 }
5275
5276 if (needs_recompute)
5277 return RETRY;
5278
5279 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005280}
5281
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005282static void hsw_compute_ips_config(struct intel_crtc *crtc,
5283 struct intel_crtc_config *pipe_config)
5284{
Jani Nikulad330a952014-01-21 11:24:25 +02005285 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005286 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005287 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005288}
5289
Daniel Vettera43f6e02013-06-07 23:10:32 +02005290static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01005291 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005292{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005293 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005294 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005295
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005296 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005297 if (INTEL_INFO(dev)->gen < 4) {
5298 struct drm_i915_private *dev_priv = dev->dev_private;
5299 int clock_limit =
5300 dev_priv->display.get_display_clock_speed(dev);
5301
5302 /*
5303 * Enable pixel doubling when the dot clock
5304 * is > 90% of the (display) core speed.
5305 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005306 * GDG double wide on either pipe,
5307 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005308 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005309 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005310 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005311 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005312 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005313 }
5314
Damien Lespiau241bfc32013-09-25 16:45:37 +01005315 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005316 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005317 }
Chris Wilson89749352010-09-12 18:25:19 +01005318
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005319 /*
5320 * Pipe horizontal size must be even in:
5321 * - DVO ganged mode
5322 * - LVDS dual channel mode
5323 * - Double wide pipe
5324 */
5325 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5326 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5327 pipe_config->pipe_src_w &= ~1;
5328
Damien Lespiau8693a822013-05-03 18:48:11 +01005329 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5330 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005331 */
5332 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5333 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005334 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005335
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005336 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005337 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005338 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005339 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5340 * for lvds. */
5341 pipe_config->pipe_bpp = 8*3;
5342 }
5343
Damien Lespiauf5adf942013-06-24 18:29:34 +01005344 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005345 hsw_compute_ips_config(crtc, pipe_config);
5346
Daniel Vetter12030432014-06-25 22:02:00 +03005347 /*
5348 * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
5349 * old clock survives for now.
5350 */
5351 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005352 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005353
Daniel Vetter877d48d2013-04-19 11:24:43 +02005354 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005355 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005356
Daniel Vettere29c22c2013-02-21 00:00:16 +01005357 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005358}
5359
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005360static int valleyview_get_display_clock_speed(struct drm_device *dev)
5361{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005362 struct drm_i915_private *dev_priv = dev->dev_private;
5363 int vco = valleyview_get_vco(dev_priv);
5364 u32 val;
5365 int divider;
5366
Ville Syrjäläd49a3402014-06-28 02:03:58 +03005367 /* FIXME: Punit isn't quite ready yet */
5368 if (IS_CHERRYVIEW(dev))
5369 return 400000;
5370
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005371 mutex_lock(&dev_priv->dpio_lock);
5372 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5373 mutex_unlock(&dev_priv->dpio_lock);
5374
5375 divider = val & DISPLAY_FREQUENCY_VALUES;
5376
Ville Syrjälä7d007f42014-06-13 13:37:53 +03005377 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5378 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5379 "cdclk change in progress\n");
5380
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005381 return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005382}
5383
Jesse Barnese70236a2009-09-21 10:42:27 -07005384static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005385{
Jesse Barnese70236a2009-09-21 10:42:27 -07005386 return 400000;
5387}
Jesse Barnes79e53942008-11-07 14:24:08 -08005388
Jesse Barnese70236a2009-09-21 10:42:27 -07005389static int i915_get_display_clock_speed(struct drm_device *dev)
5390{
5391 return 333000;
5392}
Jesse Barnes79e53942008-11-07 14:24:08 -08005393
Jesse Barnese70236a2009-09-21 10:42:27 -07005394static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5395{
5396 return 200000;
5397}
Jesse Barnes79e53942008-11-07 14:24:08 -08005398
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005399static int pnv_get_display_clock_speed(struct drm_device *dev)
5400{
5401 u16 gcfgc = 0;
5402
5403 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5404
5405 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5406 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5407 return 267000;
5408 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5409 return 333000;
5410 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5411 return 444000;
5412 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5413 return 200000;
5414 default:
5415 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5416 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5417 return 133000;
5418 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5419 return 167000;
5420 }
5421}
5422
Jesse Barnese70236a2009-09-21 10:42:27 -07005423static int i915gm_get_display_clock_speed(struct drm_device *dev)
5424{
5425 u16 gcfgc = 0;
5426
5427 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5428
5429 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005430 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005431 else {
5432 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5433 case GC_DISPLAY_CLOCK_333_MHZ:
5434 return 333000;
5435 default:
5436 case GC_DISPLAY_CLOCK_190_200_MHZ:
5437 return 190000;
5438 }
5439 }
5440}
Jesse Barnes79e53942008-11-07 14:24:08 -08005441
Jesse Barnese70236a2009-09-21 10:42:27 -07005442static int i865_get_display_clock_speed(struct drm_device *dev)
5443{
5444 return 266000;
5445}
5446
5447static int i855_get_display_clock_speed(struct drm_device *dev)
5448{
5449 u16 hpllcc = 0;
5450 /* Assume that the hardware is in the high speed state. This
5451 * should be the default.
5452 */
5453 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5454 case GC_CLOCK_133_200:
5455 case GC_CLOCK_100_200:
5456 return 200000;
5457 case GC_CLOCK_166_250:
5458 return 250000;
5459 case GC_CLOCK_100_133:
5460 return 133000;
5461 }
5462
5463 /* Shouldn't happen */
5464 return 0;
5465}
5466
5467static int i830_get_display_clock_speed(struct drm_device *dev)
5468{
5469 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005470}
5471
Zhenyu Wang2c072452009-06-05 15:38:42 +08005472static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005473intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005474{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005475 while (*num > DATA_LINK_M_N_MASK ||
5476 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005477 *num >>= 1;
5478 *den >>= 1;
5479 }
5480}
5481
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005482static void compute_m_n(unsigned int m, unsigned int n,
5483 uint32_t *ret_m, uint32_t *ret_n)
5484{
5485 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5486 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5487 intel_reduce_m_n_ratio(ret_m, ret_n);
5488}
5489
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005490void
5491intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5492 int pixel_clock, int link_clock,
5493 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005494{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005495 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005496
5497 compute_m_n(bits_per_pixel * pixel_clock,
5498 link_clock * nlanes * 8,
5499 &m_n->gmch_m, &m_n->gmch_n);
5500
5501 compute_m_n(pixel_clock, link_clock,
5502 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005503}
5504
Chris Wilsona7615032011-01-12 17:04:08 +00005505static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5506{
Jani Nikulad330a952014-01-21 11:24:25 +02005507 if (i915.panel_use_ssc >= 0)
5508 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005509 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005510 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005511}
5512
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005513static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5514{
5515 struct drm_device *dev = crtc->dev;
5516 struct drm_i915_private *dev_priv = dev->dev_private;
5517 int refclk;
5518
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005519 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005520 refclk = 100000;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005521 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005522 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005523 refclk = dev_priv->vbt.lvds_ssc_freq;
5524 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005525 } else if (!IS_GEN2(dev)) {
5526 refclk = 96000;
5527 } else {
5528 refclk = 48000;
5529 }
5530
5531 return refclk;
5532}
5533
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005534static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005535{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005536 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005537}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005538
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005539static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5540{
5541 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005542}
5543
Daniel Vetterf47709a2013-03-28 10:42:02 +01005544static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005545 intel_clock_t *reduced_clock)
5546{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005547 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005548 u32 fp, fp2 = 0;
5549
5550 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005551 fp = pnv_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 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005554 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005555 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005556 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005557 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005558 }
5559
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005560 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005561
Daniel Vetterf47709a2013-03-28 10:42:02 +01005562 crtc->lowfreq_avail = false;
5563 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005564 reduced_clock && i915.powersave) {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005565 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005566 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005567 } else {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005568 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005569 }
5570}
5571
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005572static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5573 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005574{
5575 u32 reg_val;
5576
5577 /*
5578 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5579 * and set it to a reasonable value instead.
5580 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005581 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005582 reg_val &= 0xffffff00;
5583 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005584 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005585
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005586 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005587 reg_val &= 0x8cffffff;
5588 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005589 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, 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_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005592 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005593 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005594
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005595 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005596 reg_val &= 0x00ffffff;
5597 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005598 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005599}
5600
Daniel Vetterb5518422013-05-03 11:49:48 +02005601static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5602 struct intel_link_m_n *m_n)
5603{
5604 struct drm_device *dev = crtc->base.dev;
5605 struct drm_i915_private *dev_priv = dev->dev_private;
5606 int pipe = crtc->pipe;
5607
Daniel Vettere3b95f12013-05-03 11:49:49 +02005608 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5609 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5610 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5611 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005612}
5613
5614static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07005615 struct intel_link_m_n *m_n,
5616 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02005617{
5618 struct drm_device *dev = crtc->base.dev;
5619 struct drm_i915_private *dev_priv = dev->dev_private;
5620 int pipe = crtc->pipe;
5621 enum transcoder transcoder = crtc->config.cpu_transcoder;
5622
5623 if (INTEL_INFO(dev)->gen >= 5) {
5624 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5625 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5626 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5627 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07005628 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5629 * for gen < 8) and if DRRS is supported (to make sure the
5630 * registers are not unnecessarily accessed).
5631 */
5632 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
5633 crtc->config.has_drrs) {
5634 I915_WRITE(PIPE_DATA_M2(transcoder),
5635 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5636 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5637 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5638 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5639 }
Daniel Vetterb5518422013-05-03 11:49:48 +02005640 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02005641 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5642 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5643 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5644 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005645 }
5646}
5647
Vandana Kannanf769cd22014-08-05 07:51:22 -07005648void intel_dp_set_m_n(struct intel_crtc *crtc)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005649{
5650 if (crtc->config.has_pch_encoder)
5651 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5652 else
Vandana Kannanf769cd22014-08-05 07:51:22 -07005653 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
5654 &crtc->config.dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005655}
5656
Daniel Vetterf47709a2013-03-28 10:42:02 +01005657static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005658{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005659 u32 dpll, dpll_md;
5660
5661 /*
5662 * Enable DPIO clock input. We should never disable the reference
5663 * clock for pipe B, since VGA hotplug / manual detection depends
5664 * on it.
5665 */
5666 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5667 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5668 /* We should never disable this, set it here for state tracking */
5669 if (crtc->pipe == PIPE_B)
5670 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5671 dpll |= DPLL_VCO_ENABLE;
5672 crtc->config.dpll_hw_state.dpll = dpll;
5673
5674 dpll_md = (crtc->config.pixel_multiplier - 1)
5675 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5676 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5677}
5678
5679static void vlv_prepare_pll(struct intel_crtc *crtc)
5680{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005681 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005682 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005683 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005684 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005685 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005686 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005687
Daniel Vetter09153002012-12-12 14:06:44 +01005688 mutex_lock(&dev_priv->dpio_lock);
5689
Daniel Vetterf47709a2013-03-28 10:42:02 +01005690 bestn = crtc->config.dpll.n;
5691 bestm1 = crtc->config.dpll.m1;
5692 bestm2 = crtc->config.dpll.m2;
5693 bestp1 = crtc->config.dpll.p1;
5694 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005695
Jesse Barnes89b667f2013-04-18 14:51:36 -07005696 /* See eDP HDMI DPIO driver vbios notes doc */
5697
5698 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005699 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005700 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005701
5702 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005703 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005704
5705 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005706 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005707 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005708 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005709
5710 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005711 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005712
5713 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005714 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5715 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5716 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005717 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07005718
5719 /*
5720 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5721 * but we don't support that).
5722 * Note: don't use the DAC post divider as it seems unstable.
5723 */
5724 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005725 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005726
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005727 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005728 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005729
Jesse Barnes89b667f2013-04-18 14:51:36 -07005730 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02005731 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03005732 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07005733 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005734 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03005735 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005736 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005737 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005738 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005739
Jesse Barnes89b667f2013-04-18 14:51:36 -07005740 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5741 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5742 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005743 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005744 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005745 0x0df40000);
5746 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005747 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005748 0x0df70000);
5749 } else { /* HDMI or VGA */
5750 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005751 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005752 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005753 0x0df70000);
5754 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005755 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005756 0x0df40000);
5757 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005758
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005759 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005760 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5761 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5762 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5763 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005764 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005765
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005766 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01005767 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005768}
5769
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005770static void chv_update_pll(struct intel_crtc *crtc)
5771{
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005772 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5773 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5774 DPLL_VCO_ENABLE;
5775 if (crtc->pipe != PIPE_A)
5776 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5777
5778 crtc->config.dpll_hw_state.dpll_md =
5779 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5780}
5781
5782static void chv_prepare_pll(struct intel_crtc *crtc)
5783{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005784 struct drm_device *dev = crtc->base.dev;
5785 struct drm_i915_private *dev_priv = dev->dev_private;
5786 int pipe = crtc->pipe;
5787 int dpll_reg = DPLL(crtc->pipe);
5788 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ville Syrjälä580d3812014-04-09 13:29:00 +03005789 u32 loopfilter, intcoeff;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005790 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5791 int refclk;
5792
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005793 bestn = crtc->config.dpll.n;
5794 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5795 bestm1 = crtc->config.dpll.m1;
5796 bestm2 = crtc->config.dpll.m2 >> 22;
5797 bestp1 = crtc->config.dpll.p1;
5798 bestp2 = crtc->config.dpll.p2;
5799
5800 /*
5801 * Enable Refclk and SSC
5802 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005803 I915_WRITE(dpll_reg,
5804 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5805
5806 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005807
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005808 /* p1 and p2 divider */
5809 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5810 5 << DPIO_CHV_S1_DIV_SHIFT |
5811 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5812 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5813 1 << DPIO_CHV_K_DIV_SHIFT);
5814
5815 /* Feedback post-divider - m2 */
5816 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5817
5818 /* Feedback refclk divider - n and m1 */
5819 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5820 DPIO_CHV_M1_DIV_BY_2 |
5821 1 << DPIO_CHV_N_DIV_SHIFT);
5822
5823 /* M2 fraction division */
5824 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5825
5826 /* M2 fraction division enable */
5827 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5828 DPIO_CHV_FRAC_DIV_EN |
5829 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5830
5831 /* Loop filter */
5832 refclk = i9xx_get_refclk(&crtc->base, 0);
5833 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5834 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5835 if (refclk == 100000)
5836 intcoeff = 11;
5837 else if (refclk == 38400)
5838 intcoeff = 10;
5839 else
5840 intcoeff = 9;
5841 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5842 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5843
5844 /* AFC Recal */
5845 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5846 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5847 DPIO_AFC_RECAL);
5848
5849 mutex_unlock(&dev_priv->dpio_lock);
5850}
5851
Daniel Vetterf47709a2013-03-28 10:42:02 +01005852static void i9xx_update_pll(struct intel_crtc *crtc,
5853 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005854 int num_connectors)
5855{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005856 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005857 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005858 u32 dpll;
5859 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005860 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005861
Daniel Vetterf47709a2013-03-28 10:42:02 +01005862 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305863
Daniel Vetterf47709a2013-03-28 10:42:02 +01005864 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5865 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005866
5867 dpll = DPLL_VGA_MODE_DIS;
5868
Daniel Vetterf47709a2013-03-28 10:42:02 +01005869 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005870 dpll |= DPLLB_MODE_LVDS;
5871 else
5872 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005873
Daniel Vetteref1b4602013-06-01 17:17:04 +02005874 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005875 dpll |= (crtc->config.pixel_multiplier - 1)
5876 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005877 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005878
5879 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005880 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005881
Daniel Vetterf47709a2013-03-28 10:42:02 +01005882 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005883 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005884
5885 /* compute bitmask from p1 value */
5886 if (IS_PINEVIEW(dev))
5887 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5888 else {
5889 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5890 if (IS_G4X(dev) && reduced_clock)
5891 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5892 }
5893 switch (clock->p2) {
5894 case 5:
5895 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5896 break;
5897 case 7:
5898 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5899 break;
5900 case 10:
5901 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5902 break;
5903 case 14:
5904 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5905 break;
5906 }
5907 if (INTEL_INFO(dev)->gen >= 4)
5908 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5909
Daniel Vetter09ede542013-04-30 14:01:45 +02005910 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005911 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005912 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005913 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5914 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5915 else
5916 dpll |= PLL_REF_INPUT_DREFCLK;
5917
5918 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005919 crtc->config.dpll_hw_state.dpll = dpll;
5920
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005921 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005922 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5923 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005924 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005925 }
5926}
5927
Daniel Vetterf47709a2013-03-28 10:42:02 +01005928static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005929 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005930 int num_connectors)
5931{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005932 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005933 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005934 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005935 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005936
Daniel Vetterf47709a2013-03-28 10:42:02 +01005937 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305938
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005939 dpll = DPLL_VGA_MODE_DIS;
5940
Daniel Vetterf47709a2013-03-28 10:42:02 +01005941 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005942 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5943 } else {
5944 if (clock->p1 == 2)
5945 dpll |= PLL_P1_DIVIDE_BY_TWO;
5946 else
5947 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5948 if (clock->p2 == 4)
5949 dpll |= PLL_P2_DIVIDE_BY_4;
5950 }
5951
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005952 if (!IS_I830(dev) && intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005953 dpll |= DPLL_DVO_2X_MODE;
5954
Daniel Vetterf47709a2013-03-28 10:42:02 +01005955 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005956 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5957 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5958 else
5959 dpll |= PLL_REF_INPUT_DREFCLK;
5960
5961 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005962 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005963}
5964
Daniel Vetter8a654f32013-06-01 17:16:22 +02005965static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005966{
5967 struct drm_device *dev = intel_crtc->base.dev;
5968 struct drm_i915_private *dev_priv = dev->dev_private;
5969 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005970 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005971 struct drm_display_mode *adjusted_mode =
5972 &intel_crtc->config.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005973 uint32_t crtc_vtotal, crtc_vblank_end;
5974 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005975
5976 /* We need to be careful not to changed the adjusted mode, for otherwise
5977 * the hw state checker will get angry at the mismatch. */
5978 crtc_vtotal = adjusted_mode->crtc_vtotal;
5979 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005980
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005981 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005982 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005983 crtc_vtotal -= 1;
5984 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005985
5986 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5987 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5988 else
5989 vsyncshift = adjusted_mode->crtc_hsync_start -
5990 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005991 if (vsyncshift < 0)
5992 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005993 }
5994
5995 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005996 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005997
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005998 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005999 (adjusted_mode->crtc_hdisplay - 1) |
6000 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006001 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006002 (adjusted_mode->crtc_hblank_start - 1) |
6003 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006004 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006005 (adjusted_mode->crtc_hsync_start - 1) |
6006 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6007
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006008 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006009 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006010 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006011 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006012 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006013 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006014 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006015 (adjusted_mode->crtc_vsync_start - 1) |
6016 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6017
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006018 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6019 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6020 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6021 * bits. */
6022 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6023 (pipe == PIPE_B || pipe == PIPE_C))
6024 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6025
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006026 /* pipesrc controls the size that is scaled from, which should
6027 * always be the user's requested size.
6028 */
6029 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006030 ((intel_crtc->config.pipe_src_w - 1) << 16) |
6031 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006032}
6033
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006034static void intel_get_pipe_timings(struct intel_crtc *crtc,
6035 struct intel_crtc_config *pipe_config)
6036{
6037 struct drm_device *dev = crtc->base.dev;
6038 struct drm_i915_private *dev_priv = dev->dev_private;
6039 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6040 uint32_t tmp;
6041
6042 tmp = I915_READ(HTOTAL(cpu_transcoder));
6043 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6044 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
6045 tmp = I915_READ(HBLANK(cpu_transcoder));
6046 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6047 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
6048 tmp = I915_READ(HSYNC(cpu_transcoder));
6049 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6050 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
6051
6052 tmp = I915_READ(VTOTAL(cpu_transcoder));
6053 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6054 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
6055 tmp = I915_READ(VBLANK(cpu_transcoder));
6056 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6057 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
6058 tmp = I915_READ(VSYNC(cpu_transcoder));
6059 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6060 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
6061
6062 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
6063 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6064 pipe_config->adjusted_mode.crtc_vtotal += 1;
6065 pipe_config->adjusted_mode.crtc_vblank_end += 1;
6066 }
6067
6068 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006069 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6070 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6071
6072 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
6073 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006074}
6075
Daniel Vetterf6a83282014-02-11 15:28:57 -08006076void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6077 struct intel_crtc_config *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03006078{
Daniel Vetterf6a83282014-02-11 15:28:57 -08006079 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
6080 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
6081 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
6082 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006083
Daniel Vetterf6a83282014-02-11 15:28:57 -08006084 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
6085 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
6086 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
6087 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006088
Daniel Vetterf6a83282014-02-11 15:28:57 -08006089 mode->flags = pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006090
Daniel Vetterf6a83282014-02-11 15:28:57 -08006091 mode->clock = pipe_config->adjusted_mode.crtc_clock;
6092 mode->flags |= pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006093}
6094
Daniel Vetter84b046f2013-02-19 18:48:54 +01006095static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6096{
6097 struct drm_device *dev = intel_crtc->base.dev;
6098 struct drm_i915_private *dev_priv = dev->dev_private;
6099 uint32_t pipeconf;
6100
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006101 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006102
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03006103 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6104 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6105 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02006106
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006107 if (intel_crtc->config.double_wide)
6108 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006109
Daniel Vetterff9ce462013-04-24 14:57:17 +02006110 /* only g4x and later have fancy bpc/dither controls */
6111 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006112 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6113 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
6114 pipeconf |= PIPECONF_DITHER_EN |
6115 PIPECONF_DITHER_TYPE_SP;
6116
6117 switch (intel_crtc->config.pipe_bpp) {
6118 case 18:
6119 pipeconf |= PIPECONF_6BPC;
6120 break;
6121 case 24:
6122 pipeconf |= PIPECONF_8BPC;
6123 break;
6124 case 30:
6125 pipeconf |= PIPECONF_10BPC;
6126 break;
6127 default:
6128 /* Case prevented by intel_choose_pipe_bpp_dither. */
6129 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01006130 }
6131 }
6132
6133 if (HAS_PIPE_CXSR(dev)) {
6134 if (intel_crtc->lowfreq_avail) {
6135 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6136 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6137 } else {
6138 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01006139 }
6140 }
6141
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006142 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6143 if (INTEL_INFO(dev)->gen < 4 ||
6144 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
6145 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6146 else
6147 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6148 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01006149 pipeconf |= PIPECONF_PROGRESSIVE;
6150
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006151 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
6152 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03006153
Daniel Vetter84b046f2013-02-19 18:48:54 +01006154 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6155 POSTING_READ(PIPECONF(intel_crtc->pipe));
6156}
6157
Eric Anholtf564048e2011-03-30 13:01:02 -07006158static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07006159 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006160 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006161{
6162 struct drm_device *dev = crtc->dev;
6163 struct drm_i915_private *dev_priv = dev->dev_private;
6164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtc751ce42010-03-25 11:48:48 -07006165 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006166 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02006167 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006168 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006169 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006170 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08006171
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02006172 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01006173 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006174 case INTEL_OUTPUT_LVDS:
6175 is_lvds = true;
6176 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006177 case INTEL_OUTPUT_DSI:
6178 is_dsi = true;
6179 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006180 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006181
Eric Anholtc751ce42010-03-25 11:48:48 -07006182 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006183 }
6184
Jani Nikulaf2335332013-09-13 11:03:09 +03006185 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006186 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006187
Jani Nikulaf2335332013-09-13 11:03:09 +03006188 if (!intel_crtc->config.clock_set) {
6189 refclk = i9xx_get_refclk(crtc, num_connectors);
6190
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006191 /*
6192 * Returns a set of divisors for the desired target clock with
6193 * the given refclk, or FALSE. The returned values represent
6194 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6195 * 2) / p1 / p2.
6196 */
6197 limit = intel_limit(crtc, refclk);
6198 ok = dev_priv->display.find_dpll(limit, crtc,
6199 intel_crtc->config.port_clock,
6200 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006201 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006202 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6203 return -EINVAL;
6204 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006205
Jani Nikulaf2335332013-09-13 11:03:09 +03006206 if (is_lvds && dev_priv->lvds_downclock_avail) {
6207 /*
6208 * Ensure we match the reduced clock's P to the target
6209 * clock. If the clocks don't match, we can't switch
6210 * the display clock by using the FP0/FP1. In such case
6211 * we will disable the LVDS downclock feature.
6212 */
6213 has_reduced_clock =
6214 dev_priv->display.find_dpll(limit, crtc,
6215 dev_priv->lvds_downclock,
6216 refclk, &clock,
6217 &reduced_clock);
6218 }
6219 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01006220 intel_crtc->config.dpll.n = clock.n;
6221 intel_crtc->config.dpll.m1 = clock.m1;
6222 intel_crtc->config.dpll.m2 = clock.m2;
6223 intel_crtc->config.dpll.p1 = clock.p1;
6224 intel_crtc->config.dpll.p2 = clock.p2;
6225 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006226
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006227 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02006228 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306229 has_reduced_clock ? &reduced_clock : NULL,
6230 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006231 } else if (IS_CHERRYVIEW(dev)) {
6232 chv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006233 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03006234 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006235 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01006236 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006237 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006238 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006239 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006240
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006241 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006242}
6243
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006244static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6245 struct intel_crtc_config *pipe_config)
6246{
6247 struct drm_device *dev = crtc->base.dev;
6248 struct drm_i915_private *dev_priv = dev->dev_private;
6249 uint32_t tmp;
6250
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006251 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6252 return;
6253
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006254 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006255 if (!(tmp & PFIT_ENABLE))
6256 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006257
Daniel Vetter06922822013-07-11 13:35:40 +02006258 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006259 if (INTEL_INFO(dev)->gen < 4) {
6260 if (crtc->pipe != PIPE_B)
6261 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006262 } else {
6263 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6264 return;
6265 }
6266
Daniel Vetter06922822013-07-11 13:35:40 +02006267 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006268 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6269 if (INTEL_INFO(dev)->gen < 5)
6270 pipe_config->gmch_pfit.lvds_border_bits =
6271 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6272}
6273
Jesse Barnesacbec812013-09-20 11:29:32 -07006274static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6275 struct intel_crtc_config *pipe_config)
6276{
6277 struct drm_device *dev = crtc->base.dev;
6278 struct drm_i915_private *dev_priv = dev->dev_private;
6279 int pipe = pipe_config->cpu_transcoder;
6280 intel_clock_t clock;
6281 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006282 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006283
Shobhit Kumarf573de52014-07-30 20:32:37 +05306284 /* In case of MIPI DPLL will not even be used */
6285 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6286 return;
6287
Jesse Barnesacbec812013-09-20 11:29:32 -07006288 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006289 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006290 mutex_unlock(&dev_priv->dpio_lock);
6291
6292 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6293 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6294 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6295 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6296 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6297
Ville Syrjäläf6466282013-10-14 14:50:31 +03006298 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006299
Ville Syrjäläf6466282013-10-14 14:50:31 +03006300 /* clock.dot is the fast clock */
6301 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006302}
6303
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006304static void i9xx_get_plane_config(struct intel_crtc *crtc,
6305 struct intel_plane_config *plane_config)
6306{
6307 struct drm_device *dev = crtc->base.dev;
6308 struct drm_i915_private *dev_priv = dev->dev_private;
6309 u32 val, base, offset;
6310 int pipe = crtc->pipe, plane = crtc->plane;
6311 int fourcc, pixel_format;
6312 int aligned_height;
6313
Dave Airlie66e514c2014-04-03 07:51:54 +10006314 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6315 if (!crtc->base.primary->fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006316 DRM_DEBUG_KMS("failed to alloc fb\n");
6317 return;
6318 }
6319
6320 val = I915_READ(DSPCNTR(plane));
6321
6322 if (INTEL_INFO(dev)->gen >= 4)
6323 if (val & DISPPLANE_TILED)
6324 plane_config->tiled = true;
6325
6326 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6327 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10006328 crtc->base.primary->fb->pixel_format = fourcc;
6329 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006330 drm_format_plane_cpp(fourcc, 0) * 8;
6331
6332 if (INTEL_INFO(dev)->gen >= 4) {
6333 if (plane_config->tiled)
6334 offset = I915_READ(DSPTILEOFF(plane));
6335 else
6336 offset = I915_READ(DSPLINOFF(plane));
6337 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6338 } else {
6339 base = I915_READ(DSPADDR(plane));
6340 }
6341 plane_config->base = base;
6342
6343 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006344 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6345 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006346
6347 val = I915_READ(DSPSTRIDE(pipe));
Rafael Barbalho026b96e2014-07-28 19:56:27 +01006348 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006349
Dave Airlie66e514c2014-04-03 07:51:54 +10006350 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006351 plane_config->tiled);
6352
Fabian Frederick1267a262014-07-01 20:39:41 +02006353 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
6354 aligned_height);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006355
6356 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 +10006357 pipe, plane, crtc->base.primary->fb->width,
6358 crtc->base.primary->fb->height,
6359 crtc->base.primary->fb->bits_per_pixel, base,
6360 crtc->base.primary->fb->pitches[0],
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006361 plane_config->size);
6362
6363}
6364
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006365static void chv_crtc_clock_get(struct intel_crtc *crtc,
6366 struct intel_crtc_config *pipe_config)
6367{
6368 struct drm_device *dev = crtc->base.dev;
6369 struct drm_i915_private *dev_priv = dev->dev_private;
6370 int pipe = pipe_config->cpu_transcoder;
6371 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6372 intel_clock_t clock;
6373 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6374 int refclk = 100000;
6375
6376 mutex_lock(&dev_priv->dpio_lock);
6377 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6378 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6379 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6380 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6381 mutex_unlock(&dev_priv->dpio_lock);
6382
6383 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6384 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6385 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6386 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6387 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6388
6389 chv_clock(refclk, &clock);
6390
6391 /* clock.dot is the fast clock */
6392 pipe_config->port_clock = clock.dot / 5;
6393}
6394
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006395static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6396 struct intel_crtc_config *pipe_config)
6397{
6398 struct drm_device *dev = crtc->base.dev;
6399 struct drm_i915_private *dev_priv = dev->dev_private;
6400 uint32_t tmp;
6401
Imre Deakb5482bd2014-03-05 16:20:55 +02006402 if (!intel_display_power_enabled(dev_priv,
6403 POWER_DOMAIN_PIPE(crtc->pipe)))
6404 return false;
6405
Daniel Vettere143a212013-07-04 12:01:15 +02006406 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006407 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006408
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006409 tmp = I915_READ(PIPECONF(crtc->pipe));
6410 if (!(tmp & PIPECONF_ENABLE))
6411 return false;
6412
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006413 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6414 switch (tmp & PIPECONF_BPC_MASK) {
6415 case PIPECONF_6BPC:
6416 pipe_config->pipe_bpp = 18;
6417 break;
6418 case PIPECONF_8BPC:
6419 pipe_config->pipe_bpp = 24;
6420 break;
6421 case PIPECONF_10BPC:
6422 pipe_config->pipe_bpp = 30;
6423 break;
6424 default:
6425 break;
6426 }
6427 }
6428
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02006429 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6430 pipe_config->limited_color_range = true;
6431
Ville Syrjälä282740f2013-09-04 18:30:03 +03006432 if (INTEL_INFO(dev)->gen < 4)
6433 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6434
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006435 intel_get_pipe_timings(crtc, pipe_config);
6436
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006437 i9xx_get_pfit_config(crtc, pipe_config);
6438
Daniel Vetter6c49f242013-06-06 12:45:25 +02006439 if (INTEL_INFO(dev)->gen >= 4) {
6440 tmp = I915_READ(DPLL_MD(crtc->pipe));
6441 pipe_config->pixel_multiplier =
6442 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6443 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006444 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02006445 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6446 tmp = I915_READ(DPLL(crtc->pipe));
6447 pipe_config->pixel_multiplier =
6448 ((tmp & SDVO_MULTIPLIER_MASK)
6449 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6450 } else {
6451 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6452 * port and will be fixed up in the encoder->get_config
6453 * function. */
6454 pipe_config->pixel_multiplier = 1;
6455 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006456 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6457 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006458 /*
6459 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6460 * on 830. Filter it out here so that we don't
6461 * report errors due to that.
6462 */
6463 if (IS_I830(dev))
6464 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6465
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006466 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6467 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03006468 } else {
6469 /* Mask out read-only status bits. */
6470 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6471 DPLL_PORTC_READY_MASK |
6472 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006473 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02006474
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006475 if (IS_CHERRYVIEW(dev))
6476 chv_crtc_clock_get(crtc, pipe_config);
6477 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07006478 vlv_crtc_clock_get(crtc, pipe_config);
6479 else
6480 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03006481
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006482 return true;
6483}
6484
Paulo Zanonidde86e22012-12-01 12:04:25 -02006485static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07006486{
6487 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006488 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006489 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006490 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006491 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006492 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07006493 bool has_ck505 = false;
6494 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006495
6496 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01006497 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07006498 switch (encoder->type) {
6499 case INTEL_OUTPUT_LVDS:
6500 has_panel = true;
6501 has_lvds = true;
6502 break;
6503 case INTEL_OUTPUT_EDP:
6504 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03006505 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07006506 has_cpu_edp = true;
6507 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006508 }
6509 }
6510
Keith Packard99eb6a02011-09-26 14:29:12 -07006511 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006512 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07006513 can_ssc = has_ck505;
6514 } else {
6515 has_ck505 = false;
6516 can_ssc = true;
6517 }
6518
Imre Deak2de69052013-05-08 13:14:04 +03006519 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6520 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006521
6522 /* Ironlake: try to setup display ref clock before DPLL
6523 * enabling. This is only under driver's control after
6524 * PCH B stepping, previous chipset stepping should be
6525 * ignoring this setting.
6526 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006527 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006528
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006529 /* As we must carefully and slowly disable/enable each source in turn,
6530 * compute the final state we want first and check if we need to
6531 * make any changes at all.
6532 */
6533 final = val;
6534 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07006535 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006536 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07006537 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006538 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6539
6540 final &= ~DREF_SSC_SOURCE_MASK;
6541 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6542 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006543
Keith Packard199e5d72011-09-22 12:01:57 -07006544 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006545 final |= DREF_SSC_SOURCE_ENABLE;
6546
6547 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6548 final |= DREF_SSC1_ENABLE;
6549
6550 if (has_cpu_edp) {
6551 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6552 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6553 else
6554 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6555 } else
6556 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6557 } else {
6558 final |= DREF_SSC_SOURCE_DISABLE;
6559 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6560 }
6561
6562 if (final == val)
6563 return;
6564
6565 /* Always enable nonspread source */
6566 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6567
6568 if (has_ck505)
6569 val |= DREF_NONSPREAD_CK505_ENABLE;
6570 else
6571 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6572
6573 if (has_panel) {
6574 val &= ~DREF_SSC_SOURCE_MASK;
6575 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006576
Keith Packard199e5d72011-09-22 12:01:57 -07006577 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07006578 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006579 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006580 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02006581 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006582 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006583
6584 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006585 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006586 POSTING_READ(PCH_DREF_CONTROL);
6587 udelay(200);
6588
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006589 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006590
6591 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07006592 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07006593 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006594 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006595 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02006596 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006597 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07006598 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006599 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006600
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006601 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006602 POSTING_READ(PCH_DREF_CONTROL);
6603 udelay(200);
6604 } else {
6605 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6606
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006607 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07006608
6609 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006610 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006611
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006612 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006613 POSTING_READ(PCH_DREF_CONTROL);
6614 udelay(200);
6615
6616 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006617 val &= ~DREF_SSC_SOURCE_MASK;
6618 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006619
6620 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006621 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006622
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006623 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006624 POSTING_READ(PCH_DREF_CONTROL);
6625 udelay(200);
6626 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006627
6628 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006629}
6630
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006631static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02006632{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006633 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006634
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006635 tmp = I915_READ(SOUTH_CHICKEN2);
6636 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6637 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006638
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006639 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6640 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6641 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02006642
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006643 tmp = I915_READ(SOUTH_CHICKEN2);
6644 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6645 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006646
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006647 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6648 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6649 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006650}
6651
6652/* WaMPhyProgramming:hsw */
6653static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6654{
6655 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006656
6657 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6658 tmp &= ~(0xFF << 24);
6659 tmp |= (0x12 << 24);
6660 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6661
Paulo Zanonidde86e22012-12-01 12:04:25 -02006662 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6663 tmp |= (1 << 11);
6664 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6665
6666 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6667 tmp |= (1 << 11);
6668 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6669
Paulo Zanonidde86e22012-12-01 12:04:25 -02006670 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6671 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6672 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6673
6674 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6675 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6676 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6677
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006678 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6679 tmp &= ~(7 << 13);
6680 tmp |= (5 << 13);
6681 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006682
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006683 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6684 tmp &= ~(7 << 13);
6685 tmp |= (5 << 13);
6686 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006687
6688 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6689 tmp &= ~0xFF;
6690 tmp |= 0x1C;
6691 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6692
6693 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6694 tmp &= ~0xFF;
6695 tmp |= 0x1C;
6696 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6697
6698 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6699 tmp &= ~(0xFF << 16);
6700 tmp |= (0x1C << 16);
6701 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6702
6703 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6704 tmp &= ~(0xFF << 16);
6705 tmp |= (0x1C << 16);
6706 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6707
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006708 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6709 tmp |= (1 << 27);
6710 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006711
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006712 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6713 tmp |= (1 << 27);
6714 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006715
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006716 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6717 tmp &= ~(0xF << 28);
6718 tmp |= (4 << 28);
6719 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006720
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006721 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6722 tmp &= ~(0xF << 28);
6723 tmp |= (4 << 28);
6724 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006725}
6726
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006727/* Implements 3 different sequences from BSpec chapter "Display iCLK
6728 * Programming" based on the parameters passed:
6729 * - Sequence to enable CLKOUT_DP
6730 * - Sequence to enable CLKOUT_DP without spread
6731 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6732 */
6733static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6734 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006735{
6736 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006737 uint32_t reg, tmp;
6738
6739 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6740 with_spread = true;
6741 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6742 with_fdi, "LP PCH doesn't have FDI\n"))
6743 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006744
6745 mutex_lock(&dev_priv->dpio_lock);
6746
6747 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6748 tmp &= ~SBI_SSCCTL_DISABLE;
6749 tmp |= SBI_SSCCTL_PATHALT;
6750 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6751
6752 udelay(24);
6753
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006754 if (with_spread) {
6755 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6756 tmp &= ~SBI_SSCCTL_PATHALT;
6757 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006758
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006759 if (with_fdi) {
6760 lpt_reset_fdi_mphy(dev_priv);
6761 lpt_program_fdi_mphy(dev_priv);
6762 }
6763 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02006764
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006765 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6766 SBI_GEN0 : SBI_DBUFF0;
6767 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6768 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6769 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01006770
6771 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006772}
6773
Paulo Zanoni47701c32013-07-23 11:19:25 -03006774/* Sequence to disable CLKOUT_DP */
6775static void lpt_disable_clkout_dp(struct drm_device *dev)
6776{
6777 struct drm_i915_private *dev_priv = dev->dev_private;
6778 uint32_t reg, tmp;
6779
6780 mutex_lock(&dev_priv->dpio_lock);
6781
6782 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6783 SBI_GEN0 : SBI_DBUFF0;
6784 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6785 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6786 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6787
6788 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6789 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6790 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6791 tmp |= SBI_SSCCTL_PATHALT;
6792 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6793 udelay(32);
6794 }
6795 tmp |= SBI_SSCCTL_DISABLE;
6796 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6797 }
6798
6799 mutex_unlock(&dev_priv->dpio_lock);
6800}
6801
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006802static void lpt_init_pch_refclk(struct drm_device *dev)
6803{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006804 struct intel_encoder *encoder;
6805 bool has_vga = false;
6806
Damien Lespiaub2784e12014-08-05 11:29:37 +01006807 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006808 switch (encoder->type) {
6809 case INTEL_OUTPUT_ANALOG:
6810 has_vga = true;
6811 break;
6812 }
6813 }
6814
Paulo Zanoni47701c32013-07-23 11:19:25 -03006815 if (has_vga)
6816 lpt_enable_clkout_dp(dev, true, true);
6817 else
6818 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006819}
6820
Paulo Zanonidde86e22012-12-01 12:04:25 -02006821/*
6822 * Initialize reference clocks when the driver loads
6823 */
6824void intel_init_pch_refclk(struct drm_device *dev)
6825{
6826 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6827 ironlake_init_pch_refclk(dev);
6828 else if (HAS_PCH_LPT(dev))
6829 lpt_init_pch_refclk(dev);
6830}
6831
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006832static int ironlake_get_refclk(struct drm_crtc *crtc)
6833{
6834 struct drm_device *dev = crtc->dev;
6835 struct drm_i915_private *dev_priv = dev->dev_private;
6836 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006837 int num_connectors = 0;
6838 bool is_lvds = false;
6839
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02006840 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006841 switch (encoder->type) {
6842 case INTEL_OUTPUT_LVDS:
6843 is_lvds = true;
6844 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006845 }
6846 num_connectors++;
6847 }
6848
6849 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006850 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006851 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006852 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006853 }
6854
6855 return 120000;
6856}
6857
Daniel Vetter6ff93602013-04-19 11:24:36 +02006858static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03006859{
6860 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
6861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6862 int pipe = intel_crtc->pipe;
6863 uint32_t val;
6864
Daniel Vetter78114072013-06-13 00:54:57 +02006865 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03006866
Daniel Vetter965e0c42013-03-27 00:44:57 +01006867 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03006868 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006869 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006870 break;
6871 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006872 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006873 break;
6874 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006875 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006876 break;
6877 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006878 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006879 break;
6880 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03006881 /* Case prevented by intel_choose_pipe_bpp_dither. */
6882 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03006883 }
6884
Daniel Vetterd8b32242013-04-25 17:54:44 +02006885 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03006886 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6887
Daniel Vetter6ff93602013-04-19 11:24:36 +02006888 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03006889 val |= PIPECONF_INTERLACED_ILK;
6890 else
6891 val |= PIPECONF_PROGRESSIVE;
6892
Daniel Vetter50f3b012013-03-27 00:44:56 +01006893 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006894 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006895
Paulo Zanonic8203562012-09-12 10:06:29 -03006896 I915_WRITE(PIPECONF(pipe), val);
6897 POSTING_READ(PIPECONF(pipe));
6898}
6899
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006900/*
6901 * Set up the pipe CSC unit.
6902 *
6903 * Currently only full range RGB to limited range RGB conversion
6904 * is supported, but eventually this should handle various
6905 * RGB<->YCbCr scenarios as well.
6906 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01006907static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006908{
6909 struct drm_device *dev = crtc->dev;
6910 struct drm_i915_private *dev_priv = dev->dev_private;
6911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6912 int pipe = intel_crtc->pipe;
6913 uint16_t coeff = 0x7800; /* 1.0 */
6914
6915 /*
6916 * TODO: Check what kind of values actually come out of the pipe
6917 * with these coeff/postoff values and adjust to get the best
6918 * accuracy. Perhaps we even need to take the bpc value into
6919 * consideration.
6920 */
6921
Daniel Vetter50f3b012013-03-27 00:44:56 +01006922 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006923 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6924
6925 /*
6926 * GY/GU and RY/RU should be the other way around according
6927 * to BSpec, but reality doesn't agree. Just set them up in
6928 * a way that results in the correct picture.
6929 */
6930 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6931 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6932
6933 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6934 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6935
6936 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6937 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6938
6939 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6940 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6941 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6942
6943 if (INTEL_INFO(dev)->gen > 6) {
6944 uint16_t postoff = 0;
6945
Daniel Vetter50f3b012013-03-27 00:44:56 +01006946 if (intel_crtc->config.limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02006947 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006948
6949 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6950 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6951 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6952
6953 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6954 } else {
6955 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6956
Daniel Vetter50f3b012013-03-27 00:44:56 +01006957 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006958 mode |= CSC_BLACK_SCREEN_OFFSET;
6959
6960 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6961 }
6962}
6963
Daniel Vetter6ff93602013-04-19 11:24:36 +02006964static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006965{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006966 struct drm_device *dev = crtc->dev;
6967 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006968 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006969 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006970 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006971 uint32_t val;
6972
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006973 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006974
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006975 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006976 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6977
Daniel Vetter6ff93602013-04-19 11:24:36 +02006978 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006979 val |= PIPECONF_INTERLACED_ILK;
6980 else
6981 val |= PIPECONF_PROGRESSIVE;
6982
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006983 I915_WRITE(PIPECONF(cpu_transcoder), val);
6984 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006985
6986 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6987 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006988
6989 if (IS_BROADWELL(dev)) {
6990 val = 0;
6991
6992 switch (intel_crtc->config.pipe_bpp) {
6993 case 18:
6994 val |= PIPEMISC_DITHER_6_BPC;
6995 break;
6996 case 24:
6997 val |= PIPEMISC_DITHER_8_BPC;
6998 break;
6999 case 30:
7000 val |= PIPEMISC_DITHER_10_BPC;
7001 break;
7002 case 36:
7003 val |= PIPEMISC_DITHER_12_BPC;
7004 break;
7005 default:
7006 /* Case prevented by pipe_config_set_bpp. */
7007 BUG();
7008 }
7009
7010 if (intel_crtc->config.dither)
7011 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7012
7013 I915_WRITE(PIPEMISC(pipe), val);
7014 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007015}
7016
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007017static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007018 intel_clock_t *clock,
7019 bool *has_reduced_clock,
7020 intel_clock_t *reduced_clock)
7021{
7022 struct drm_device *dev = crtc->dev;
7023 struct drm_i915_private *dev_priv = dev->dev_private;
7024 struct intel_encoder *intel_encoder;
7025 int refclk;
7026 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02007027 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007028
7029 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7030 switch (intel_encoder->type) {
7031 case INTEL_OUTPUT_LVDS:
7032 is_lvds = true;
7033 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007034 }
7035 }
7036
7037 refclk = ironlake_get_refclk(crtc);
7038
7039 /*
7040 * Returns a set of divisors for the desired target clock with the given
7041 * refclk, or FALSE. The returned values represent the clock equation:
7042 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7043 */
7044 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02007045 ret = dev_priv->display.find_dpll(limit, crtc,
7046 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007047 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007048 if (!ret)
7049 return false;
7050
7051 if (is_lvds && dev_priv->lvds_downclock_avail) {
7052 /*
7053 * Ensure we match the reduced clock's P to the target clock.
7054 * If the clocks don't match, we can't switch the display clock
7055 * by using the FP0/FP1. In such case we will disable the LVDS
7056 * downclock feature.
7057 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02007058 *has_reduced_clock =
7059 dev_priv->display.find_dpll(limit, crtc,
7060 dev_priv->lvds_downclock,
7061 refclk, clock,
7062 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007063 }
7064
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007065 return true;
7066}
7067
Paulo Zanonid4b19312012-11-29 11:29:32 -02007068int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7069{
7070 /*
7071 * Account for spread spectrum to avoid
7072 * oversubscribing the link. Max center spread
7073 * is 2.5%; use 5% for safety's sake.
7074 */
7075 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02007076 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02007077}
7078
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007079static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02007080{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007081 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007082}
7083
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007084static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007085 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007086 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007087{
7088 struct drm_crtc *crtc = &intel_crtc->base;
7089 struct drm_device *dev = crtc->dev;
7090 struct drm_i915_private *dev_priv = dev->dev_private;
7091 struct intel_encoder *intel_encoder;
7092 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007093 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02007094 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007095
7096 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7097 switch (intel_encoder->type) {
7098 case INTEL_OUTPUT_LVDS:
7099 is_lvds = true;
7100 break;
7101 case INTEL_OUTPUT_SDVO:
7102 case INTEL_OUTPUT_HDMI:
7103 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007104 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007105 }
7106
7107 num_connectors++;
7108 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007109
Chris Wilsonc1858122010-12-03 21:35:48 +00007110 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07007111 factor = 21;
7112 if (is_lvds) {
7113 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007114 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02007115 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07007116 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02007117 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07007118 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00007119
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007120 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02007121 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00007122
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007123 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7124 *fp2 |= FP_CB_TUNE;
7125
Chris Wilson5eddb702010-09-11 13:48:45 +01007126 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007127
Eric Anholta07d6782011-03-30 13:01:08 -07007128 if (is_lvds)
7129 dpll |= DPLLB_MODE_LVDS;
7130 else
7131 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007132
Daniel Vetteref1b4602013-06-01 17:17:04 +02007133 dpll |= (intel_crtc->config.pixel_multiplier - 1)
7134 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007135
7136 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007137 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02007138 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007139 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08007140
Eric Anholta07d6782011-03-30 13:01:08 -07007141 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007142 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007143 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007144 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007145
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007146 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07007147 case 5:
7148 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7149 break;
7150 case 7:
7151 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7152 break;
7153 case 10:
7154 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7155 break;
7156 case 14:
7157 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7158 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007159 }
7160
Daniel Vetterb4c09f32013-04-30 14:01:42 +02007161 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007162 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08007163 else
7164 dpll |= PLL_REF_INPUT_DREFCLK;
7165
Daniel Vetter959e16d2013-06-05 13:34:21 +02007166 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007167}
7168
Jesse Barnes79e53942008-11-07 14:24:08 -08007169static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08007170 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007171 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08007172{
7173 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007174 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007175 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007176 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007177 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007178 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01007179 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007180 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02007181 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007182
7183 for_each_encoder_on_crtc(dev, crtc, encoder) {
7184 switch (encoder->type) {
7185 case INTEL_OUTPUT_LVDS:
7186 is_lvds = true;
7187 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007188 }
7189
7190 num_connectors++;
7191 }
7192
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007193 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7194 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7195
Daniel Vetterff9a6752013-06-01 17:16:21 +02007196 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007197 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02007198 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007199 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7200 return -EINVAL;
7201 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007202 /* Compat-code for transition, will disappear. */
7203 if (!intel_crtc->config.clock_set) {
7204 intel_crtc->config.dpll.n = clock.n;
7205 intel_crtc->config.dpll.m1 = clock.m1;
7206 intel_crtc->config.dpll.m2 = clock.m2;
7207 intel_crtc->config.dpll.p1 = clock.p1;
7208 intel_crtc->config.dpll.p2 = clock.p2;
7209 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007210
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007211 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01007212 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007213 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007214 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007215 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007216
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007217 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007218 &fp, &reduced_clock,
7219 has_reduced_clock ? &fp2 : NULL);
7220
Daniel Vetter959e16d2013-06-05 13:34:21 +02007221 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007222 intel_crtc->config.dpll_hw_state.fp0 = fp;
7223 if (has_reduced_clock)
7224 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7225 else
7226 intel_crtc->config.dpll_hw_state.fp1 = fp;
7227
Daniel Vetterb89a1d32013-06-05 13:34:24 +02007228 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007229 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007230 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Daniel Vetter29407aa2014-04-24 23:55:08 +02007231 pipe_name(intel_crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007232 return -EINVAL;
7233 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007234 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02007235 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007236
Jani Nikulad330a952014-01-21 11:24:25 +02007237 if (is_lvds && has_reduced_clock && i915.powersave)
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007238 intel_crtc->lowfreq_avail = true;
7239 else
7240 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007241
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007242 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007243}
7244
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007245static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7246 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007247{
7248 struct drm_device *dev = crtc->base.dev;
7249 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007250 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007251
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007252 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7253 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7254 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7255 & ~TU_SIZE_MASK;
7256 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7257 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7258 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7259}
7260
7261static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7262 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007263 struct intel_link_m_n *m_n,
7264 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007265{
7266 struct drm_device *dev = crtc->base.dev;
7267 struct drm_i915_private *dev_priv = dev->dev_private;
7268 enum pipe pipe = crtc->pipe;
7269
7270 if (INTEL_INFO(dev)->gen >= 5) {
7271 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7272 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7273 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7274 & ~TU_SIZE_MASK;
7275 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7276 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7277 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007278 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7279 * gen < 8) and if DRRS is supported (to make sure the
7280 * registers are not unnecessarily read).
7281 */
7282 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
7283 crtc->config.has_drrs) {
7284 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7285 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7286 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7287 & ~TU_SIZE_MASK;
7288 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7289 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7290 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7291 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007292 } else {
7293 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7294 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7295 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7296 & ~TU_SIZE_MASK;
7297 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7298 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7299 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7300 }
7301}
7302
7303void intel_dp_get_m_n(struct intel_crtc *crtc,
7304 struct intel_crtc_config *pipe_config)
7305{
7306 if (crtc->config.has_pch_encoder)
7307 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7308 else
7309 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007310 &pipe_config->dp_m_n,
7311 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007312}
7313
Daniel Vetter72419202013-04-04 13:28:53 +02007314static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7315 struct intel_crtc_config *pipe_config)
7316{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007317 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007318 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02007319}
7320
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007321static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7322 struct intel_crtc_config *pipe_config)
7323{
7324 struct drm_device *dev = crtc->base.dev;
7325 struct drm_i915_private *dev_priv = dev->dev_private;
7326 uint32_t tmp;
7327
7328 tmp = I915_READ(PF_CTL(crtc->pipe));
7329
7330 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01007331 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007332 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7333 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02007334
7335 /* We currently do not free assignements of panel fitters on
7336 * ivb/hsw (since we don't use the higher upscaling modes which
7337 * differentiates them) so just WARN about this case for now. */
7338 if (IS_GEN7(dev)) {
7339 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7340 PF_PIPE_SEL_IVB(crtc->pipe));
7341 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007342 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007343}
7344
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007345static void ironlake_get_plane_config(struct intel_crtc *crtc,
7346 struct intel_plane_config *plane_config)
7347{
7348 struct drm_device *dev = crtc->base.dev;
7349 struct drm_i915_private *dev_priv = dev->dev_private;
7350 u32 val, base, offset;
7351 int pipe = crtc->pipe, plane = crtc->plane;
7352 int fourcc, pixel_format;
7353 int aligned_height;
7354
Dave Airlie66e514c2014-04-03 07:51:54 +10007355 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7356 if (!crtc->base.primary->fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007357 DRM_DEBUG_KMS("failed to alloc fb\n");
7358 return;
7359 }
7360
7361 val = I915_READ(DSPCNTR(plane));
7362
7363 if (INTEL_INFO(dev)->gen >= 4)
7364 if (val & DISPPLANE_TILED)
7365 plane_config->tiled = true;
7366
7367 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7368 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10007369 crtc->base.primary->fb->pixel_format = fourcc;
7370 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007371 drm_format_plane_cpp(fourcc, 0) * 8;
7372
7373 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7374 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7375 offset = I915_READ(DSPOFFSET(plane));
7376 } else {
7377 if (plane_config->tiled)
7378 offset = I915_READ(DSPTILEOFF(plane));
7379 else
7380 offset = I915_READ(DSPLINOFF(plane));
7381 }
7382 plane_config->base = base;
7383
7384 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007385 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7386 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007387
7388 val = I915_READ(DSPSTRIDE(pipe));
Rafael Barbalho026b96e2014-07-28 19:56:27 +01007389 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007390
Dave Airlie66e514c2014-04-03 07:51:54 +10007391 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007392 plane_config->tiled);
7393
Fabian Frederick1267a262014-07-01 20:39:41 +02007394 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
7395 aligned_height);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007396
7397 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 +10007398 pipe, plane, crtc->base.primary->fb->width,
7399 crtc->base.primary->fb->height,
7400 crtc->base.primary->fb->bits_per_pixel, base,
7401 crtc->base.primary->fb->pitches[0],
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007402 plane_config->size);
7403}
7404
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007405static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7406 struct intel_crtc_config *pipe_config)
7407{
7408 struct drm_device *dev = crtc->base.dev;
7409 struct drm_i915_private *dev_priv = dev->dev_private;
7410 uint32_t tmp;
7411
Paulo Zanoni930e8c92014-07-04 13:38:34 -03007412 if (!intel_display_power_enabled(dev_priv,
7413 POWER_DOMAIN_PIPE(crtc->pipe)))
7414 return false;
7415
Daniel Vettere143a212013-07-04 12:01:15 +02007416 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007417 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007418
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007419 tmp = I915_READ(PIPECONF(crtc->pipe));
7420 if (!(tmp & PIPECONF_ENABLE))
7421 return false;
7422
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007423 switch (tmp & PIPECONF_BPC_MASK) {
7424 case PIPECONF_6BPC:
7425 pipe_config->pipe_bpp = 18;
7426 break;
7427 case PIPECONF_8BPC:
7428 pipe_config->pipe_bpp = 24;
7429 break;
7430 case PIPECONF_10BPC:
7431 pipe_config->pipe_bpp = 30;
7432 break;
7433 case PIPECONF_12BPC:
7434 pipe_config->pipe_bpp = 36;
7435 break;
7436 default:
7437 break;
7438 }
7439
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007440 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7441 pipe_config->limited_color_range = true;
7442
Daniel Vetterab9412b2013-05-03 11:49:46 +02007443 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02007444 struct intel_shared_dpll *pll;
7445
Daniel Vetter88adfff2013-03-28 10:42:01 +01007446 pipe_config->has_pch_encoder = true;
7447
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007448 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7449 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7450 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007451
7452 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007453
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007454 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02007455 pipe_config->shared_dpll =
7456 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007457 } else {
7458 tmp = I915_READ(PCH_DPLL_SEL);
7459 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7460 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7461 else
7462 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7463 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02007464
7465 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7466
7467 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7468 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02007469
7470 tmp = pipe_config->dpll_hw_state.dpll;
7471 pipe_config->pixel_multiplier =
7472 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7473 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007474
7475 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007476 } else {
7477 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007478 }
7479
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007480 intel_get_pipe_timings(crtc, pipe_config);
7481
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007482 ironlake_get_pfit_config(crtc, pipe_config);
7483
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007484 return true;
7485}
7486
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007487static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7488{
7489 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007490 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007491
Damien Lespiaud3fcc802014-05-13 23:32:22 +01007492 for_each_intel_crtc(dev, crtc)
Paulo Zanoni798183c2013-12-06 20:29:01 -02007493 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007494 pipe_name(crtc->pipe));
7495
7496 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
Daniel Vetter8cc3e162014-06-25 22:01:46 +03007497 WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7498 WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7499 WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007500 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7501 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7502 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03007503 if (IS_HASWELL(dev))
7504 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7505 "CPU PWM2 enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007506 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7507 "PCH PWM1 enabled\n");
7508 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7509 "Utility pin enabled\n");
7510 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7511
Paulo Zanoni9926ada2014-04-01 19:39:47 -03007512 /*
7513 * In theory we can still leave IRQs enabled, as long as only the HPD
7514 * interrupts remain enabled. We used to check for that, but since it's
7515 * gen-specific and since we only disable LCPLL after we fully disable
7516 * the interrupts, the check below should be enough.
7517 */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07007518 WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007519}
7520
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007521static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7522{
7523 struct drm_device *dev = dev_priv->dev;
7524
7525 if (IS_HASWELL(dev))
7526 return I915_READ(D_COMP_HSW);
7527 else
7528 return I915_READ(D_COMP_BDW);
7529}
7530
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007531static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7532{
7533 struct drm_device *dev = dev_priv->dev;
7534
7535 if (IS_HASWELL(dev)) {
7536 mutex_lock(&dev_priv->rps.hw_lock);
7537 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7538 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03007539 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007540 mutex_unlock(&dev_priv->rps.hw_lock);
7541 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007542 I915_WRITE(D_COMP_BDW, val);
7543 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007544 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007545}
7546
7547/*
7548 * This function implements pieces of two sequences from BSpec:
7549 * - Sequence for display software to disable LCPLL
7550 * - Sequence for display software to allow package C8+
7551 * The steps implemented here are just the steps that actually touch the LCPLL
7552 * register. Callers should take care of disabling all the display engine
7553 * functions, doing the mode unset, fixing interrupts, etc.
7554 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007555static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7556 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007557{
7558 uint32_t val;
7559
7560 assert_can_disable_lcpll(dev_priv);
7561
7562 val = I915_READ(LCPLL_CTL);
7563
7564 if (switch_to_fclk) {
7565 val |= LCPLL_CD_SOURCE_FCLK;
7566 I915_WRITE(LCPLL_CTL, val);
7567
7568 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7569 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7570 DRM_ERROR("Switching to FCLK failed\n");
7571
7572 val = I915_READ(LCPLL_CTL);
7573 }
7574
7575 val |= LCPLL_PLL_DISABLE;
7576 I915_WRITE(LCPLL_CTL, val);
7577 POSTING_READ(LCPLL_CTL);
7578
7579 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7580 DRM_ERROR("LCPLL still locked\n");
7581
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007582 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007583 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007584 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007585 ndelay(100);
7586
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007587 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7588 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007589 DRM_ERROR("D_COMP RCOMP still in progress\n");
7590
7591 if (allow_power_down) {
7592 val = I915_READ(LCPLL_CTL);
7593 val |= LCPLL_POWER_DOWN_ALLOW;
7594 I915_WRITE(LCPLL_CTL, val);
7595 POSTING_READ(LCPLL_CTL);
7596 }
7597}
7598
7599/*
7600 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7601 * source.
7602 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007603static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007604{
7605 uint32_t val;
7606
7607 val = I915_READ(LCPLL_CTL);
7608
7609 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7610 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7611 return;
7612
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007613 /*
7614 * Make sure we're not on PC8 state before disabling PC8, otherwise
7615 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7616 *
7617 * The other problem is that hsw_restore_lcpll() is called as part of
7618 * the runtime PM resume sequence, so we can't just call
7619 * gen6_gt_force_wake_get() because that function calls
7620 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7621 * while we are on the resume sequence. So to solve this problem we have
7622 * to call special forcewake code that doesn't touch runtime PM and
7623 * doesn't enable the forcewake delayed work.
7624 */
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007625 spin_lock_irq(&dev_priv->uncore.lock);
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007626 if (dev_priv->uncore.forcewake_count++ == 0)
7627 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007628 spin_unlock_irq(&dev_priv->uncore.lock);
Paulo Zanoni215733f2013-08-19 13:18:07 -03007629
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007630 if (val & LCPLL_POWER_DOWN_ALLOW) {
7631 val &= ~LCPLL_POWER_DOWN_ALLOW;
7632 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02007633 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007634 }
7635
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007636 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007637 val |= D_COMP_COMP_FORCE;
7638 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007639 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007640
7641 val = I915_READ(LCPLL_CTL);
7642 val &= ~LCPLL_PLL_DISABLE;
7643 I915_WRITE(LCPLL_CTL, val);
7644
7645 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7646 DRM_ERROR("LCPLL not locked yet\n");
7647
7648 if (val & LCPLL_CD_SOURCE_FCLK) {
7649 val = I915_READ(LCPLL_CTL);
7650 val &= ~LCPLL_CD_SOURCE_FCLK;
7651 I915_WRITE(LCPLL_CTL, val);
7652
7653 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7654 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7655 DRM_ERROR("Switching back to LCPLL failed\n");
7656 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03007657
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007658 /* See the big comment above. */
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007659 spin_lock_irq(&dev_priv->uncore.lock);
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007660 if (--dev_priv->uncore.forcewake_count == 0)
7661 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007662 spin_unlock_irq(&dev_priv->uncore.lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007663}
7664
Paulo Zanoni765dab672014-03-07 20:08:18 -03007665/*
7666 * Package states C8 and deeper are really deep PC states that can only be
7667 * reached when all the devices on the system allow it, so even if the graphics
7668 * device allows PC8+, it doesn't mean the system will actually get to these
7669 * states. Our driver only allows PC8+ when going into runtime PM.
7670 *
7671 * The requirements for PC8+ are that all the outputs are disabled, the power
7672 * well is disabled and most interrupts are disabled, and these are also
7673 * requirements for runtime PM. When these conditions are met, we manually do
7674 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7675 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7676 * hang the machine.
7677 *
7678 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7679 * the state of some registers, so when we come back from PC8+ we need to
7680 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7681 * need to take care of the registers kept by RC6. Notice that this happens even
7682 * if we don't put the device in PCI D3 state (which is what currently happens
7683 * because of the runtime PM support).
7684 *
7685 * For more, read "Display Sequences for Package C8" on the hardware
7686 * documentation.
7687 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007688void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007689{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007690 struct drm_device *dev = dev_priv->dev;
7691 uint32_t val;
7692
Paulo Zanonic67a4702013-08-19 13:18:09 -03007693 DRM_DEBUG_KMS("Enabling package C8+\n");
7694
Paulo Zanonic67a4702013-08-19 13:18:09 -03007695 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7696 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7697 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7698 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7699 }
7700
7701 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007702 hsw_disable_lcpll(dev_priv, true, true);
7703}
7704
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007705void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007706{
7707 struct drm_device *dev = dev_priv->dev;
7708 uint32_t val;
7709
Paulo Zanonic67a4702013-08-19 13:18:09 -03007710 DRM_DEBUG_KMS("Disabling package C8+\n");
7711
7712 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007713 lpt_init_pch_refclk(dev);
7714
7715 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7716 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7717 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7718 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7719 }
7720
7721 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007722}
7723
Paulo Zanoni9a952a02014-03-07 20:12:34 -03007724static void snb_modeset_global_resources(struct drm_device *dev)
7725{
7726 modeset_update_crtc_power_domains(dev);
7727}
7728
Imre Deak4f074122013-10-16 17:25:51 +03007729static void haswell_modeset_global_resources(struct drm_device *dev)
7730{
Paulo Zanonida723562013-12-19 11:54:51 -02007731 modeset_update_crtc_power_domains(dev);
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02007732}
7733
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007734static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007735 int x, int y,
7736 struct drm_framebuffer *fb)
7737{
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007739
Paulo Zanoni566b7342013-11-25 15:27:08 -02007740 if (!intel_ddi_pll_select(intel_crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007741 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03007742
Daniel Vetter644cef32014-04-24 23:55:07 +02007743 intel_crtc->lowfreq_avail = false;
7744
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007745 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007746}
7747
Damien Lespiau7d2c8172014-07-29 18:06:18 +01007748static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
7749 enum port port,
7750 struct intel_crtc_config *pipe_config)
7751{
7752 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
7753
7754 switch (pipe_config->ddi_pll_sel) {
7755 case PORT_CLK_SEL_WRPLL1:
7756 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
7757 break;
7758 case PORT_CLK_SEL_WRPLL2:
7759 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
7760 break;
7761 }
7762}
7763
Daniel Vetter26804af2014-06-25 22:01:55 +03007764static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
7765 struct intel_crtc_config *pipe_config)
7766{
7767 struct drm_device *dev = crtc->base.dev;
7768 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03007769 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03007770 enum port port;
7771 uint32_t tmp;
7772
7773 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7774
7775 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
7776
Damien Lespiau7d2c8172014-07-29 18:06:18 +01007777 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03007778
Daniel Vetterd452c5b2014-07-04 11:27:39 -03007779 if (pipe_config->shared_dpll >= 0) {
7780 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7781
7782 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7783 &pipe_config->dpll_hw_state));
7784 }
7785
Daniel Vetter26804af2014-06-25 22:01:55 +03007786 /*
7787 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7788 * DDI E. So just check whether this pipe is wired to DDI E and whether
7789 * the PCH transcoder is on.
7790 */
7791 if ((port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
7792 pipe_config->has_pch_encoder = true;
7793
7794 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7795 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7796 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7797
7798 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7799 }
7800}
7801
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007802static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7803 struct intel_crtc_config *pipe_config)
7804{
7805 struct drm_device *dev = crtc->base.dev;
7806 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007807 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007808 uint32_t tmp;
7809
Imre Deakb5482bd2014-03-05 16:20:55 +02007810 if (!intel_display_power_enabled(dev_priv,
7811 POWER_DOMAIN_PIPE(crtc->pipe)))
7812 return false;
7813
Daniel Vettere143a212013-07-04 12:01:15 +02007814 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007815 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7816
Daniel Vettereccb1402013-05-22 00:50:22 +02007817 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7818 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7819 enum pipe trans_edp_pipe;
7820 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7821 default:
7822 WARN(1, "unknown pipe linked to edp transcoder\n");
7823 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7824 case TRANS_DDI_EDP_INPUT_A_ON:
7825 trans_edp_pipe = PIPE_A;
7826 break;
7827 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7828 trans_edp_pipe = PIPE_B;
7829 break;
7830 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7831 trans_edp_pipe = PIPE_C;
7832 break;
7833 }
7834
7835 if (trans_edp_pipe == crtc->pipe)
7836 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7837 }
7838
Imre Deakda7e29b2014-02-18 00:02:02 +02007839 if (!intel_display_power_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02007840 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03007841 return false;
7842
Daniel Vettereccb1402013-05-22 00:50:22 +02007843 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007844 if (!(tmp & PIPECONF_ENABLE))
7845 return false;
7846
Daniel Vetter26804af2014-06-25 22:01:55 +03007847 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007848
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007849 intel_get_pipe_timings(crtc, pipe_config);
7850
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007851 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Imre Deakda7e29b2014-02-18 00:02:02 +02007852 if (intel_display_power_enabled(dev_priv, pfit_domain))
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007853 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01007854
Jesse Barnese59150d2014-01-07 13:30:45 -08007855 if (IS_HASWELL(dev))
7856 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7857 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007858
Daniel Vetter6c49f242013-06-06 12:45:25 +02007859 pipe_config->pixel_multiplier = 1;
7860
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007861 return true;
7862}
7863
Jani Nikula1a915102013-10-16 12:34:48 +03007864static struct {
7865 int clock;
7866 u32 config;
7867} hdmi_audio_clock[] = {
7868 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7869 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7870 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7871 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7872 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7873 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7874 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7875 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7876 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7877 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7878};
7879
7880/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7881static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7882{
7883 int i;
7884
7885 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7886 if (mode->clock == hdmi_audio_clock[i].clock)
7887 break;
7888 }
7889
7890 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7891 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7892 i = 1;
7893 }
7894
7895 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7896 hdmi_audio_clock[i].clock,
7897 hdmi_audio_clock[i].config);
7898
7899 return hdmi_audio_clock[i].config;
7900}
7901
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007902static bool intel_eld_uptodate(struct drm_connector *connector,
7903 int reg_eldv, uint32_t bits_eldv,
7904 int reg_elda, uint32_t bits_elda,
7905 int reg_edid)
7906{
7907 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7908 uint8_t *eld = connector->eld;
7909 uint32_t i;
7910
7911 i = I915_READ(reg_eldv);
7912 i &= bits_eldv;
7913
7914 if (!eld[0])
7915 return !i;
7916
7917 if (!i)
7918 return false;
7919
7920 i = I915_READ(reg_elda);
7921 i &= ~bits_elda;
7922 I915_WRITE(reg_elda, i);
7923
7924 for (i = 0; i < eld[2]; i++)
7925 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7926 return false;
7927
7928 return true;
7929}
7930
Wu Fengguange0dac652011-09-05 14:25:34 +08007931static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007932 struct drm_crtc *crtc,
7933 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007934{
7935 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7936 uint8_t *eld = connector->eld;
7937 uint32_t eldv;
7938 uint32_t len;
7939 uint32_t i;
7940
7941 i = I915_READ(G4X_AUD_VID_DID);
7942
7943 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7944 eldv = G4X_ELDV_DEVCL_DEVBLC;
7945 else
7946 eldv = G4X_ELDV_DEVCTG;
7947
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007948 if (intel_eld_uptodate(connector,
7949 G4X_AUD_CNTL_ST, eldv,
7950 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7951 G4X_HDMIW_HDMIEDID))
7952 return;
7953
Wu Fengguange0dac652011-09-05 14:25:34 +08007954 i = I915_READ(G4X_AUD_CNTL_ST);
7955 i &= ~(eldv | G4X_ELD_ADDR);
7956 len = (i >> 9) & 0x1f; /* ELD buffer size */
7957 I915_WRITE(G4X_AUD_CNTL_ST, i);
7958
7959 if (!eld[0])
7960 return;
7961
7962 len = min_t(uint8_t, eld[2], len);
7963 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7964 for (i = 0; i < len; i++)
7965 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7966
7967 i = I915_READ(G4X_AUD_CNTL_ST);
7968 i |= eldv;
7969 I915_WRITE(G4X_AUD_CNTL_ST, i);
7970}
7971
Wang Xingchao83358c852012-08-16 22:43:37 +08007972static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007973 struct drm_crtc *crtc,
7974 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007975{
7976 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7977 uint8_t *eld = connector->eld;
Wang Xingchao83358c852012-08-16 22:43:37 +08007978 uint32_t eldv;
7979 uint32_t i;
7980 int len;
7981 int pipe = to_intel_crtc(crtc)->pipe;
7982 int tmp;
7983
7984 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7985 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7986 int aud_config = HSW_AUD_CFG(pipe);
7987 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7988
Wang Xingchao83358c852012-08-16 22:43:37 +08007989 /* Audio output enable */
7990 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7991 tmp = I915_READ(aud_cntrl_st2);
7992 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7993 I915_WRITE(aud_cntrl_st2, tmp);
Daniel Vetterc7905792014-04-16 16:56:09 +02007994 POSTING_READ(aud_cntrl_st2);
Wang Xingchao83358c852012-08-16 22:43:37 +08007995
Daniel Vetterc7905792014-04-16 16:56:09 +02007996 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Wang Xingchao83358c852012-08-16 22:43:37 +08007997
7998 /* Set ELD valid state */
7999 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02008000 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08008001 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
8002 I915_WRITE(aud_cntrl_st2, tmp);
8003 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02008004 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08008005
8006 /* Enable HDMI mode */
8007 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02008008 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08008009 /* clear N_programing_enable and N_value_index */
8010 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
8011 I915_WRITE(aud_config, tmp);
8012
8013 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
8014
8015 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
8016
8017 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8018 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8019 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
8020 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03008021 } else {
8022 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8023 }
Wang Xingchao83358c852012-08-16 22:43:37 +08008024
8025 if (intel_eld_uptodate(connector,
8026 aud_cntrl_st2, eldv,
8027 aud_cntl_st, IBX_ELD_ADDRESS,
8028 hdmiw_hdmiedid))
8029 return;
8030
8031 i = I915_READ(aud_cntrl_st2);
8032 i &= ~eldv;
8033 I915_WRITE(aud_cntrl_st2, i);
8034
8035 if (!eld[0])
8036 return;
8037
8038 i = I915_READ(aud_cntl_st);
8039 i &= ~IBX_ELD_ADDRESS;
8040 I915_WRITE(aud_cntl_st, i);
8041 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
8042 DRM_DEBUG_DRIVER("port num:%d\n", i);
8043
8044 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8045 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8046 for (i = 0; i < len; i++)
8047 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8048
8049 i = I915_READ(aud_cntrl_st2);
8050 i |= eldv;
8051 I915_WRITE(aud_cntrl_st2, i);
8052
8053}
8054
Wu Fengguange0dac652011-09-05 14:25:34 +08008055static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03008056 struct drm_crtc *crtc,
8057 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08008058{
8059 struct drm_i915_private *dev_priv = connector->dev->dev_private;
8060 uint8_t *eld = connector->eld;
8061 uint32_t eldv;
8062 uint32_t i;
8063 int len;
8064 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06008065 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08008066 int aud_cntl_st;
8067 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08008068 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08008069
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08008070 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08008071 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
8072 aud_config = IBX_AUD_CFG(pipe);
8073 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08008074 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04008075 } else if (IS_VALLEYVIEW(connector->dev)) {
8076 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
8077 aud_config = VLV_AUD_CFG(pipe);
8078 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
8079 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08008080 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08008081 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
8082 aud_config = CPT_AUD_CFG(pipe);
8083 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08008084 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08008085 }
8086
Wang Xingchao9b138a82012-08-09 16:52:18 +08008087 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08008088
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04008089 if (IS_VALLEYVIEW(connector->dev)) {
8090 struct intel_encoder *intel_encoder;
8091 struct intel_digital_port *intel_dig_port;
8092
8093 intel_encoder = intel_attached_encoder(connector);
8094 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
8095 i = intel_dig_port->port;
8096 } else {
8097 i = I915_READ(aud_cntl_st);
8098 i = (i >> 29) & DIP_PORT_SEL_MASK;
8099 /* DIP_Port_Select, 0x1 = PortB */
8100 }
8101
Wu Fengguange0dac652011-09-05 14:25:34 +08008102 if (!i) {
8103 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
8104 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08008105 eldv = IBX_ELD_VALIDB;
8106 eldv |= IBX_ELD_VALIDB << 4;
8107 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08008108 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03008109 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08008110 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08008111 }
8112
Wu Fengguang3a9627f2011-12-09 20:42:19 +08008113 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
8114 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
8115 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06008116 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03008117 } else {
8118 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
8119 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08008120
8121 if (intel_eld_uptodate(connector,
8122 aud_cntrl_st2, eldv,
8123 aud_cntl_st, IBX_ELD_ADDRESS,
8124 hdmiw_hdmiedid))
8125 return;
8126
Wu Fengguange0dac652011-09-05 14:25:34 +08008127 i = I915_READ(aud_cntrl_st2);
8128 i &= ~eldv;
8129 I915_WRITE(aud_cntrl_st2, i);
8130
8131 if (!eld[0])
8132 return;
8133
Wu Fengguange0dac652011-09-05 14:25:34 +08008134 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08008135 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08008136 I915_WRITE(aud_cntl_st, i);
8137
8138 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
8139 DRM_DEBUG_DRIVER("ELD size %d\n", len);
8140 for (i = 0; i < len; i++)
8141 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
8142
8143 i = I915_READ(aud_cntrl_st2);
8144 i |= eldv;
8145 I915_WRITE(aud_cntrl_st2, i);
8146}
8147
8148void intel_write_eld(struct drm_encoder *encoder,
8149 struct drm_display_mode *mode)
8150{
8151 struct drm_crtc *crtc = encoder->crtc;
8152 struct drm_connector *connector;
8153 struct drm_device *dev = encoder->dev;
8154 struct drm_i915_private *dev_priv = dev->dev_private;
8155
8156 connector = drm_select_eld(encoder, mode);
8157 if (!connector)
8158 return;
8159
8160 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8161 connector->base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03008162 connector->name,
Wu Fengguange0dac652011-09-05 14:25:34 +08008163 connector->encoder->base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +03008164 connector->encoder->name);
Wu Fengguange0dac652011-09-05 14:25:34 +08008165
8166 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
8167
8168 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03008169 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08008170}
8171
Chris Wilson560b85b2010-08-07 11:01:38 +01008172static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8173{
8174 struct drm_device *dev = crtc->dev;
8175 struct drm_i915_private *dev_priv = dev->dev_private;
8176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03008177 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01008178
Ville Syrjälädc41c152014-08-13 11:57:05 +03008179 if (base) {
8180 unsigned int width = intel_crtc->cursor_width;
8181 unsigned int height = intel_crtc->cursor_height;
8182 unsigned int stride = roundup_pow_of_two(width) * 4;
8183
8184 switch (stride) {
8185 default:
8186 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8187 width, stride);
8188 stride = 256;
8189 /* fallthrough */
8190 case 256:
8191 case 512:
8192 case 1024:
8193 case 2048:
8194 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008195 }
8196
Ville Syrjälädc41c152014-08-13 11:57:05 +03008197 cntl |= CURSOR_ENABLE |
8198 CURSOR_GAMMA_ENABLE |
8199 CURSOR_FORMAT_ARGB |
8200 CURSOR_STRIDE(stride);
8201
8202 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008203 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008204
Ville Syrjälädc41c152014-08-13 11:57:05 +03008205 if (intel_crtc->cursor_cntl != 0 &&
8206 (intel_crtc->cursor_base != base ||
8207 intel_crtc->cursor_size != size ||
8208 intel_crtc->cursor_cntl != cntl)) {
8209 /* On these chipsets we can only modify the base/size/stride
8210 * whilst the cursor is disabled.
8211 */
8212 I915_WRITE(_CURACNTR, 0);
8213 POSTING_READ(_CURACNTR);
8214 intel_crtc->cursor_cntl = 0;
8215 }
8216
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008217 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03008218 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008219 intel_crtc->cursor_base = base;
8220 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03008221
8222 if (intel_crtc->cursor_size != size) {
8223 I915_WRITE(CURSIZE, size);
8224 intel_crtc->cursor_size = size;
8225 }
8226
Chris Wilson4b0e3332014-05-30 16:35:26 +03008227 if (intel_crtc->cursor_cntl != cntl) {
8228 I915_WRITE(_CURACNTR, cntl);
8229 POSTING_READ(_CURACNTR);
8230 intel_crtc->cursor_cntl = cntl;
8231 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008232}
8233
8234static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8235{
8236 struct drm_device *dev = crtc->dev;
8237 struct drm_i915_private *dev_priv = dev->dev_private;
8238 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8239 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008240 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01008241
Chris Wilson4b0e3332014-05-30 16:35:26 +03008242 cntl = 0;
8243 if (base) {
8244 cntl = MCURSOR_GAMMA_ENABLE;
8245 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308246 case 64:
8247 cntl |= CURSOR_MODE_64_ARGB_AX;
8248 break;
8249 case 128:
8250 cntl |= CURSOR_MODE_128_ARGB_AX;
8251 break;
8252 case 256:
8253 cntl |= CURSOR_MODE_256_ARGB_AX;
8254 break;
8255 default:
8256 WARN_ON(1);
8257 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01008258 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008259 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03008260
8261 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8262 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01008263 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008264
8265 if (intel_crtc->cursor_cntl != cntl) {
8266 I915_WRITE(CURCNTR(pipe), cntl);
8267 POSTING_READ(CURCNTR(pipe));
8268 intel_crtc->cursor_cntl = cntl;
8269 }
8270
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008271 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008272 I915_WRITE(CURBASE(pipe), base);
8273 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008274
8275 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008276}
8277
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008278/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008279static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8280 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008281{
8282 struct drm_device *dev = crtc->dev;
8283 struct drm_i915_private *dev_priv = dev->dev_private;
8284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8285 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008286 int x = crtc->cursor_x;
8287 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008288 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008289
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008290 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008291 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008292
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008293 if (x >= intel_crtc->config.pipe_src_w)
8294 base = 0;
8295
8296 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008297 base = 0;
8298
8299 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008300 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008301 base = 0;
8302
8303 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8304 x = -x;
8305 }
8306 pos |= x << CURSOR_X_SHIFT;
8307
8308 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008309 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008310 base = 0;
8311
8312 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8313 y = -y;
8314 }
8315 pos |= y << CURSOR_Y_SHIFT;
8316
Chris Wilson4b0e3332014-05-30 16:35:26 +03008317 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008318 return;
8319
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008320 I915_WRITE(CURPOS(pipe), pos);
8321
Ville Syrjälä8ac54662014-08-12 19:39:54 +03008322 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008323 i845_update_cursor(crtc, base);
8324 else
8325 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008326}
8327
Ville Syrjälädc41c152014-08-13 11:57:05 +03008328static bool cursor_size_ok(struct drm_device *dev,
8329 uint32_t width, uint32_t height)
8330{
8331 if (width == 0 || height == 0)
8332 return false;
8333
8334 /*
8335 * 845g/865g are special in that they are only limited by
8336 * the width of their cursors, the height is arbitrary up to
8337 * the precision of the register. Everything else requires
8338 * square cursors, limited to a few power-of-two sizes.
8339 */
8340 if (IS_845G(dev) || IS_I865G(dev)) {
8341 if ((width & 63) != 0)
8342 return false;
8343
8344 if (width > (IS_845G(dev) ? 64 : 512))
8345 return false;
8346
8347 if (height > 1023)
8348 return false;
8349 } else {
8350 switch (width | height) {
8351 case 256:
8352 case 128:
8353 if (IS_GEN2(dev))
8354 return false;
8355 case 64:
8356 break;
8357 default:
8358 return false;
8359 }
8360 }
8361
8362 return true;
8363}
8364
Matt Ropere3287952014-06-10 08:28:12 -07008365/*
8366 * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
8367 *
8368 * Note that the object's reference will be consumed if the update fails. If
8369 * the update succeeds, the reference of the old object (if any) will be
8370 * consumed.
8371 */
8372static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8373 struct drm_i915_gem_object *obj,
8374 uint32_t width, uint32_t height)
Jesse Barnes79e53942008-11-07 14:24:08 -08008375{
8376 struct drm_device *dev = crtc->dev;
8377 struct drm_i915_private *dev_priv = dev->dev_private;
8378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02008379 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälädc41c152014-08-13 11:57:05 +03008380 unsigned old_width, stride;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008381 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008382 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008383
Jesse Barnes79e53942008-11-07 14:24:08 -08008384 /* if we want to turn off the cursor ignore width and height */
Matt Ropere3287952014-06-10 08:28:12 -07008385 if (!obj) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008386 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008387 addr = 0;
Pierre Willenbrock50044172009-02-23 10:12:15 +10008388 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008389 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08008390 }
8391
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308392 /* Check for which cursor types we support */
Ville Syrjälädc41c152014-08-13 11:57:05 +03008393 if (!cursor_size_ok(dev, width, height)) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308394 DRM_DEBUG("Cursor dimension not supported\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08008395 return -EINVAL;
8396 }
8397
Ville Syrjälädc41c152014-08-13 11:57:05 +03008398 stride = roundup_pow_of_two(width) * 4;
8399 if (obj->base.size < stride * height) {
Matt Ropere3287952014-06-10 08:28:12 -07008400 DRM_DEBUG_KMS("buffer is too small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10008401 ret = -ENOMEM;
8402 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008403 }
8404
Dave Airlie71acb5e2008-12-30 20:31:46 +10008405 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008406 mutex_lock(&dev->struct_mutex);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008407 if (!INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00008408 unsigned alignment;
8409
Chris Wilsond9e86c02010-11-10 16:40:20 +00008410 if (obj->tiling_mode) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008411 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Chris Wilsond9e86c02010-11-10 16:40:20 +00008412 ret = -EINVAL;
8413 goto fail_locked;
8414 }
8415
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008416 /*
8417 * Global gtt pte registers are special registers which actually
8418 * forward writes to a chunk of system memory. Which means that
8419 * there is no risk that the register values disappear as soon
8420 * as we call intel_runtime_pm_put(), so it is correct to wrap
8421 * only the pin/unpin/fence and not more.
8422 */
8423 intel_runtime_pm_get(dev_priv);
8424
Chris Wilson693db182013-03-05 14:52:39 +00008425 /* Note that the w/a also requires 2 PTE of padding following
8426 * the bo. We currently fill all unused PTE with the shadow
8427 * page and so we should always have valid PTE following the
8428 * cursor preventing the VT-d warning.
8429 */
8430 alignment = 0;
8431 if (need_vtd_wa(dev))
8432 alignment = 64*1024;
8433
8434 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01008435 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008436 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008437 intel_runtime_pm_put(dev_priv);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01008438 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008439 }
8440
Chris Wilsond9e86c02010-11-10 16:40:20 +00008441 ret = i915_gem_object_put_fence(obj);
8442 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008443 DRM_DEBUG_KMS("failed to release fence for cursor");
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008444 intel_runtime_pm_put(dev_priv);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008445 goto fail_unpin;
8446 }
8447
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008448 addr = i915_gem_obj_ggtt_offset(obj);
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008449
8450 intel_runtime_pm_put(dev_priv);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008451 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01008452 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson00731152014-05-21 12:42:56 +01008453 ret = i915_gem_object_attach_phys(obj, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008454 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008455 DRM_DEBUG_KMS("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008456 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10008457 }
Chris Wilson00731152014-05-21 12:42:56 +01008458 addr = obj->phys_handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008459 }
8460
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008461 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008462 if (intel_crtc->cursor_bo) {
Chris Wilson00731152014-05-21 12:42:56 +01008463 if (!INTEL_INFO(dev)->cursor_needs_physical)
Chris Wilsoncc98b412013-08-09 12:25:09 +01008464 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008465 }
Jesse Barnes80824002009-09-10 15:28:06 -07008466
Daniel Vettera071fa02014-06-18 23:28:09 +02008467 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8468 INTEL_FRONTBUFFER_CURSOR(pipe));
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008469 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008470
Chris Wilson64f962e2014-03-26 12:38:15 +00008471 old_width = intel_crtc->cursor_width;
8472
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008473 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00008474 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008475 intel_crtc->cursor_width = width;
8476 intel_crtc->cursor_height = height;
8477
Chris Wilson64f962e2014-03-26 12:38:15 +00008478 if (intel_crtc->active) {
8479 if (old_width != width)
8480 intel_update_watermarks(crtc);
Ville Syrjäläf2f5f7712013-09-17 18:33:44 +03008481 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Chris Wilson64f962e2014-03-26 12:38:15 +00008482 }
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008483
Daniel Vetterf99d7062014-06-19 16:01:59 +02008484 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
8485
Jesse Barnes79e53942008-11-07 14:24:08 -08008486 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008487fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01008488 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008489fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10008490 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00008491fail:
Chris Wilson05394f32010-11-08 19:18:58 +00008492 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10008493 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008494}
8495
Jesse Barnes79e53942008-11-07 14:24:08 -08008496static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008497 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008498{
James Simmons72034252010-08-03 01:33:19 +01008499 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008500 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008501
James Simmons72034252010-08-03 01:33:19 +01008502 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008503 intel_crtc->lut_r[i] = red[i] >> 8;
8504 intel_crtc->lut_g[i] = green[i] >> 8;
8505 intel_crtc->lut_b[i] = blue[i] >> 8;
8506 }
8507
8508 intel_crtc_load_lut(crtc);
8509}
8510
Jesse Barnes79e53942008-11-07 14:24:08 -08008511/* VESA 640x480x72Hz mode to set on the pipe */
8512static struct drm_display_mode load_detect_mode = {
8513 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8514 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8515};
8516
Daniel Vettera8bb6812014-02-10 18:00:39 +01008517struct drm_framebuffer *
8518__intel_framebuffer_create(struct drm_device *dev,
8519 struct drm_mode_fb_cmd2 *mode_cmd,
8520 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008521{
8522 struct intel_framebuffer *intel_fb;
8523 int ret;
8524
8525 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8526 if (!intel_fb) {
8527 drm_gem_object_unreference_unlocked(&obj->base);
8528 return ERR_PTR(-ENOMEM);
8529 }
8530
8531 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008532 if (ret)
8533 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008534
8535 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008536err:
8537 drm_gem_object_unreference_unlocked(&obj->base);
8538 kfree(intel_fb);
8539
8540 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008541}
8542
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008543static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008544intel_framebuffer_create(struct drm_device *dev,
8545 struct drm_mode_fb_cmd2 *mode_cmd,
8546 struct drm_i915_gem_object *obj)
8547{
8548 struct drm_framebuffer *fb;
8549 int ret;
8550
8551 ret = i915_mutex_lock_interruptible(dev);
8552 if (ret)
8553 return ERR_PTR(ret);
8554 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8555 mutex_unlock(&dev->struct_mutex);
8556
8557 return fb;
8558}
8559
Chris Wilsond2dff872011-04-19 08:36:26 +01008560static u32
8561intel_framebuffer_pitch_for_width(int width, int bpp)
8562{
8563 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8564 return ALIGN(pitch, 64);
8565}
8566
8567static u32
8568intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8569{
8570 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02008571 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01008572}
8573
8574static struct drm_framebuffer *
8575intel_framebuffer_create_for_mode(struct drm_device *dev,
8576 struct drm_display_mode *mode,
8577 int depth, int bpp)
8578{
8579 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008580 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008581
8582 obj = i915_gem_alloc_object(dev,
8583 intel_framebuffer_size_for_mode(mode, bpp));
8584 if (obj == NULL)
8585 return ERR_PTR(-ENOMEM);
8586
8587 mode_cmd.width = mode->hdisplay;
8588 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008589 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8590 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008591 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008592
8593 return intel_framebuffer_create(dev, &mode_cmd, obj);
8594}
8595
8596static struct drm_framebuffer *
8597mode_fits_in_fbdev(struct drm_device *dev,
8598 struct drm_display_mode *mode)
8599{
Daniel Vetter4520f532013-10-09 09:18:51 +02008600#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008601 struct drm_i915_private *dev_priv = dev->dev_private;
8602 struct drm_i915_gem_object *obj;
8603 struct drm_framebuffer *fb;
8604
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008605 if (!dev_priv->fbdev)
8606 return NULL;
8607
8608 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008609 return NULL;
8610
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008611 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008612 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008613
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008614 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008615 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8616 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008617 return NULL;
8618
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008619 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008620 return NULL;
8621
8622 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008623#else
8624 return NULL;
8625#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008626}
8627
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008628bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008629 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008630 struct intel_load_detect_pipe *old,
8631 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008632{
8633 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008634 struct intel_encoder *intel_encoder =
8635 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008636 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008637 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008638 struct drm_crtc *crtc = NULL;
8639 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008640 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008641 struct drm_mode_config *config = &dev->mode_config;
8642 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008643
Chris Wilsond2dff872011-04-19 08:36:26 +01008644 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008645 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008646 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008647
Rob Clark51fd3712013-11-19 12:10:12 -05008648retry:
8649 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8650 if (ret)
8651 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008652
Jesse Barnes79e53942008-11-07 14:24:08 -08008653 /*
8654 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008655 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008656 * - if the connector already has an assigned crtc, use it (but make
8657 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008658 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008659 * - try to find the first unused crtc that can drive this connector,
8660 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008661 */
8662
8663 /* See if we already have a CRTC for this connector */
8664 if (encoder->crtc) {
8665 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008666
Rob Clark51fd3712013-11-19 12:10:12 -05008667 ret = drm_modeset_lock(&crtc->mutex, ctx);
8668 if (ret)
8669 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01008670
Daniel Vetter24218aa2012-08-12 19:27:11 +02008671 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008672 old->load_detect_temp = false;
8673
8674 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008675 if (connector->dpms != DRM_MODE_DPMS_ON)
8676 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008677
Chris Wilson71731882011-04-19 23:10:58 +01008678 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008679 }
8680
8681 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008682 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008683 i++;
8684 if (!(encoder->possible_crtcs & (1 << i)))
8685 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +03008686 if (possible_crtc->enabled)
8687 continue;
8688 /* This can occur when applying the pipe A quirk on resume. */
8689 if (to_intel_crtc(possible_crtc)->new_enabled)
8690 continue;
8691
8692 crtc = possible_crtc;
8693 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008694 }
8695
8696 /*
8697 * If we didn't find an unused CRTC, don't use any.
8698 */
8699 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008700 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05008701 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08008702 }
8703
Rob Clark51fd3712013-11-19 12:10:12 -05008704 ret = drm_modeset_lock(&crtc->mutex, ctx);
8705 if (ret)
8706 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02008707 intel_encoder->new_crtc = to_intel_crtc(crtc);
8708 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008709
8710 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008711 intel_crtc->new_enabled = true;
8712 intel_crtc->new_config = &intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008713 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008714 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008715 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008716
Chris Wilson64927112011-04-20 07:25:26 +01008717 if (!mode)
8718 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008719
Chris Wilsond2dff872011-04-19 08:36:26 +01008720 /* We need a framebuffer large enough to accommodate all accesses
8721 * that the plane may generate whilst we perform load detection.
8722 * We can not rely on the fbcon either being present (we get called
8723 * during its initialisation to detect all boot displays, or it may
8724 * not even exist) or that it is large enough to satisfy the
8725 * requested mode.
8726 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008727 fb = mode_fits_in_fbdev(dev, mode);
8728 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008729 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008730 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8731 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008732 } else
8733 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008734 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008735 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008736 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008737 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008738
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008739 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008740 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008741 if (old->release_fb)
8742 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008743 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008744 }
Chris Wilson71731882011-04-19 23:10:58 +01008745
Jesse Barnes79e53942008-11-07 14:24:08 -08008746 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008747 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008748 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008749
8750 fail:
8751 intel_crtc->new_enabled = crtc->enabled;
8752 if (intel_crtc->new_enabled)
8753 intel_crtc->new_config = &intel_crtc->config;
8754 else
8755 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05008756fail_unlock:
8757 if (ret == -EDEADLK) {
8758 drm_modeset_backoff(ctx);
8759 goto retry;
8760 }
8761
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008762 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008763}
8764
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008765void intel_release_load_detect_pipe(struct drm_connector *connector,
Ville Syrjälä208bf9f2014-08-11 13:15:35 +03008766 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08008767{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008768 struct intel_encoder *intel_encoder =
8769 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008770 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008771 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008773
Chris Wilsond2dff872011-04-19 08:36:26 +01008774 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008775 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008776 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008777
Chris Wilson8261b192011-04-19 23:18:09 +01008778 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008779 to_intel_connector(connector)->new_encoder = NULL;
8780 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008781 intel_crtc->new_enabled = false;
8782 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008783 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01008784
Daniel Vetter36206362012-12-10 20:42:17 +01008785 if (old->release_fb) {
8786 drm_framebuffer_unregister_private(old->release_fb);
8787 drm_framebuffer_unreference(old->release_fb);
8788 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008789
Chris Wilson0622a532011-04-21 09:32:11 +01008790 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008791 }
8792
Eric Anholtc751ce42010-03-25 11:48:48 -07008793 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008794 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8795 connector->funcs->dpms(connector, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008796}
8797
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008798static int i9xx_pll_refclk(struct drm_device *dev,
8799 const struct intel_crtc_config *pipe_config)
8800{
8801 struct drm_i915_private *dev_priv = dev->dev_private;
8802 u32 dpll = pipe_config->dpll_hw_state.dpll;
8803
8804 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008805 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008806 else if (HAS_PCH_SPLIT(dev))
8807 return 120000;
8808 else if (!IS_GEN2(dev))
8809 return 96000;
8810 else
8811 return 48000;
8812}
8813
Jesse Barnes79e53942008-11-07 14:24:08 -08008814/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008815static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8816 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08008817{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008818 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008819 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008820 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008821 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008822 u32 fp;
8823 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008824 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08008825
8826 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03008827 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008828 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008829 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008830
8831 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008832 if (IS_PINEVIEW(dev)) {
8833 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8834 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008835 } else {
8836 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8837 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8838 }
8839
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008840 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008841 if (IS_PINEVIEW(dev))
8842 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8843 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008844 else
8845 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008846 DPLL_FPA01_P1_POST_DIV_SHIFT);
8847
8848 switch (dpll & DPLL_MODE_MASK) {
8849 case DPLLB_MODE_DAC_SERIAL:
8850 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8851 5 : 10;
8852 break;
8853 case DPLLB_MODE_LVDS:
8854 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8855 7 : 14;
8856 break;
8857 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008858 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008859 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008860 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008861 }
8862
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008863 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008864 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008865 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008866 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008867 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008868 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008869 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008870
8871 if (is_lvds) {
8872 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8873 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008874
8875 if (lvds & LVDS_CLKB_POWER_UP)
8876 clock.p2 = 7;
8877 else
8878 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008879 } else {
8880 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8881 clock.p1 = 2;
8882 else {
8883 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8884 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8885 }
8886 if (dpll & PLL_P2_DIVIDE_BY_4)
8887 clock.p2 = 4;
8888 else
8889 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008890 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008891
8892 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008893 }
8894
Ville Syrjälä18442d02013-09-13 16:00:08 +03008895 /*
8896 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008897 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03008898 * encoder's get_config() function.
8899 */
8900 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008901}
8902
Ville Syrjälä6878da02013-09-13 15:59:11 +03008903int intel_dotclock_calculate(int link_freq,
8904 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008905{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008906 /*
8907 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008908 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008909 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008910 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008911 *
8912 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008913 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008914 */
8915
Ville Syrjälä6878da02013-09-13 15:59:11 +03008916 if (!m_n->link_n)
8917 return 0;
8918
8919 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8920}
8921
Ville Syrjälä18442d02013-09-13 16:00:08 +03008922static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8923 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008924{
8925 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008926
8927 /* read out port_clock from the DPLL */
8928 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008929
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008930 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008931 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008932 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008933 * agree once we know their relationship in the encoder's
8934 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008935 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008936 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008937 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8938 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008939}
8940
8941/** Returns the currently programmed mode of the given pipe. */
8942struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8943 struct drm_crtc *crtc)
8944{
Jesse Barnes548f2452011-02-17 10:40:53 -08008945 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008947 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008948 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008949 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008950 int htot = I915_READ(HTOTAL(cpu_transcoder));
8951 int hsync = I915_READ(HSYNC(cpu_transcoder));
8952 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8953 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008954 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008955
8956 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8957 if (!mode)
8958 return NULL;
8959
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008960 /*
8961 * Construct a pipe_config sufficient for getting the clock info
8962 * back out of crtc_clock_get.
8963 *
8964 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8965 * to use a real value here instead.
8966 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008967 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008968 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008969 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8970 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8971 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008972 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8973
Ville Syrjälä773ae032013-09-23 17:48:20 +03008974 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008975 mode->hdisplay = (htot & 0xffff) + 1;
8976 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8977 mode->hsync_start = (hsync & 0xffff) + 1;
8978 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8979 mode->vdisplay = (vtot & 0xffff) + 1;
8980 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8981 mode->vsync_start = (vsync & 0xffff) + 1;
8982 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8983
8984 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008985
8986 return mode;
8987}
8988
Daniel Vettercc365132014-06-18 13:59:13 +02008989static void intel_increase_pllclock(struct drm_device *dev,
8990 enum pipe pipe)
Jesse Barnes652c3932009-08-17 13:31:43 -07008991{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008992 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008993 int dpll_reg = DPLL(pipe);
8994 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008995
Sonika Jindalbaff2962014-07-22 11:16:35 +05308996 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008997 return;
8998
8999 if (!dev_priv->lvds_downclock_avail)
9000 return;
9001
Jesse Barnesdbdc6472010-12-30 09:36:39 -08009002 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07009003 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08009004 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009005
Sean Paul8ac5a6d2012-02-13 13:14:51 -05009006 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009007
9008 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
9009 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009010 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08009011
Jesse Barnes652c3932009-08-17 13:31:43 -07009012 dpll = I915_READ(dpll_reg);
9013 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08009014 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009015 }
Jesse Barnes652c3932009-08-17 13:31:43 -07009016}
9017
9018static void intel_decrease_pllclock(struct drm_crtc *crtc)
9019{
9020 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03009021 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07009022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009023
Sonika Jindalbaff2962014-07-22 11:16:35 +05309024 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07009025 return;
9026
9027 if (!dev_priv->lvds_downclock_avail)
9028 return;
9029
9030 /*
9031 * Since this is called by a timer, we should never get here in
9032 * the manual case.
9033 */
9034 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01009035 int pipe = intel_crtc->pipe;
9036 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02009037 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01009038
Zhao Yakui44d98a62009-10-09 11:39:40 +08009039 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009040
Sean Paul8ac5a6d2012-02-13 13:14:51 -05009041 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009042
Chris Wilson074b5e12012-05-02 12:07:06 +01009043 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07009044 dpll |= DISPLAY_RATE_SELECT_FPA1;
9045 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009046 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07009047 dpll = I915_READ(dpll_reg);
9048 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08009049 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07009050 }
9051
9052}
9053
Chris Wilsonf047e392012-07-21 12:31:41 +01009054void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07009055{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009056 struct drm_i915_private *dev_priv = dev->dev_private;
9057
Chris Wilsonf62a0072014-02-21 17:55:39 +00009058 if (dev_priv->mm.busy)
9059 return;
9060
Paulo Zanoni43694d62014-03-07 20:08:08 -03009061 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009062 i915_update_gfx_val(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00009063 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01009064}
9065
9066void intel_mark_idle(struct drm_device *dev)
9067{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009068 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00009069 struct drm_crtc *crtc;
9070
Chris Wilsonf62a0072014-02-21 17:55:39 +00009071 if (!dev_priv->mm.busy)
9072 return;
9073
9074 dev_priv->mm.busy = false;
9075
Jani Nikulad330a952014-01-21 11:24:25 +02009076 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009077 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00009078
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009079 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07009080 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00009081 continue;
9082
9083 intel_decrease_pllclock(crtc);
9084 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009085
Damien Lespiau3d13ef22014-02-07 19:12:47 +00009086 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01009087 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03009088
9089out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03009090 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01009091}
9092
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -07009093
Daniel Vetterf99d7062014-06-19 16:01:59 +02009094/**
9095 * intel_mark_fb_busy - mark given planes as busy
9096 * @dev: DRM device
9097 * @frontbuffer_bits: bits for the affected planes
9098 * @ring: optional ring for asynchronous commands
9099 *
9100 * This function gets called every time the screen contents change. It can be
9101 * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
9102 */
9103static void intel_mark_fb_busy(struct drm_device *dev,
9104 unsigned frontbuffer_bits,
9105 struct intel_engine_cs *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01009106{
Damien Lespiau055e3932014-08-18 13:49:10 +01009107 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettercc365132014-06-18 13:59:13 +02009108 enum pipe pipe;
Jesse Barnes652c3932009-08-17 13:31:43 -07009109
Jani Nikulad330a952014-01-21 11:24:25 +02009110 if (!i915.powersave)
Jesse Barnes652c3932009-08-17 13:31:43 -07009111 return;
9112
Damien Lespiau055e3932014-08-18 13:49:10 +01009113 for_each_pipe(dev_priv, pipe) {
Daniel Vetterf99d7062014-06-19 16:01:59 +02009114 if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
Jesse Barnes652c3932009-08-17 13:31:43 -07009115 continue;
9116
Daniel Vettercc365132014-06-18 13:59:13 +02009117 intel_increase_pllclock(dev, pipe);
Chris Wilsonc65355b2013-06-06 16:53:41 -03009118 if (ring && intel_fbc_enabled(dev))
9119 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07009120 }
Jesse Barnes652c3932009-08-17 13:31:43 -07009121}
9122
Daniel Vetterf99d7062014-06-19 16:01:59 +02009123/**
9124 * intel_fb_obj_invalidate - invalidate frontbuffer object
9125 * @obj: GEM object to invalidate
9126 * @ring: set for asynchronous rendering
9127 *
9128 * This function gets called every time rendering on the given object starts and
9129 * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
9130 * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
9131 * until the rendering completes or a flip on this frontbuffer plane is
9132 * scheduled.
9133 */
9134void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
9135 struct intel_engine_cs *ring)
9136{
9137 struct drm_device *dev = obj->base.dev;
9138 struct drm_i915_private *dev_priv = dev->dev_private;
9139
9140 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9141
9142 if (!obj->frontbuffer_bits)
9143 return;
9144
9145 if (ring) {
9146 mutex_lock(&dev_priv->fb_tracking.lock);
9147 dev_priv->fb_tracking.busy_bits
9148 |= obj->frontbuffer_bits;
9149 dev_priv->fb_tracking.flip_bits
9150 &= ~obj->frontbuffer_bits;
9151 mutex_unlock(&dev_priv->fb_tracking.lock);
9152 }
9153
9154 intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
9155
Daniel Vetter9ca15302014-07-11 10:30:16 -07009156 intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009157}
9158
9159/**
9160 * intel_frontbuffer_flush - flush frontbuffer
9161 * @dev: DRM device
9162 * @frontbuffer_bits: frontbuffer plane tracking bits
9163 *
9164 * This function gets called every time rendering on the given planes has
9165 * completed and frontbuffer caching can be started again. Flushes will get
9166 * delayed if they're blocked by some oustanding asynchronous rendering.
9167 *
9168 * Can be called without any locks held.
9169 */
9170void intel_frontbuffer_flush(struct drm_device *dev,
9171 unsigned frontbuffer_bits)
9172{
9173 struct drm_i915_private *dev_priv = dev->dev_private;
9174
9175 /* Delay flushing when rings are still busy.*/
9176 mutex_lock(&dev_priv->fb_tracking.lock);
9177 frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
9178 mutex_unlock(&dev_priv->fb_tracking.lock);
9179
9180 intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
9181
Daniel Vetter9ca15302014-07-11 10:30:16 -07009182 intel_edp_psr_flush(dev, frontbuffer_bits);
Rodrigo Vivic5ad0112014-08-04 03:51:38 -07009183
Ville Syrjäläc317adc2014-09-03 14:09:50 +03009184 /*
9185 * FIXME: Unconditional fbc flushing here is a rather gross hack and
9186 * needs to be reworked into a proper frontbuffer tracking scheme like
9187 * psr employs.
9188 */
9189 if (IS_BROADWELL(dev))
Rodrigo Vivic5ad0112014-08-04 03:51:38 -07009190 gen8_fbc_sw_flush(dev, FBC_REND_CACHE_CLEAN);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009191}
9192
9193/**
9194 * intel_fb_obj_flush - flush frontbuffer object
9195 * @obj: GEM object to flush
9196 * @retire: set when retiring asynchronous rendering
9197 *
9198 * This function gets called every time rendering on the given object has
9199 * completed and frontbuffer caching can be started again. If @retire is true
9200 * then any delayed flushes will be unblocked.
9201 */
9202void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
9203 bool retire)
9204{
9205 struct drm_device *dev = obj->base.dev;
9206 struct drm_i915_private *dev_priv = dev->dev_private;
9207 unsigned frontbuffer_bits;
9208
9209 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
9210
9211 if (!obj->frontbuffer_bits)
9212 return;
9213
9214 frontbuffer_bits = obj->frontbuffer_bits;
9215
9216 if (retire) {
9217 mutex_lock(&dev_priv->fb_tracking.lock);
9218 /* Filter out new bits since rendering started. */
9219 frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
9220
9221 dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
9222 mutex_unlock(&dev_priv->fb_tracking.lock);
9223 }
9224
9225 intel_frontbuffer_flush(dev, frontbuffer_bits);
9226}
9227
9228/**
9229 * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
9230 * @dev: DRM device
9231 * @frontbuffer_bits: frontbuffer plane tracking bits
9232 *
9233 * This function gets called after scheduling a flip on @obj. The actual
9234 * frontbuffer flushing will be delayed until completion is signalled with
9235 * intel_frontbuffer_flip_complete. If an invalidate happens in between this
9236 * flush will be cancelled.
9237 *
9238 * Can be called without any locks held.
9239 */
9240void intel_frontbuffer_flip_prepare(struct drm_device *dev,
9241 unsigned frontbuffer_bits)
9242{
9243 struct drm_i915_private *dev_priv = dev->dev_private;
9244
9245 mutex_lock(&dev_priv->fb_tracking.lock);
9246 dev_priv->fb_tracking.flip_bits
9247 |= frontbuffer_bits;
9248 mutex_unlock(&dev_priv->fb_tracking.lock);
9249}
9250
9251/**
9252 * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
9253 * @dev: DRM device
9254 * @frontbuffer_bits: frontbuffer plane tracking bits
9255 *
9256 * This function gets called after the flip has been latched and will complete
9257 * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
9258 *
9259 * Can be called without any locks held.
9260 */
9261void intel_frontbuffer_flip_complete(struct drm_device *dev,
9262 unsigned frontbuffer_bits)
9263{
9264 struct drm_i915_private *dev_priv = dev->dev_private;
9265
9266 mutex_lock(&dev_priv->fb_tracking.lock);
9267 /* Mask any cancelled flips. */
9268 frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
9269 dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
9270 mutex_unlock(&dev_priv->fb_tracking.lock);
9271
9272 intel_frontbuffer_flush(dev, frontbuffer_bits);
9273}
9274
Jesse Barnes79e53942008-11-07 14:24:08 -08009275static void intel_crtc_destroy(struct drm_crtc *crtc)
9276{
9277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009278 struct drm_device *dev = crtc->dev;
9279 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +02009280
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009281 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009282 work = intel_crtc->unpin_work;
9283 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009284 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009285
9286 if (work) {
9287 cancel_work_sync(&work->work);
9288 kfree(work);
9289 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009290
9291 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02009292
Jesse Barnes79e53942008-11-07 14:24:08 -08009293 kfree(intel_crtc);
9294}
9295
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009296static void intel_unpin_work_fn(struct work_struct *__work)
9297{
9298 struct intel_unpin_work *work =
9299 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009300 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +02009301 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009302
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009303 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01009304 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00009305 drm_gem_object_unreference(&work->pending_flip_obj->base);
9306 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00009307
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009308 intel_update_fbc(dev);
9309 mutex_unlock(&dev->struct_mutex);
9310
Daniel Vetterf99d7062014-06-19 16:01:59 +02009311 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9312
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009313 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9314 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9315
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009316 kfree(work);
9317}
9318
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009319static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01009320 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009321{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9323 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009324 unsigned long flags;
9325
9326 /* Ignore early vblank irqs */
9327 if (intel_crtc == NULL)
9328 return;
9329
Daniel Vetterf3260382014-09-15 14:55:23 +02009330 /*
9331 * This is called both by irq handlers and the reset code (to complete
9332 * lost pageflips) so needs the full irqsave spinlocks.
9333 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009334 spin_lock_irqsave(&dev->event_lock, flags);
9335 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00009336
9337 /* Ensure we don't miss a work->pending update ... */
9338 smp_rmb();
9339
9340 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009341 spin_unlock_irqrestore(&dev->event_lock, flags);
9342 return;
9343 }
9344
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009345 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01009346
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009347 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009348}
9349
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009350void intel_finish_page_flip(struct drm_device *dev, int pipe)
9351{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009352 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009353 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9354
Mario Kleiner49b14a52010-12-09 07:00:07 +01009355 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009356}
9357
9358void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9359{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009360 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009361 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9362
Mario Kleiner49b14a52010-12-09 07:00:07 +01009363 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07009364}
9365
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009366/* Is 'a' after or equal to 'b'? */
9367static bool g4x_flip_count_after_eq(u32 a, u32 b)
9368{
9369 return !((a - b) & 0x80000000);
9370}
9371
9372static bool page_flip_finished(struct intel_crtc *crtc)
9373{
9374 struct drm_device *dev = crtc->base.dev;
9375 struct drm_i915_private *dev_priv = dev->dev_private;
9376
9377 /*
9378 * The relevant registers doen't exist on pre-ctg.
9379 * As the flip done interrupt doesn't trigger for mmio
9380 * flips on gmch platforms, a flip count check isn't
9381 * really needed there. But since ctg has the registers,
9382 * include it in the check anyway.
9383 */
9384 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9385 return true;
9386
9387 /*
9388 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9389 * used the same base address. In that case the mmio flip might
9390 * have completed, but the CS hasn't even executed the flip yet.
9391 *
9392 * A flip count check isn't enough as the CS might have updated
9393 * the base address just after start of vblank, but before we
9394 * managed to process the interrupt. This means we'd complete the
9395 * CS flip too soon.
9396 *
9397 * Combining both checks should get us a good enough result. It may
9398 * still happen that the CS flip has been executed, but has not
9399 * yet actually completed. But in case the base address is the same
9400 * anyway, we don't really care.
9401 */
9402 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9403 crtc->unpin_work->gtt_offset &&
9404 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9405 crtc->unpin_work->flip_count);
9406}
9407
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009408void intel_prepare_page_flip(struct drm_device *dev, int plane)
9409{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009410 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009411 struct intel_crtc *intel_crtc =
9412 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9413 unsigned long flags;
9414
Daniel Vetterf3260382014-09-15 14:55:23 +02009415
9416 /*
9417 * This is called both by irq handlers and the reset code (to complete
9418 * lost pageflips) so needs the full irqsave spinlocks.
9419 *
9420 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +00009421 * generate a page-flip completion irq, i.e. every modeset
9422 * is also accompanied by a spurious intel_prepare_page_flip().
9423 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009424 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009425 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009426 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009427 spin_unlock_irqrestore(&dev->event_lock, flags);
9428}
9429
Robin Schroereba905b2014-05-18 02:24:50 +02009430static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009431{
9432 /* Ensure that the work item is consistent when activating it ... */
9433 smp_wmb();
9434 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9435 /* and that it is marked active as soon as the irq could fire. */
9436 smp_wmb();
9437}
9438
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009439static int intel_gen2_queue_flip(struct drm_device *dev,
9440 struct drm_crtc *crtc,
9441 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009442 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009443 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009444 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009445{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009446 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009447 u32 flip_mask;
9448 int ret;
9449
Daniel Vetter6d90c952012-04-26 23:28:05 +02009450 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009451 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009452 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009453
9454 /* Can't queue multiple flips, so wait for the previous
9455 * one to finish before executing the next.
9456 */
9457 if (intel_crtc->plane)
9458 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9459 else
9460 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009461 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9462 intel_ring_emit(ring, MI_NOOP);
9463 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9464 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9465 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009466 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009467 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009468
9469 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009470 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009471 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009472}
9473
9474static int intel_gen3_queue_flip(struct drm_device *dev,
9475 struct drm_crtc *crtc,
9476 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009477 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009478 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009479 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009480{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009481 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009482 u32 flip_mask;
9483 int ret;
9484
Daniel Vetter6d90c952012-04-26 23:28:05 +02009485 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009486 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009487 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009488
9489 if (intel_crtc->plane)
9490 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9491 else
9492 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009493 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9494 intel_ring_emit(ring, MI_NOOP);
9495 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9496 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9497 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009498 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009499 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009500
Chris Wilsone7d841c2012-12-03 11:36:30 +00009501 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009502 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009503 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009504}
9505
9506static int intel_gen4_queue_flip(struct drm_device *dev,
9507 struct drm_crtc *crtc,
9508 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009509 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009510 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009511 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009512{
9513 struct drm_i915_private *dev_priv = dev->dev_private;
9514 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9515 uint32_t pf, pipesrc;
9516 int ret;
9517
Daniel Vetter6d90c952012-04-26 23:28:05 +02009518 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009519 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009520 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009521
9522 /* i965+ uses the linear or tiled offsets from the
9523 * Display Registers (which do not change across a page-flip)
9524 * so we need only reprogram the base address.
9525 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009526 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9527 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9528 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009529 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009530 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009531
9532 /* XXX Enabling the panel-fitter across page-flip is so far
9533 * untested on non-native modes, so ignore it for now.
9534 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9535 */
9536 pf = 0;
9537 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009538 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009539
9540 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009541 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009542 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009543}
9544
9545static int intel_gen6_queue_flip(struct drm_device *dev,
9546 struct drm_crtc *crtc,
9547 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009548 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009549 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009550 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009551{
9552 struct drm_i915_private *dev_priv = dev->dev_private;
9553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9554 uint32_t pf, pipesrc;
9555 int ret;
9556
Daniel Vetter6d90c952012-04-26 23:28:05 +02009557 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009558 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009559 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009560
Daniel Vetter6d90c952012-04-26 23:28:05 +02009561 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9562 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9563 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009564 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009565
Chris Wilson99d9acd2012-04-17 20:37:00 +01009566 /* Contrary to the suggestions in the documentation,
9567 * "Enable Panel Fitter" does not seem to be required when page
9568 * flipping with a non-native mode, and worse causes a normal
9569 * modeset to fail.
9570 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9571 */
9572 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009573 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009574 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009575
9576 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009577 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009578 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009579}
9580
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009581static int intel_gen7_queue_flip(struct drm_device *dev,
9582 struct drm_crtc *crtc,
9583 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009584 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009585 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009586 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009587{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009588 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009589 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009590 int len, ret;
9591
Robin Schroereba905b2014-05-18 02:24:50 +02009592 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009593 case PLANE_A:
9594 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9595 break;
9596 case PLANE_B:
9597 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9598 break;
9599 case PLANE_C:
9600 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9601 break;
9602 default:
9603 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009604 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009605 }
9606
Chris Wilsonffe74d72013-08-26 20:58:12 +01009607 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009608 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009609 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009610 /*
9611 * On Gen 8, SRM is now taking an extra dword to accommodate
9612 * 48bits addresses, and we need a NOOP for the batch size to
9613 * stay even.
9614 */
9615 if (IS_GEN8(dev))
9616 len += 2;
9617 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009618
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009619 /*
9620 * BSpec MI_DISPLAY_FLIP for IVB:
9621 * "The full packet must be contained within the same cache line."
9622 *
9623 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9624 * cacheline, if we ever start emitting more commands before
9625 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9626 * then do the cacheline alignment, and finally emit the
9627 * MI_DISPLAY_FLIP.
9628 */
9629 ret = intel_ring_cacheline_align(ring);
9630 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009631 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009632
Chris Wilsonffe74d72013-08-26 20:58:12 +01009633 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009634 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009635 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009636
Chris Wilsonffe74d72013-08-26 20:58:12 +01009637 /* Unmask the flip-done completion message. Note that the bspec says that
9638 * we should do this for both the BCS and RCS, and that we must not unmask
9639 * more than one flip event at any time (or ensure that one flip message
9640 * can be sent by waiting for flip-done prior to queueing new flips).
9641 * Experimentation says that BCS works despite DERRMR masking all
9642 * flip-done completion events and that unmasking all planes at once
9643 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9644 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9645 */
9646 if (ring->id == RCS) {
9647 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9648 intel_ring_emit(ring, DERRMR);
9649 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9650 DERRMR_PIPEB_PRI_FLIP_DONE |
9651 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009652 if (IS_GEN8(dev))
9653 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9654 MI_SRM_LRM_GLOBAL_GTT);
9655 else
9656 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9657 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009658 intel_ring_emit(ring, DERRMR);
9659 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009660 if (IS_GEN8(dev)) {
9661 intel_ring_emit(ring, 0);
9662 intel_ring_emit(ring, MI_NOOP);
9663 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009664 }
9665
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009666 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009667 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009668 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009669 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009670
9671 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009672 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009673 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009674}
9675
Sourab Gupta84c33a62014-06-02 16:47:17 +05309676static bool use_mmio_flip(struct intel_engine_cs *ring,
9677 struct drm_i915_gem_object *obj)
9678{
9679 /*
9680 * This is not being used for older platforms, because
9681 * non-availability of flip done interrupt forces us to use
9682 * CS flips. Older platforms derive flip done using some clever
9683 * tricks involving the flip_pending status bits and vblank irqs.
9684 * So using MMIO flips there would disrupt this mechanism.
9685 */
9686
Chris Wilson8e09bf82014-07-08 10:40:30 +01009687 if (ring == NULL)
9688 return true;
9689
Sourab Gupta84c33a62014-06-02 16:47:17 +05309690 if (INTEL_INFO(ring->dev)->gen < 5)
9691 return false;
9692
9693 if (i915.use_mmio_flip < 0)
9694 return false;
9695 else if (i915.use_mmio_flip > 0)
9696 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +01009697 else if (i915.enable_execlists)
9698 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +05309699 else
9700 return ring != obj->ring;
9701}
9702
9703static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9704{
9705 struct drm_device *dev = intel_crtc->base.dev;
9706 struct drm_i915_private *dev_priv = dev->dev_private;
9707 struct intel_framebuffer *intel_fb =
9708 to_intel_framebuffer(intel_crtc->base.primary->fb);
9709 struct drm_i915_gem_object *obj = intel_fb->obj;
9710 u32 dspcntr;
9711 u32 reg;
9712
9713 intel_mark_page_flip_active(intel_crtc);
9714
9715 reg = DSPCNTR(intel_crtc->plane);
9716 dspcntr = I915_READ(reg);
9717
9718 if (INTEL_INFO(dev)->gen >= 4) {
9719 if (obj->tiling_mode != I915_TILING_NONE)
9720 dspcntr |= DISPPLANE_TILED;
9721 else
9722 dspcntr &= ~DISPPLANE_TILED;
9723 }
9724 I915_WRITE(reg, dspcntr);
9725
9726 I915_WRITE(DSPSURF(intel_crtc->plane),
9727 intel_crtc->unpin_work->gtt_offset);
9728 POSTING_READ(DSPSURF(intel_crtc->plane));
9729}
9730
9731static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9732{
9733 struct intel_engine_cs *ring;
9734 int ret;
9735
9736 lockdep_assert_held(&obj->base.dev->struct_mutex);
9737
9738 if (!obj->last_write_seqno)
9739 return 0;
9740
9741 ring = obj->ring;
9742
9743 if (i915_seqno_passed(ring->get_seqno(ring, true),
9744 obj->last_write_seqno))
9745 return 0;
9746
9747 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9748 if (ret)
9749 return ret;
9750
9751 if (WARN_ON(!ring->irq_get(ring)))
9752 return 0;
9753
9754 return 1;
9755}
9756
9757void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9758{
9759 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9760 struct intel_crtc *intel_crtc;
9761 unsigned long irq_flags;
9762 u32 seqno;
9763
9764 seqno = ring->get_seqno(ring, false);
9765
9766 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9767 for_each_intel_crtc(ring->dev, intel_crtc) {
9768 struct intel_mmio_flip *mmio_flip;
9769
9770 mmio_flip = &intel_crtc->mmio_flip;
9771 if (mmio_flip->seqno == 0)
9772 continue;
9773
9774 if (ring->id != mmio_flip->ring_id)
9775 continue;
9776
9777 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9778 intel_do_mmio_flip(intel_crtc);
9779 mmio_flip->seqno = 0;
9780 ring->irq_put(ring);
9781 }
9782 }
9783 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9784}
9785
9786static int intel_queue_mmio_flip(struct drm_device *dev,
9787 struct drm_crtc *crtc,
9788 struct drm_framebuffer *fb,
9789 struct drm_i915_gem_object *obj,
9790 struct intel_engine_cs *ring,
9791 uint32_t flags)
9792{
9793 struct drm_i915_private *dev_priv = dev->dev_private;
9794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309795 int ret;
9796
9797 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9798 return -EBUSY;
9799
9800 ret = intel_postpone_flip(obj);
9801 if (ret < 0)
9802 return ret;
9803 if (ret == 0) {
9804 intel_do_mmio_flip(intel_crtc);
9805 return 0;
9806 }
9807
Daniel Vetter24955f22014-09-15 14:55:32 +02009808 spin_lock_irq(&dev_priv->mmio_flip_lock);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309809 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9810 intel_crtc->mmio_flip.ring_id = obj->ring->id;
Daniel Vetter24955f22014-09-15 14:55:32 +02009811 spin_unlock_irq(&dev_priv->mmio_flip_lock);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309812
9813 /*
9814 * Double check to catch cases where irq fired before
9815 * mmio flip data was ready
9816 */
9817 intel_notify_mmio_flip(obj->ring);
9818 return 0;
9819}
9820
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009821static int intel_default_queue_flip(struct drm_device *dev,
9822 struct drm_crtc *crtc,
9823 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009824 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009825 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009826 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009827{
9828 return -ENODEV;
9829}
9830
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009831static bool __intel_pageflip_stall_check(struct drm_device *dev,
9832 struct drm_crtc *crtc)
9833{
9834 struct drm_i915_private *dev_priv = dev->dev_private;
9835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9836 struct intel_unpin_work *work = intel_crtc->unpin_work;
9837 u32 addr;
9838
9839 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9840 return true;
9841
9842 if (!work->enable_stall_check)
9843 return false;
9844
9845 if (work->flip_ready_vblank == 0) {
9846 if (work->flip_queued_ring &&
9847 !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
9848 work->flip_queued_seqno))
9849 return false;
9850
9851 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9852 }
9853
9854 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9855 return false;
9856
9857 /* Potential stall - if we see that the flip has happened,
9858 * assume a missed interrupt. */
9859 if (INTEL_INFO(dev)->gen >= 4)
9860 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9861 else
9862 addr = I915_READ(DSPADDR(intel_crtc->plane));
9863
9864 /* There is a potential issue here with a false positive after a flip
9865 * to the same address. We could address this by checking for a
9866 * non-incrementing frame counter.
9867 */
9868 return addr == work->gtt_offset;
9869}
9870
9871void intel_check_page_flip(struct drm_device *dev, int pipe)
9872{
9873 struct drm_i915_private *dev_priv = dev->dev_private;
9874 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterf3260382014-09-15 14:55:23 +02009876
9877 WARN_ON(!in_irq());
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009878
9879 if (crtc == NULL)
9880 return;
9881
Daniel Vetterf3260382014-09-15 14:55:23 +02009882 spin_lock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009883 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9884 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9885 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9886 page_flip_completed(intel_crtc);
9887 }
Daniel Vetterf3260382014-09-15 14:55:23 +02009888 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009889}
9890
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009891static int intel_crtc_page_flip(struct drm_crtc *crtc,
9892 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009893 struct drm_pending_vblank_event *event,
9894 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009895{
9896 struct drm_device *dev = crtc->dev;
9897 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009898 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009899 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02009901 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009902 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009903 struct intel_engine_cs *ring;
Chris Wilson52e68632010-08-08 10:15:59 +01009904 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009905
Daisy Sunc76bb612014-08-11 11:08:38 -07009906 //trigger software GT busyness calculation
9907 gen8_flip_interrupt(dev);
9908
Matt Roper2ff8fde2014-07-08 07:50:07 -07009909 /*
9910 * drm_mode_page_flip_ioctl() should already catch this, but double
9911 * check to be safe. In the future we may enable pageflipping from
9912 * a disabled primary plane.
9913 */
9914 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9915 return -EBUSY;
9916
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009917 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009918 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009919 return -EINVAL;
9920
9921 /*
9922 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9923 * Note that pitch changes could also affect these register.
9924 */
9925 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009926 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9927 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009928 return -EINVAL;
9929
Chris Wilsonf900db42014-02-20 09:26:13 +00009930 if (i915_terminally_wedged(&dev_priv->gpu_error))
9931 goto out_hang;
9932
Daniel Vetterb14c5672013-09-19 12:18:32 +02009933 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009934 if (work == NULL)
9935 return -ENOMEM;
9936
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009937 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009938 work->crtc = crtc;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009939 work->old_fb_obj = intel_fb_obj(old_fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009940 INIT_WORK(&work->work, intel_unpin_work_fn);
9941
Daniel Vetter87b6b102014-05-15 15:33:46 +02009942 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009943 if (ret)
9944 goto free_work;
9945
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009946 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009947 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009948 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009949 /* Before declaring the flip queue wedged, check if
9950 * the hardware completed the operation behind our backs.
9951 */
9952 if (__intel_pageflip_stall_check(dev, crtc)) {
9953 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9954 page_flip_completed(intel_crtc);
9955 } else {
9956 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009957 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +01009958
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009959 drm_crtc_vblank_put(crtc);
9960 kfree(work);
9961 return -EBUSY;
9962 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009963 }
9964 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009965 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009966
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009967 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9968 flush_workqueue(dev_priv->wq);
9969
Chris Wilson79158102012-05-23 11:13:58 +01009970 ret = i915_mutex_lock_interruptible(dev);
9971 if (ret)
9972 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009973
Jesse Barnes75dfca82010-02-10 15:09:44 -08009974 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00009975 drm_gem_object_reference(&work->old_fb_obj->base);
9976 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009977
Matt Roperf4510a22014-04-01 15:22:40 -07009978 crtc->primary->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01009979
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009980 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009981
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009982 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02009983 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009984
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009985 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +02009986 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009987
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009988 if (IS_VALLEYVIEW(dev)) {
9989 ring = &dev_priv->ring[BCS];
Chris Wilson8e09bf82014-07-08 10:40:30 +01009990 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
9991 /* vlv: DISPLAY_FLIP fails to change tiling */
9992 ring = NULL;
Chris Wilson2a92d5b2014-07-08 10:40:29 +01009993 } else if (IS_IVYBRIDGE(dev)) {
9994 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009995 } else if (INTEL_INFO(dev)->gen >= 7) {
9996 ring = obj->ring;
9997 if (ring == NULL || ring->id != RCS)
9998 ring = &dev_priv->ring[BCS];
9999 } else {
10000 ring = &dev_priv->ring[RCS];
10001 }
10002
10003 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010004 if (ret)
10005 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010006
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010007 work->gtt_offset =
10008 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
10009
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010010 if (use_mmio_flip(ring, obj)) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010011 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10012 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010013 if (ret)
10014 goto cleanup_unpin;
10015
10016 work->flip_queued_seqno = obj->last_write_seqno;
10017 work->flip_queued_ring = obj->ring;
10018 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010019 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010020 page_flip_flags);
10021 if (ret)
10022 goto cleanup_unpin;
10023
10024 work->flip_queued_seqno = intel_ring_get_seqno(ring);
10025 work->flip_queued_ring = ring;
10026 }
10027
10028 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
10029 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010030
Daniel Vettera071fa02014-06-18 23:28:09 +020010031 i915_gem_track_fb(work->old_fb_obj, obj,
10032 INTEL_FRONTBUFFER_PRIMARY(pipe));
10033
Chris Wilson7782de32011-07-08 12:22:41 +010010034 intel_disable_fbc(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010035 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010036 mutex_unlock(&dev->struct_mutex);
10037
Jesse Barnese5510fa2010-07-01 16:48:37 -070010038 trace_i915_flip_request(intel_crtc->plane, obj);
10039
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010040 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010010041
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010042cleanup_unpin:
10043 intel_unpin_fb_obj(obj);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010044cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010045 atomic_dec(&intel_crtc->unpin_work_count);
Matt Roperf4510a22014-04-01 15:22:40 -070010046 crtc->primary->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +000010047 drm_gem_object_unreference(&work->old_fb_obj->base);
10048 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +010010049 mutex_unlock(&dev->struct_mutex);
10050
Chris Wilson79158102012-05-23 11:13:58 +010010051cleanup:
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010052 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010053 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010054 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010055
Daniel Vetter87b6b102014-05-15 15:33:46 +020010056 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010057free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010010058 kfree(work);
10059
Chris Wilsonf900db42014-02-20 09:26:13 +000010060 if (ret == -EIO) {
10061out_hang:
10062 intel_crtc_wait_for_pending_flips(crtc);
10063 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010064 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010065 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020010066 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010067 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010068 }
Chris Wilsonf900db42014-02-20 09:26:13 +000010069 }
Chris Wilson96b099f2010-06-07 14:03:04 +010010070 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010071}
10072
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010073static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010074 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10075 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010076};
10077
Daniel Vetter9a935852012-07-05 22:34:27 +020010078/**
10079 * intel_modeset_update_staged_output_state
10080 *
10081 * Updates the staged output configuration state, e.g. after we've read out the
10082 * current hw state.
10083 */
10084static void intel_modeset_update_staged_output_state(struct drm_device *dev)
10085{
Ville Syrjälä76688512014-01-10 11:28:06 +020010086 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010087 struct intel_encoder *encoder;
10088 struct intel_connector *connector;
10089
10090 list_for_each_entry(connector, &dev->mode_config.connector_list,
10091 base.head) {
10092 connector->new_encoder =
10093 to_intel_encoder(connector->base.encoder);
10094 }
10095
Damien Lespiaub2784e12014-08-05 11:29:37 +010010096 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010097 encoder->new_crtc =
10098 to_intel_crtc(encoder->base.crtc);
10099 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010100
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010101 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010102 crtc->new_enabled = crtc->base.enabled;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010103
10104 if (crtc->new_enabled)
10105 crtc->new_config = &crtc->config;
10106 else
10107 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010108 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010109}
10110
10111/**
10112 * intel_modeset_commit_output_state
10113 *
10114 * This function copies the stage display pipe configuration to the real one.
10115 */
10116static void intel_modeset_commit_output_state(struct drm_device *dev)
10117{
Ville Syrjälä76688512014-01-10 11:28:06 +020010118 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010119 struct intel_encoder *encoder;
10120 struct intel_connector *connector;
10121
10122 list_for_each_entry(connector, &dev->mode_config.connector_list,
10123 base.head) {
10124 connector->base.encoder = &connector->new_encoder->base;
10125 }
10126
Damien Lespiaub2784e12014-08-05 11:29:37 +010010127 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010128 encoder->base.crtc = &encoder->new_crtc->base;
10129 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010130
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010131 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010132 crtc->base.enabled = crtc->new_enabled;
10133 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010134}
10135
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010136static void
Robin Schroereba905b2014-05-18 02:24:50 +020010137connected_sink_compute_bpp(struct intel_connector *connector,
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010138 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010139{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010140 int bpp = pipe_config->pipe_bpp;
10141
10142 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10143 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010144 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010145
10146 /* Don't use an invalid EDID bpc value */
10147 if (connector->base.display_info.bpc &&
10148 connector->base.display_info.bpc * 3 < bpp) {
10149 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10150 bpp, connector->base.display_info.bpc*3);
10151 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10152 }
10153
10154 /* Clamp bpp to 8 on screens without EDID 1.4 */
10155 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10156 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10157 bpp);
10158 pipe_config->pipe_bpp = 24;
10159 }
10160}
10161
10162static int
10163compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10164 struct drm_framebuffer *fb,
10165 struct intel_crtc_config *pipe_config)
10166{
10167 struct drm_device *dev = crtc->base.dev;
10168 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010169 int bpp;
10170
Daniel Vetterd42264b2013-03-28 16:38:08 +010010171 switch (fb->pixel_format) {
10172 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010173 bpp = 8*3; /* since we go through a colormap */
10174 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010175 case DRM_FORMAT_XRGB1555:
10176 case DRM_FORMAT_ARGB1555:
10177 /* checked in intel_framebuffer_init already */
10178 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10179 return -EINVAL;
10180 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010181 bpp = 6*3; /* min is 18bpp */
10182 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010183 case DRM_FORMAT_XBGR8888:
10184 case DRM_FORMAT_ABGR8888:
10185 /* checked in intel_framebuffer_init already */
10186 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10187 return -EINVAL;
10188 case DRM_FORMAT_XRGB8888:
10189 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010190 bpp = 8*3;
10191 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +010010192 case DRM_FORMAT_XRGB2101010:
10193 case DRM_FORMAT_ARGB2101010:
10194 case DRM_FORMAT_XBGR2101010:
10195 case DRM_FORMAT_ABGR2101010:
10196 /* checked in intel_framebuffer_init already */
10197 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +010010198 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010199 bpp = 10*3;
10200 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +010010201 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010202 default:
10203 DRM_DEBUG_KMS("unsupported depth\n");
10204 return -EINVAL;
10205 }
10206
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010207 pipe_config->pipe_bpp = bpp;
10208
10209 /* Clamp display bpp to EDID value */
10210 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010211 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +020010212 if (!connector->new_encoder ||
10213 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010214 continue;
10215
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010216 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010217 }
10218
10219 return bpp;
10220}
10221
Daniel Vetter644db712013-09-19 14:53:58 +020010222static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10223{
10224 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10225 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010010226 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020010227 mode->crtc_hdisplay, mode->crtc_hsync_start,
10228 mode->crtc_hsync_end, mode->crtc_htotal,
10229 mode->crtc_vdisplay, mode->crtc_vsync_start,
10230 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10231}
10232
Daniel Vetterc0b03412013-05-28 12:05:54 +020010233static void intel_dump_pipe_config(struct intel_crtc *crtc,
10234 struct intel_crtc_config *pipe_config,
10235 const char *context)
10236{
10237 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10238 context, pipe_name(crtc->pipe));
10239
10240 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10241 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10242 pipe_config->pipe_bpp, pipe_config->dither);
10243 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10244 pipe_config->has_pch_encoder,
10245 pipe_config->fdi_lanes,
10246 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10247 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10248 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010249 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10250 pipe_config->has_dp_encoder,
10251 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10252 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10253 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010254
10255 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10256 pipe_config->has_dp_encoder,
10257 pipe_config->dp_m2_n2.gmch_m,
10258 pipe_config->dp_m2_n2.gmch_n,
10259 pipe_config->dp_m2_n2.link_m,
10260 pipe_config->dp_m2_n2.link_n,
10261 pipe_config->dp_m2_n2.tu);
10262
Daniel Vetterc0b03412013-05-28 12:05:54 +020010263 DRM_DEBUG_KMS("requested mode:\n");
10264 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
10265 DRM_DEBUG_KMS("adjusted mode:\n");
10266 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +020010267 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030010268 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010269 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10270 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010271 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10272 pipe_config->gmch_pfit.control,
10273 pipe_config->gmch_pfit.pgm_ratios,
10274 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010275 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020010276 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010277 pipe_config->pch_pfit.size,
10278 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010279 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030010280 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +020010281}
10282
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010283static bool encoders_cloneable(const struct intel_encoder *a,
10284 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010285{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010286 /* masks could be asymmetric, so check both ways */
10287 return a == b || (a->cloneable & (1 << b->type) &&
10288 b->cloneable & (1 << a->type));
10289}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010290
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010291static bool check_single_encoder_cloning(struct intel_crtc *crtc,
10292 struct intel_encoder *encoder)
10293{
10294 struct drm_device *dev = crtc->base.dev;
10295 struct intel_encoder *source_encoder;
10296
Damien Lespiaub2784e12014-08-05 11:29:37 +010010297 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010298 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010299 continue;
10300
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010301 if (!encoders_cloneable(encoder, source_encoder))
10302 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010303 }
10304
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010305 return true;
10306}
10307
10308static bool check_encoder_cloning(struct intel_crtc *crtc)
10309{
10310 struct drm_device *dev = crtc->base.dev;
10311 struct intel_encoder *encoder;
10312
Damien Lespiaub2784e12014-08-05 11:29:37 +010010313 for_each_intel_encoder(dev, encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010314 if (encoder->new_crtc != crtc)
10315 continue;
10316
10317 if (!check_single_encoder_cloning(crtc, encoder))
10318 return false;
10319 }
10320
10321 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010322}
10323
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010324static struct intel_crtc_config *
10325intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010326 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010327 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +020010328{
10329 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +020010330 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010331 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +010010332 int plane_bpp, ret = -EINVAL;
10333 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020010334
Ville Syrjäläbc079e82014-03-03 16:15:28 +020010335 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020010336 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10337 return ERR_PTR(-EINVAL);
10338 }
10339
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010340 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
10341 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020010342 return ERR_PTR(-ENOMEM);
10343
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010344 drm_mode_copy(&pipe_config->adjusted_mode, mode);
10345 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010346
Daniel Vettere143a212013-07-04 12:01:15 +020010347 pipe_config->cpu_transcoder =
10348 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010349 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010350
Imre Deak2960bc92013-07-30 13:36:32 +030010351 /*
10352 * Sanitize sync polarity flags based on requested ones. If neither
10353 * positive or negative polarity is requested, treat this as meaning
10354 * negative polarity.
10355 */
10356 if (!(pipe_config->adjusted_mode.flags &
10357 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
10358 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
10359
10360 if (!(pipe_config->adjusted_mode.flags &
10361 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
10362 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
10363
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010364 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10365 * plane pixel format and any sink constraints into account. Returns the
10366 * source plane bpp so that dithering can be selected on mismatches
10367 * after encoders and crtc also have had their say. */
10368 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10369 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010370 if (plane_bpp < 0)
10371 goto fail;
10372
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030010373 /*
10374 * Determine the real pipe dimensions. Note that stereo modes can
10375 * increase the actual pipe size due to the frame doubling and
10376 * insertion of additional space for blanks between the frame. This
10377 * is stored in the crtc timings. We use the requested mode to do this
10378 * computation to clearly distinguish it from the adjusted mode, which
10379 * can be changed by the connectors in the below retry loop.
10380 */
10381 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
10382 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
10383 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
10384
Daniel Vettere29c22c2013-02-21 00:00:16 +010010385encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020010386 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020010387 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020010388 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010389
Daniel Vetter135c81b2013-07-21 21:37:09 +020010390 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +010010391 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020010392
Daniel Vetter7758a112012-07-08 19:40:39 +020010393 /* Pass our mode to the connectors and the CRTC to give them a chance to
10394 * adjust it according to limitations or connector properties, and also
10395 * a chance to reject the mode entirely.
10396 */
Damien Lespiaub2784e12014-08-05 11:29:37 +010010397 for_each_intel_encoder(dev, encoder) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010398
10399 if (&encoder->new_crtc->base != crtc)
10400 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010010401
Daniel Vetterefea6e82013-07-21 21:36:59 +020010402 if (!(encoder->compute_config(encoder, pipe_config))) {
10403 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020010404 goto fail;
10405 }
10406 }
10407
Daniel Vetterff9a6752013-06-01 17:16:21 +020010408 /* Set default port clock if not overwritten by the encoder. Needs to be
10409 * done afterwards in case the encoder adjusts the mode. */
10410 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +010010411 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
10412 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010413
Daniel Vettera43f6e02013-06-07 23:10:32 +020010414 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010415 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010416 DRM_DEBUG_KMS("CRTC fixup failed\n");
10417 goto fail;
10418 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010010419
10420 if (ret == RETRY) {
10421 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10422 ret = -EINVAL;
10423 goto fail;
10424 }
10425
10426 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10427 retry = false;
10428 goto encoder_retry;
10429 }
10430
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010431 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10432 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10433 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10434
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010435 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020010436fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010437 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010438 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020010439}
10440
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010441/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10442 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10443static void
10444intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10445 unsigned *prepare_pipes, unsigned *disable_pipes)
10446{
10447 struct intel_crtc *intel_crtc;
10448 struct drm_device *dev = crtc->dev;
10449 struct intel_encoder *encoder;
10450 struct intel_connector *connector;
10451 struct drm_crtc *tmp_crtc;
10452
10453 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
10454
10455 /* Check which crtcs have changed outputs connected to them, these need
10456 * to be part of the prepare_pipes mask. We don't (yet) support global
10457 * modeset across multiple crtcs, so modeset_pipes will only have one
10458 * bit set at most. */
10459 list_for_each_entry(connector, &dev->mode_config.connector_list,
10460 base.head) {
10461 if (connector->base.encoder == &connector->new_encoder->base)
10462 continue;
10463
10464 if (connector->base.encoder) {
10465 tmp_crtc = connector->base.encoder->crtc;
10466
10467 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10468 }
10469
10470 if (connector->new_encoder)
10471 *prepare_pipes |=
10472 1 << connector->new_encoder->new_crtc->pipe;
10473 }
10474
Damien Lespiaub2784e12014-08-05 11:29:37 +010010475 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010476 if (encoder->base.crtc == &encoder->new_crtc->base)
10477 continue;
10478
10479 if (encoder->base.crtc) {
10480 tmp_crtc = encoder->base.crtc;
10481
10482 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10483 }
10484
10485 if (encoder->new_crtc)
10486 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
10487 }
10488
Ville Syrjälä76688512014-01-10 11:28:06 +020010489 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010490 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010491 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010492 continue;
10493
Ville Syrjälä76688512014-01-10 11:28:06 +020010494 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010495 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020010496 else
10497 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010498 }
10499
10500
10501 /* set_mode is also used to update properties on life display pipes. */
10502 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020010503 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010504 *prepare_pipes |= 1 << intel_crtc->pipe;
10505
Daniel Vetterb6c51642013-04-12 18:48:43 +020010506 /*
10507 * For simplicity do a full modeset on any pipe where the output routing
10508 * changed. We could be more clever, but that would require us to be
10509 * more careful with calling the relevant encoder->mode_set functions.
10510 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010511 if (*prepare_pipes)
10512 *modeset_pipes = *prepare_pipes;
10513
10514 /* ... and mask these out. */
10515 *modeset_pipes &= ~(*disable_pipes);
10516 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020010517
10518 /*
10519 * HACK: We don't (yet) fully support global modesets. intel_set_config
10520 * obies this rule, but the modeset restore mode of
10521 * intel_modeset_setup_hw_state does not.
10522 */
10523 *modeset_pipes &= 1 << intel_crtc->pipe;
10524 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020010525
10526 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10527 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010528}
10529
Daniel Vetterea9d7582012-07-10 10:42:52 +020010530static bool intel_crtc_in_use(struct drm_crtc *crtc)
10531{
10532 struct drm_encoder *encoder;
10533 struct drm_device *dev = crtc->dev;
10534
10535 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10536 if (encoder->crtc == crtc)
10537 return true;
10538
10539 return false;
10540}
10541
10542static void
10543intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10544{
10545 struct intel_encoder *intel_encoder;
10546 struct intel_crtc *intel_crtc;
10547 struct drm_connector *connector;
10548
Damien Lespiaub2784e12014-08-05 11:29:37 +010010549 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020010550 if (!intel_encoder->base.crtc)
10551 continue;
10552
10553 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10554
10555 if (prepare_pipes & (1 << intel_crtc->pipe))
10556 intel_encoder->connectors_active = false;
10557 }
10558
10559 intel_modeset_commit_output_state(dev);
10560
Ville Syrjälä76688512014-01-10 11:28:06 +020010561 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010562 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010563 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010564 WARN_ON(intel_crtc->new_config &&
10565 intel_crtc->new_config != &intel_crtc->config);
10566 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010567 }
10568
10569 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10570 if (!connector->encoder || !connector->encoder->crtc)
10571 continue;
10572
10573 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10574
10575 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020010576 struct drm_property *dpms_property =
10577 dev->mode_config.dpms_property;
10578
Daniel Vetterea9d7582012-07-10 10:42:52 +020010579 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050010580 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020010581 dpms_property,
10582 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010583
10584 intel_encoder = to_intel_encoder(connector->encoder);
10585 intel_encoder->connectors_active = true;
10586 }
10587 }
10588
10589}
10590
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010591static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010592{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010593 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010594
10595 if (clock1 == clock2)
10596 return true;
10597
10598 if (!clock1 || !clock2)
10599 return false;
10600
10601 diff = abs(clock1 - clock2);
10602
10603 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10604 return true;
10605
10606 return false;
10607}
10608
Daniel Vetter25c5b262012-07-08 22:08:04 +020010609#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10610 list_for_each_entry((intel_crtc), \
10611 &(dev)->mode_config.crtc_list, \
10612 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020010613 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020010614
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010615static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010616intel_pipe_config_compare(struct drm_device *dev,
10617 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010618 struct intel_crtc_config *pipe_config)
10619{
Daniel Vetter66e985c2013-06-05 13:34:20 +020010620#define PIPE_CONF_CHECK_X(name) \
10621 if (current_config->name != pipe_config->name) { \
10622 DRM_ERROR("mismatch in " #name " " \
10623 "(expected 0x%08x, found 0x%08x)\n", \
10624 current_config->name, \
10625 pipe_config->name); \
10626 return false; \
10627 }
10628
Daniel Vetter08a24032013-04-19 11:25:34 +020010629#define PIPE_CONF_CHECK_I(name) \
10630 if (current_config->name != pipe_config->name) { \
10631 DRM_ERROR("mismatch in " #name " " \
10632 "(expected %i, found %i)\n", \
10633 current_config->name, \
10634 pipe_config->name); \
10635 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010010636 }
10637
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010638/* This is required for BDW+ where there is only one set of registers for
10639 * switching between high and low RR.
10640 * This macro can be used whenever a comparison has to be made between one
10641 * hw state and multiple sw state variables.
10642 */
10643#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10644 if ((current_config->name != pipe_config->name) && \
10645 (current_config->alt_name != pipe_config->name)) { \
10646 DRM_ERROR("mismatch in " #name " " \
10647 "(expected %i or %i, found %i)\n", \
10648 current_config->name, \
10649 current_config->alt_name, \
10650 pipe_config->name); \
10651 return false; \
10652 }
10653
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010654#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10655 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070010656 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010657 "(expected %i, found %i)\n", \
10658 current_config->name & (mask), \
10659 pipe_config->name & (mask)); \
10660 return false; \
10661 }
10662
Ville Syrjälä5e550652013-09-06 23:29:07 +030010663#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10664 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10665 DRM_ERROR("mismatch in " #name " " \
10666 "(expected %i, found %i)\n", \
10667 current_config->name, \
10668 pipe_config->name); \
10669 return false; \
10670 }
10671
Daniel Vetterbb760062013-06-06 14:55:52 +020010672#define PIPE_CONF_QUIRK(quirk) \
10673 ((current_config->quirks | pipe_config->quirks) & (quirk))
10674
Daniel Vettereccb1402013-05-22 00:50:22 +020010675 PIPE_CONF_CHECK_I(cpu_transcoder);
10676
Daniel Vetter08a24032013-04-19 11:25:34 +020010677 PIPE_CONF_CHECK_I(has_pch_encoder);
10678 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020010679 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10680 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10681 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10682 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10683 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020010684
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010685 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010686
10687 if (INTEL_INFO(dev)->gen < 8) {
10688 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10689 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10690 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10691 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10692 PIPE_CONF_CHECK_I(dp_m_n.tu);
10693
10694 if (current_config->has_drrs) {
10695 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10696 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10697 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10698 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10699 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10700 }
10701 } else {
10702 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10703 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10704 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10705 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10706 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10707 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010708
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010709 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10710 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10711 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10712 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10713 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10714 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10715
10716 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10717 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10718 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10719 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10720 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10721 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10722
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010723 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020010724 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020010725 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10726 IS_VALLEYVIEW(dev))
10727 PIPE_CONF_CHECK_I(limited_color_range);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010728
Daniel Vetter9ed109a2014-04-24 23:54:52 +020010729 PIPE_CONF_CHECK_I(has_audio);
10730
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010731 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10732 DRM_MODE_FLAG_INTERLACE);
10733
Daniel Vetterbb760062013-06-06 14:55:52 +020010734 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10735 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10736 DRM_MODE_FLAG_PHSYNC);
10737 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10738 DRM_MODE_FLAG_NHSYNC);
10739 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10740 DRM_MODE_FLAG_PVSYNC);
10741 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10742 DRM_MODE_FLAG_NVSYNC);
10743 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010744
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010745 PIPE_CONF_CHECK_I(pipe_src_w);
10746 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010747
Daniel Vetter99535992014-04-13 12:00:33 +020010748 /*
10749 * FIXME: BIOS likes to set up a cloned config with lvds+external
10750 * screen. Since we don't yet re-compute the pipe config when moving
10751 * just the lvds port away to another pipe the sw tracking won't match.
10752 *
10753 * Proper atomic modesets with recomputed global state will fix this.
10754 * Until then just don't check gmch state for inherited modes.
10755 */
10756 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10757 PIPE_CONF_CHECK_I(gmch_pfit.control);
10758 /* pfit ratios are autocomputed by the hw on gen4+ */
10759 if (INTEL_INFO(dev)->gen < 4)
10760 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10761 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10762 }
10763
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010764 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10765 if (current_config->pch_pfit.enabled) {
10766 PIPE_CONF_CHECK_I(pch_pfit.pos);
10767 PIPE_CONF_CHECK_I(pch_pfit.size);
10768 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010769
Jesse Barnese59150d2014-01-07 13:30:45 -080010770 /* BDW+ don't expose a synchronous way to read the state */
10771 if (IS_HASWELL(dev))
10772 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010773
Ville Syrjälä282740f2013-09-04 18:30:03 +030010774 PIPE_CONF_CHECK_I(double_wide);
10775
Daniel Vetter26804af2014-06-25 22:01:55 +030010776 PIPE_CONF_CHECK_X(ddi_pll_sel);
10777
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010778 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010779 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020010780 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010781 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10782 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010783 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010784
Ville Syrjälä42571ae2013-09-06 23:29:00 +030010785 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10786 PIPE_CONF_CHECK_I(pipe_bpp);
10787
Jesse Barnesa9a7e982014-01-20 14:18:04 -080010788 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10789 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030010790
Daniel Vetter66e985c2013-06-05 13:34:20 +020010791#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020010792#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010793#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010794#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030010795#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020010796#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010797
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010798 return true;
10799}
10800
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010801static void
10802check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010803{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010804 struct intel_connector *connector;
10805
10806 list_for_each_entry(connector, &dev->mode_config.connector_list,
10807 base.head) {
10808 /* This also checks the encoder/connector hw state with the
10809 * ->get_hw_state callbacks. */
10810 intel_connector_check_state(connector);
10811
10812 WARN(&connector->new_encoder->base != connector->base.encoder,
10813 "connector's staged encoder doesn't match current encoder\n");
10814 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010815}
10816
10817static void
10818check_encoder_state(struct drm_device *dev)
10819{
10820 struct intel_encoder *encoder;
10821 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010822
Damien Lespiaub2784e12014-08-05 11:29:37 +010010823 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010824 bool enabled = false;
10825 bool active = false;
10826 enum pipe pipe, tracked_pipe;
10827
10828 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10829 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030010830 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010831
10832 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10833 "encoder's stage crtc doesn't match current crtc\n");
10834 WARN(encoder->connectors_active && !encoder->base.crtc,
10835 "encoder's active_connectors set, but no crtc\n");
10836
10837 list_for_each_entry(connector, &dev->mode_config.connector_list,
10838 base.head) {
10839 if (connector->base.encoder != &encoder->base)
10840 continue;
10841 enabled = true;
10842 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10843 active = true;
10844 }
Dave Airlie0e32b392014-05-02 14:02:48 +100010845 /*
10846 * for MST connectors if we unplug the connector is gone
10847 * away but the encoder is still connected to a crtc
10848 * until a modeset happens in response to the hotplug.
10849 */
10850 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10851 continue;
10852
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010853 WARN(!!encoder->base.crtc != enabled,
10854 "encoder's enabled state mismatch "
10855 "(expected %i, found %i)\n",
10856 !!encoder->base.crtc, enabled);
10857 WARN(active && !encoder->base.crtc,
10858 "active encoder with no crtc\n");
10859
10860 WARN(encoder->connectors_active != active,
10861 "encoder's computed active state doesn't match tracked active state "
10862 "(expected %i, found %i)\n", active, encoder->connectors_active);
10863
10864 active = encoder->get_hw_state(encoder, &pipe);
10865 WARN(active != encoder->connectors_active,
10866 "encoder's hw state doesn't match sw tracking "
10867 "(expected %i, found %i)\n",
10868 encoder->connectors_active, active);
10869
10870 if (!encoder->base.crtc)
10871 continue;
10872
10873 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10874 WARN(active && pipe != tracked_pipe,
10875 "active encoder's pipe doesn't match"
10876 "(expected %i, found %i)\n",
10877 tracked_pipe, pipe);
10878
10879 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010880}
10881
10882static void
10883check_crtc_state(struct drm_device *dev)
10884{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010885 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010886 struct intel_crtc *crtc;
10887 struct intel_encoder *encoder;
10888 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010889
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010890 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010891 bool enabled = false;
10892 bool active = false;
10893
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010894 memset(&pipe_config, 0, sizeof(pipe_config));
10895
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010896 DRM_DEBUG_KMS("[CRTC:%d]\n",
10897 crtc->base.base.id);
10898
10899 WARN(crtc->active && !crtc->base.enabled,
10900 "active crtc, but not enabled in sw tracking\n");
10901
Damien Lespiaub2784e12014-08-05 11:29:37 +010010902 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010903 if (encoder->base.crtc != &crtc->base)
10904 continue;
10905 enabled = true;
10906 if (encoder->connectors_active)
10907 active = true;
10908 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010909
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010910 WARN(active != crtc->active,
10911 "crtc's computed active state doesn't match tracked active state "
10912 "(expected %i, found %i)\n", active, crtc->active);
10913 WARN(enabled != crtc->base.enabled,
10914 "crtc's computed enabled state doesn't match tracked enabled state "
10915 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10916
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010917 active = dev_priv->display.get_pipe_config(crtc,
10918 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020010919
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030010920 /* hw state is inconsistent with the pipe quirk */
10921 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10922 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020010923 active = crtc->active;
10924
Damien Lespiaub2784e12014-08-05 11:29:37 +010010925 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030010926 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020010927 if (encoder->base.crtc != &crtc->base)
10928 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010010929 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020010930 encoder->get_config(encoder, &pipe_config);
10931 }
10932
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010933 WARN(crtc->active != active,
10934 "crtc active state doesn't match with hw state "
10935 "(expected %i, found %i)\n", crtc->active, active);
10936
Daniel Vetterc0b03412013-05-28 12:05:54 +020010937 if (active &&
10938 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10939 WARN(1, "pipe state doesn't match!\n");
10940 intel_dump_pipe_config(crtc, &pipe_config,
10941 "[hw state]");
10942 intel_dump_pipe_config(crtc, &crtc->config,
10943 "[sw state]");
10944 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010945 }
10946}
10947
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010948static void
10949check_shared_dpll_state(struct drm_device *dev)
10950{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010951 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010952 struct intel_crtc *crtc;
10953 struct intel_dpll_hw_state dpll_hw_state;
10954 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020010955
10956 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10957 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10958 int enabled_crtcs = 0, active_crtcs = 0;
10959 bool active;
10960
10961 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10962
10963 DRM_DEBUG_KMS("%s\n", pll->name);
10964
10965 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10966
10967 WARN(pll->active > pll->refcount,
10968 "more active pll users than references: %i vs %i\n",
10969 pll->active, pll->refcount);
10970 WARN(pll->active && !pll->on,
10971 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +020010972 WARN(pll->on && !pll->active,
10973 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010974 WARN(pll->on != active,
10975 "pll on state mismatch (expected %i, found %i)\n",
10976 pll->on, active);
10977
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010978 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020010979 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10980 enabled_crtcs++;
10981 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10982 active_crtcs++;
10983 }
10984 WARN(pll->active != active_crtcs,
10985 "pll active crtcs mismatch (expected %i, found %i)\n",
10986 pll->active, active_crtcs);
10987 WARN(pll->refcount != enabled_crtcs,
10988 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10989 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010990
10991 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10992 sizeof(dpll_hw_state)),
10993 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010994 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010995}
10996
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010997void
10998intel_modeset_check_state(struct drm_device *dev)
10999{
11000 check_connector_state(dev);
11001 check_encoder_state(dev);
11002 check_crtc_state(dev);
11003 check_shared_dpll_state(dev);
11004}
11005
Ville Syrjälä18442d02013-09-13 16:00:08 +030011006void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
11007 int dotclock)
11008{
11009 /*
11010 * FDI already provided one idea for the dotclock.
11011 * Yell if the encoder disagrees.
11012 */
Damien Lespiau241bfc32013-09-25 16:45:37 +010011013 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030011014 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +010011015 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030011016}
11017
Ville Syrjälä80715b22014-05-15 20:23:23 +030011018static void update_scanline_offset(struct intel_crtc *crtc)
11019{
11020 struct drm_device *dev = crtc->base.dev;
11021
11022 /*
11023 * The scanline counter increments at the leading edge of hsync.
11024 *
11025 * On most platforms it starts counting from vtotal-1 on the
11026 * first active line. That means the scanline counter value is
11027 * always one less than what we would expect. Ie. just after
11028 * start of vblank, which also occurs at start of hsync (on the
11029 * last active line), the scanline counter will read vblank_start-1.
11030 *
11031 * On gen2 the scanline counter starts counting from 1 instead
11032 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11033 * to keep the value positive), instead of adding one.
11034 *
11035 * On HSW+ the behaviour of the scanline counter depends on the output
11036 * type. For DP ports it behaves like most other platforms, but on HDMI
11037 * there's an extra 1 line difference. So we need to add two instead of
11038 * one to the value.
11039 */
11040 if (IS_GEN2(dev)) {
11041 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
11042 int vtotal;
11043
11044 vtotal = mode->crtc_vtotal;
11045 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11046 vtotal /= 2;
11047
11048 crtc->scanline_offset = vtotal - 1;
11049 } else if (HAS_DDI(dev) &&
11050 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
11051 crtc->scanline_offset = 2;
11052 } else
11053 crtc->scanline_offset = 1;
11054}
11055
Daniel Vetterf30da182013-04-11 20:22:50 +020011056static int __intel_set_mode(struct drm_crtc *crtc,
11057 struct drm_display_mode *mode,
11058 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +020011059{
11060 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030011061 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011062 struct drm_display_mode *saved_mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011063 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020011064 struct intel_crtc *intel_crtc;
11065 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011066 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020011067
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011068 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011069 if (!saved_mode)
11070 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020011071
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011072 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +020011073 &prepare_pipes, &disable_pipes);
11074
Tim Gardner3ac18232012-12-07 07:54:26 -070011075 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011076
Daniel Vetter25c5b262012-07-08 22:08:04 +020011077 /* Hack: Because we don't (yet) support global modeset on multiple
11078 * crtcs, we don't keep track of the new mode for more than one crtc.
11079 * Hence simply check whether any bit is set in modeset_pipes in all the
11080 * pieces of code that are not yet converted to deal with mutliple crtcs
11081 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +020011082 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011083 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011084 if (IS_ERR(pipe_config)) {
11085 ret = PTR_ERR(pipe_config);
11086 pipe_config = NULL;
11087
Tim Gardner3ac18232012-12-07 07:54:26 -070011088 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +020011089 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011090 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11091 "[modeset]");
Ville Syrjälä50741ab2014-01-10 11:28:07 +020011092 to_intel_crtc(crtc)->new_config = pipe_config;
Daniel Vettera6778b32012-07-02 09:56:42 +020011093 }
11094
Jesse Barnes30a970c2013-11-04 13:48:12 -080011095 /*
11096 * See if the config requires any additional preparation, e.g.
11097 * to adjust global state with pipes off. We need to do this
11098 * here so we can get the modeset_pipe updated config for the new
11099 * mode set on this crtc. For other crtcs we need to use the
11100 * adjusted_mode bits in the crtc directly.
11101 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020011102 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020011103 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080011104
Ville Syrjäläc164f832013-11-05 22:34:12 +020011105 /* may have added more to prepare_pipes than we should */
11106 prepare_pipes &= ~disable_pipes;
11107 }
11108
Daniel Vetter460da9162013-03-27 00:44:51 +010011109 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11110 intel_crtc_disable(&intel_crtc->base);
11111
Daniel Vetterea9d7582012-07-10 10:42:52 +020011112 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11113 if (intel_crtc->base.enabled)
11114 dev_priv->display.crtc_disable(&intel_crtc->base);
11115 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011116
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020011117 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11118 * to set it here already despite that we pass it down the callchain.
11119 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011120 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020011121 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011122 /* mode_set/enable/disable functions rely on a correct pipe
11123 * config. */
11124 to_intel_crtc(crtc)->config = *pipe_config;
Ville Syrjälä50741ab2014-01-10 11:28:07 +020011125 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020011126
11127 /*
11128 * Calculate and store various constants which
11129 * are later needed by vblank and swap-completion
11130 * timestamping. They are derived from true hwmode.
11131 */
11132 drm_calc_timestamping_constants(crtc,
11133 &pipe_config->adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011134 }
Daniel Vetter7758a112012-07-08 19:40:39 +020011135
Daniel Vetterea9d7582012-07-10 10:42:52 +020011136 /* Only after disabling all output pipelines that will be changed can we
11137 * update the the output configuration. */
11138 intel_modeset_update_state(dev, prepare_pipes);
11139
Daniel Vetter47fab732012-10-26 10:58:18 +020011140 if (dev_priv->display.modeset_global_resources)
11141 dev_priv->display.modeset_global_resources(dev);
11142
Daniel Vettera6778b32012-07-02 09:56:42 +020011143 /* Set up the DPLL and any encoders state that needs to adjust or depend
11144 * on the DPLL.
11145 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020011146 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070011147 struct drm_framebuffer *old_fb = crtc->primary->fb;
11148 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
11149 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Daniel Vetter4c107942014-04-24 23:55:05 +020011150
11151 mutex_lock(&dev->struct_mutex);
11152 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vettera071fa02014-06-18 23:28:09 +020011153 obj,
Daniel Vetter4c107942014-04-24 23:55:05 +020011154 NULL);
11155 if (ret != 0) {
11156 DRM_ERROR("pin & fence failed\n");
11157 mutex_unlock(&dev->struct_mutex);
11158 goto done;
11159 }
Matt Roper2ff8fde2014-07-08 07:50:07 -070011160 if (old_fb)
Daniel Vettera071fa02014-06-18 23:28:09 +020011161 intel_unpin_fb_obj(old_obj);
Daniel Vettera071fa02014-06-18 23:28:09 +020011162 i915_gem_track_fb(old_obj, obj,
11163 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Daniel Vetter4c107942014-04-24 23:55:05 +020011164 mutex_unlock(&dev->struct_mutex);
11165
11166 crtc->primary->fb = fb;
11167 crtc->x = x;
11168 crtc->y = y;
11169
Daniel Vetter4271b752014-04-24 23:55:00 +020011170 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
11171 x, y, fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011172 if (ret)
11173 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +020011174 }
11175
11176 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030011177 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11178 update_scanline_offset(intel_crtc);
11179
Daniel Vetter25c5b262012-07-08 22:08:04 +020011180 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030011181 }
Daniel Vettera6778b32012-07-02 09:56:42 +020011182
Daniel Vettera6778b32012-07-02 09:56:42 +020011183 /* FIXME: add subpixel order */
11184done:
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030011185 if (ret && crtc->enabled)
Tim Gardner3ac18232012-12-07 07:54:26 -070011186 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020011187
Tim Gardner3ac18232012-12-07 07:54:26 -070011188out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011189 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -070011190 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020011191 return ret;
11192}
11193
Damien Lespiaue7457a92013-08-08 22:28:59 +010011194static int intel_set_mode(struct drm_crtc *crtc,
11195 struct drm_display_mode *mode,
11196 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020011197{
11198 int ret;
11199
11200 ret = __intel_set_mode(crtc, mode, x, y, fb);
11201
11202 if (ret == 0)
11203 intel_modeset_check_state(crtc->dev);
11204
11205 return ret;
11206}
11207
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011208void intel_crtc_restore_mode(struct drm_crtc *crtc)
11209{
Matt Roperf4510a22014-04-01 15:22:40 -070011210 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011211}
11212
Daniel Vetter25c5b262012-07-08 22:08:04 +020011213#undef for_each_intel_crtc_masked
11214
Daniel Vetterd9e55602012-07-04 22:16:09 +020011215static void intel_set_config_free(struct intel_set_config *config)
11216{
11217 if (!config)
11218 return;
11219
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011220 kfree(config->save_connector_encoders);
11221 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020011222 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020011223 kfree(config);
11224}
11225
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011226static int intel_set_config_save_state(struct drm_device *dev,
11227 struct intel_set_config *config)
11228{
Ville Syrjälä76688512014-01-10 11:28:06 +020011229 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011230 struct drm_encoder *encoder;
11231 struct drm_connector *connector;
11232 int count;
11233
Ville Syrjälä76688512014-01-10 11:28:06 +020011234 config->save_crtc_enabled =
11235 kcalloc(dev->mode_config.num_crtc,
11236 sizeof(bool), GFP_KERNEL);
11237 if (!config->save_crtc_enabled)
11238 return -ENOMEM;
11239
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011240 config->save_encoder_crtcs =
11241 kcalloc(dev->mode_config.num_encoder,
11242 sizeof(struct drm_crtc *), GFP_KERNEL);
11243 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011244 return -ENOMEM;
11245
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011246 config->save_connector_encoders =
11247 kcalloc(dev->mode_config.num_connector,
11248 sizeof(struct drm_encoder *), GFP_KERNEL);
11249 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011250 return -ENOMEM;
11251
11252 /* Copy data. Note that driver private data is not affected.
11253 * Should anything bad happen only the expected state is
11254 * restored, not the drivers personal bookkeeping.
11255 */
11256 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010011257 for_each_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011258 config->save_crtc_enabled[count++] = crtc->enabled;
11259 }
11260
11261 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011262 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011263 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011264 }
11265
11266 count = 0;
11267 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020011268 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011269 }
11270
11271 return 0;
11272}
11273
11274static void intel_set_config_restore_state(struct drm_device *dev,
11275 struct intel_set_config *config)
11276{
Ville Syrjälä76688512014-01-10 11:28:06 +020011277 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011278 struct intel_encoder *encoder;
11279 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011280 int count;
11281
11282 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011283 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011284 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011285
11286 if (crtc->new_enabled)
11287 crtc->new_config = &crtc->config;
11288 else
11289 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011290 }
11291
11292 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010011293 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011294 encoder->new_crtc =
11295 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011296 }
11297
11298 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020011299 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11300 connector->new_encoder =
11301 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020011302 }
11303}
11304
Imre Deake3de42b2013-05-03 19:44:07 +020011305static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010011306is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020011307{
11308 int i;
11309
Chris Wilson2e57f472013-07-17 12:14:40 +010011310 if (set->num_connectors == 0)
11311 return false;
11312
11313 if (WARN_ON(set->connectors == NULL))
11314 return false;
11315
11316 for (i = 0; i < set->num_connectors; i++)
11317 if (set->connectors[i]->encoder &&
11318 set->connectors[i]->encoder->crtc == set->crtc &&
11319 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020011320 return true;
11321
11322 return false;
11323}
11324
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011325static void
11326intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11327 struct intel_set_config *config)
11328{
11329
11330 /* We should be able to check here if the fb has the same properties
11331 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010011332 if (is_crtc_connector_off(set)) {
11333 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070011334 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070011335 /*
11336 * If we have no fb, we can only flip as long as the crtc is
11337 * active, otherwise we need a full mode set. The crtc may
11338 * be active if we've only disabled the primary plane, or
11339 * in fastboot situations.
11340 */
Matt Roperf4510a22014-04-01 15:22:40 -070011341 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011342 struct intel_crtc *intel_crtc =
11343 to_intel_crtc(set->crtc);
11344
Matt Roper3b150f02014-05-29 08:06:53 -070011345 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030011346 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11347 config->fb_changed = true;
11348 } else {
11349 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
11350 config->mode_changed = true;
11351 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011352 } else if (set->fb == NULL) {
11353 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010011354 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070011355 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011356 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011357 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011358 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020011359 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011360 }
11361
Daniel Vetter835c5872012-07-10 18:11:08 +020011362 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011363 config->fb_changed = true;
11364
11365 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
11366 DRM_DEBUG_KMS("modes are different, full mode set\n");
11367 drm_mode_debug_printmodeline(&set->crtc->mode);
11368 drm_mode_debug_printmodeline(set->mode);
11369 config->mode_changed = true;
11370 }
Chris Wilsona1d95702013-08-13 18:48:47 +010011371
11372 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
11373 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011374}
11375
Daniel Vetter2e431052012-07-04 22:42:15 +020011376static int
Daniel Vetter9a935852012-07-05 22:34:27 +020011377intel_modeset_stage_output_state(struct drm_device *dev,
11378 struct drm_mode_set *set,
11379 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020011380{
Daniel Vetter9a935852012-07-05 22:34:27 +020011381 struct intel_connector *connector;
11382 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020011383 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030011384 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020011385
Damien Lespiau9abdda72013-02-13 13:29:23 +000011386 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020011387 * of connectors. For paranoia, double-check this. */
11388 WARN_ON(!set->fb && (set->num_connectors != 0));
11389 WARN_ON(set->fb && (set->num_connectors == 0));
11390
Daniel Vetter9a935852012-07-05 22:34:27 +020011391 list_for_each_entry(connector, &dev->mode_config.connector_list,
11392 base.head) {
11393 /* Otherwise traverse passed in connector list and get encoders
11394 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020011395 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011396 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100011397 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020011398 break;
11399 }
11400 }
11401
Daniel Vetter9a935852012-07-05 22:34:27 +020011402 /* If we disable the crtc, disable all its connectors. Also, if
11403 * the connector is on the changing crtc but not on the new
11404 * connector list, disable it. */
11405 if ((!set->fb || ro == set->num_connectors) &&
11406 connector->base.encoder &&
11407 connector->base.encoder->crtc == set->crtc) {
11408 connector->new_encoder = NULL;
11409
11410 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11411 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011412 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020011413 }
11414
11415
11416 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011417 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011418 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011419 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011420 }
11421 /* connector->new_encoder is now updated for all connectors. */
11422
11423 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +020011424 list_for_each_entry(connector, &dev->mode_config.connector_list,
11425 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011426 struct drm_crtc *new_crtc;
11427
Daniel Vetter9a935852012-07-05 22:34:27 +020011428 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020011429 continue;
11430
Daniel Vetter9a935852012-07-05 22:34:27 +020011431 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020011432
11433 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011434 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020011435 new_crtc = set->crtc;
11436 }
11437
11438 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010011439 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11440 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011441 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020011442 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011443 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020011444
11445 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11446 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011447 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020011448 new_crtc->base.id);
11449 }
11450
11451 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010011452 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011453 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020011454 list_for_each_entry(connector,
11455 &dev->mode_config.connector_list,
11456 base.head) {
11457 if (connector->new_encoder == encoder) {
11458 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011459 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020011460 }
11461 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011462
11463 if (num_connectors == 0)
11464 encoder->new_crtc = NULL;
11465 else if (num_connectors > 1)
11466 return -EINVAL;
11467
Daniel Vetter9a935852012-07-05 22:34:27 +020011468 /* Only now check for crtc changes so we don't miss encoders
11469 * that will be disabled. */
11470 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011471 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011472 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011473 }
11474 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011475 /* Now we've also updated encoder->new_crtc for all encoders. */
Dave Airlie0e32b392014-05-02 14:02:48 +100011476 list_for_each_entry(connector, &dev->mode_config.connector_list,
11477 base.head) {
11478 if (connector->new_encoder)
11479 if (connector->new_encoder != connector->encoder)
11480 connector->encoder = connector->new_encoder;
11481 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011482 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011483 crtc->new_enabled = false;
11484
Damien Lespiaub2784e12014-08-05 11:29:37 +010011485 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011486 if (encoder->new_crtc == crtc) {
11487 crtc->new_enabled = true;
11488 break;
11489 }
11490 }
11491
11492 if (crtc->new_enabled != crtc->base.enabled) {
11493 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11494 crtc->new_enabled ? "en" : "dis");
11495 config->mode_changed = true;
11496 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011497
11498 if (crtc->new_enabled)
11499 crtc->new_config = &crtc->config;
11500 else
11501 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011502 }
11503
Daniel Vetter2e431052012-07-04 22:42:15 +020011504 return 0;
11505}
11506
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011507static void disable_crtc_nofb(struct intel_crtc *crtc)
11508{
11509 struct drm_device *dev = crtc->base.dev;
11510 struct intel_encoder *encoder;
11511 struct intel_connector *connector;
11512
11513 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11514 pipe_name(crtc->pipe));
11515
11516 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11517 if (connector->new_encoder &&
11518 connector->new_encoder->new_crtc == crtc)
11519 connector->new_encoder = NULL;
11520 }
11521
Damien Lespiaub2784e12014-08-05 11:29:37 +010011522 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011523 if (encoder->new_crtc == crtc)
11524 encoder->new_crtc = NULL;
11525 }
11526
11527 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011528 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011529}
11530
Daniel Vetter2e431052012-07-04 22:42:15 +020011531static int intel_crtc_set_config(struct drm_mode_set *set)
11532{
11533 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020011534 struct drm_mode_set save_set;
11535 struct intel_set_config *config;
11536 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020011537
Daniel Vetter8d3e3752012-07-05 16:09:09 +020011538 BUG_ON(!set);
11539 BUG_ON(!set->crtc);
11540 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020011541
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010011542 /* Enforce sane interface api - has been abused by the fb helper. */
11543 BUG_ON(!set->mode && set->fb);
11544 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020011545
Daniel Vetter2e431052012-07-04 22:42:15 +020011546 if (set->fb) {
11547 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11548 set->crtc->base.id, set->fb->base.id,
11549 (int)set->num_connectors, set->x, set->y);
11550 } else {
11551 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020011552 }
11553
11554 dev = set->crtc->dev;
11555
11556 ret = -ENOMEM;
11557 config = kzalloc(sizeof(*config), GFP_KERNEL);
11558 if (!config)
11559 goto out_config;
11560
11561 ret = intel_set_config_save_state(dev, config);
11562 if (ret)
11563 goto out_config;
11564
11565 save_set.crtc = set->crtc;
11566 save_set.mode = &set->crtc->mode;
11567 save_set.x = set->crtc->x;
11568 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070011569 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020011570
11571 /* Compute whether we need a full modeset, only an fb base update or no
11572 * change at all. In the future we might also check whether only the
11573 * mode changed, e.g. for LVDS where we only change the panel fitter in
11574 * such cases. */
11575 intel_set_config_compute_mode_changes(set, config);
11576
Daniel Vetter9a935852012-07-05 22:34:27 +020011577 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020011578 if (ret)
11579 goto fail;
11580
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011581 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011582 ret = intel_set_mode(set->crtc, set->mode,
11583 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011584 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070011585 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11586
Ville Syrjälä4878cae2013-02-18 19:08:48 +020011587 intel_crtc_wait_for_pending_flips(set->crtc);
11588
Daniel Vetter4f660f42012-07-02 09:47:37 +020011589 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020011590 set->x, set->y, set->fb);
Matt Roper3b150f02014-05-29 08:06:53 -070011591
11592 /*
11593 * We need to make sure the primary plane is re-enabled if it
11594 * has previously been turned off.
11595 */
11596 if (!intel_crtc->primary_enabled && ret == 0) {
11597 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a62014-08-08 21:51:11 +030011598 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070011599 }
11600
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011601 /*
11602 * In the fastboot case this may be our only check of the
11603 * state after boot. It would be better to only do it on
11604 * the first update, but we don't have a nice way of doing that
11605 * (and really, set_config isn't used much for high freq page
11606 * flipping, so increasing its cost here shouldn't be a big
11607 * deal).
11608 */
Jani Nikulad330a952014-01-21 11:24:25 +020011609 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011610 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020011611 }
11612
Chris Wilson2d05eae2013-05-03 17:36:25 +010011613 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020011614 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11615 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020011616fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010011617 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011618
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011619 /*
11620 * HACK: if the pipe was on, but we didn't have a framebuffer,
11621 * force the pipe off to avoid oopsing in the modeset code
11622 * due to fb==NULL. This should only happen during boot since
11623 * we don't yet reconstruct the FB from the hardware state.
11624 */
11625 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11626 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11627
Chris Wilson2d05eae2013-05-03 17:36:25 +010011628 /* Try to restore the config */
11629 if (config->mode_changed &&
11630 intel_set_mode(save_set.crtc, save_set.mode,
11631 save_set.x, save_set.y, save_set.fb))
11632 DRM_ERROR("failed to restore config after modeset failure\n");
11633 }
Daniel Vetter50f56112012-07-02 09:35:43 +020011634
Daniel Vetterd9e55602012-07-04 22:16:09 +020011635out_config:
11636 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011637 return ret;
11638}
11639
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011640static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011641 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020011642 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011643 .destroy = intel_crtc_destroy,
11644 .page_flip = intel_crtc_page_flip,
11645};
11646
Daniel Vetter53589012013-06-05 13:34:16 +020011647static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11648 struct intel_shared_dpll *pll,
11649 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011650{
Daniel Vetter53589012013-06-05 13:34:16 +020011651 uint32_t val;
11652
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030011653 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
11654 return false;
11655
Daniel Vetter53589012013-06-05 13:34:16 +020011656 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020011657 hw_state->dpll = val;
11658 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11659 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020011660
11661 return val & DPLL_VCO_ENABLE;
11662}
11663
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011664static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11665 struct intel_shared_dpll *pll)
11666{
11667 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11668 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11669}
11670
Daniel Vettere7b903d2013-06-05 13:34:14 +020011671static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11672 struct intel_shared_dpll *pll)
11673{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011674 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020011675 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020011676
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011677 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11678
11679 /* Wait for the clocks to stabilize. */
11680 POSTING_READ(PCH_DPLL(pll->id));
11681 udelay(150);
11682
11683 /* The pixel multiplier can only be updated once the
11684 * DPLL is enabled and the clocks are stable.
11685 *
11686 * So write it again.
11687 */
11688 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11689 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011690 udelay(200);
11691}
11692
11693static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11694 struct intel_shared_dpll *pll)
11695{
11696 struct drm_device *dev = dev_priv->dev;
11697 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011698
11699 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011700 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020011701 if (intel_crtc_to_shared_dpll(crtc) == pll)
11702 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
11703 }
11704
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011705 I915_WRITE(PCH_DPLL(pll->id), 0);
11706 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011707 udelay(200);
11708}
11709
Daniel Vetter46edb022013-06-05 13:34:12 +020011710static char *ibx_pch_dpll_names[] = {
11711 "PCH DPLL A",
11712 "PCH DPLL B",
11713};
11714
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011715static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011716{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011717 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011718 int i;
11719
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011720 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011721
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011722 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020011723 dev_priv->shared_dplls[i].id = i;
11724 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011725 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011726 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11727 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020011728 dev_priv->shared_dplls[i].get_hw_state =
11729 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011730 }
11731}
11732
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011733static void intel_shared_dpll_init(struct drm_device *dev)
11734{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011735 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011736
Daniel Vetter9cd86932014-06-25 22:01:57 +030011737 if (HAS_DDI(dev))
11738 intel_ddi_pll_init(dev);
11739 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011740 ibx_pch_dpll_init(dev);
11741 else
11742 dev_priv->num_shared_dpll = 0;
11743
11744 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011745}
11746
Matt Roper465c1202014-05-29 08:06:54 -070011747static int
11748intel_primary_plane_disable(struct drm_plane *plane)
11749{
11750 struct drm_device *dev = plane->dev;
Matt Roper465c1202014-05-29 08:06:54 -070011751 struct intel_crtc *intel_crtc;
11752
11753 if (!plane->fb)
11754 return 0;
11755
11756 BUG_ON(!plane->crtc);
11757
11758 intel_crtc = to_intel_crtc(plane->crtc);
11759
11760 /*
11761 * Even though we checked plane->fb above, it's still possible that
11762 * the primary plane has been implicitly disabled because the crtc
11763 * coordinates given weren't visible, or because we detected
11764 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11765 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11766 * In either case, we need to unpin the FB and let the fb pointer get
11767 * updated, but otherwise we don't need to touch the hardware.
11768 */
11769 if (!intel_crtc->primary_enabled)
11770 goto disable_unpin;
11771
11772 intel_crtc_wait_for_pending_flips(plane->crtc);
Ville Syrjäläfdd508a62014-08-08 21:51:11 +030011773 intel_disable_primary_hw_plane(plane, plane->crtc);
11774
Matt Roper465c1202014-05-29 08:06:54 -070011775disable_unpin:
Matt Roper4c345742014-07-09 16:22:10 -070011776 mutex_lock(&dev->struct_mutex);
Matt Roper2ff8fde2014-07-08 07:50:07 -070011777 i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
Daniel Vettera071fa02014-06-18 23:28:09 +020011778 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Matt Roper2ff8fde2014-07-08 07:50:07 -070011779 intel_unpin_fb_obj(intel_fb_obj(plane->fb));
Matt Roper4c345742014-07-09 16:22:10 -070011780 mutex_unlock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070011781 plane->fb = NULL;
11782
11783 return 0;
11784}
11785
11786static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011787intel_check_primary_plane(struct drm_plane *plane,
11788 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070011789{
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011790 struct drm_crtc *crtc = state->crtc;
11791 struct drm_framebuffer *fb = state->fb;
11792 struct drm_rect *dest = &state->dst;
11793 struct drm_rect *src = &state->src;
11794 const struct drm_rect *clip = &state->clip;
11795
11796 return drm_plane_helper_check_update(plane, crtc, fb,
11797 src, dest, clip,
11798 DRM_PLANE_HELPER_NO_SCALING,
11799 DRM_PLANE_HELPER_NO_SCALING,
11800 false, true, &state->visible);
11801}
11802
11803static int
11804intel_commit_primary_plane(struct drm_plane *plane,
11805 struct intel_plane_state *state)
11806{
11807 struct drm_crtc *crtc = state->crtc;
11808 struct drm_framebuffer *fb = state->fb;
Matt Roper465c1202014-05-29 08:06:54 -070011809 struct drm_device *dev = crtc->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053011810 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper465c1202014-05-29 08:06:54 -070011811 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper2ff8fde2014-07-08 07:50:07 -070011812 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11813 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Sonika Jindalce54d852014-08-21 11:44:39 +053011814 struct intel_plane *intel_plane = to_intel_plane(plane);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011815 struct drm_rect *src = &state->src;
Matt Roper465c1202014-05-29 08:06:54 -070011816 int ret;
11817
Matt Roper465c1202014-05-29 08:06:54 -070011818 intel_crtc_wait_for_pending_flips(crtc);
11819
11820 /*
11821 * If clipping results in a non-visible primary plane, we'll disable
11822 * the primary plane. Note that this is a bit different than what
11823 * happens if userspace explicitly disables the plane by passing fb=0
11824 * because plane->fb still gets set and pinned.
11825 */
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011826 if (!state->visible) {
Matt Roper4c345742014-07-09 16:22:10 -070011827 mutex_lock(&dev->struct_mutex);
11828
Matt Roper465c1202014-05-29 08:06:54 -070011829 /*
11830 * Try to pin the new fb first so that we can bail out if we
11831 * fail.
11832 */
11833 if (plane->fb != fb) {
Daniel Vettera071fa02014-06-18 23:28:09 +020011834 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
Matt Roper4c345742014-07-09 16:22:10 -070011835 if (ret) {
11836 mutex_unlock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070011837 return ret;
Matt Roper4c345742014-07-09 16:22:10 -070011838 }
Matt Roper465c1202014-05-29 08:06:54 -070011839 }
11840
Daniel Vettera071fa02014-06-18 23:28:09 +020011841 i915_gem_track_fb(old_obj, obj,
11842 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11843
Matt Roper465c1202014-05-29 08:06:54 -070011844 if (intel_crtc->primary_enabled)
Ville Syrjäläfdd508a62014-08-08 21:51:11 +030011845 intel_disable_primary_hw_plane(plane, crtc);
Matt Roper465c1202014-05-29 08:06:54 -070011846
11847
11848 if (plane->fb != fb)
11849 if (plane->fb)
Daniel Vettera071fa02014-06-18 23:28:09 +020011850 intel_unpin_fb_obj(old_obj);
Matt Roper465c1202014-05-29 08:06:54 -070011851
Matt Roper4c345742014-07-09 16:22:10 -070011852 mutex_unlock(&dev->struct_mutex);
11853
Sonika Jindalce54d852014-08-21 11:44:39 +053011854 } else {
Sonika Jindal48404c12014-08-22 14:06:04 +053011855 if (intel_crtc && intel_crtc->active &&
11856 intel_crtc->primary_enabled) {
11857 /*
11858 * FBC does not work on some platforms for rotated
11859 * planes, so disable it when rotation is not 0 and
11860 * update it when rotation is set back to 0.
11861 *
11862 * FIXME: This is redundant with the fbc update done in
11863 * the primary plane enable function except that that
11864 * one is done too late. We eventually need to unify
11865 * this.
11866 */
11867 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11868 dev_priv->fbc.plane == intel_crtc->plane &&
11869 intel_plane->rotation != BIT(DRM_ROTATE_0)) {
11870 intel_disable_fbc(dev);
11871 }
11872 }
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011873 ret = intel_pipe_set_base(crtc, src->x1, src->y1, fb);
Sonika Jindalce54d852014-08-21 11:44:39 +053011874 if (ret)
11875 return ret;
11876
11877 if (!intel_crtc->primary_enabled)
11878 intel_enable_primary_hw_plane(plane, crtc);
Matt Roper465c1202014-05-29 08:06:54 -070011879 }
11880
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011881 intel_plane->crtc_x = state->orig_dst.x1;
11882 intel_plane->crtc_y = state->orig_dst.y1;
11883 intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
11884 intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
11885 intel_plane->src_x = state->orig_src.x1;
11886 intel_plane->src_y = state->orig_src.y1;
11887 intel_plane->src_w = drm_rect_width(&state->orig_src);
11888 intel_plane->src_h = drm_rect_height(&state->orig_src);
Sonika Jindalce54d852014-08-21 11:44:39 +053011889 intel_plane->obj = obj;
Matt Roper465c1202014-05-29 08:06:54 -070011890
11891 return 0;
11892}
11893
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011894static int
11895intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11896 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11897 unsigned int crtc_w, unsigned int crtc_h,
11898 uint32_t src_x, uint32_t src_y,
11899 uint32_t src_w, uint32_t src_h)
11900{
11901 struct intel_plane_state state;
11902 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11903 int ret;
11904
11905 state.crtc = crtc;
11906 state.fb = fb;
11907
11908 /* sample coordinates in 16.16 fixed point */
11909 state.src.x1 = src_x;
11910 state.src.x2 = src_x + src_w;
11911 state.src.y1 = src_y;
11912 state.src.y2 = src_y + src_h;
11913
11914 /* integer pixels */
11915 state.dst.x1 = crtc_x;
11916 state.dst.x2 = crtc_x + crtc_w;
11917 state.dst.y1 = crtc_y;
11918 state.dst.y2 = crtc_y + crtc_h;
11919
11920 state.clip.x1 = 0;
11921 state.clip.y1 = 0;
11922 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
11923 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
11924
11925 state.orig_src = state.src;
11926 state.orig_dst = state.dst;
11927
11928 ret = intel_check_primary_plane(plane, &state);
11929 if (ret)
11930 return ret;
11931
11932 intel_commit_primary_plane(plane, &state);
11933
11934 return 0;
11935}
11936
Matt Roper3d7d6512014-06-10 08:28:13 -070011937/* Common destruction function for both primary and cursor planes */
11938static void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070011939{
11940 struct intel_plane *intel_plane = to_intel_plane(plane);
11941 drm_plane_cleanup(plane);
11942 kfree(intel_plane);
11943}
11944
11945static const struct drm_plane_funcs intel_primary_plane_funcs = {
11946 .update_plane = intel_primary_plane_setplane,
11947 .disable_plane = intel_primary_plane_disable,
Matt Roper3d7d6512014-06-10 08:28:13 -070011948 .destroy = intel_plane_destroy,
Sonika Jindal48404c12014-08-22 14:06:04 +053011949 .set_property = intel_plane_set_property
Matt Roper465c1202014-05-29 08:06:54 -070011950};
11951
11952static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11953 int pipe)
11954{
11955 struct intel_plane *primary;
11956 const uint32_t *intel_primary_formats;
11957 int num_formats;
11958
11959 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11960 if (primary == NULL)
11961 return NULL;
11962
11963 primary->can_scale = false;
11964 primary->max_downscale = 1;
11965 primary->pipe = pipe;
11966 primary->plane = pipe;
Sonika Jindal48404c12014-08-22 14:06:04 +053011967 primary->rotation = BIT(DRM_ROTATE_0);
Matt Roper465c1202014-05-29 08:06:54 -070011968 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11969 primary->plane = !pipe;
11970
11971 if (INTEL_INFO(dev)->gen <= 3) {
11972 intel_primary_formats = intel_primary_formats_gen2;
11973 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11974 } else {
11975 intel_primary_formats = intel_primary_formats_gen4;
11976 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11977 }
11978
11979 drm_universal_plane_init(dev, &primary->base, 0,
11980 &intel_primary_plane_funcs,
11981 intel_primary_formats, num_formats,
11982 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053011983
11984 if (INTEL_INFO(dev)->gen >= 4) {
11985 if (!dev->mode_config.rotation_property)
11986 dev->mode_config.rotation_property =
11987 drm_mode_create_rotation_property(dev,
11988 BIT(DRM_ROTATE_0) |
11989 BIT(DRM_ROTATE_180));
11990 if (dev->mode_config.rotation_property)
11991 drm_object_attach_property(&primary->base.base,
11992 dev->mode_config.rotation_property,
11993 primary->rotation);
11994 }
11995
Matt Roper465c1202014-05-29 08:06:54 -070011996 return &primary->base;
11997}
11998
Matt Roper3d7d6512014-06-10 08:28:13 -070011999static int
12000intel_cursor_plane_disable(struct drm_plane *plane)
12001{
12002 if (!plane->fb)
12003 return 0;
12004
12005 BUG_ON(!plane->crtc);
12006
12007 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
12008}
12009
12010static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030012011intel_check_cursor_plane(struct drm_plane *plane,
12012 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070012013{
Gustavo Padovan852e7872014-09-05 17:22:31 -030012014 struct drm_crtc *crtc = state->crtc;
12015 struct drm_framebuffer *fb = state->fb;
12016 struct drm_rect *dest = &state->dst;
12017 struct drm_rect *src = &state->src;
12018 const struct drm_rect *clip = &state->clip;
12019
12020 return drm_plane_helper_check_update(plane, crtc, fb,
12021 src, dest, clip,
12022 DRM_PLANE_HELPER_NO_SCALING,
12023 DRM_PLANE_HELPER_NO_SCALING,
12024 true, true, &state->visible);
12025}
12026
12027static int
12028intel_commit_cursor_plane(struct drm_plane *plane,
12029 struct intel_plane_state *state)
12030{
12031 struct drm_crtc *crtc = state->crtc;
12032 struct drm_framebuffer *fb = state->fb;
Matt Roper3d7d6512014-06-10 08:28:13 -070012033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12034 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
12035 struct drm_i915_gem_object *obj = intel_fb->obj;
Gustavo Padovan852e7872014-09-05 17:22:31 -030012036 int crtc_w, crtc_h;
Matt Roper3d7d6512014-06-10 08:28:13 -070012037
Gustavo Padovan852e7872014-09-05 17:22:31 -030012038 crtc->cursor_x = state->orig_dst.x1;
12039 crtc->cursor_y = state->orig_dst.y1;
Matt Roper3d7d6512014-06-10 08:28:13 -070012040 if (fb != crtc->cursor->fb) {
Gustavo Padovan852e7872014-09-05 17:22:31 -030012041 crtc_w = drm_rect_width(&state->orig_dst);
12042 crtc_h = drm_rect_height(&state->orig_dst);
Matt Roper3d7d6512014-06-10 08:28:13 -070012043 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
12044 } else {
Gustavo Padovan852e7872014-09-05 17:22:31 -030012045 intel_crtc_update_cursor(crtc, state->visible);
Daniel Vetter4ed91092014-08-08 20:27:01 +020012046
12047 intel_frontbuffer_flip(crtc->dev,
12048 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
12049
Matt Roper3d7d6512014-06-10 08:28:13 -070012050 return 0;
12051 }
12052}
Gustavo Padovan852e7872014-09-05 17:22:31 -030012053
12054static int
12055intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
12056 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
12057 unsigned int crtc_w, unsigned int crtc_h,
12058 uint32_t src_x, uint32_t src_y,
12059 uint32_t src_w, uint32_t src_h)
12060{
12061 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12062 struct intel_plane_state state;
12063 int ret;
12064
12065 state.crtc = crtc;
12066 state.fb = fb;
12067
12068 /* sample coordinates in 16.16 fixed point */
12069 state.src.x1 = src_x;
12070 state.src.x2 = src_x + src_w;
12071 state.src.y1 = src_y;
12072 state.src.y2 = src_y + src_h;
12073
12074 /* integer pixels */
12075 state.dst.x1 = crtc_x;
12076 state.dst.x2 = crtc_x + crtc_w;
12077 state.dst.y1 = crtc_y;
12078 state.dst.y2 = crtc_y + crtc_h;
12079
12080 state.clip.x1 = 0;
12081 state.clip.y1 = 0;
12082 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
12083 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
12084
12085 state.orig_src = state.src;
12086 state.orig_dst = state.dst;
12087
12088 ret = intel_check_cursor_plane(plane, &state);
12089 if (ret)
12090 return ret;
12091
12092 return intel_commit_cursor_plane(plane, &state);
12093}
12094
Matt Roper3d7d6512014-06-10 08:28:13 -070012095static const struct drm_plane_funcs intel_cursor_plane_funcs = {
12096 .update_plane = intel_cursor_plane_update,
12097 .disable_plane = intel_cursor_plane_disable,
12098 .destroy = intel_plane_destroy,
12099};
12100
12101static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12102 int pipe)
12103{
12104 struct intel_plane *cursor;
12105
12106 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12107 if (cursor == NULL)
12108 return NULL;
12109
12110 cursor->can_scale = false;
12111 cursor->max_downscale = 1;
12112 cursor->pipe = pipe;
12113 cursor->plane = pipe;
12114
12115 drm_universal_plane_init(dev, &cursor->base, 0,
12116 &intel_cursor_plane_funcs,
12117 intel_cursor_formats,
12118 ARRAY_SIZE(intel_cursor_formats),
12119 DRM_PLANE_TYPE_CURSOR);
12120 return &cursor->base;
12121}
12122
Hannes Ederb358d0a2008-12-18 21:18:47 +010012123static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080012124{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012125 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080012126 struct intel_crtc *intel_crtc;
Matt Roper3d7d6512014-06-10 08:28:13 -070012127 struct drm_plane *primary = NULL;
12128 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070012129 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080012130
Daniel Vetter955382f2013-09-19 14:05:45 +020012131 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080012132 if (intel_crtc == NULL)
12133 return;
12134
Matt Roper465c1202014-05-29 08:06:54 -070012135 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012136 if (!primary)
12137 goto fail;
12138
12139 cursor = intel_cursor_plane_create(dev, pipe);
12140 if (!cursor)
12141 goto fail;
12142
Matt Roper465c1202014-05-29 08:06:54 -070012143 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070012144 cursor, &intel_crtc_funcs);
12145 if (ret)
12146 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080012147
12148 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080012149 for (i = 0; i < 256; i++) {
12150 intel_crtc->lut_r[i] = i;
12151 intel_crtc->lut_g[i] = i;
12152 intel_crtc->lut_b[i] = i;
12153 }
12154
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012155 /*
12156 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020012157 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020012158 */
Jesse Barnes80824002009-09-10 15:28:06 -070012159 intel_crtc->pipe = pipe;
12160 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010012161 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080012162 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010012163 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070012164 }
12165
Chris Wilson4b0e3332014-05-30 16:35:26 +030012166 intel_crtc->cursor_base = ~0;
12167 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030012168 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030012169
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080012170 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
12171 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
12172 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
12173 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
12174
Jesse Barnes79e53942008-11-07 14:24:08 -080012175 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020012176
12177 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070012178 return;
12179
12180fail:
12181 if (primary)
12182 drm_plane_cleanup(primary);
12183 if (cursor)
12184 drm_plane_cleanup(cursor);
12185 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080012186}
12187
Jesse Barnes752aa882013-10-31 18:55:49 +020012188enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
12189{
12190 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012191 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020012192
Rob Clark51fd3712013-11-19 12:10:12 -050012193 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020012194
12195 if (!encoder)
12196 return INVALID_PIPE;
12197
12198 return to_intel_crtc(encoder->crtc)->pipe;
12199}
12200
Carl Worth08d7b3d2009-04-29 14:43:54 -070012201int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000012202 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070012203{
Carl Worth08d7b3d2009-04-29 14:43:54 -070012204 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040012205 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020012206 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012207
Daniel Vetter1cff8f62012-04-24 09:55:08 +020012208 if (!drm_core_check_feature(dev, DRIVER_MODESET))
12209 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012210
Rob Clark7707e652014-07-17 23:30:04 -040012211 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070012212
Rob Clark7707e652014-07-17 23:30:04 -040012213 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070012214 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030012215 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012216 }
12217
Rob Clark7707e652014-07-17 23:30:04 -040012218 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020012219 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012220
Daniel Vetterc05422d2009-08-11 16:05:30 +020012221 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070012222}
12223
Daniel Vetter66a92782012-07-12 20:08:18 +020012224static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080012225{
Daniel Vetter66a92782012-07-12 20:08:18 +020012226 struct drm_device *dev = encoder->base.dev;
12227 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080012228 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080012229 int entry = 0;
12230
Damien Lespiaub2784e12014-08-05 11:29:37 +010012231 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012232 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020012233 index_mask |= (1 << entry);
12234
Jesse Barnes79e53942008-11-07 14:24:08 -080012235 entry++;
12236 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010012237
Jesse Barnes79e53942008-11-07 14:24:08 -080012238 return index_mask;
12239}
12240
Chris Wilson4d302442010-12-14 19:21:29 +000012241static bool has_edp_a(struct drm_device *dev)
12242{
12243 struct drm_i915_private *dev_priv = dev->dev_private;
12244
12245 if (!IS_MOBILE(dev))
12246 return false;
12247
12248 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
12249 return false;
12250
Damien Lespiaue3589902014-02-07 19:12:50 +000012251 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000012252 return false;
12253
12254 return true;
12255}
12256
Damien Lespiauba0fbca2014-01-08 14:18:23 +000012257const char *intel_output_name(int output)
12258{
12259 static const char *names[] = {
12260 [INTEL_OUTPUT_UNUSED] = "Unused",
12261 [INTEL_OUTPUT_ANALOG] = "Analog",
12262 [INTEL_OUTPUT_DVO] = "DVO",
12263 [INTEL_OUTPUT_SDVO] = "SDVO",
12264 [INTEL_OUTPUT_LVDS] = "LVDS",
12265 [INTEL_OUTPUT_TVOUT] = "TV",
12266 [INTEL_OUTPUT_HDMI] = "HDMI",
12267 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
12268 [INTEL_OUTPUT_EDP] = "eDP",
12269 [INTEL_OUTPUT_DSI] = "DSI",
12270 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
12271 };
12272
12273 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
12274 return "Invalid";
12275
12276 return names[output];
12277}
12278
Jesse Barnes84b4e042014-06-25 08:24:29 -070012279static bool intel_crt_present(struct drm_device *dev)
12280{
12281 struct drm_i915_private *dev_priv = dev->dev_private;
12282
12283 if (IS_ULT(dev))
12284 return false;
12285
12286 if (IS_CHERRYVIEW(dev))
12287 return false;
12288
12289 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12290 return false;
12291
12292 return true;
12293}
12294
Jesse Barnes79e53942008-11-07 14:24:08 -080012295static void intel_setup_outputs(struct drm_device *dev)
12296{
Eric Anholt725e30a2009-01-22 13:01:02 -080012297 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010012298 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012299 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080012300
Daniel Vetterc9093352013-06-06 22:22:47 +020012301 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012302
Jesse Barnes84b4e042014-06-25 08:24:29 -070012303 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020012304 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012305
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012306 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030012307 int found;
12308
12309 /* Haswell uses DDI functions to detect digital outputs */
12310 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12311 /* DDI A only supports eDP */
12312 if (found)
12313 intel_ddi_init(dev, PORT_A);
12314
12315 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12316 * register */
12317 found = I915_READ(SFUSE_STRAP);
12318
12319 if (found & SFUSE_STRAP_DDIB_DETECTED)
12320 intel_ddi_init(dev, PORT_B);
12321 if (found & SFUSE_STRAP_DDIC_DETECTED)
12322 intel_ddi_init(dev, PORT_C);
12323 if (found & SFUSE_STRAP_DDID_DETECTED)
12324 intel_ddi_init(dev, PORT_D);
12325 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012326 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020012327 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020012328
12329 if (has_edp_a(dev))
12330 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012331
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012332 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080012333 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010012334 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012335 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012336 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012337 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012338 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012339 }
12340
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012341 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012342 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012343
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012344 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012345 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012346
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012347 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012348 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012349
Daniel Vetter270b3042012-10-27 15:52:05 +020012350 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012351 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070012352 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012353 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
12354 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12355 PORT_B);
12356 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
12357 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
12358 }
12359
Jesse Barnes6f6005a2013-08-09 09:34:35 -070012360 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
12361 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12362 PORT_C);
12363 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020012364 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Jesse Barnes6f6005a2013-08-09 09:34:35 -070012365 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053012366
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012367 if (IS_CHERRYVIEW(dev)) {
12368 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
12369 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12370 PORT_D);
12371 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12372 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
12373 }
12374 }
12375
Jani Nikula3cfca972013-08-27 15:12:26 +030012376 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080012377 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012378 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080012379
Paulo Zanonie2debe92013-02-18 19:00:27 -030012380 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012381 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012382 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012383 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12384 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012385 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012386 }
Ma Ling27185ae2009-08-24 13:50:23 +080012387
Imre Deake7281ea2013-05-08 13:14:08 +030012388 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012389 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080012390 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012391
12392 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012393
Paulo Zanonie2debe92013-02-18 19:00:27 -030012394 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012395 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012396 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012397 }
Ma Ling27185ae2009-08-24 13:50:23 +080012398
Paulo Zanonie2debe92013-02-18 19:00:27 -030012399 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012400
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012401 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12402 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012403 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012404 }
Imre Deake7281ea2013-05-08 13:14:08 +030012405 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012406 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080012407 }
Ma Ling27185ae2009-08-24 13:50:23 +080012408
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012409 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030012410 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012411 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070012412 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012413 intel_dvo_init(dev);
12414
Zhenyu Wang103a1962009-11-27 11:44:36 +080012415 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012416 intel_tv_init(dev);
12417
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070012418 intel_edp_psr_init(dev);
12419
Damien Lespiaub2784e12014-08-05 11:29:37 +010012420 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010012421 encoder->base.possible_crtcs = encoder->crtc_mask;
12422 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020012423 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080012424 }
Chris Wilson47356eb2011-01-11 17:06:04 +000012425
Paulo Zanonidde86e22012-12-01 12:04:25 -020012426 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020012427
12428 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012429}
12430
12431static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12432{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012433 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080012434 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080012435
Daniel Vetteref2d6332014-02-10 18:00:38 +010012436 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012437 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010012438 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012439 drm_gem_object_unreference(&intel_fb->obj->base);
12440 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012441 kfree(intel_fb);
12442}
12443
12444static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000012445 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080012446 unsigned int *handle)
12447{
12448 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000012449 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012450
Chris Wilson05394f32010-11-08 19:18:58 +000012451 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080012452}
12453
12454static const struct drm_framebuffer_funcs intel_fb_funcs = {
12455 .destroy = intel_user_framebuffer_destroy,
12456 .create_handle = intel_user_framebuffer_create_handle,
12457};
12458
Daniel Vetterb5ea6422014-03-02 21:18:00 +010012459static int intel_framebuffer_init(struct drm_device *dev,
12460 struct intel_framebuffer *intel_fb,
12461 struct drm_mode_fb_cmd2 *mode_cmd,
12462 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080012463{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080012464 int aligned_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012465 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080012466 int ret;
12467
Daniel Vetterdd4916c2013-10-09 21:23:51 +020012468 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12469
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012470 if (obj->tiling_mode == I915_TILING_Y) {
12471 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010012472 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012473 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012474
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012475 if (mode_cmd->pitches[0] & 63) {
12476 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12477 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010012478 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012479 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012480
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012481 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12482 pitch_limit = 32*1024;
12483 } else if (INTEL_INFO(dev)->gen >= 4) {
12484 if (obj->tiling_mode)
12485 pitch_limit = 16*1024;
12486 else
12487 pitch_limit = 32*1024;
12488 } else if (INTEL_INFO(dev)->gen >= 3) {
12489 if (obj->tiling_mode)
12490 pitch_limit = 8*1024;
12491 else
12492 pitch_limit = 16*1024;
12493 } else
12494 /* XXX DSPC is limited to 4k tiled */
12495 pitch_limit = 8*1024;
12496
12497 if (mode_cmd->pitches[0] > pitch_limit) {
12498 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12499 obj->tiling_mode ? "tiled" : "linear",
12500 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012501 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012502 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012503
12504 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012505 mode_cmd->pitches[0] != obj->stride) {
12506 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12507 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012508 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012509 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012510
Ville Syrjälä57779d02012-10-31 17:50:14 +020012511 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012512 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020012513 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012514 case DRM_FORMAT_RGB565:
12515 case DRM_FORMAT_XRGB8888:
12516 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012517 break;
12518 case DRM_FORMAT_XRGB1555:
12519 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012520 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012521 DRM_DEBUG("unsupported pixel format: %s\n",
12522 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012523 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012524 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020012525 break;
12526 case DRM_FORMAT_XBGR8888:
12527 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012528 case DRM_FORMAT_XRGB2101010:
12529 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012530 case DRM_FORMAT_XBGR2101010:
12531 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012532 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012533 DRM_DEBUG("unsupported pixel format: %s\n",
12534 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012535 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012536 }
Jesse Barnesb5626742011-06-24 12:19:27 -070012537 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020012538 case DRM_FORMAT_YUYV:
12539 case DRM_FORMAT_UYVY:
12540 case DRM_FORMAT_YVYU:
12541 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012542 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012543 DRM_DEBUG("unsupported pixel format: %s\n",
12544 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012545 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012546 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012547 break;
12548 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012549 DRM_DEBUG("unsupported pixel format: %s\n",
12550 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010012551 return -EINVAL;
12552 }
12553
Ville Syrjälä90f9a332012-10-31 17:50:19 +020012554 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12555 if (mode_cmd->offsets[0] != 0)
12556 return -EINVAL;
12557
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080012558 aligned_height = intel_align_height(dev, mode_cmd->height,
12559 obj->tiling_mode);
Daniel Vetter53155c02013-10-09 21:55:33 +020012560 /* FIXME drm helper for size checks (especially planar formats)? */
12561 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12562 return -EINVAL;
12563
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012564 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12565 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020012566 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012567
Jesse Barnes79e53942008-11-07 14:24:08 -080012568 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12569 if (ret) {
12570 DRM_ERROR("framebuffer init failed %d\n", ret);
12571 return ret;
12572 }
12573
Jesse Barnes79e53942008-11-07 14:24:08 -080012574 return 0;
12575}
12576
Jesse Barnes79e53942008-11-07 14:24:08 -080012577static struct drm_framebuffer *
12578intel_user_framebuffer_create(struct drm_device *dev,
12579 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012580 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080012581{
Chris Wilson05394f32010-11-08 19:18:58 +000012582 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012583
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012584 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12585 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000012586 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010012587 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080012588
Chris Wilsond2dff872011-04-19 08:36:26 +010012589 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080012590}
12591
Daniel Vetter4520f532013-10-09 09:18:51 +020012592#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020012593static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020012594{
12595}
12596#endif
12597
Jesse Barnes79e53942008-11-07 14:24:08 -080012598static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080012599 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020012600 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080012601};
12602
Jesse Barnese70236a2009-09-21 10:42:27 -070012603/* Set up chip specific display functions */
12604static void intel_init_display(struct drm_device *dev)
12605{
12606 struct drm_i915_private *dev_priv = dev->dev_private;
12607
Daniel Vetteree9300b2013-06-03 22:40:22 +020012608 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12609 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030012610 else if (IS_CHERRYVIEW(dev))
12611 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020012612 else if (IS_VALLEYVIEW(dev))
12613 dev_priv->display.find_dpll = vlv_find_best_dpll;
12614 else if (IS_PINEVIEW(dev))
12615 dev_priv->display.find_dpll = pnv_find_best_dpll;
12616 else
12617 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12618
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012619 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012620 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080012621 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030012622 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020012623 dev_priv->display.crtc_enable = haswell_crtc_enable;
12624 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030012625 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012626 dev_priv->display.update_primary_plane =
12627 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030012628 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012629 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080012630 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070012631 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020012632 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12633 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012634 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012635 dev_priv->display.update_primary_plane =
12636 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070012637 } else if (IS_VALLEYVIEW(dev)) {
12638 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080012639 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Jesse Barnes89b667f2013-04-18 14:51:36 -070012640 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12641 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12642 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12643 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012644 dev_priv->display.update_primary_plane =
12645 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070012646 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012647 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080012648 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070012649 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020012650 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12651 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012652 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012653 dev_priv->display.update_primary_plane =
12654 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070012655 }
Jesse Barnese70236a2009-09-21 10:42:27 -070012656
Jesse Barnese70236a2009-09-21 10:42:27 -070012657 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070012658 if (IS_VALLEYVIEW(dev))
12659 dev_priv->display.get_display_clock_speed =
12660 valleyview_get_display_clock_speed;
12661 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070012662 dev_priv->display.get_display_clock_speed =
12663 i945_get_display_clock_speed;
12664 else if (IS_I915G(dev))
12665 dev_priv->display.get_display_clock_speed =
12666 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012667 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012668 dev_priv->display.get_display_clock_speed =
12669 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012670 else if (IS_PINEVIEW(dev))
12671 dev_priv->display.get_display_clock_speed =
12672 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070012673 else if (IS_I915GM(dev))
12674 dev_priv->display.get_display_clock_speed =
12675 i915gm_get_display_clock_speed;
12676 else if (IS_I865G(dev))
12677 dev_priv->display.get_display_clock_speed =
12678 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020012679 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012680 dev_priv->display.get_display_clock_speed =
12681 i855_get_display_clock_speed;
12682 else /* 852, 830 */
12683 dev_priv->display.get_display_clock_speed =
12684 i830_get_display_clock_speed;
12685
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012686 if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080012687 dev_priv->display.write_eld = g4x_write_eld;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012688 } else if (IS_GEN5(dev)) {
12689 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
12690 dev_priv->display.write_eld = ironlake_write_eld;
12691 } else if (IS_GEN6(dev)) {
12692 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
12693 dev_priv->display.write_eld = ironlake_write_eld;
12694 dev_priv->display.modeset_global_resources =
12695 snb_modeset_global_resources;
12696 } else if (IS_IVYBRIDGE(dev)) {
12697 /* FIXME: detect B0+ stepping and use auto training */
12698 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
12699 dev_priv->display.write_eld = ironlake_write_eld;
12700 dev_priv->display.modeset_global_resources =
12701 ivb_modeset_global_resources;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030012702 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012703 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
12704 dev_priv->display.write_eld = haswell_write_eld;
12705 dev_priv->display.modeset_global_resources =
12706 haswell_modeset_global_resources;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012707 } else if (IS_VALLEYVIEW(dev)) {
12708 dev_priv->display.modeset_global_resources =
12709 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040012710 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070012711 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012712
12713 /* Default just returns -ENODEV to indicate unsupported */
12714 dev_priv->display.queue_flip = intel_default_queue_flip;
12715
12716 switch (INTEL_INFO(dev)->gen) {
12717 case 2:
12718 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12719 break;
12720
12721 case 3:
12722 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12723 break;
12724
12725 case 4:
12726 case 5:
12727 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12728 break;
12729
12730 case 6:
12731 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12732 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012733 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070012734 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012735 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12736 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012737 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020012738
12739 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030012740
12741 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070012742}
12743
Jesse Barnesb690e962010-07-19 13:53:12 -070012744/*
12745 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12746 * resume, or other times. This quirk makes sure that's the case for
12747 * affected systems.
12748 */
Akshay Joshi0206e352011-08-16 15:34:10 -040012749static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070012750{
12751 struct drm_i915_private *dev_priv = dev->dev_private;
12752
12753 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012754 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070012755}
12756
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012757static void quirk_pipeb_force(struct drm_device *dev)
12758{
12759 struct drm_i915_private *dev_priv = dev->dev_private;
12760
12761 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
12762 DRM_INFO("applying pipe b force quirk\n");
12763}
12764
Keith Packard435793d2011-07-12 14:56:22 -070012765/*
12766 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12767 */
12768static void quirk_ssc_force_disable(struct drm_device *dev)
12769{
12770 struct drm_i915_private *dev_priv = dev->dev_private;
12771 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012772 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070012773}
12774
Carsten Emde4dca20e2012-03-15 15:56:26 +010012775/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010012776 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12777 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010012778 */
12779static void quirk_invert_brightness(struct drm_device *dev)
12780{
12781 struct drm_i915_private *dev_priv = dev->dev_private;
12782 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012783 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070012784}
12785
Scot Doyle9c72cc62014-07-03 23:27:50 +000012786/* Some VBT's incorrectly indicate no backlight is present */
12787static void quirk_backlight_present(struct drm_device *dev)
12788{
12789 struct drm_i915_private *dev_priv = dev->dev_private;
12790 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
12791 DRM_INFO("applying backlight present quirk\n");
12792}
12793
Jesse Barnesb690e962010-07-19 13:53:12 -070012794struct intel_quirk {
12795 int device;
12796 int subsystem_vendor;
12797 int subsystem_device;
12798 void (*hook)(struct drm_device *dev);
12799};
12800
Egbert Eich5f85f172012-10-14 15:46:38 +020012801/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12802struct intel_dmi_quirk {
12803 void (*hook)(struct drm_device *dev);
12804 const struct dmi_system_id (*dmi_id_list)[];
12805};
12806
12807static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12808{
12809 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12810 return 1;
12811}
12812
12813static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12814 {
12815 .dmi_id_list = &(const struct dmi_system_id[]) {
12816 {
12817 .callback = intel_dmi_reverse_brightness,
12818 .ident = "NCR Corporation",
12819 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12820 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12821 },
12822 },
12823 { } /* terminating entry */
12824 },
12825 .hook = quirk_invert_brightness,
12826 },
12827};
12828
Ben Widawskyc43b5632012-04-16 14:07:40 -070012829static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070012830 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040012831 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070012832
Jesse Barnesb690e962010-07-19 13:53:12 -070012833 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12834 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12835
Jesse Barnesb690e962010-07-19 13:53:12 -070012836 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12837 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12838
Ville Syrjälä5f080c02014-08-15 01:22:06 +030012839 /* 830 needs to leave pipe A & dpll A up */
12840 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
12841
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012842 /* 830 needs to leave pipe B & dpll B up */
12843 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
12844
Keith Packard435793d2011-07-12 14:56:22 -070012845 /* Lenovo U160 cannot use SSC on LVDS */
12846 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020012847
12848 /* Sony Vaio Y cannot use SSC on LVDS */
12849 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010012850
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010012851 /* Acer Aspire 5734Z must invert backlight brightness */
12852 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12853
12854 /* Acer/eMachines G725 */
12855 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12856
12857 /* Acer/eMachines e725 */
12858 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12859
12860 /* Acer/Packard Bell NCL20 */
12861 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12862
12863 /* Acer Aspire 4736Z */
12864 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020012865
12866 /* Acer Aspire 5336 */
12867 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000012868
12869 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
12870 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000012871
Scot Doyledfb3d47b2014-08-21 16:08:02 +000012872 /* Acer C720 Chromebook (Core i3 4005U) */
12873 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
12874
Scot Doyled4967d82014-07-03 23:27:52 +000012875 /* Toshiba CB35 Chromebook (Celeron 2955U) */
12876 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000012877
12878 /* HP Chromebook 14 (Celeron 2955U) */
12879 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070012880};
12881
12882static void intel_init_quirks(struct drm_device *dev)
12883{
12884 struct pci_dev *d = dev->pdev;
12885 int i;
12886
12887 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12888 struct intel_quirk *q = &intel_quirks[i];
12889
12890 if (d->device == q->device &&
12891 (d->subsystem_vendor == q->subsystem_vendor ||
12892 q->subsystem_vendor == PCI_ANY_ID) &&
12893 (d->subsystem_device == q->subsystem_device ||
12894 q->subsystem_device == PCI_ANY_ID))
12895 q->hook(dev);
12896 }
Egbert Eich5f85f172012-10-14 15:46:38 +020012897 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12898 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12899 intel_dmi_quirks[i].hook(dev);
12900 }
Jesse Barnesb690e962010-07-19 13:53:12 -070012901}
12902
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012903/* Disable the VGA plane that we never use */
12904static void i915_disable_vga(struct drm_device *dev)
12905{
12906 struct drm_i915_private *dev_priv = dev->dev_private;
12907 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012908 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012909
Ville Syrjälä2b37c612014-01-22 21:32:38 +020012910 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012911 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070012912 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012913 sr1 = inb(VGA_SR_DATA);
12914 outb(sr1 | 1<<5, VGA_SR_DATA);
12915 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12916 udelay(300);
12917
Ville Syrjälä69769f92014-08-15 01:22:08 +030012918 /*
12919 * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
12920 * from S3 without preserving (some of?) the other bits.
12921 */
12922 I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012923 POSTING_READ(vga_reg);
12924}
12925
Daniel Vetterf8175862012-04-10 15:50:11 +020012926void intel_modeset_init_hw(struct drm_device *dev)
12927{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030012928 intel_prepare_ddi(dev);
12929
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030012930 if (IS_VALLEYVIEW(dev))
12931 vlv_update_cdclk(dev);
12932
Daniel Vetterf8175862012-04-10 15:50:11 +020012933 intel_init_clock_gating(dev);
12934
Daniel Vetter8090c6b2012-06-24 16:42:32 +020012935 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020012936}
12937
Imre Deak7d708ee2013-04-17 14:04:50 +030012938void intel_modeset_suspend_hw(struct drm_device *dev)
12939{
12940 intel_suspend_hw(dev);
12941}
12942
Jesse Barnes79e53942008-11-07 14:24:08 -080012943void intel_modeset_init(struct drm_device *dev)
12944{
Jesse Barnes652c3932009-08-17 13:31:43 -070012945 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000012946 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012947 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080012948 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080012949
12950 drm_mode_config_init(dev);
12951
12952 dev->mode_config.min_width = 0;
12953 dev->mode_config.min_height = 0;
12954
Dave Airlie019d96c2011-09-29 16:20:42 +010012955 dev->mode_config.preferred_depth = 24;
12956 dev->mode_config.prefer_shadow = 1;
12957
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020012958 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080012959
Jesse Barnesb690e962010-07-19 13:53:12 -070012960 intel_init_quirks(dev);
12961
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030012962 intel_init_pm(dev);
12963
Ben Widawskye3c74752013-04-05 13:12:39 -070012964 if (INTEL_INFO(dev)->num_pipes == 0)
12965 return;
12966
Jesse Barnese70236a2009-09-21 10:42:27 -070012967 intel_init_display(dev);
12968
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012969 if (IS_GEN2(dev)) {
12970 dev->mode_config.max_width = 2048;
12971 dev->mode_config.max_height = 2048;
12972 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070012973 dev->mode_config.max_width = 4096;
12974 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080012975 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012976 dev->mode_config.max_width = 8192;
12977 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080012978 }
Damien Lespiau068be562014-03-28 14:17:49 +000012979
Ville Syrjälädc41c152014-08-13 11:57:05 +030012980 if (IS_845G(dev) || IS_I865G(dev)) {
12981 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
12982 dev->mode_config.cursor_height = 1023;
12983 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000012984 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12985 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12986 } else {
12987 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12988 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12989 }
12990
Ben Widawsky5d4545a2013-01-17 12:45:15 -080012991 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080012992
Zhao Yakui28c97732009-10-09 11:39:41 +080012993 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012994 INTEL_INFO(dev)->num_pipes,
12995 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080012996
Damien Lespiau055e3932014-08-18 13:49:10 +010012997 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012998 intel_crtc_init(dev, pipe);
Damien Lespiau1fe47782014-03-03 17:31:47 +000012999 for_each_sprite(pipe, sprite) {
13000 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070013001 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030013002 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000013003 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070013004 }
Jesse Barnes79e53942008-11-07 14:24:08 -080013005 }
13006
Jesse Barnesf42bb702013-12-16 16:34:23 -080013007 intel_init_dpio(dev);
13008
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013009 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013010
Ville Syrjälä69769f92014-08-15 01:22:08 +030013011 /* save the BIOS value before clobbering it */
13012 dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
Jesse Barnes9cce37f2010-08-13 15:11:26 -070013013 /* Just disable it once at startup */
13014 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013015 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000013016
13017 /* Just in case the BIOS is doing something questionable. */
13018 intel_disable_fbc(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080013019
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013020 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080013021 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013022 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080013023
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013024 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080013025 if (!crtc->active)
13026 continue;
13027
Jesse Barnes46f297f2014-03-07 08:57:48 -080013028 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080013029 * Note that reserving the BIOS fb up front prevents us
13030 * from stuffing other stolen allocations like the ring
13031 * on top. This prevents some ugliness at boot time, and
13032 * can even allow for smooth boot transitions if the BIOS
13033 * fb is large enough for the active pipe configuration.
13034 */
13035 if (dev_priv->display.get_plane_config) {
13036 dev_priv->display.get_plane_config(crtc,
13037 &crtc->plane_config);
13038 /*
13039 * If the fb is shared between multiple heads, we'll
13040 * just get the first one.
13041 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080013042 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080013043 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080013044 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010013045}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080013046
Daniel Vetter7fad7982012-07-04 17:51:47 +020013047static void intel_enable_pipe_a(struct drm_device *dev)
13048{
13049 struct intel_connector *connector;
13050 struct drm_connector *crt = NULL;
13051 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013052 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020013053
13054 /* We can't just switch on the pipe A, we need to set things up with a
13055 * proper mode and output configuration. As a gross hack, enable pipe A
13056 * by enabling the load detect pipe once. */
13057 list_for_each_entry(connector,
13058 &dev->mode_config.connector_list,
13059 base.head) {
13060 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
13061 crt = &connector->base;
13062 break;
13063 }
13064 }
13065
13066 if (!crt)
13067 return;
13068
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030013069 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
13070 intel_release_load_detect_pipe(crt, &load_detect_temp);
Daniel Vetter7fad7982012-07-04 17:51:47 +020013071}
13072
Daniel Vetterfa555832012-10-10 23:14:00 +020013073static bool
13074intel_check_plane_mapping(struct intel_crtc *crtc)
13075{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013076 struct drm_device *dev = crtc->base.dev;
13077 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013078 u32 reg, val;
13079
Ben Widawsky7eb552a2013-03-13 14:05:41 -070013080 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020013081 return true;
13082
13083 reg = DSPCNTR(!crtc->plane);
13084 val = I915_READ(reg);
13085
13086 if ((val & DISPLAY_PLANE_ENABLE) &&
13087 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
13088 return false;
13089
13090 return true;
13091}
13092
Daniel Vetter24929352012-07-02 20:28:59 +020013093static void intel_sanitize_crtc(struct intel_crtc *crtc)
13094{
13095 struct drm_device *dev = crtc->base.dev;
13096 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020013097 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020013098
Daniel Vetter24929352012-07-02 20:28:59 +020013099 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020013100 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020013101 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
13102
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013103 /* restore vblank interrupts to correct state */
Ville Syrjäläd297e102014-08-06 14:50:01 +030013104 if (crtc->active) {
13105 update_scanline_offset(crtc);
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013106 drm_vblank_on(dev, crtc->pipe);
Ville Syrjäläd297e102014-08-06 14:50:01 +030013107 } else
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030013108 drm_vblank_off(dev, crtc->pipe);
13109
Daniel Vetter24929352012-07-02 20:28:59 +020013110 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020013111 * disable the crtc (and hence change the state) if it is wrong. Note
13112 * that gen4+ has a fixed plane -> pipe mapping. */
13113 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020013114 struct intel_connector *connector;
13115 bool plane;
13116
Daniel Vetter24929352012-07-02 20:28:59 +020013117 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
13118 crtc->base.base.id);
13119
13120 /* Pipe has the wrong plane attached and the plane is active.
13121 * Temporarily change the plane mapping and disable everything
13122 * ... */
13123 plane = crtc->plane;
13124 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020013125 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020013126 dev_priv->display.crtc_disable(&crtc->base);
13127 crtc->plane = plane;
13128
13129 /* ... and break all links. */
13130 list_for_each_entry(connector, &dev->mode_config.connector_list,
13131 base.head) {
13132 if (connector->encoder->base.crtc != &crtc->base)
13133 continue;
13134
Egbert Eich7f1950f2014-04-25 10:56:22 +020013135 connector->base.dpms = DRM_MODE_DPMS_OFF;
13136 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020013137 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020013138 /* multiple connectors may have the same encoder:
13139 * handle them and break crtc link separately */
13140 list_for_each_entry(connector, &dev->mode_config.connector_list,
13141 base.head)
13142 if (connector->encoder->base.crtc == &crtc->base) {
13143 connector->encoder->base.crtc = NULL;
13144 connector->encoder->connectors_active = false;
13145 }
Daniel Vetter24929352012-07-02 20:28:59 +020013146
13147 WARN_ON(crtc->active);
13148 crtc->base.enabled = false;
13149 }
Daniel Vetter24929352012-07-02 20:28:59 +020013150
Daniel Vetter7fad7982012-07-04 17:51:47 +020013151 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
13152 crtc->pipe == PIPE_A && !crtc->active) {
13153 /* BIOS forgot to enable pipe A, this mostly happens after
13154 * resume. Force-enable the pipe to fix this, the update_dpms
13155 * call below we restore the pipe to the right state, but leave
13156 * the required bits on. */
13157 intel_enable_pipe_a(dev);
13158 }
13159
Daniel Vetter24929352012-07-02 20:28:59 +020013160 /* Adjust the state of the output pipe according to whether we
13161 * have active connectors/encoders. */
13162 intel_crtc_update_dpms(&crtc->base);
13163
13164 if (crtc->active != crtc->base.enabled) {
13165 struct intel_encoder *encoder;
13166
13167 /* This can happen either due to bugs in the get_hw_state
13168 * functions or because the pipe is force-enabled due to the
13169 * pipe A quirk. */
13170 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
13171 crtc->base.base.id,
13172 crtc->base.enabled ? "enabled" : "disabled",
13173 crtc->active ? "enabled" : "disabled");
13174
13175 crtc->base.enabled = crtc->active;
13176
13177 /* Because we only establish the connector -> encoder ->
13178 * crtc links if something is active, this means the
13179 * crtc is now deactivated. Break the links. connector
13180 * -> encoder links are only establish when things are
13181 * actually up, hence no need to break them. */
13182 WARN_ON(crtc->active);
13183
13184 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
13185 WARN_ON(encoder->connectors_active);
13186 encoder->base.crtc = NULL;
13187 }
13188 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013189
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030013190 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010013191 /*
13192 * We start out with underrun reporting disabled to avoid races.
13193 * For correct bookkeeping mark this on active crtcs.
13194 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020013195 * Also on gmch platforms we dont have any hardware bits to
13196 * disable the underrun reporting. Which means we need to start
13197 * out with underrun reporting disabled also on inactive pipes,
13198 * since otherwise we'll complain about the garbage we read when
13199 * e.g. coming up after runtime pm.
13200 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010013201 * No protection against concurrent access is required - at
13202 * worst a fifo underrun happens which also sets this to false.
13203 */
13204 crtc->cpu_fifo_underrun_disabled = true;
13205 crtc->pch_fifo_underrun_disabled = true;
13206 }
Daniel Vetter24929352012-07-02 20:28:59 +020013207}
13208
13209static void intel_sanitize_encoder(struct intel_encoder *encoder)
13210{
13211 struct intel_connector *connector;
13212 struct drm_device *dev = encoder->base.dev;
13213
13214 /* We need to check both for a crtc link (meaning that the
13215 * encoder is active and trying to read from a pipe) and the
13216 * pipe itself being active. */
13217 bool has_active_crtc = encoder->base.crtc &&
13218 to_intel_crtc(encoder->base.crtc)->active;
13219
13220 if (encoder->connectors_active && !has_active_crtc) {
13221 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13222 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013223 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020013224
13225 /* Connector is active, but has no active pipe. This is
13226 * fallout from our resume register restoring. Disable
13227 * the encoder manually again. */
13228 if (encoder->base.crtc) {
13229 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
13230 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013231 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020013232 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030013233 if (encoder->post_disable)
13234 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020013235 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020013236 encoder->base.crtc = NULL;
13237 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020013238
13239 /* Inconsistent output/port/pipe state happens presumably due to
13240 * a bug in one of the get_hw_state functions. Or someplace else
13241 * in our code, like the register restore mess on resume. Clamp
13242 * things to off as a safer default. */
13243 list_for_each_entry(connector,
13244 &dev->mode_config.connector_list,
13245 base.head) {
13246 if (connector->encoder != encoder)
13247 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020013248 connector->base.dpms = DRM_MODE_DPMS_OFF;
13249 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020013250 }
13251 }
13252 /* Enabled encoders without active connectors will be fixed in
13253 * the crtc fixup. */
13254}
13255
Imre Deak04098752014-02-18 00:02:16 +020013256void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013257{
13258 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020013259 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013260
Imre Deak04098752014-02-18 00:02:16 +020013261 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
13262 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
13263 i915_disable_vga(dev);
13264 }
13265}
13266
13267void i915_redisable_vga(struct drm_device *dev)
13268{
13269 struct drm_i915_private *dev_priv = dev->dev_private;
13270
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013271 /* This function can be called both from intel_modeset_setup_hw_state or
13272 * at a very early point in our resume sequence, where the power well
13273 * structures are not yet restored. Since this function is at a very
13274 * paranoid "someone might have enabled VGA while we were not looking"
13275 * level, just check if the power well is enabled instead of trying to
13276 * follow the "don't touch the power well if we don't need it" policy
13277 * the rest of the driver uses. */
Imre Deak04098752014-02-18 00:02:16 +020013278 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013279 return;
13280
Imre Deak04098752014-02-18 00:02:16 +020013281 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013282}
13283
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013284static bool primary_get_hw_state(struct intel_crtc *crtc)
13285{
13286 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13287
13288 if (!crtc->active)
13289 return false;
13290
13291 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13292}
13293
Daniel Vetter30e984d2013-06-05 13:34:17 +020013294static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020013295{
13296 struct drm_i915_private *dev_priv = dev->dev_private;
13297 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020013298 struct intel_crtc *crtc;
13299 struct intel_encoder *encoder;
13300 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020013301 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020013302
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013303 for_each_intel_crtc(dev, crtc) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010013304 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020013305
Daniel Vetter99535992014-04-13 12:00:33 +020013306 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
13307
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013308 crtc->active = dev_priv->display.get_pipe_config(crtc,
13309 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013310
13311 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013312 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020013313
13314 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13315 crtc->base.base.id,
13316 crtc->active ? "enabled" : "disabled");
13317 }
13318
Daniel Vetter53589012013-06-05 13:34:16 +020013319 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13320 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13321
13322 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
13323 pll->active = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013324 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020013325 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13326 pll->active++;
13327 }
13328 pll->refcount = pll->active;
13329
Daniel Vetter35c95372013-07-17 06:55:04 +020013330 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
13331 pll->name, pll->refcount, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013332
13333 if (pll->refcount)
13334 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020013335 }
13336
Damien Lespiaub2784e12014-08-05 11:29:37 +010013337 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013338 pipe = 0;
13339
13340 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013341 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13342 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010013343 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013344 } else {
13345 encoder->base.crtc = NULL;
13346 }
13347
13348 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013349 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020013350 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013351 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013352 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013353 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020013354 }
13355
13356 list_for_each_entry(connector, &dev->mode_config.connector_list,
13357 base.head) {
13358 if (connector->get_hw_state(connector)) {
13359 connector->base.dpms = DRM_MODE_DPMS_ON;
13360 connector->encoder->connectors_active = true;
13361 connector->base.encoder = &connector->encoder->base;
13362 } else {
13363 connector->base.dpms = DRM_MODE_DPMS_OFF;
13364 connector->base.encoder = NULL;
13365 }
13366 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13367 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013368 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013369 connector->base.encoder ? "enabled" : "disabled");
13370 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020013371}
13372
13373/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13374 * and i915 state tracking structures. */
13375void intel_modeset_setup_hw_state(struct drm_device *dev,
13376 bool force_restore)
13377{
13378 struct drm_i915_private *dev_priv = dev->dev_private;
13379 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013380 struct intel_crtc *crtc;
13381 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020013382 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013383
13384 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020013385
Jesse Barnesbabea612013-06-26 18:57:38 +030013386 /*
13387 * Now that we have the config, copy it to each CRTC struct
13388 * Note that this could go away if we move to using crtc_config
13389 * checking everywhere.
13390 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013391 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020013392 if (crtc->active && i915.fastboot) {
Daniel Vetterf6a83282014-02-11 15:28:57 -080013393 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030013394 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13395 crtc->base.base.id);
13396 drm_mode_debug_printmodeline(&crtc->base.mode);
13397 }
13398 }
13399
Daniel Vetter24929352012-07-02 20:28:59 +020013400 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010013401 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013402 intel_sanitize_encoder(encoder);
13403 }
13404
Damien Lespiau055e3932014-08-18 13:49:10 +010013405 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020013406 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13407 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020013408 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020013409 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013410
Daniel Vetter35c95372013-07-17 06:55:04 +020013411 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13412 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13413
13414 if (!pll->on || pll->active)
13415 continue;
13416
13417 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13418
13419 pll->disable(dev_priv, pll);
13420 pll->on = false;
13421 }
13422
Ville Syrjälä96f90c52013-12-05 15:51:38 +020013423 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030013424 ilk_wm_get_hw_state(dev);
13425
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013426 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030013427 i915_redisable_vga(dev);
13428
Daniel Vetterf30da182013-04-11 20:22:50 +020013429 /*
13430 * We need to use raw interfaces for restoring state to avoid
13431 * checking (bogus) intermediate states.
13432 */
Damien Lespiau055e3932014-08-18 13:49:10 +010013433 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070013434 struct drm_crtc *crtc =
13435 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020013436
13437 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
Matt Roperf4510a22014-04-01 15:22:40 -070013438 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013439 }
13440 } else {
13441 intel_modeset_update_staged_output_state(dev);
13442 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013443
13444 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010013445}
13446
13447void intel_modeset_gem_init(struct drm_device *dev)
13448{
Jesse Barnes484b41d2014-03-07 08:57:55 -080013449 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070013450 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013451
Imre Deakae484342014-03-31 15:10:44 +030013452 mutex_lock(&dev->struct_mutex);
13453 intel_init_gt_powersave(dev);
13454 mutex_unlock(&dev->struct_mutex);
13455
Chris Wilson1833b132012-05-09 11:56:28 +010013456 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020013457
13458 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080013459
13460 /*
13461 * Make sure any fbs we allocated at startup are properly
13462 * pinned & fenced. When we do the allocation it's too early
13463 * for this.
13464 */
13465 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010013466 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070013467 obj = intel_fb_obj(c->primary->fb);
13468 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080013469 continue;
13470
Matt Roper2ff8fde2014-07-08 07:50:07 -070013471 if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080013472 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13473 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100013474 drm_framebuffer_unreference(c->primary->fb);
13475 c->primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013476 }
13477 }
13478 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013479}
13480
Imre Deak4932e2c2014-02-11 17:12:48 +020013481void intel_connector_unregister(struct intel_connector *intel_connector)
13482{
13483 struct drm_connector *connector = &intel_connector->base;
13484
13485 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010013486 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020013487}
13488
Jesse Barnes79e53942008-11-07 14:24:08 -080013489void intel_modeset_cleanup(struct drm_device *dev)
13490{
Jesse Barnes652c3932009-08-17 13:31:43 -070013491 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030013492 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070013493
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013494 /*
13495 * Interrupts and polling as the first thing to avoid creating havoc.
13496 * Too much stuff here (turning of rps, connectors, ...) would
13497 * experience fancy races otherwise.
13498 */
13499 drm_irq_uninstall(dev);
Imre Deak1d0d3432014-08-18 14:42:44 +030013500 intel_hpd_cancel_work(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070013501 dev_priv->pm._irqs_disabled = true;
13502
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013503 /*
13504 * Due to the hpd irq storm handling the hotplug work can re-arm the
13505 * poll handlers. Hence disable polling after hpd handling is shut down.
13506 */
Keith Packardf87ea762010-10-03 19:36:26 -070013507 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013508
Jesse Barnes652c3932009-08-17 13:31:43 -070013509 mutex_lock(&dev->struct_mutex);
13510
Jesse Barnes723bfd72010-10-07 16:01:13 -070013511 intel_unregister_dsm_handler();
13512
Chris Wilson973d04f2011-07-08 12:22:37 +010013513 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013514
Daniel Vetter8090c6b2012-06-24 16:42:32 +020013515 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000013516
Daniel Vetter930ebb42012-06-29 23:32:16 +020013517 ironlake_teardown_rc6(dev);
13518
Kristian Høgsberg69341a52009-11-11 12:19:17 -050013519 mutex_unlock(&dev->struct_mutex);
13520
Chris Wilson1630fe72011-07-08 12:22:42 +010013521 /* flush any delayed tasks or pending work */
13522 flush_scheduled_work();
13523
Jani Nikuladb31af1d2013-11-08 16:48:53 +020013524 /* destroy the backlight and sysfs files before encoders/connectors */
13525 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020013526 struct intel_connector *intel_connector;
13527
13528 intel_connector = to_intel_connector(connector);
13529 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020013530 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030013531
Jesse Barnes79e53942008-11-07 14:24:08 -080013532 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010013533
13534 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030013535
13536 mutex_lock(&dev->struct_mutex);
13537 intel_cleanup_gt_powersave(dev);
13538 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013539}
13540
Dave Airlie28d52042009-09-21 14:33:58 +100013541/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080013542 * Return which encoder is currently attached for connector.
13543 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010013544struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080013545{
Chris Wilsondf0e9242010-09-09 16:20:55 +010013546 return &intel_attached_encoder(connector)->base;
13547}
Jesse Barnes79e53942008-11-07 14:24:08 -080013548
Chris Wilsondf0e9242010-09-09 16:20:55 +010013549void intel_connector_attach_encoder(struct intel_connector *connector,
13550 struct intel_encoder *encoder)
13551{
13552 connector->encoder = encoder;
13553 drm_mode_connector_attach_encoder(&connector->base,
13554 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080013555}
Dave Airlie28d52042009-09-21 14:33:58 +100013556
13557/*
13558 * set vga decode state - true == enable VGA decode
13559 */
13560int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13561{
13562 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000013563 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100013564 u16 gmch_ctrl;
13565
Chris Wilson75fa0412014-02-07 18:37:02 -020013566 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13567 DRM_ERROR("failed to read control word\n");
13568 return -EIO;
13569 }
13570
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020013571 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13572 return 0;
13573
Dave Airlie28d52042009-09-21 14:33:58 +100013574 if (state)
13575 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13576 else
13577 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020013578
13579 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13580 DRM_ERROR("failed to write control word\n");
13581 return -EIO;
13582 }
13583
Dave Airlie28d52042009-09-21 14:33:58 +100013584 return 0;
13585}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013586
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013587struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013588
13589 u32 power_well_driver;
13590
Chris Wilson63b66e52013-08-08 15:12:06 +020013591 int num_transcoders;
13592
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013593 struct intel_cursor_error_state {
13594 u32 control;
13595 u32 position;
13596 u32 base;
13597 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010013598 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013599
13600 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013601 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013602 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030013603 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010013604 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013605
13606 struct intel_plane_error_state {
13607 u32 control;
13608 u32 stride;
13609 u32 size;
13610 u32 pos;
13611 u32 addr;
13612 u32 surface;
13613 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010013614 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020013615
13616 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013617 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020013618 enum transcoder cpu_transcoder;
13619
13620 u32 conf;
13621
13622 u32 htotal;
13623 u32 hblank;
13624 u32 hsync;
13625 u32 vtotal;
13626 u32 vblank;
13627 u32 vsync;
13628 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013629};
13630
13631struct intel_display_error_state *
13632intel_display_capture_error_state(struct drm_device *dev)
13633{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013634 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013635 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020013636 int transcoders[] = {
13637 TRANSCODER_A,
13638 TRANSCODER_B,
13639 TRANSCODER_C,
13640 TRANSCODER_EDP,
13641 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013642 int i;
13643
Chris Wilson63b66e52013-08-08 15:12:06 +020013644 if (INTEL_INFO(dev)->num_pipes == 0)
13645 return NULL;
13646
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013647 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013648 if (error == NULL)
13649 return NULL;
13650
Imre Deak190be112013-11-25 17:15:31 +020013651 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013652 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13653
Damien Lespiau055e3932014-08-18 13:49:10 +010013654 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020013655 error->pipe[i].power_domain_on =
Imre Deakbfafe932014-06-05 20:31:47 +030013656 intel_display_power_enabled_unlocked(dev_priv,
13657 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020013658 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013659 continue;
13660
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030013661 error->cursor[i].control = I915_READ(CURCNTR(i));
13662 error->cursor[i].position = I915_READ(CURPOS(i));
13663 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013664
13665 error->plane[i].control = I915_READ(DSPCNTR(i));
13666 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013667 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030013668 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013669 error->plane[i].pos = I915_READ(DSPPOS(i));
13670 }
Paulo Zanonica291362013-03-06 20:03:14 -030013671 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13672 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013673 if (INTEL_INFO(dev)->gen >= 4) {
13674 error->plane[i].surface = I915_READ(DSPSURF(i));
13675 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13676 }
13677
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013678 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030013679
Sonika Jindal3abfce72014-07-21 15:23:43 +053013680 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030013681 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020013682 }
13683
13684 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13685 if (HAS_DDI(dev_priv->dev))
13686 error->num_transcoders++; /* Account for eDP. */
13687
13688 for (i = 0; i < error->num_transcoders; i++) {
13689 enum transcoder cpu_transcoder = transcoders[i];
13690
Imre Deakddf9c532013-11-27 22:02:02 +020013691 error->transcoder[i].power_domain_on =
Imre Deakbfafe932014-06-05 20:31:47 +030013692 intel_display_power_enabled_unlocked(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020013693 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020013694 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013695 continue;
13696
Chris Wilson63b66e52013-08-08 15:12:06 +020013697 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13698
13699 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13700 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13701 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13702 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13703 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13704 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13705 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013706 }
13707
13708 return error;
13709}
13710
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013711#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13712
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013713void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013714intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013715 struct drm_device *dev,
13716 struct intel_display_error_state *error)
13717{
Damien Lespiau055e3932014-08-18 13:49:10 +010013718 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013719 int i;
13720
Chris Wilson63b66e52013-08-08 15:12:06 +020013721 if (!error)
13722 return;
13723
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013724 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020013725 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013726 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013727 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010013728 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013729 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020013730 err_printf(m, " Power: %s\n",
13731 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013732 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030013733 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013734
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013735 err_printf(m, "Plane [%d]:\n", i);
13736 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13737 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013738 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013739 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13740 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013741 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030013742 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013743 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013744 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013745 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13746 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013747 }
13748
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013749 err_printf(m, "Cursor [%d]:\n", i);
13750 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13751 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13752 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013753 }
Chris Wilson63b66e52013-08-08 15:12:06 +020013754
13755 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010013756 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020013757 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020013758 err_printf(m, " Power: %s\n",
13759 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020013760 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13761 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13762 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13763 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13764 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13765 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13766 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13767 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013768}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013769
13770void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
13771{
13772 struct intel_crtc *crtc;
13773
13774 for_each_intel_crtc(dev, crtc) {
13775 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013776
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020013777 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013778
13779 work = crtc->unpin_work;
13780
13781 if (work && work->event &&
13782 work->event->base.file_priv == file) {
13783 kfree(work->event);
13784 work->event = NULL;
13785 }
13786
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020013787 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013788 }
13789}