blob: a514779a5918b0f7dcc0d1561fbcaaa75d3ed5d6 [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 Vetter3dec0092010-08-20 21:40:52 +020079static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010080static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080081
Jesse Barnesf1f644d2013-06-27 00:39:25 +030082static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
83 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030084static void ironlake_pch_clock_get(struct intel_crtc *crtc,
85 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030086
Damien Lespiaue7457a92013-08-08 22:28:59 +010087static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
88 int x, int y, struct drm_framebuffer *old_fb);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080089static int intel_framebuffer_init(struct drm_device *dev,
90 struct intel_framebuffer *ifb,
91 struct drm_mode_fb_cmd2 *mode_cmd,
92 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020093static void intel_dp_set_m_n(struct intel_crtc *crtc);
94static 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,
97 struct intel_link_m_n *m_n);
98static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020099static void haswell_set_pipeconf(struct drm_crtc *crtc);
100static void intel_set_pipe_csc(struct drm_crtc *crtc);
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +0200101static void vlv_prepare_pll(struct intel_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100102
Jesse Barnes79e53942008-11-07 14:24:08 -0800103typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400104 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800105} intel_range_t;
106
107typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400108 int dot_limit;
109 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800110} intel_p2_t;
111
Ma Lingd4906092009-03-18 20:13:27 +0800112typedef struct intel_limit intel_limit_t;
113struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400114 intel_range_t dot, vco, n, m, m1, m2, p, p1;
115 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800116};
Jesse Barnes79e53942008-11-07 14:24:08 -0800117
Daniel Vetterd2acd212012-10-20 20:57:43 +0200118int
119intel_pch_rawclk(struct drm_device *dev)
120{
121 struct drm_i915_private *dev_priv = dev->dev_private;
122
123 WARN_ON(!HAS_PCH_SPLIT(dev));
124
125 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
126}
127
Chris Wilson021357a2010-09-07 20:54:59 +0100128static inline u32 /* units of 100MHz */
129intel_fdi_link_freq(struct drm_device *dev)
130{
Chris Wilson8b99e682010-10-13 09:59:17 +0100131 if (IS_GEN5(dev)) {
132 struct drm_i915_private *dev_priv = dev->dev_private;
133 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
134 } else
135 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100136}
137
Daniel Vetter5d536e22013-07-06 12:52:06 +0200138static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400139 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200140 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200141 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400142 .m = { .min = 96, .max = 140 },
143 .m1 = { .min = 18, .max = 26 },
144 .m2 = { .min = 6, .max = 16 },
145 .p = { .min = 4, .max = 128 },
146 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700147 .p2 = { .dot_limit = 165000,
148 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700149};
150
Daniel Vetter5d536e22013-07-06 12:52:06 +0200151static const intel_limit_t intel_limits_i8xx_dvo = {
152 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200153 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200154 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200155 .m = { .min = 96, .max = 140 },
156 .m1 = { .min = 18, .max = 26 },
157 .m2 = { .min = 6, .max = 16 },
158 .p = { .min = 4, .max = 128 },
159 .p1 = { .min = 2, .max = 33 },
160 .p2 = { .dot_limit = 165000,
161 .p2_slow = 4, .p2_fast = 4 },
162};
163
Keith Packarde4b36692009-06-05 19:22:17 -0700164static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400165 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200166 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200167 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400168 .m = { .min = 96, .max = 140 },
169 .m1 = { .min = 18, .max = 26 },
170 .m2 = { .min = 6, .max = 16 },
171 .p = { .min = 4, .max = 128 },
172 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700173 .p2 = { .dot_limit = 165000,
174 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700175};
Eric Anholt273e27c2011-03-30 13:01:10 -0700176
Keith Packarde4b36692009-06-05 19:22:17 -0700177static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400178 .dot = { .min = 20000, .max = 400000 },
179 .vco = { .min = 1400000, .max = 2800000 },
180 .n = { .min = 1, .max = 6 },
181 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100182 .m1 = { .min = 8, .max = 18 },
183 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400184 .p = { .min = 5, .max = 80 },
185 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700186 .p2 = { .dot_limit = 200000,
187 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700188};
189
190static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400191 .dot = { .min = 20000, .max = 400000 },
192 .vco = { .min = 1400000, .max = 2800000 },
193 .n = { .min = 1, .max = 6 },
194 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100195 .m1 = { .min = 8, .max = 18 },
196 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400197 .p = { .min = 7, .max = 98 },
198 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700199 .p2 = { .dot_limit = 112000,
200 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700201};
202
Eric Anholt273e27c2011-03-30 13:01:10 -0700203
Keith Packarde4b36692009-06-05 19:22:17 -0700204static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700205 .dot = { .min = 25000, .max = 270000 },
206 .vco = { .min = 1750000, .max = 3500000},
207 .n = { .min = 1, .max = 4 },
208 .m = { .min = 104, .max = 138 },
209 .m1 = { .min = 17, .max = 23 },
210 .m2 = { .min = 5, .max = 11 },
211 .p = { .min = 10, .max = 30 },
212 .p1 = { .min = 1, .max = 3},
213 .p2 = { .dot_limit = 270000,
214 .p2_slow = 10,
215 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800216 },
Keith Packarde4b36692009-06-05 19:22:17 -0700217};
218
219static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700220 .dot = { .min = 22000, .max = 400000 },
221 .vco = { .min = 1750000, .max = 3500000},
222 .n = { .min = 1, .max = 4 },
223 .m = { .min = 104, .max = 138 },
224 .m1 = { .min = 16, .max = 23 },
225 .m2 = { .min = 5, .max = 11 },
226 .p = { .min = 5, .max = 80 },
227 .p1 = { .min = 1, .max = 8},
228 .p2 = { .dot_limit = 165000,
229 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700230};
231
232static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700233 .dot = { .min = 20000, .max = 115000 },
234 .vco = { .min = 1750000, .max = 3500000 },
235 .n = { .min = 1, .max = 3 },
236 .m = { .min = 104, .max = 138 },
237 .m1 = { .min = 17, .max = 23 },
238 .m2 = { .min = 5, .max = 11 },
239 .p = { .min = 28, .max = 112 },
240 .p1 = { .min = 2, .max = 8 },
241 .p2 = { .dot_limit = 0,
242 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800243 },
Keith Packarde4b36692009-06-05 19:22:17 -0700244};
245
246static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700247 .dot = { .min = 80000, .max = 224000 },
248 .vco = { .min = 1750000, .max = 3500000 },
249 .n = { .min = 1, .max = 3 },
250 .m = { .min = 104, .max = 138 },
251 .m1 = { .min = 17, .max = 23 },
252 .m2 = { .min = 5, .max = 11 },
253 .p = { .min = 14, .max = 42 },
254 .p1 = { .min = 2, .max = 6 },
255 .p2 = { .dot_limit = 0,
256 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800257 },
Keith Packarde4b36692009-06-05 19:22:17 -0700258};
259
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500260static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400261 .dot = { .min = 20000, .max = 400000},
262 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700263 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400264 .n = { .min = 3, .max = 6 },
265 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700266 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400267 .m1 = { .min = 0, .max = 0 },
268 .m2 = { .min = 0, .max = 254 },
269 .p = { .min = 5, .max = 80 },
270 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700271 .p2 = { .dot_limit = 200000,
272 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700273};
274
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500275static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400276 .dot = { .min = 20000, .max = 400000 },
277 .vco = { .min = 1700000, .max = 3500000 },
278 .n = { .min = 3, .max = 6 },
279 .m = { .min = 2, .max = 256 },
280 .m1 = { .min = 0, .max = 0 },
281 .m2 = { .min = 0, .max = 254 },
282 .p = { .min = 7, .max = 112 },
283 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700284 .p2 = { .dot_limit = 112000,
285 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700286};
287
Eric Anholt273e27c2011-03-30 13:01:10 -0700288/* Ironlake / Sandybridge
289 *
290 * We calculate clock using (register_value + 2) for N/M1/M2, so here
291 * the range value for them is (actual_value - 2).
292 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800293static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700294 .dot = { .min = 25000, .max = 350000 },
295 .vco = { .min = 1760000, .max = 3510000 },
296 .n = { .min = 1, .max = 5 },
297 .m = { .min = 79, .max = 127 },
298 .m1 = { .min = 12, .max = 22 },
299 .m2 = { .min = 5, .max = 9 },
300 .p = { .min = 5, .max = 80 },
301 .p1 = { .min = 1, .max = 8 },
302 .p2 = { .dot_limit = 225000,
303 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700304};
305
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800306static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700307 .dot = { .min = 25000, .max = 350000 },
308 .vco = { .min = 1760000, .max = 3510000 },
309 .n = { .min = 1, .max = 3 },
310 .m = { .min = 79, .max = 118 },
311 .m1 = { .min = 12, .max = 22 },
312 .m2 = { .min = 5, .max = 9 },
313 .p = { .min = 28, .max = 112 },
314 .p1 = { .min = 2, .max = 8 },
315 .p2 = { .dot_limit = 225000,
316 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800317};
318
319static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700320 .dot = { .min = 25000, .max = 350000 },
321 .vco = { .min = 1760000, .max = 3510000 },
322 .n = { .min = 1, .max = 3 },
323 .m = { .min = 79, .max = 127 },
324 .m1 = { .min = 12, .max = 22 },
325 .m2 = { .min = 5, .max = 9 },
326 .p = { .min = 14, .max = 56 },
327 .p1 = { .min = 2, .max = 8 },
328 .p2 = { .dot_limit = 225000,
329 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800330};
331
Eric Anholt273e27c2011-03-30 13:01:10 -0700332/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800333static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700334 .dot = { .min = 25000, .max = 350000 },
335 .vco = { .min = 1760000, .max = 3510000 },
336 .n = { .min = 1, .max = 2 },
337 .m = { .min = 79, .max = 126 },
338 .m1 = { .min = 12, .max = 22 },
339 .m2 = { .min = 5, .max = 9 },
340 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400341 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700342 .p2 = { .dot_limit = 225000,
343 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800344};
345
346static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700347 .dot = { .min = 25000, .max = 350000 },
348 .vco = { .min = 1760000, .max = 3510000 },
349 .n = { .min = 1, .max = 3 },
350 .m = { .min = 79, .max = 126 },
351 .m1 = { .min = 12, .max = 22 },
352 .m2 = { .min = 5, .max = 9 },
353 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400354 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700355 .p2 = { .dot_limit = 225000,
356 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800357};
358
Ville Syrjälädc730512013-09-24 21:26:30 +0300359static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300360 /*
361 * These are the data rate limits (measured in fast clocks)
362 * since those are the strictest limits we have. The fast
363 * clock and actual rate limits are more relaxed, so checking
364 * them would make no difference.
365 */
366 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200367 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700368 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700369 .m1 = { .min = 2, .max = 3 },
370 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300371 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300372 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700373};
374
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300375static const intel_limit_t intel_limits_chv = {
376 /*
377 * These are the data rate limits (measured in fast clocks)
378 * since those are the strictest limits we have. The fast
379 * clock and actual rate limits are more relaxed, so checking
380 * them would make no difference.
381 */
382 .dot = { .min = 25000 * 5, .max = 540000 * 5},
383 .vco = { .min = 4860000, .max = 6700000 },
384 .n = { .min = 1, .max = 1 },
385 .m1 = { .min = 2, .max = 2 },
386 .m2 = { .min = 24 << 22, .max = 175 << 22 },
387 .p1 = { .min = 2, .max = 4 },
388 .p2 = { .p2_slow = 1, .p2_fast = 14 },
389};
390
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300391static void vlv_clock(int refclk, intel_clock_t *clock)
392{
393 clock->m = clock->m1 * clock->m2;
394 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200395 if (WARN_ON(clock->n == 0 || clock->p == 0))
396 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300397 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
398 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300399}
400
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300401/**
402 * Returns whether any output on the specified pipe is of the specified type
403 */
404static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
405{
406 struct drm_device *dev = crtc->dev;
407 struct intel_encoder *encoder;
408
409 for_each_encoder_on_crtc(dev, crtc, encoder)
410 if (encoder->type == type)
411 return true;
412
413 return false;
414}
415
Chris Wilson1b894b52010-12-14 20:04:54 +0000416static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
417 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800418{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800419 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800420 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800421
422 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100423 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000424 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800425 limit = &intel_limits_ironlake_dual_lvds_100m;
426 else
427 limit = &intel_limits_ironlake_dual_lvds;
428 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000429 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800430 limit = &intel_limits_ironlake_single_lvds_100m;
431 else
432 limit = &intel_limits_ironlake_single_lvds;
433 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200434 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800435 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800436
437 return limit;
438}
439
Ma Ling044c7c42009-03-18 20:13:23 +0800440static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
441{
442 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800443 const intel_limit_t *limit;
444
445 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100446 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700447 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800448 else
Keith Packarde4b36692009-06-05 19:22:17 -0700449 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800450 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
451 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700452 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800453 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700454 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800455 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700456 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800457
458 return limit;
459}
460
Chris Wilson1b894b52010-12-14 20:04:54 +0000461static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800462{
463 struct drm_device *dev = crtc->dev;
464 const intel_limit_t *limit;
465
Eric Anholtbad720f2009-10-22 16:11:14 -0700466 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000467 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800468 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800469 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500470 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800471 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500472 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800473 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500474 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300475 } else if (IS_CHERRYVIEW(dev)) {
476 limit = &intel_limits_chv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700477 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300478 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100479 } else if (!IS_GEN2(dev)) {
480 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
481 limit = &intel_limits_i9xx_lvds;
482 else
483 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800484 } else {
485 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700486 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200487 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700488 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200489 else
490 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800491 }
492 return limit;
493}
494
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500495/* m1 is reserved as 0 in Pineview, n is a ring counter */
496static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800497{
Shaohua Li21778322009-02-23 15:19:16 +0800498 clock->m = clock->m2 + 2;
499 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200500 if (WARN_ON(clock->n == 0 || clock->p == 0))
501 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300502 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
503 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800504}
505
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200506static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
507{
508 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
509}
510
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200511static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800512{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200513 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800514 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200515 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
516 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300517 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
518 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800519}
520
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300521static void chv_clock(int refclk, intel_clock_t *clock)
522{
523 clock->m = clock->m1 * clock->m2;
524 clock->p = clock->p1 * clock->p2;
525 if (WARN_ON(clock->n == 0 || clock->p == 0))
526 return;
527 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
528 clock->n << 22);
529 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
530}
531
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800532#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800533/**
534 * Returns whether the given set of divisors are valid for a given refclk with
535 * the given connectors.
536 */
537
Chris Wilson1b894b52010-12-14 20:04:54 +0000538static bool intel_PLL_is_valid(struct drm_device *dev,
539 const intel_limit_t *limit,
540 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800541{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300542 if (clock->n < limit->n.min || limit->n.max < clock->n)
543 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800544 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400545 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800546 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400547 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800548 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400549 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300550
551 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
552 if (clock->m1 <= clock->m2)
553 INTELPllInvalid("m1 <= m2\n");
554
555 if (!IS_VALLEYVIEW(dev)) {
556 if (clock->p < limit->p.min || limit->p.max < clock->p)
557 INTELPllInvalid("p out of range\n");
558 if (clock->m < limit->m.min || limit->m.max < clock->m)
559 INTELPllInvalid("m out of range\n");
560 }
561
Jesse Barnes79e53942008-11-07 14:24:08 -0800562 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400563 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800564 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
565 * connector, etc., rather than just a single range.
566 */
567 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400568 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800569
570 return true;
571}
572
Ma Lingd4906092009-03-18 20:13:27 +0800573static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200574i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800575 int target, int refclk, intel_clock_t *match_clock,
576 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800577{
578 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800579 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800580 int err = target;
581
Daniel Vettera210b022012-11-26 17:22:08 +0100582 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800583 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100584 * For LVDS just rely on its current settings for dual-channel.
585 * We haven't figured out how to reliably set up different
586 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800587 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100588 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800589 clock.p2 = limit->p2.p2_fast;
590 else
591 clock.p2 = limit->p2.p2_slow;
592 } else {
593 if (target < limit->p2.dot_limit)
594 clock.p2 = limit->p2.p2_slow;
595 else
596 clock.p2 = limit->p2.p2_fast;
597 }
598
Akshay Joshi0206e352011-08-16 15:34:10 -0400599 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800600
Zhao Yakui42158662009-11-20 11:24:18 +0800601 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
602 clock.m1++) {
603 for (clock.m2 = limit->m2.min;
604 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200605 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800606 break;
607 for (clock.n = limit->n.min;
608 clock.n <= limit->n.max; clock.n++) {
609 for (clock.p1 = limit->p1.min;
610 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800611 int this_err;
612
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200613 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000614 if (!intel_PLL_is_valid(dev, limit,
615 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800617 if (match_clock &&
618 clock.p != match_clock->p)
619 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800620
621 this_err = abs(clock.dot - target);
622 if (this_err < err) {
623 *best_clock = clock;
624 err = this_err;
625 }
626 }
627 }
628 }
629 }
630
631 return (err != target);
632}
633
Ma Lingd4906092009-03-18 20:13:27 +0800634static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200635pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
636 int target, int refclk, intel_clock_t *match_clock,
637 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200638{
639 struct drm_device *dev = crtc->dev;
640 intel_clock_t clock;
641 int err = target;
642
643 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
644 /*
645 * For LVDS just rely on its current settings for dual-channel.
646 * We haven't figured out how to reliably set up different
647 * single/dual channel state, if we even can.
648 */
649 if (intel_is_dual_link_lvds(dev))
650 clock.p2 = limit->p2.p2_fast;
651 else
652 clock.p2 = limit->p2.p2_slow;
653 } else {
654 if (target < limit->p2.dot_limit)
655 clock.p2 = limit->p2.p2_slow;
656 else
657 clock.p2 = limit->p2.p2_fast;
658 }
659
660 memset(best_clock, 0, sizeof(*best_clock));
661
662 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
663 clock.m1++) {
664 for (clock.m2 = limit->m2.min;
665 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200666 for (clock.n = limit->n.min;
667 clock.n <= limit->n.max; clock.n++) {
668 for (clock.p1 = limit->p1.min;
669 clock.p1 <= limit->p1.max; clock.p1++) {
670 int this_err;
671
672 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800673 if (!intel_PLL_is_valid(dev, limit,
674 &clock))
675 continue;
676 if (match_clock &&
677 clock.p != match_clock->p)
678 continue;
679
680 this_err = abs(clock.dot - target);
681 if (this_err < err) {
682 *best_clock = clock;
683 err = this_err;
684 }
685 }
686 }
687 }
688 }
689
690 return (err != target);
691}
692
Ma Lingd4906092009-03-18 20:13:27 +0800693static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200694g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
695 int target, int refclk, intel_clock_t *match_clock,
696 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800697{
698 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800699 intel_clock_t clock;
700 int max_n;
701 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400702 /* approximately equals target * 0.00585 */
703 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800704 found = false;
705
706 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100707 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800708 clock.p2 = limit->p2.p2_fast;
709 else
710 clock.p2 = limit->p2.p2_slow;
711 } else {
712 if (target < limit->p2.dot_limit)
713 clock.p2 = limit->p2.p2_slow;
714 else
715 clock.p2 = limit->p2.p2_fast;
716 }
717
718 memset(best_clock, 0, sizeof(*best_clock));
719 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200720 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800721 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200722 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800723 for (clock.m1 = limit->m1.max;
724 clock.m1 >= limit->m1.min; clock.m1--) {
725 for (clock.m2 = limit->m2.max;
726 clock.m2 >= limit->m2.min; clock.m2--) {
727 for (clock.p1 = limit->p1.max;
728 clock.p1 >= limit->p1.min; clock.p1--) {
729 int this_err;
730
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200731 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000732 if (!intel_PLL_is_valid(dev, limit,
733 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800734 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000735
736 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800737 if (this_err < err_most) {
738 *best_clock = clock;
739 err_most = this_err;
740 max_n = clock.n;
741 found = true;
742 }
743 }
744 }
745 }
746 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800747 return found;
748}
Ma Lingd4906092009-03-18 20:13:27 +0800749
Zhenyu Wang2c072452009-06-05 15:38:42 +0800750static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200751vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
752 int target, int refclk, intel_clock_t *match_clock,
753 intel_clock_t *best_clock)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700754{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300755 struct drm_device *dev = crtc->dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300756 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300757 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300758 /* min update 19.2 MHz */
759 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300760 bool found = false;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700761
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300762 target *= 5; /* fast clock */
763
764 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700765
766 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300767 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300768 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300769 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300770 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300771 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700772 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300773 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300774 unsigned int ppm, diff;
775
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300776 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
777 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300778
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300779 vlv_clock(refclk, &clock);
780
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300781 if (!intel_PLL_is_valid(dev, limit,
782 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300783 continue;
784
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300785 diff = abs(clock.dot - target);
786 ppm = div_u64(1000000ULL * diff, target);
787
788 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300789 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300790 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300791 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300792 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300793
Ville Syrjäläc6861222013-09-24 21:26:21 +0300794 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300795 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300796 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300797 found = true;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700798 }
799 }
800 }
801 }
802 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700803
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300804 return found;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700805}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700806
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300807static bool
808chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
809 int target, int refclk, intel_clock_t *match_clock,
810 intel_clock_t *best_clock)
811{
812 struct drm_device *dev = crtc->dev;
813 intel_clock_t clock;
814 uint64_t m2;
815 int found = false;
816
817 memset(best_clock, 0, sizeof(*best_clock));
818
819 /*
820 * Based on hardware doc, the n always set to 1, and m1 always
821 * set to 2. If requires to support 200Mhz refclk, we need to
822 * revisit this because n may not 1 anymore.
823 */
824 clock.n = 1, clock.m1 = 2;
825 target *= 5; /* fast clock */
826
827 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
828 for (clock.p2 = limit->p2.p2_fast;
829 clock.p2 >= limit->p2.p2_slow;
830 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
831
832 clock.p = clock.p1 * clock.p2;
833
834 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
835 clock.n) << 22, refclk * clock.m1);
836
837 if (m2 > INT_MAX/clock.m1)
838 continue;
839
840 clock.m2 = m2;
841
842 chv_clock(refclk, &clock);
843
844 if (!intel_PLL_is_valid(dev, limit, &clock))
845 continue;
846
847 /* based on hardware requirement, prefer bigger p
848 */
849 if (clock.p > best_clock->p) {
850 *best_clock = clock;
851 found = true;
852 }
853 }
854 }
855
856 return found;
857}
858
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300859bool intel_crtc_active(struct drm_crtc *crtc)
860{
861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
862
863 /* Be paranoid as we can arrive here with only partial
864 * state retrieved from the hardware during setup.
865 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100866 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300867 * as Haswell has gained clock readout/fastboot support.
868 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000869 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300870 * properly reconstruct framebuffers.
871 */
Matt Roperf4510a22014-04-01 15:22:40 -0700872 return intel_crtc->active && crtc->primary->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100873 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300874}
875
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200876enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
877 enum pipe pipe)
878{
879 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
881
Daniel Vetter3b117c82013-04-17 20:15:07 +0200882 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200883}
884
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200885static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
Paulo Zanonia928d532012-05-04 17:18:15 -0300886{
887 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200888 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300889
890 frame = I915_READ(frame_reg);
891
892 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
Jesse Barnes93937072014-04-04 16:12:09 -0700893 WARN(1, "vblank wait timed out\n");
Paulo Zanonia928d532012-05-04 17:18:15 -0300894}
895
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700896/**
897 * intel_wait_for_vblank - wait for vblank on a given pipe
898 * @dev: drm device
899 * @pipe: pipe to wait for
900 *
901 * Wait for vblank to occur on a given pipe. Needed for various bits of
902 * mode setting code.
903 */
904void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800905{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700906 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800907 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700908
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200909 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
910 g4x_wait_for_vblank(dev, pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300911 return;
912 }
913
Chris Wilson300387c2010-09-05 20:25:43 +0100914 /* Clear existing vblank status. Note this will clear any other
915 * sticky status fields as well.
916 *
917 * This races with i915_driver_irq_handler() with the result
918 * that either function could miss a vblank event. Here it is not
919 * fatal, as we will either wait upon the next vblank interrupt or
920 * timeout. Generally speaking intel_wait_for_vblank() is only
921 * called during modeset at which time the GPU should be idle and
922 * should *not* be performing page flips and thus not waiting on
923 * vblanks...
924 * Currently, the result of us stealing a vblank from the irq
925 * handler is that a single frame will be skipped during swapbuffers.
926 */
927 I915_WRITE(pipestat_reg,
928 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
929
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700930 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100931 if (wait_for(I915_READ(pipestat_reg) &
932 PIPE_VBLANK_INTERRUPT_STATUS,
933 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700934 DRM_DEBUG_KMS("vblank wait timed out\n");
935}
936
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300937static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
938{
939 struct drm_i915_private *dev_priv = dev->dev_private;
940 u32 reg = PIPEDSL(pipe);
941 u32 line1, line2;
942 u32 line_mask;
943
944 if (IS_GEN2(dev))
945 line_mask = DSL_LINEMASK_GEN2;
946 else
947 line_mask = DSL_LINEMASK_GEN3;
948
949 line1 = I915_READ(reg) & line_mask;
950 mdelay(5);
951 line2 = I915_READ(reg) & line_mask;
952
953 return line1 == line2;
954}
955
Keith Packardab7ad7f2010-10-03 00:33:06 -0700956/*
957 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700958 * @dev: drm device
959 * @pipe: pipe to wait for
960 *
961 * After disabling a pipe, we can't wait for vblank in the usual way,
962 * spinning on the vblank interrupt status bit, since we won't actually
963 * see an interrupt when the pipe is disabled.
964 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700965 * On Gen4 and above:
966 * wait for the pipe register state bit to turn off
967 *
968 * Otherwise:
969 * wait for the display line value to settle (it usually
970 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100971 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700972 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100973void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700974{
975 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200976 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
977 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700978
Keith Packardab7ad7f2010-10-03 00:33:06 -0700979 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200980 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700981
Keith Packardab7ad7f2010-10-03 00:33:06 -0700982 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100983 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
984 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200985 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700986 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700987 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300988 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200989 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700990 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800991}
992
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000993/*
994 * ibx_digital_port_connected - is the specified port connected?
995 * @dev_priv: i915 private structure
996 * @port: the port to test
997 *
998 * Returns true if @port is connected, false otherwise.
999 */
1000bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1001 struct intel_digital_port *port)
1002{
1003 u32 bit;
1004
Damien Lespiauc36346e2012-12-13 16:09:03 +00001005 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001006 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001007 case PORT_B:
1008 bit = SDE_PORTB_HOTPLUG;
1009 break;
1010 case PORT_C:
1011 bit = SDE_PORTC_HOTPLUG;
1012 break;
1013 case PORT_D:
1014 bit = SDE_PORTD_HOTPLUG;
1015 break;
1016 default:
1017 return true;
1018 }
1019 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001020 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001021 case PORT_B:
1022 bit = SDE_PORTB_HOTPLUG_CPT;
1023 break;
1024 case PORT_C:
1025 bit = SDE_PORTC_HOTPLUG_CPT;
1026 break;
1027 case PORT_D:
1028 bit = SDE_PORTD_HOTPLUG_CPT;
1029 break;
1030 default:
1031 return true;
1032 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001033 }
1034
1035 return I915_READ(SDEISR) & bit;
1036}
1037
Jesse Barnesb24e7172011-01-04 15:09:30 -08001038static const char *state_string(bool enabled)
1039{
1040 return enabled ? "on" : "off";
1041}
1042
1043/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001044void assert_pll(struct drm_i915_private *dev_priv,
1045 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001046{
1047 int reg;
1048 u32 val;
1049 bool cur_state;
1050
1051 reg = DPLL(pipe);
1052 val = I915_READ(reg);
1053 cur_state = !!(val & DPLL_VCO_ENABLE);
1054 WARN(cur_state != state,
1055 "PLL state assertion failure (expected %s, current %s)\n",
1056 state_string(state), state_string(cur_state));
1057}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001058
Jani Nikula23538ef2013-08-27 15:12:22 +03001059/* XXX: the dsi pll is shared between MIPI DSI ports */
1060static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1061{
1062 u32 val;
1063 bool cur_state;
1064
1065 mutex_lock(&dev_priv->dpio_lock);
1066 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1067 mutex_unlock(&dev_priv->dpio_lock);
1068
1069 cur_state = val & DSI_PLL_VCO_EN;
1070 WARN(cur_state != state,
1071 "DSI PLL state assertion failure (expected %s, current %s)\n",
1072 state_string(state), state_string(cur_state));
1073}
1074#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1075#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1076
Daniel Vetter55607e82013-06-16 21:42:39 +02001077struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001078intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001079{
Daniel Vettere2b78262013-06-07 23:10:03 +02001080 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1081
Daniel Vettera43f6e02013-06-07 23:10:32 +02001082 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001083 return NULL;
1084
Daniel Vettera43f6e02013-06-07 23:10:32 +02001085 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001086}
1087
Jesse Barnesb24e7172011-01-04 15:09:30 -08001088/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001089void assert_shared_dpll(struct drm_i915_private *dev_priv,
1090 struct intel_shared_dpll *pll,
1091 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001092{
Jesse Barnes040484a2011-01-03 12:14:26 -08001093 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001094 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001095
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001096 if (HAS_PCH_LPT(dev_priv->dev)) {
1097 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1098 return;
1099 }
1100
Chris Wilson92b27b02012-05-20 18:10:50 +01001101 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001102 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001103 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001104
Daniel Vetter53589012013-06-05 13:34:16 +02001105 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +01001106 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001107 "%s assertion failure (expected %s, current %s)\n",
1108 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001109}
Jesse Barnes040484a2011-01-03 12:14:26 -08001110
1111static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1112 enum pipe pipe, bool state)
1113{
1114 int reg;
1115 u32 val;
1116 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001117 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1118 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001119
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001120 if (HAS_DDI(dev_priv->dev)) {
1121 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001122 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001123 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001124 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001125 } else {
1126 reg = FDI_TX_CTL(pipe);
1127 val = I915_READ(reg);
1128 cur_state = !!(val & FDI_TX_ENABLE);
1129 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001130 WARN(cur_state != state,
1131 "FDI TX state assertion failure (expected %s, current %s)\n",
1132 state_string(state), state_string(cur_state));
1133}
1134#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1135#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1136
1137static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1138 enum pipe pipe, bool state)
1139{
1140 int reg;
1141 u32 val;
1142 bool cur_state;
1143
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001144 reg = FDI_RX_CTL(pipe);
1145 val = I915_READ(reg);
1146 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001147 WARN(cur_state != state,
1148 "FDI RX state assertion failure (expected %s, current %s)\n",
1149 state_string(state), state_string(cur_state));
1150}
1151#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1152#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1153
1154static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1155 enum pipe pipe)
1156{
1157 int reg;
1158 u32 val;
1159
1160 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001161 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001162 return;
1163
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001164 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001165 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001166 return;
1167
Jesse Barnes040484a2011-01-03 12:14:26 -08001168 reg = FDI_TX_CTL(pipe);
1169 val = I915_READ(reg);
1170 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1171}
1172
Daniel Vetter55607e82013-06-16 21:42:39 +02001173void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1174 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001175{
1176 int reg;
1177 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001178 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001179
1180 reg = FDI_RX_CTL(pipe);
1181 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001182 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1183 WARN(cur_state != state,
1184 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1185 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001186}
1187
Jesse Barnesea0760c2011-01-04 15:09:32 -08001188static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1189 enum pipe pipe)
1190{
1191 int pp_reg, lvds_reg;
1192 u32 val;
1193 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001194 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001195
1196 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1197 pp_reg = PCH_PP_CONTROL;
1198 lvds_reg = PCH_LVDS;
1199 } else {
1200 pp_reg = PP_CONTROL;
1201 lvds_reg = LVDS;
1202 }
1203
1204 val = I915_READ(pp_reg);
1205 if (!(val & PANEL_POWER_ON) ||
1206 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1207 locked = false;
1208
1209 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1210 panel_pipe = PIPE_B;
1211
1212 WARN(panel_pipe == pipe && locked,
1213 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001214 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001215}
1216
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001217static void assert_cursor(struct drm_i915_private *dev_priv,
1218 enum pipe pipe, bool state)
1219{
1220 struct drm_device *dev = dev_priv->dev;
1221 bool cur_state;
1222
Paulo Zanonid9d82082014-02-27 16:30:56 -03001223 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001224 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001225 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001226 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001227
1228 WARN(cur_state != state,
1229 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1230 pipe_name(pipe), state_string(state), state_string(cur_state));
1231}
1232#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1233#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1234
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001235void assert_pipe(struct drm_i915_private *dev_priv,
1236 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001237{
1238 int reg;
1239 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001240 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001241 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1242 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001243
Daniel Vetter8e636782012-01-22 01:36:48 +01001244 /* if we need the pipe A quirk it must be always on */
1245 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1246 state = true;
1247
Imre Deakda7e29b2014-02-18 00:02:02 +02001248 if (!intel_display_power_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001249 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001250 cur_state = false;
1251 } else {
1252 reg = PIPECONF(cpu_transcoder);
1253 val = I915_READ(reg);
1254 cur_state = !!(val & PIPECONF_ENABLE);
1255 }
1256
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001257 WARN(cur_state != state,
1258 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001259 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001260}
1261
Chris Wilson931872f2012-01-16 23:01:13 +00001262static void assert_plane(struct drm_i915_private *dev_priv,
1263 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001264{
1265 int reg;
1266 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001267 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001268
1269 reg = DSPCNTR(plane);
1270 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001271 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1272 WARN(cur_state != state,
1273 "plane %c assertion failure (expected %s, current %s)\n",
1274 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001275}
1276
Chris Wilson931872f2012-01-16 23:01:13 +00001277#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1278#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1279
Jesse Barnesb24e7172011-01-04 15:09:30 -08001280static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1281 enum pipe pipe)
1282{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001283 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001284 int reg, i;
1285 u32 val;
1286 int cur_pipe;
1287
Ville Syrjälä653e1022013-06-04 13:49:05 +03001288 /* Primary planes are fixed to pipes on gen4+ */
1289 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001290 reg = DSPCNTR(pipe);
1291 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001292 WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001293 "plane %c assertion failure, should be disabled but not\n",
1294 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001295 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001296 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001297
Jesse Barnesb24e7172011-01-04 15:09:30 -08001298 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001299 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001300 reg = DSPCNTR(i);
1301 val = I915_READ(reg);
1302 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1303 DISPPLANE_SEL_PIPE_SHIFT;
1304 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001305 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1306 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001307 }
1308}
1309
Jesse Barnes19332d72013-03-28 09:55:38 -07001310static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1311 enum pipe pipe)
1312{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001313 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001314 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001315 u32 val;
1316
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001317 if (IS_VALLEYVIEW(dev)) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001318 for_each_sprite(pipe, sprite) {
1319 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001320 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001321 WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001322 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001323 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001324 }
1325 } else if (INTEL_INFO(dev)->gen >= 7) {
1326 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001327 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001328 WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001329 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001330 plane_name(pipe), pipe_name(pipe));
1331 } else if (INTEL_INFO(dev)->gen >= 5) {
1332 reg = DVSCNTR(pipe);
1333 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001334 WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001335 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1336 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001337 }
1338}
1339
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001340static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001341{
1342 u32 val;
1343 bool enabled;
1344
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001345 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001346
Jesse Barnes92f25842011-01-04 15:09:34 -08001347 val = I915_READ(PCH_DREF_CONTROL);
1348 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1349 DREF_SUPERSPREAD_SOURCE_MASK));
1350 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1351}
1352
Daniel Vetterab9412b2013-05-03 11:49:46 +02001353static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1354 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001355{
1356 int reg;
1357 u32 val;
1358 bool enabled;
1359
Daniel Vetterab9412b2013-05-03 11:49:46 +02001360 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001361 val = I915_READ(reg);
1362 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001363 WARN(enabled,
1364 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1365 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001366}
1367
Keith Packard4e634382011-08-06 10:39:45 -07001368static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1369 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001370{
1371 if ((val & DP_PORT_EN) == 0)
1372 return false;
1373
1374 if (HAS_PCH_CPT(dev_priv->dev)) {
1375 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1376 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1377 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1378 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001379 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1380 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1381 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001382 } else {
1383 if ((val & DP_PIPE_MASK) != (pipe << 30))
1384 return false;
1385 }
1386 return true;
1387}
1388
Keith Packard1519b992011-08-06 10:35:34 -07001389static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1390 enum pipe pipe, u32 val)
1391{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001392 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001393 return false;
1394
1395 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001396 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001397 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001398 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1399 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1400 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001401 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001402 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001403 return false;
1404 }
1405 return true;
1406}
1407
1408static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1409 enum pipe pipe, u32 val)
1410{
1411 if ((val & LVDS_PORT_EN) == 0)
1412 return false;
1413
1414 if (HAS_PCH_CPT(dev_priv->dev)) {
1415 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1416 return false;
1417 } else {
1418 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1419 return false;
1420 }
1421 return true;
1422}
1423
1424static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1425 enum pipe pipe, u32 val)
1426{
1427 if ((val & ADPA_DAC_ENABLE) == 0)
1428 return false;
1429 if (HAS_PCH_CPT(dev_priv->dev)) {
1430 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1431 return false;
1432 } else {
1433 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1434 return false;
1435 }
1436 return true;
1437}
1438
Jesse Barnes291906f2011-02-02 12:28:03 -08001439static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001440 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001441{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001442 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001443 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001444 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001445 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001446
Daniel Vetter75c5da22012-09-10 21:58:29 +02001447 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1448 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001449 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001450}
1451
1452static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1453 enum pipe pipe, int reg)
1454{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001455 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001456 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001457 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001458 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001459
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001460 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001461 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001462 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001463}
1464
1465static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1466 enum pipe pipe)
1467{
1468 int reg;
1469 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001470
Keith Packardf0575e92011-07-25 22:12:43 -07001471 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1472 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1473 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001474
1475 reg = PCH_ADPA;
1476 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001477 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001478 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001479 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001480
1481 reg = PCH_LVDS;
1482 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001483 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001484 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001485 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001486
Paulo Zanonie2debe92013-02-18 19:00:27 -03001487 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1488 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1489 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001490}
1491
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001492static void intel_init_dpio(struct drm_device *dev)
1493{
1494 struct drm_i915_private *dev_priv = dev->dev_private;
1495
1496 if (!IS_VALLEYVIEW(dev))
1497 return;
1498
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001499 /*
1500 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1501 * CHV x1 PHY (DP/HDMI D)
1502 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1503 */
1504 if (IS_CHERRYVIEW(dev)) {
1505 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1506 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1507 } else {
1508 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1509 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001510}
1511
1512static void intel_reset_dpio(struct drm_device *dev)
1513{
1514 struct drm_i915_private *dev_priv = dev->dev_private;
1515
1516 if (!IS_VALLEYVIEW(dev))
1517 return;
1518
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001519 if (IS_CHERRYVIEW(dev)) {
1520 enum dpio_phy phy;
1521 u32 val;
1522
1523 for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
1524 /* Poll for phypwrgood signal */
1525 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
1526 PHY_POWERGOOD(phy), 1))
1527 DRM_ERROR("Display PHY %d is not power up\n", phy);
1528
1529 /*
1530 * Deassert common lane reset for PHY.
1531 *
1532 * This should only be done on init and resume from S3
1533 * with both PLLs disabled, or we risk losing DPIO and
1534 * PLL synchronization.
1535 */
1536 val = I915_READ(DISPLAY_PHY_CONTROL);
1537 I915_WRITE(DISPLAY_PHY_CONTROL,
1538 PHY_COM_LANE_RESET_DEASSERT(phy, val));
1539 }
1540
1541 } else {
1542 /*
Jesse Barnes57021052014-05-23 13:16:40 -07001543 * If DPIO has already been reset, e.g. by BIOS, just skip all
1544 * this.
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001545 */
Jesse Barnes57021052014-05-23 13:16:40 -07001546 if (I915_READ(DPIO_CTL) & DPIO_CMNRST)
1547 return;
1548
1549 /*
1550 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
1551 * Need to assert and de-assert PHY SB reset by gating the
1552 * common lane power, then un-gating it.
1553 * Simply ungating isn't enough to reset the PHY enough to get
1554 * ports and lanes running.
1555 */
1556 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1557 false);
1558 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1559 true);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001560 }
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001561}
1562
Daniel Vetter426115c2013-07-11 22:13:42 +02001563static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001564{
Daniel Vetter426115c2013-07-11 22:13:42 +02001565 struct drm_device *dev = crtc->base.dev;
1566 struct drm_i915_private *dev_priv = dev->dev_private;
1567 int reg = DPLL(crtc->pipe);
1568 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001569
Daniel Vetter426115c2013-07-11 22:13:42 +02001570 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001571
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001572 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001573 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1574
1575 /* PLL is protected by panel, make sure we can write it */
1576 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001577 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001578
Daniel Vetter426115c2013-07-11 22:13:42 +02001579 I915_WRITE(reg, dpll);
1580 POSTING_READ(reg);
1581 udelay(150);
1582
1583 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1584 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1585
1586 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1587 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001588
1589 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001590 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001591 POSTING_READ(reg);
1592 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001593 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001594 POSTING_READ(reg);
1595 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001596 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001597 POSTING_READ(reg);
1598 udelay(150); /* wait for warmup */
1599}
1600
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001601static void chv_enable_pll(struct intel_crtc *crtc)
1602{
1603 struct drm_device *dev = crtc->base.dev;
1604 struct drm_i915_private *dev_priv = dev->dev_private;
1605 int pipe = crtc->pipe;
1606 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001607 u32 tmp;
1608
1609 assert_pipe_disabled(dev_priv, crtc->pipe);
1610
1611 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1612
1613 mutex_lock(&dev_priv->dpio_lock);
1614
1615 /* Enable back the 10bit clock to display controller */
1616 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1617 tmp |= DPIO_DCLKP_EN;
1618 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1619
1620 /*
1621 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1622 */
1623 udelay(1);
1624
1625 /* Enable PLL */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001626 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001627
1628 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001629 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001630 DRM_ERROR("PLL %d failed to lock\n", pipe);
1631
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001632 /* not sure when this should be written */
1633 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1634 POSTING_READ(DPLL_MD(pipe));
1635
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001636 mutex_unlock(&dev_priv->dpio_lock);
1637}
1638
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001639static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001640{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001641 struct drm_device *dev = crtc->base.dev;
1642 struct drm_i915_private *dev_priv = dev->dev_private;
1643 int reg = DPLL(crtc->pipe);
1644 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001645
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001646 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001647
1648 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001649 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001650
1651 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001652 if (IS_MOBILE(dev) && !IS_I830(dev))
1653 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001654
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001655 I915_WRITE(reg, dpll);
1656
1657 /* Wait for the clocks to stabilize. */
1658 POSTING_READ(reg);
1659 udelay(150);
1660
1661 if (INTEL_INFO(dev)->gen >= 4) {
1662 I915_WRITE(DPLL_MD(crtc->pipe),
1663 crtc->config.dpll_hw_state.dpll_md);
1664 } else {
1665 /* The pixel multiplier can only be updated once the
1666 * DPLL is enabled and the clocks are stable.
1667 *
1668 * So write it again.
1669 */
1670 I915_WRITE(reg, dpll);
1671 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001672
1673 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001674 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001675 POSTING_READ(reg);
1676 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001677 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001678 POSTING_READ(reg);
1679 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001680 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001681 POSTING_READ(reg);
1682 udelay(150); /* wait for warmup */
1683}
1684
1685/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001686 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001687 * @dev_priv: i915 private structure
1688 * @pipe: pipe PLL to disable
1689 *
1690 * Disable the PLL for @pipe, making sure the pipe is off first.
1691 *
1692 * Note! This is for pre-ILK only.
1693 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001694static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001695{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001696 /* Don't disable pipe A or pipe A PLLs if needed */
1697 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1698 return;
1699
1700 /* Make sure the pipe isn't still relying on us */
1701 assert_pipe_disabled(dev_priv, pipe);
1702
Daniel Vetter50b44a42013-06-05 13:34:33 +02001703 I915_WRITE(DPLL(pipe), 0);
1704 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001705}
1706
Jesse Barnesf6071162013-10-01 10:41:38 -07001707static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1708{
1709 u32 val = 0;
1710
1711 /* Make sure the pipe isn't still relying on us */
1712 assert_pipe_disabled(dev_priv, pipe);
1713
Imre Deake5cbfbf2014-01-09 17:08:16 +02001714 /*
1715 * Leave integrated clock source and reference clock enabled for pipe B.
1716 * The latter is needed for VGA hotplug / manual detection.
1717 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001718 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001719 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001720 I915_WRITE(DPLL(pipe), val);
1721 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001722
1723}
1724
1725static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1726{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001727 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001728 u32 val;
1729
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001730 /* Make sure the pipe isn't still relying on us */
1731 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001732
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001733 /* Set PLL en = 0 */
1734 val = DPLL_SSC_REF_CLOCK_CHV;
1735 if (pipe != PIPE_A)
1736 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1737 I915_WRITE(DPLL(pipe), val);
1738 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001739
1740 mutex_lock(&dev_priv->dpio_lock);
1741
1742 /* Disable 10bit clock to display controller */
1743 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1744 val &= ~DPIO_DCLKP_EN;
1745 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1746
Ville Syrjälä61407f62014-05-27 16:32:55 +03001747 /* disable left/right clock distribution */
1748 if (pipe != PIPE_B) {
1749 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1750 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1751 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1752 } else {
1753 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1754 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1755 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1756 }
1757
Ville Syrjäläd7520482014-04-09 13:28:59 +03001758 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001759}
1760
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001761void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1762 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001763{
1764 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001765 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001766
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001767 switch (dport->port) {
1768 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001769 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001770 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001771 break;
1772 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001773 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001774 dpll_reg = DPLL(0);
1775 break;
1776 case PORT_D:
1777 port_mask = DPLL_PORTD_READY_MASK;
1778 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001779 break;
1780 default:
1781 BUG();
1782 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001783
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001784 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001785 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001786 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001787}
1788
Daniel Vetterb14b1052014-04-24 23:55:13 +02001789static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1790{
1791 struct drm_device *dev = crtc->base.dev;
1792 struct drm_i915_private *dev_priv = dev->dev_private;
1793 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1794
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001795 if (WARN_ON(pll == NULL))
1796 return;
1797
Daniel Vetterb14b1052014-04-24 23:55:13 +02001798 WARN_ON(!pll->refcount);
1799 if (pll->active == 0) {
1800 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1801 WARN_ON(pll->on);
1802 assert_shared_dpll_disabled(dev_priv, pll);
1803
1804 pll->mode_set(dev_priv, pll);
1805 }
1806}
1807
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001808/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001809 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001810 * @dev_priv: i915 private structure
1811 * @pipe: pipe PLL to enable
1812 *
1813 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1814 * drives the transcoder clock.
1815 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001816static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001817{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001818 struct drm_device *dev = crtc->base.dev;
1819 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001820 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001821
Daniel Vetter87a875b2013-06-05 13:34:19 +02001822 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001823 return;
1824
1825 if (WARN_ON(pll->refcount == 0))
1826 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001827
Daniel Vetter46edb022013-06-05 13:34:12 +02001828 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1829 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001830 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001831
Daniel Vettercdbd2312013-06-05 13:34:03 +02001832 if (pll->active++) {
1833 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001834 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001835 return;
1836 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001837 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001838
Daniel Vetter46edb022013-06-05 13:34:12 +02001839 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001840 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001841 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001842}
1843
Daniel Vettere2b78262013-06-07 23:10:03 +02001844static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001845{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001846 struct drm_device *dev = crtc->base.dev;
1847 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001848 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001849
Jesse Barnes92f25842011-01-04 15:09:34 -08001850 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001851 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001852 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001853 return;
1854
Chris Wilson48da64a2012-05-13 20:16:12 +01001855 if (WARN_ON(pll->refcount == 0))
1856 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001857
Daniel Vetter46edb022013-06-05 13:34:12 +02001858 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1859 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001860 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001861
Chris Wilson48da64a2012-05-13 20:16:12 +01001862 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001863 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001864 return;
1865 }
1866
Daniel Vettere9d69442013-06-05 13:34:15 +02001867 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001868 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001869 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001870 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001871
Daniel Vetter46edb022013-06-05 13:34:12 +02001872 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001873 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001874 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001875}
1876
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001877static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1878 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001879{
Daniel Vetter23670b322012-11-01 09:15:30 +01001880 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001881 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001883 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001884
1885 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001886 BUG_ON(INTEL_INFO(dev)->gen < 5);
Jesse Barnes040484a2011-01-03 12:14:26 -08001887
1888 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001889 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001890 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001891
1892 /* FDI must be feeding us bits for PCH ports */
1893 assert_fdi_tx_enabled(dev_priv, pipe);
1894 assert_fdi_rx_enabled(dev_priv, pipe);
1895
Daniel Vetter23670b322012-11-01 09:15:30 +01001896 if (HAS_PCH_CPT(dev)) {
1897 /* Workaround: Set the timing override bit before enabling the
1898 * pch transcoder. */
1899 reg = TRANS_CHICKEN2(pipe);
1900 val = I915_READ(reg);
1901 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1902 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001903 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001904
Daniel Vetterab9412b2013-05-03 11:49:46 +02001905 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001906 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001907 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001908
1909 if (HAS_PCH_IBX(dev_priv->dev)) {
1910 /*
1911 * make the BPC in transcoder be consistent with
1912 * that in pipeconf reg.
1913 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001914 val &= ~PIPECONF_BPC_MASK;
1915 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001916 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001917
1918 val &= ~TRANS_INTERLACE_MASK;
1919 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001920 if (HAS_PCH_IBX(dev_priv->dev) &&
1921 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1922 val |= TRANS_LEGACY_INTERLACED_ILK;
1923 else
1924 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001925 else
1926 val |= TRANS_PROGRESSIVE;
1927
Jesse Barnes040484a2011-01-03 12:14:26 -08001928 I915_WRITE(reg, val | TRANS_ENABLE);
1929 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001930 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001931}
1932
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001933static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001934 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001935{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001936 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001937
1938 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001939 BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001940
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001941 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001942 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001943 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001944
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001945 /* Workaround: set timing override bit. */
1946 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001947 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001948 I915_WRITE(_TRANSA_CHICKEN2, val);
1949
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001950 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001951 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001952
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001953 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1954 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001955 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001956 else
1957 val |= TRANS_PROGRESSIVE;
1958
Daniel Vetterab9412b2013-05-03 11:49:46 +02001959 I915_WRITE(LPT_TRANSCONF, val);
1960 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001961 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001962}
1963
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001964static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1965 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001966{
Daniel Vetter23670b322012-11-01 09:15:30 +01001967 struct drm_device *dev = dev_priv->dev;
1968 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001969
1970 /* FDI relies on the transcoder */
1971 assert_fdi_tx_disabled(dev_priv, pipe);
1972 assert_fdi_rx_disabled(dev_priv, pipe);
1973
Jesse Barnes291906f2011-02-02 12:28:03 -08001974 /* Ports must be off as well */
1975 assert_pch_ports_disabled(dev_priv, pipe);
1976
Daniel Vetterab9412b2013-05-03 11:49:46 +02001977 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001978 val = I915_READ(reg);
1979 val &= ~TRANS_ENABLE;
1980 I915_WRITE(reg, val);
1981 /* wait for PCH transcoder off, transcoder state */
1982 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001983 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001984
1985 if (!HAS_PCH_IBX(dev)) {
1986 /* Workaround: Clear the timing override chicken bit again. */
1987 reg = TRANS_CHICKEN2(pipe);
1988 val = I915_READ(reg);
1989 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1990 I915_WRITE(reg, val);
1991 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001992}
1993
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001994static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001995{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001996 u32 val;
1997
Daniel Vetterab9412b2013-05-03 11:49:46 +02001998 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001999 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002000 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002001 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002002 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002003 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002004
2005 /* Workaround: clear timing override bit. */
2006 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002007 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002008 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002009}
2010
2011/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002012 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002013 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002014 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002015 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002016 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002017 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002018static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002019{
Paulo Zanoni03722642014-01-17 13:51:09 -02002020 struct drm_device *dev = crtc->base.dev;
2021 struct drm_i915_private *dev_priv = dev->dev_private;
2022 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002023 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2024 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002025 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002026 int reg;
2027 u32 val;
2028
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002029 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002030 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002031 assert_sprites_disabled(dev_priv, pipe);
2032
Paulo Zanoni681e5812012-12-06 11:12:38 -02002033 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002034 pch_transcoder = TRANSCODER_A;
2035 else
2036 pch_transcoder = pipe;
2037
Jesse Barnesb24e7172011-01-04 15:09:30 -08002038 /*
2039 * A pipe without a PLL won't actually be able to drive bits from
2040 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2041 * need the check.
2042 */
2043 if (!HAS_PCH_SPLIT(dev_priv->dev))
Paulo Zanonifbf32182014-01-17 13:51:11 -02002044 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002045 assert_dsi_pll_enabled(dev_priv);
2046 else
2047 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002048 else {
Paulo Zanoni30421c42014-01-17 13:51:10 -02002049 if (crtc->config.has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002050 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002051 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002052 assert_fdi_tx_pll_enabled(dev_priv,
2053 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002054 }
2055 /* FIXME: assert CPU port conditions for SNB+ */
2056 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002057
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002058 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002059 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002060 if (val & PIPECONF_ENABLE) {
2061 WARN_ON(!(pipe == PIPE_A &&
2062 dev_priv->quirks & QUIRK_PIPEA_FORCE));
Chris Wilson00d70b12011-03-17 07:18:29 +00002063 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002064 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002065
2066 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002067 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002068}
2069
2070/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002071 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002072 * @dev_priv: i915 private structure
2073 * @pipe: pipe to disable
2074 *
2075 * Disable @pipe, making sure that various hardware specific requirements
2076 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
2077 *
2078 * @pipe should be %PIPE_A or %PIPE_B.
2079 *
2080 * Will wait until the pipe has shut down before returning.
2081 */
2082static void intel_disable_pipe(struct drm_i915_private *dev_priv,
2083 enum pipe pipe)
2084{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002085 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2086 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002087 int reg;
2088 u32 val;
2089
2090 /*
2091 * Make sure planes won't keep trying to pump pixels to us,
2092 * or we might hang the display.
2093 */
2094 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002095 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002096 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002097
2098 /* Don't disable pipe A or pipe A PLLs if needed */
2099 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
2100 return;
2101
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002102 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002103 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002104 if ((val & PIPECONF_ENABLE) == 0)
2105 return;
2106
2107 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002108 intel_wait_for_pipe_off(dev_priv->dev, pipe);
2109}
2110
Keith Packardd74362c2011-07-28 14:47:14 -07002111/*
2112 * Plane regs are double buffered, going from enabled->disabled needs a
2113 * trigger in order to latch. The display address reg provides this.
2114 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002115void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2116 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002117{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002118 struct drm_device *dev = dev_priv->dev;
2119 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002120
2121 I915_WRITE(reg, I915_READ(reg));
2122 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002123}
2124
Jesse Barnesb24e7172011-01-04 15:09:30 -08002125/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002126 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002127 * @dev_priv: i915 private structure
2128 * @plane: plane to enable
2129 * @pipe: pipe being fed
2130 *
2131 * Enable @plane on @pipe, making sure that @pipe is running first.
2132 */
Matt Roper262ca2b2014-03-18 17:22:55 -07002133static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
2134 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002135{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002136 struct intel_crtc *intel_crtc =
2137 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002138 int reg;
2139 u32 val;
2140
2141 /* If the pipe isn't enabled, we can't pump pixels and may hang */
2142 assert_pipe_enabled(dev_priv, pipe);
2143
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002144 if (intel_crtc->primary_enabled)
2145 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002146
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002147 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002148
Jesse Barnesb24e7172011-01-04 15:09:30 -08002149 reg = DSPCNTR(plane);
2150 val = I915_READ(reg);
Ville Syrjälä10efa932014-04-28 15:53:25 +03002151 WARN_ON(val & DISPLAY_PLANE_ENABLE);
Chris Wilson00d70b12011-03-17 07:18:29 +00002152
2153 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002154 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002155}
2156
Jesse Barnesb24e7172011-01-04 15:09:30 -08002157/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002158 * intel_disable_primary_hw_plane - disable the primary hardware plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002159 * @dev_priv: i915 private structure
2160 * @plane: plane to disable
2161 * @pipe: pipe consuming the data
2162 *
2163 * Disable @plane; should be an independent operation.
2164 */
Matt Roper262ca2b2014-03-18 17:22:55 -07002165static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
2166 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002167{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002168 struct intel_crtc *intel_crtc =
2169 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002170 int reg;
2171 u32 val;
2172
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002173 if (!intel_crtc->primary_enabled)
2174 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002175
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002176 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002177
Jesse Barnesb24e7172011-01-04 15:09:30 -08002178 reg = DSPCNTR(plane);
2179 val = I915_READ(reg);
Ville Syrjälä10efa932014-04-28 15:53:25 +03002180 WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
Chris Wilson00d70b12011-03-17 07:18:29 +00002181
2182 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002183 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002184}
2185
Chris Wilson693db182013-03-05 14:52:39 +00002186static bool need_vtd_wa(struct drm_device *dev)
2187{
2188#ifdef CONFIG_INTEL_IOMMU
2189 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2190 return true;
2191#endif
2192 return false;
2193}
2194
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002195static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2196{
2197 int tile_height;
2198
2199 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2200 return ALIGN(height, tile_height);
2201}
2202
Chris Wilson127bd2a2010-07-23 23:32:05 +01002203int
Chris Wilson48b956c2010-09-14 12:50:34 +01002204intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002205 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002206 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002207{
Chris Wilsonce453d82011-02-21 14:43:56 +00002208 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002209 u32 alignment;
2210 int ret;
2211
Chris Wilson05394f32010-11-08 19:18:58 +00002212 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002213 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01002214 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2215 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002216 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002217 alignment = 4 * 1024;
2218 else
2219 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002220 break;
2221 case I915_TILING_X:
2222 /* pin() will align the object as required by fence */
2223 alignment = 0;
2224 break;
2225 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02002226 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002227 return -EINVAL;
2228 default:
2229 BUG();
2230 }
2231
Chris Wilson693db182013-03-05 14:52:39 +00002232 /* Note that the w/a also requires 64 PTE of padding following the
2233 * bo. We currently fill all unused PTE with the shadow page and so
2234 * we should always have valid PTE following the scanout preventing
2235 * the VT-d warning.
2236 */
2237 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2238 alignment = 256 * 1024;
2239
Chris Wilsonce453d82011-02-21 14:43:56 +00002240 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002241 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002242 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002243 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002244
2245 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2246 * fence, whereas 965+ only requires a fence if using
2247 * framebuffer compression. For simplicity, we always install
2248 * a fence as the cost is not that onerous.
2249 */
Chris Wilson06d98132012-04-17 15:31:24 +01002250 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002251 if (ret)
2252 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002253
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002254 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002255
Chris Wilsonce453d82011-02-21 14:43:56 +00002256 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002257 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002258
2259err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002260 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002261err_interruptible:
2262 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01002263 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002264}
2265
Chris Wilson1690e1e2011-12-14 13:57:08 +01002266void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2267{
2268 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002269 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002270}
2271
Daniel Vetterc2c75132012-07-05 12:17:30 +02002272/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2273 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002274unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2275 unsigned int tiling_mode,
2276 unsigned int cpp,
2277 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002278{
Chris Wilsonbc752862013-02-21 20:04:31 +00002279 if (tiling_mode != I915_TILING_NONE) {
2280 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002281
Chris Wilsonbc752862013-02-21 20:04:31 +00002282 tile_rows = *y / 8;
2283 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002284
Chris Wilsonbc752862013-02-21 20:04:31 +00002285 tiles = *x / (512/cpp);
2286 *x %= 512/cpp;
2287
2288 return tile_rows * pitch * 8 + tiles * 4096;
2289 } else {
2290 unsigned int offset;
2291
2292 offset = *y * pitch + *x * cpp;
2293 *y = 0;
2294 *x = (offset & 4095) / cpp;
2295 return offset & -4096;
2296 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002297}
2298
Jesse Barnes46f297f2014-03-07 08:57:48 -08002299int intel_format_to_fourcc(int format)
2300{
2301 switch (format) {
2302 case DISPPLANE_8BPP:
2303 return DRM_FORMAT_C8;
2304 case DISPPLANE_BGRX555:
2305 return DRM_FORMAT_XRGB1555;
2306 case DISPPLANE_BGRX565:
2307 return DRM_FORMAT_RGB565;
2308 default:
2309 case DISPPLANE_BGRX888:
2310 return DRM_FORMAT_XRGB8888;
2311 case DISPPLANE_RGBX888:
2312 return DRM_FORMAT_XBGR8888;
2313 case DISPPLANE_BGRX101010:
2314 return DRM_FORMAT_XRGB2101010;
2315 case DISPPLANE_RGBX101010:
2316 return DRM_FORMAT_XBGR2101010;
2317 }
2318}
2319
Jesse Barnes484b41d2014-03-07 08:57:55 -08002320static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -08002321 struct intel_plane_config *plane_config)
2322{
2323 struct drm_device *dev = crtc->base.dev;
2324 struct drm_i915_gem_object *obj = NULL;
2325 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2326 u32 base = plane_config->base;
2327
Chris Wilsonff2652e2014-03-10 08:07:02 +00002328 if (plane_config->size == 0)
2329 return false;
2330
Jesse Barnes46f297f2014-03-07 08:57:48 -08002331 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2332 plane_config->size);
2333 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002334 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002335
2336 if (plane_config->tiled) {
2337 obj->tiling_mode = I915_TILING_X;
Dave Airlie66e514c2014-04-03 07:51:54 +10002338 obj->stride = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002339 }
2340
Dave Airlie66e514c2014-04-03 07:51:54 +10002341 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2342 mode_cmd.width = crtc->base.primary->fb->width;
2343 mode_cmd.height = crtc->base.primary->fb->height;
2344 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002345
2346 mutex_lock(&dev->struct_mutex);
2347
Dave Airlie66e514c2014-04-03 07:51:54 +10002348 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002349 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002350 DRM_DEBUG_KMS("intel fb init failed\n");
2351 goto out_unref_obj;
2352 }
2353
2354 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002355
2356 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2357 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002358
2359out_unref_obj:
2360 drm_gem_object_unreference(&obj->base);
2361 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002362 return false;
2363}
2364
2365static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2366 struct intel_plane_config *plane_config)
2367{
2368 struct drm_device *dev = intel_crtc->base.dev;
2369 struct drm_crtc *c;
2370 struct intel_crtc *i;
2371 struct intel_framebuffer *fb;
2372
Dave Airlie66e514c2014-04-03 07:51:54 +10002373 if (!intel_crtc->base.primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002374 return;
2375
2376 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2377 return;
2378
Dave Airlie66e514c2014-04-03 07:51:54 +10002379 kfree(intel_crtc->base.primary->fb);
2380 intel_crtc->base.primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002381
2382 /*
2383 * Failed to alloc the obj, check to see if we should share
2384 * an fb with another CRTC instead
2385 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002386 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002387 i = to_intel_crtc(c);
2388
2389 if (c == &intel_crtc->base)
2390 continue;
2391
Dave Airlie66e514c2014-04-03 07:51:54 +10002392 if (!i->active || !c->primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002393 continue;
2394
Dave Airlie66e514c2014-04-03 07:51:54 +10002395 fb = to_intel_framebuffer(c->primary->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002396 if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
Dave Airlie66e514c2014-04-03 07:51:54 +10002397 drm_framebuffer_reference(c->primary->fb);
2398 intel_crtc->base.primary->fb = c->primary->fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002399 break;
2400 }
2401 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002402}
2403
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002404static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2405 struct drm_framebuffer *fb,
2406 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002407{
2408 struct drm_device *dev = crtc->dev;
2409 struct drm_i915_private *dev_priv = dev->dev_private;
2410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2411 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002412 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002413 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002414 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002415 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002416 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002417
Jesse Barnes81255562010-08-02 12:07:50 -07002418 intel_fb = to_intel_framebuffer(fb);
2419 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002420
Chris Wilson5eddb702010-09-11 13:48:45 +01002421 reg = DSPCNTR(plane);
2422 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002423 /* Mask out pixel format bits in case we change it */
2424 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002425 switch (fb->pixel_format) {
2426 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002427 dspcntr |= DISPPLANE_8BPP;
2428 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002429 case DRM_FORMAT_XRGB1555:
2430 case DRM_FORMAT_ARGB1555:
2431 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002432 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002433 case DRM_FORMAT_RGB565:
2434 dspcntr |= DISPPLANE_BGRX565;
2435 break;
2436 case DRM_FORMAT_XRGB8888:
2437 case DRM_FORMAT_ARGB8888:
2438 dspcntr |= DISPPLANE_BGRX888;
2439 break;
2440 case DRM_FORMAT_XBGR8888:
2441 case DRM_FORMAT_ABGR8888:
2442 dspcntr |= DISPPLANE_RGBX888;
2443 break;
2444 case DRM_FORMAT_XRGB2101010:
2445 case DRM_FORMAT_ARGB2101010:
2446 dspcntr |= DISPPLANE_BGRX101010;
2447 break;
2448 case DRM_FORMAT_XBGR2101010:
2449 case DRM_FORMAT_ABGR2101010:
2450 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002451 break;
2452 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002453 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002454 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002455
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002456 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002457 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002458 dspcntr |= DISPPLANE_TILED;
2459 else
2460 dspcntr &= ~DISPPLANE_TILED;
2461 }
2462
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002463 if (IS_G4X(dev))
2464 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2465
Chris Wilson5eddb702010-09-11 13:48:45 +01002466 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002467
Daniel Vettere506a0c2012-07-05 12:17:29 +02002468 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002469
Daniel Vetterc2c75132012-07-05 12:17:30 +02002470 if (INTEL_INFO(dev)->gen >= 4) {
2471 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002472 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2473 fb->bits_per_pixel / 8,
2474 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002475 linear_offset -= intel_crtc->dspaddr_offset;
2476 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002477 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002478 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002479
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002480 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2481 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2482 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002483 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002484 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002485 I915_WRITE(DSPSURF(plane),
2486 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002487 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002488 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002489 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002490 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002491 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002492}
2493
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002494static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2495 struct drm_framebuffer *fb,
2496 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002497{
2498 struct drm_device *dev = crtc->dev;
2499 struct drm_i915_private *dev_priv = dev->dev_private;
2500 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2501 struct intel_framebuffer *intel_fb;
2502 struct drm_i915_gem_object *obj;
2503 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002504 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002505 u32 dspcntr;
2506 u32 reg;
2507
Jesse Barnes17638cd2011-06-24 12:19:23 -07002508 intel_fb = to_intel_framebuffer(fb);
2509 obj = intel_fb->obj;
2510
2511 reg = DSPCNTR(plane);
2512 dspcntr = I915_READ(reg);
2513 /* Mask out pixel format bits in case we change it */
2514 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002515 switch (fb->pixel_format) {
2516 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002517 dspcntr |= DISPPLANE_8BPP;
2518 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002519 case DRM_FORMAT_RGB565:
2520 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002521 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002522 case DRM_FORMAT_XRGB8888:
2523 case DRM_FORMAT_ARGB8888:
2524 dspcntr |= DISPPLANE_BGRX888;
2525 break;
2526 case DRM_FORMAT_XBGR8888:
2527 case DRM_FORMAT_ABGR8888:
2528 dspcntr |= DISPPLANE_RGBX888;
2529 break;
2530 case DRM_FORMAT_XRGB2101010:
2531 case DRM_FORMAT_ARGB2101010:
2532 dspcntr |= DISPPLANE_BGRX101010;
2533 break;
2534 case DRM_FORMAT_XBGR2101010:
2535 case DRM_FORMAT_ABGR2101010:
2536 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002537 break;
2538 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002539 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002540 }
2541
2542 if (obj->tiling_mode != I915_TILING_NONE)
2543 dspcntr |= DISPPLANE_TILED;
2544 else
2545 dspcntr &= ~DISPPLANE_TILED;
2546
Ville Syrjäläb42c6002013-11-03 13:47:27 +02002547 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002548 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2549 else
2550 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002551
2552 I915_WRITE(reg, dspcntr);
2553
Daniel Vettere506a0c2012-07-05 12:17:29 +02002554 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002555 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002556 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2557 fb->bits_per_pixel / 8,
2558 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002559 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002560
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002561 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2562 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2563 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002564 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002565 I915_WRITE(DSPSURF(plane),
2566 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002567 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002568 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2569 } else {
2570 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2571 I915_WRITE(DSPLINOFF(plane), linear_offset);
2572 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002573 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002574}
2575
2576/* Assume fb object is pinned & idle & fenced and just update base pointers */
2577static int
2578intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2579 int x, int y, enum mode_set_atomic state)
2580{
2581 struct drm_device *dev = crtc->dev;
2582 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002583
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002584 if (dev_priv->display.disable_fbc)
2585 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002586 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002587
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002588 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2589
2590 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07002591}
2592
Ville Syrjälä96a02912013-02-18 19:08:49 +02002593void intel_display_handle_reset(struct drm_device *dev)
2594{
2595 struct drm_i915_private *dev_priv = dev->dev_private;
2596 struct drm_crtc *crtc;
2597
2598 /*
2599 * Flips in the rings have been nuked by the reset,
2600 * so complete all pending flips so that user space
2601 * will get its events and not get stuck.
2602 *
2603 * Also update the base address of all primary
2604 * planes to the the last fb to make sure we're
2605 * showing the correct fb after a reset.
2606 *
2607 * Need to make two loops over the crtcs so that we
2608 * don't try to grab a crtc mutex before the
2609 * pending_flip_queue really got woken up.
2610 */
2611
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002612 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002613 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2614 enum plane plane = intel_crtc->plane;
2615
2616 intel_prepare_page_flip(dev, plane);
2617 intel_finish_page_flip_plane(dev, plane);
2618 }
2619
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002620 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2622
Rob Clark51fd3712013-11-19 12:10:12 -05002623 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002624 /*
2625 * FIXME: Once we have proper support for primary planes (and
2626 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002627 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002628 */
Matt Roperf4510a22014-04-01 15:22:40 -07002629 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002630 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002631 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002632 crtc->x,
2633 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05002634 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002635 }
2636}
2637
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002638static int
Chris Wilson14667a42012-04-03 17:58:35 +01002639intel_finish_fb(struct drm_framebuffer *old_fb)
2640{
2641 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2642 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2643 bool was_interruptible = dev_priv->mm.interruptible;
2644 int ret;
2645
Chris Wilson14667a42012-04-03 17:58:35 +01002646 /* Big Hammer, we also need to ensure that any pending
2647 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2648 * current scanout is retired before unpinning the old
2649 * framebuffer.
2650 *
2651 * This should only fail upon a hung GPU, in which case we
2652 * can safely continue.
2653 */
2654 dev_priv->mm.interruptible = false;
2655 ret = i915_gem_object_finish_gpu(obj);
2656 dev_priv->mm.interruptible = was_interruptible;
2657
2658 return ret;
2659}
2660
Chris Wilson7d5e3792014-03-04 13:15:08 +00002661static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2662{
2663 struct drm_device *dev = crtc->dev;
2664 struct drm_i915_private *dev_priv = dev->dev_private;
2665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2666 unsigned long flags;
2667 bool pending;
2668
2669 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2670 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2671 return false;
2672
2673 spin_lock_irqsave(&dev->event_lock, flags);
2674 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2675 spin_unlock_irqrestore(&dev->event_lock, flags);
2676
2677 return pending;
2678}
2679
Chris Wilson14667a42012-04-03 17:58:35 +01002680static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002681intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002682 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002683{
2684 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002685 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002686 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002687 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002688 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002689
Chris Wilson7d5e3792014-03-04 13:15:08 +00002690 if (intel_crtc_has_pending_flip(crtc)) {
2691 DRM_ERROR("pipe is still busy with an old pageflip\n");
2692 return -EBUSY;
2693 }
2694
Jesse Barnes79e53942008-11-07 14:24:08 -08002695 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002696 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002697 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002698 return 0;
2699 }
2700
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002701 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002702 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2703 plane_name(intel_crtc->plane),
2704 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002705 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002706 }
2707
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002708 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002709 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002710 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002711 NULL);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002712 mutex_unlock(&dev->struct_mutex);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002713 if (ret != 0) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002714 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002715 return ret;
2716 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002717
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002718 /*
2719 * Update pipe size and adjust fitter if needed: the reason for this is
2720 * that in compute_mode_changes we check the native mode (not the pfit
2721 * mode) to see if we can flip rather than do a full mode set. In the
2722 * fastboot case, we'll flip, but if we don't update the pipesrc and
2723 * pfit state, we'll end up with a big fb scanned out into the wrong
2724 * sized surface.
2725 *
2726 * To fix this properly, we need to hoist the checks up into
2727 * compute_mode_changes (or above), check the actual pfit state and
2728 * whether the platform allows pfit disable with pipe active, and only
2729 * then update the pipesrc and pfit state, even on the flip path.
2730 */
Jani Nikulad330a952014-01-21 11:24:25 +02002731 if (i915.fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002732 const struct drm_display_mode *adjusted_mode =
2733 &intel_crtc->config.adjusted_mode;
2734
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002735 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002736 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2737 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002738 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002739 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2740 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2741 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2742 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2743 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2744 }
Jesse Barnes0637d602013-12-19 10:48:01 -08002745 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2746 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002747 }
2748
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002749 dev_priv->display.update_primary_plane(crtc, fb, x, y);
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002750
Matt Roperf4510a22014-04-01 15:22:40 -07002751 old_fb = crtc->primary->fb;
2752 crtc->primary->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002753 crtc->x = x;
2754 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002755
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002756 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002757 if (intel_crtc->active && old_fb != fb)
2758 intel_wait_for_vblank(dev, intel_crtc->pipe);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002759 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002760 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002761 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002762 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002763
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002764 mutex_lock(&dev->struct_mutex);
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002765 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002766 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002767 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002768
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002769 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002770}
2771
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002772static void intel_fdi_normal_train(struct drm_crtc *crtc)
2773{
2774 struct drm_device *dev = crtc->dev;
2775 struct drm_i915_private *dev_priv = dev->dev_private;
2776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2777 int pipe = intel_crtc->pipe;
2778 u32 reg, temp;
2779
2780 /* enable normal train */
2781 reg = FDI_TX_CTL(pipe);
2782 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002783 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002784 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2785 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002786 } else {
2787 temp &= ~FDI_LINK_TRAIN_NONE;
2788 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002789 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002790 I915_WRITE(reg, temp);
2791
2792 reg = FDI_RX_CTL(pipe);
2793 temp = I915_READ(reg);
2794 if (HAS_PCH_CPT(dev)) {
2795 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2796 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2797 } else {
2798 temp &= ~FDI_LINK_TRAIN_NONE;
2799 temp |= FDI_LINK_TRAIN_NONE;
2800 }
2801 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2802
2803 /* wait one idle pattern time */
2804 POSTING_READ(reg);
2805 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002806
2807 /* IVB wants error correction enabled */
2808 if (IS_IVYBRIDGE(dev))
2809 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2810 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002811}
2812
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002813static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002814{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002815 return crtc->base.enabled && crtc->active &&
2816 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002817}
2818
Daniel Vetter01a415f2012-10-27 15:58:40 +02002819static void ivb_modeset_global_resources(struct drm_device *dev)
2820{
2821 struct drm_i915_private *dev_priv = dev->dev_private;
2822 struct intel_crtc *pipe_B_crtc =
2823 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2824 struct intel_crtc *pipe_C_crtc =
2825 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2826 uint32_t temp;
2827
Daniel Vetter1e833f42013-02-19 22:31:57 +01002828 /*
2829 * When everything is off disable fdi C so that we could enable fdi B
2830 * with all lanes. Note that we don't care about enabled pipes without
2831 * an enabled pch encoder.
2832 */
2833 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2834 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002835 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2836 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2837
2838 temp = I915_READ(SOUTH_CHICKEN1);
2839 temp &= ~FDI_BC_BIFURCATION_SELECT;
2840 DRM_DEBUG_KMS("disabling fdi C rx\n");
2841 I915_WRITE(SOUTH_CHICKEN1, temp);
2842 }
2843}
2844
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002845/* The FDI link training functions for ILK/Ibexpeak. */
2846static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2847{
2848 struct drm_device *dev = crtc->dev;
2849 struct drm_i915_private *dev_priv = dev->dev_private;
2850 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2851 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002852 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002853
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03002854 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002855 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002856
Adam Jacksone1a44742010-06-25 15:32:14 -04002857 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2858 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002859 reg = FDI_RX_IMR(pipe);
2860 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002861 temp &= ~FDI_RX_SYMBOL_LOCK;
2862 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002863 I915_WRITE(reg, temp);
2864 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002865 udelay(150);
2866
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002867 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002868 reg = FDI_TX_CTL(pipe);
2869 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002870 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2871 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002872 temp &= ~FDI_LINK_TRAIN_NONE;
2873 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002874 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002875
Chris Wilson5eddb702010-09-11 13:48:45 +01002876 reg = FDI_RX_CTL(pipe);
2877 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002878 temp &= ~FDI_LINK_TRAIN_NONE;
2879 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002880 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2881
2882 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002883 udelay(150);
2884
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002885 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002886 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2887 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2888 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002889
Chris Wilson5eddb702010-09-11 13:48:45 +01002890 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002891 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002892 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002893 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2894
2895 if ((temp & FDI_RX_BIT_LOCK)) {
2896 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002897 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002898 break;
2899 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002900 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002901 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002902 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002903
2904 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002905 reg = FDI_TX_CTL(pipe);
2906 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002907 temp &= ~FDI_LINK_TRAIN_NONE;
2908 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002909 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002910
Chris Wilson5eddb702010-09-11 13:48:45 +01002911 reg = FDI_RX_CTL(pipe);
2912 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002913 temp &= ~FDI_LINK_TRAIN_NONE;
2914 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002915 I915_WRITE(reg, temp);
2916
2917 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002918 udelay(150);
2919
Chris Wilson5eddb702010-09-11 13:48:45 +01002920 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002921 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002922 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002923 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2924
2925 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002926 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002927 DRM_DEBUG_KMS("FDI train 2 done.\n");
2928 break;
2929 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002930 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002931 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002932 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002933
2934 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002935
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002936}
2937
Akshay Joshi0206e352011-08-16 15:34:10 -04002938static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002939 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2940 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2941 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2942 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2943};
2944
2945/* The FDI link training functions for SNB/Cougarpoint. */
2946static void gen6_fdi_link_train(struct drm_crtc *crtc)
2947{
2948 struct drm_device *dev = crtc->dev;
2949 struct drm_i915_private *dev_priv = dev->dev_private;
2950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2951 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002952 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002953
Adam Jacksone1a44742010-06-25 15:32:14 -04002954 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2955 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002956 reg = FDI_RX_IMR(pipe);
2957 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002958 temp &= ~FDI_RX_SYMBOL_LOCK;
2959 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002960 I915_WRITE(reg, temp);
2961
2962 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002963 udelay(150);
2964
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002965 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002966 reg = FDI_TX_CTL(pipe);
2967 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002968 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2969 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002970 temp &= ~FDI_LINK_TRAIN_NONE;
2971 temp |= FDI_LINK_TRAIN_PATTERN_1;
2972 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2973 /* SNB-B */
2974 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002975 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002976
Daniel Vetterd74cf322012-10-26 10:58:13 +02002977 I915_WRITE(FDI_RX_MISC(pipe),
2978 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2979
Chris Wilson5eddb702010-09-11 13:48:45 +01002980 reg = FDI_RX_CTL(pipe);
2981 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002982 if (HAS_PCH_CPT(dev)) {
2983 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2984 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2985 } else {
2986 temp &= ~FDI_LINK_TRAIN_NONE;
2987 temp |= FDI_LINK_TRAIN_PATTERN_1;
2988 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002989 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2990
2991 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002992 udelay(150);
2993
Akshay Joshi0206e352011-08-16 15:34:10 -04002994 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002995 reg = FDI_TX_CTL(pipe);
2996 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002997 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2998 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002999 I915_WRITE(reg, temp);
3000
3001 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003002 udelay(500);
3003
Sean Paulfa37d392012-03-02 12:53:39 -05003004 for (retry = 0; retry < 5; retry++) {
3005 reg = FDI_RX_IIR(pipe);
3006 temp = I915_READ(reg);
3007 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3008 if (temp & FDI_RX_BIT_LOCK) {
3009 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3010 DRM_DEBUG_KMS("FDI train 1 done.\n");
3011 break;
3012 }
3013 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003014 }
Sean Paulfa37d392012-03-02 12:53:39 -05003015 if (retry < 5)
3016 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003017 }
3018 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003019 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003020
3021 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003022 reg = FDI_TX_CTL(pipe);
3023 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003024 temp &= ~FDI_LINK_TRAIN_NONE;
3025 temp |= FDI_LINK_TRAIN_PATTERN_2;
3026 if (IS_GEN6(dev)) {
3027 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3028 /* SNB-B */
3029 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3030 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003031 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003032
Chris Wilson5eddb702010-09-11 13:48:45 +01003033 reg = FDI_RX_CTL(pipe);
3034 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003035 if (HAS_PCH_CPT(dev)) {
3036 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3037 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3038 } else {
3039 temp &= ~FDI_LINK_TRAIN_NONE;
3040 temp |= FDI_LINK_TRAIN_PATTERN_2;
3041 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003042 I915_WRITE(reg, temp);
3043
3044 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003045 udelay(150);
3046
Akshay Joshi0206e352011-08-16 15:34:10 -04003047 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003048 reg = FDI_TX_CTL(pipe);
3049 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003050 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3051 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003052 I915_WRITE(reg, temp);
3053
3054 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003055 udelay(500);
3056
Sean Paulfa37d392012-03-02 12:53:39 -05003057 for (retry = 0; retry < 5; retry++) {
3058 reg = FDI_RX_IIR(pipe);
3059 temp = I915_READ(reg);
3060 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3061 if (temp & FDI_RX_SYMBOL_LOCK) {
3062 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3063 DRM_DEBUG_KMS("FDI train 2 done.\n");
3064 break;
3065 }
3066 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003067 }
Sean Paulfa37d392012-03-02 12:53:39 -05003068 if (retry < 5)
3069 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003070 }
3071 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003072 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003073
3074 DRM_DEBUG_KMS("FDI train done.\n");
3075}
3076
Jesse Barnes357555c2011-04-28 15:09:55 -07003077/* Manual link training for Ivy Bridge A0 parts */
3078static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3079{
3080 struct drm_device *dev = crtc->dev;
3081 struct drm_i915_private *dev_priv = dev->dev_private;
3082 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3083 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003084 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003085
3086 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3087 for train result */
3088 reg = FDI_RX_IMR(pipe);
3089 temp = I915_READ(reg);
3090 temp &= ~FDI_RX_SYMBOL_LOCK;
3091 temp &= ~FDI_RX_BIT_LOCK;
3092 I915_WRITE(reg, temp);
3093
3094 POSTING_READ(reg);
3095 udelay(150);
3096
Daniel Vetter01a415f2012-10-27 15:58:40 +02003097 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3098 I915_READ(FDI_RX_IIR(pipe)));
3099
Jesse Barnes139ccd32013-08-19 11:04:55 -07003100 /* Try each vswing and preemphasis setting twice before moving on */
3101 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3102 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003103 reg = FDI_TX_CTL(pipe);
3104 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003105 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3106 temp &= ~FDI_TX_ENABLE;
3107 I915_WRITE(reg, temp);
3108
3109 reg = FDI_RX_CTL(pipe);
3110 temp = I915_READ(reg);
3111 temp &= ~FDI_LINK_TRAIN_AUTO;
3112 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3113 temp &= ~FDI_RX_ENABLE;
3114 I915_WRITE(reg, temp);
3115
3116 /* enable CPU FDI TX and PCH FDI RX */
3117 reg = FDI_TX_CTL(pipe);
3118 temp = I915_READ(reg);
3119 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3120 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3121 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003122 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003123 temp |= snb_b_fdi_train_param[j/2];
3124 temp |= FDI_COMPOSITE_SYNC;
3125 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3126
3127 I915_WRITE(FDI_RX_MISC(pipe),
3128 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3129
3130 reg = FDI_RX_CTL(pipe);
3131 temp = I915_READ(reg);
3132 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3133 temp |= FDI_COMPOSITE_SYNC;
3134 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3135
3136 POSTING_READ(reg);
3137 udelay(1); /* should be 0.5us */
3138
3139 for (i = 0; i < 4; i++) {
3140 reg = FDI_RX_IIR(pipe);
3141 temp = I915_READ(reg);
3142 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3143
3144 if (temp & FDI_RX_BIT_LOCK ||
3145 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3146 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3147 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3148 i);
3149 break;
3150 }
3151 udelay(1); /* should be 0.5us */
3152 }
3153 if (i == 4) {
3154 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3155 continue;
3156 }
3157
3158 /* Train 2 */
3159 reg = FDI_TX_CTL(pipe);
3160 temp = I915_READ(reg);
3161 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3162 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3163 I915_WRITE(reg, temp);
3164
3165 reg = FDI_RX_CTL(pipe);
3166 temp = I915_READ(reg);
3167 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3168 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003169 I915_WRITE(reg, temp);
3170
3171 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003172 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003173
Jesse Barnes139ccd32013-08-19 11:04:55 -07003174 for (i = 0; i < 4; i++) {
3175 reg = FDI_RX_IIR(pipe);
3176 temp = I915_READ(reg);
3177 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003178
Jesse Barnes139ccd32013-08-19 11:04:55 -07003179 if (temp & FDI_RX_SYMBOL_LOCK ||
3180 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3181 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3182 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3183 i);
3184 goto train_done;
3185 }
3186 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003187 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003188 if (i == 4)
3189 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003190 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003191
Jesse Barnes139ccd32013-08-19 11:04:55 -07003192train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003193 DRM_DEBUG_KMS("FDI train done.\n");
3194}
3195
Daniel Vetter88cefb62012-08-12 19:27:14 +02003196static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003197{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003198 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003199 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003200 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003201 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003202
Jesse Barnesc64e3112010-09-10 11:27:03 -07003203
Jesse Barnes0e23b992010-09-10 11:10:00 -07003204 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003205 reg = FDI_RX_CTL(pipe);
3206 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003207 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3208 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003209 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003210 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3211
3212 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003213 udelay(200);
3214
3215 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003216 temp = I915_READ(reg);
3217 I915_WRITE(reg, temp | FDI_PCDCLK);
3218
3219 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003220 udelay(200);
3221
Paulo Zanoni20749732012-11-23 15:30:38 -02003222 /* Enable CPU FDI TX PLL, always on for Ironlake */
3223 reg = FDI_TX_CTL(pipe);
3224 temp = I915_READ(reg);
3225 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3226 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003227
Paulo Zanoni20749732012-11-23 15:30:38 -02003228 POSTING_READ(reg);
3229 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003230 }
3231}
3232
Daniel Vetter88cefb62012-08-12 19:27:14 +02003233static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3234{
3235 struct drm_device *dev = intel_crtc->base.dev;
3236 struct drm_i915_private *dev_priv = dev->dev_private;
3237 int pipe = intel_crtc->pipe;
3238 u32 reg, temp;
3239
3240 /* Switch from PCDclk to Rawclk */
3241 reg = FDI_RX_CTL(pipe);
3242 temp = I915_READ(reg);
3243 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3244
3245 /* Disable CPU FDI TX PLL */
3246 reg = FDI_TX_CTL(pipe);
3247 temp = I915_READ(reg);
3248 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3249
3250 POSTING_READ(reg);
3251 udelay(100);
3252
3253 reg = FDI_RX_CTL(pipe);
3254 temp = I915_READ(reg);
3255 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3256
3257 /* Wait for the clocks to turn off. */
3258 POSTING_READ(reg);
3259 udelay(100);
3260}
3261
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003262static void ironlake_fdi_disable(struct drm_crtc *crtc)
3263{
3264 struct drm_device *dev = crtc->dev;
3265 struct drm_i915_private *dev_priv = dev->dev_private;
3266 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3267 int pipe = intel_crtc->pipe;
3268 u32 reg, temp;
3269
3270 /* disable CPU FDI tx and PCH FDI rx */
3271 reg = FDI_TX_CTL(pipe);
3272 temp = I915_READ(reg);
3273 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3274 POSTING_READ(reg);
3275
3276 reg = FDI_RX_CTL(pipe);
3277 temp = I915_READ(reg);
3278 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003279 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003280 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3281
3282 POSTING_READ(reg);
3283 udelay(100);
3284
3285 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003286 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003287 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003288
3289 /* still set train pattern 1 */
3290 reg = FDI_TX_CTL(pipe);
3291 temp = I915_READ(reg);
3292 temp &= ~FDI_LINK_TRAIN_NONE;
3293 temp |= FDI_LINK_TRAIN_PATTERN_1;
3294 I915_WRITE(reg, temp);
3295
3296 reg = FDI_RX_CTL(pipe);
3297 temp = I915_READ(reg);
3298 if (HAS_PCH_CPT(dev)) {
3299 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3300 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3301 } else {
3302 temp &= ~FDI_LINK_TRAIN_NONE;
3303 temp |= FDI_LINK_TRAIN_PATTERN_1;
3304 }
3305 /* BPC in FDI rx is consistent with that in PIPECONF */
3306 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003307 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003308 I915_WRITE(reg, temp);
3309
3310 POSTING_READ(reg);
3311 udelay(100);
3312}
3313
Chris Wilson5dce5b932014-01-20 10:17:36 +00003314bool intel_has_pending_fb_unpin(struct drm_device *dev)
3315{
3316 struct intel_crtc *crtc;
3317
3318 /* Note that we don't need to be called with mode_config.lock here
3319 * as our list of CRTC objects is static for the lifetime of the
3320 * device and so cannot disappear as we iterate. Similarly, we can
3321 * happily treat the predicates as racy, atomic checks as userspace
3322 * cannot claim and pin a new fb without at least acquring the
3323 * struct_mutex and so serialising with us.
3324 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003325 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003326 if (atomic_read(&crtc->unpin_work_count) == 0)
3327 continue;
3328
3329 if (crtc->unpin_work)
3330 intel_wait_for_vblank(dev, crtc->pipe);
3331
3332 return true;
3333 }
3334
3335 return false;
3336}
3337
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003338void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003339{
Chris Wilson0f911282012-04-17 10:05:38 +01003340 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003341 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003342
Matt Roperf4510a22014-04-01 15:22:40 -07003343 if (crtc->primary->fb == NULL)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003344 return;
3345
Daniel Vetter2c10d572012-12-20 21:24:07 +01003346 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3347
Daniel Vettereed6d672014-05-19 16:09:35 +02003348 WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3349 !intel_crtc_has_pending_flip(crtc),
3350 60*HZ) == 0);
Chris Wilson5bb61642012-09-27 21:25:58 +01003351
Chris Wilson0f911282012-04-17 10:05:38 +01003352 mutex_lock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07003353 intel_finish_fb(crtc->primary->fb);
Chris Wilson0f911282012-04-17 10:05:38 +01003354 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003355}
3356
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003357/* Program iCLKIP clock to the desired frequency */
3358static void lpt_program_iclkip(struct drm_crtc *crtc)
3359{
3360 struct drm_device *dev = crtc->dev;
3361 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003362 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003363 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3364 u32 temp;
3365
Daniel Vetter09153002012-12-12 14:06:44 +01003366 mutex_lock(&dev_priv->dpio_lock);
3367
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003368 /* It is necessary to ungate the pixclk gate prior to programming
3369 * the divisors, and gate it back when it is done.
3370 */
3371 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3372
3373 /* Disable SSCCTL */
3374 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003375 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3376 SBI_SSCCTL_DISABLE,
3377 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003378
3379 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003380 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003381 auxdiv = 1;
3382 divsel = 0x41;
3383 phaseinc = 0x20;
3384 } else {
3385 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003386 * but the adjusted_mode->crtc_clock in in KHz. To get the
3387 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003388 * convert the virtual clock precision to KHz here for higher
3389 * precision.
3390 */
3391 u32 iclk_virtual_root_freq = 172800 * 1000;
3392 u32 iclk_pi_range = 64;
3393 u32 desired_divisor, msb_divisor_value, pi_value;
3394
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003395 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003396 msb_divisor_value = desired_divisor / iclk_pi_range;
3397 pi_value = desired_divisor % iclk_pi_range;
3398
3399 auxdiv = 0;
3400 divsel = msb_divisor_value - 2;
3401 phaseinc = pi_value;
3402 }
3403
3404 /* This should not happen with any sane values */
3405 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3406 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3407 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3408 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3409
3410 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 +03003411 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003412 auxdiv,
3413 divsel,
3414 phasedir,
3415 phaseinc);
3416
3417 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003418 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003419 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3420 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3421 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3422 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3423 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3424 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003425 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003426
3427 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003428 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003429 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3430 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003431 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003432
3433 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003434 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003435 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003436 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003437
3438 /* Wait for initialization time */
3439 udelay(24);
3440
3441 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003442
3443 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003444}
3445
Daniel Vetter275f01b22013-05-03 11:49:47 +02003446static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3447 enum pipe pch_transcoder)
3448{
3449 struct drm_device *dev = crtc->base.dev;
3450 struct drm_i915_private *dev_priv = dev->dev_private;
3451 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3452
3453 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3454 I915_READ(HTOTAL(cpu_transcoder)));
3455 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3456 I915_READ(HBLANK(cpu_transcoder)));
3457 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3458 I915_READ(HSYNC(cpu_transcoder)));
3459
3460 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3461 I915_READ(VTOTAL(cpu_transcoder)));
3462 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3463 I915_READ(VBLANK(cpu_transcoder)));
3464 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3465 I915_READ(VSYNC(cpu_transcoder)));
3466 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3467 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3468}
3469
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003470static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3471{
3472 struct drm_i915_private *dev_priv = dev->dev_private;
3473 uint32_t temp;
3474
3475 temp = I915_READ(SOUTH_CHICKEN1);
3476 if (temp & FDI_BC_BIFURCATION_SELECT)
3477 return;
3478
3479 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3480 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3481
3482 temp |= FDI_BC_BIFURCATION_SELECT;
3483 DRM_DEBUG_KMS("enabling fdi C rx\n");
3484 I915_WRITE(SOUTH_CHICKEN1, temp);
3485 POSTING_READ(SOUTH_CHICKEN1);
3486}
3487
3488static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3489{
3490 struct drm_device *dev = intel_crtc->base.dev;
3491 struct drm_i915_private *dev_priv = dev->dev_private;
3492
3493 switch (intel_crtc->pipe) {
3494 case PIPE_A:
3495 break;
3496 case PIPE_B:
3497 if (intel_crtc->config.fdi_lanes > 2)
3498 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3499 else
3500 cpt_enable_fdi_bc_bifurcation(dev);
3501
3502 break;
3503 case PIPE_C:
3504 cpt_enable_fdi_bc_bifurcation(dev);
3505
3506 break;
3507 default:
3508 BUG();
3509 }
3510}
3511
Jesse Barnesf67a5592011-01-05 10:31:48 -08003512/*
3513 * Enable PCH resources required for PCH ports:
3514 * - PCH PLLs
3515 * - FDI training & RX/TX
3516 * - update transcoder timings
3517 * - DP transcoding bits
3518 * - transcoder
3519 */
3520static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003521{
3522 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003523 struct drm_i915_private *dev_priv = dev->dev_private;
3524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3525 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003526 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003527
Daniel Vetterab9412b2013-05-03 11:49:46 +02003528 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003529
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003530 if (IS_IVYBRIDGE(dev))
3531 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3532
Daniel Vettercd986ab2012-10-26 10:58:12 +02003533 /* Write the TU size bits before fdi link training, so that error
3534 * detection works. */
3535 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3536 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3537
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003538 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003539 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003540
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003541 /* We need to program the right clock selection before writing the pixel
3542 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003543 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003544 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003545
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003546 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003547 temp |= TRANS_DPLL_ENABLE(pipe);
3548 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003549 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003550 temp |= sel;
3551 else
3552 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003553 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003554 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003555
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003556 /* XXX: pch pll's can be enabled any time before we enable the PCH
3557 * transcoder, and we actually should do this to not upset any PCH
3558 * transcoder that already use the clock when we share it.
3559 *
3560 * Note that enable_shared_dpll tries to do the right thing, but
3561 * get_shared_dpll unconditionally resets the pll - we need that to have
3562 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02003563 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003564
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003565 /* set transcoder timing, panel must allow it */
3566 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003567 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003568
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003569 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003570
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003571 /* For PCH DP, enable TRANS_DP_CTL */
3572 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003573 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3574 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003575 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003576 reg = TRANS_DP_CTL(pipe);
3577 temp = I915_READ(reg);
3578 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003579 TRANS_DP_SYNC_MASK |
3580 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003581 temp |= (TRANS_DP_OUTPUT_ENABLE |
3582 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003583 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003584
3585 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003586 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003587 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003588 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003589
3590 switch (intel_trans_dp_port_sel(crtc)) {
3591 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003592 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003593 break;
3594 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003595 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003596 break;
3597 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003598 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003599 break;
3600 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003601 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003602 }
3603
Chris Wilson5eddb702010-09-11 13:48:45 +01003604 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003605 }
3606
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003607 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003608}
3609
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003610static void lpt_pch_enable(struct drm_crtc *crtc)
3611{
3612 struct drm_device *dev = crtc->dev;
3613 struct drm_i915_private *dev_priv = dev->dev_private;
3614 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003615 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003616
Daniel Vetterab9412b2013-05-03 11:49:46 +02003617 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003618
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003619 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003620
Paulo Zanoni0540e482012-10-31 18:12:40 -02003621 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003622 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003623
Paulo Zanoni937bb612012-10-31 18:12:47 -02003624 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003625}
3626
Daniel Vettere2b78262013-06-07 23:10:03 +02003627static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003628{
Daniel Vettere2b78262013-06-07 23:10:03 +02003629 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003630
3631 if (pll == NULL)
3632 return;
3633
3634 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003635 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003636 return;
3637 }
3638
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003639 if (--pll->refcount == 0) {
3640 WARN_ON(pll->on);
3641 WARN_ON(pll->active);
3642 }
3643
Daniel Vettera43f6e02013-06-07 23:10:32 +02003644 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003645}
3646
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003647static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003648{
Daniel Vettere2b78262013-06-07 23:10:03 +02003649 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3650 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3651 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003652
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003653 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003654 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3655 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003656 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003657 }
3658
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003659 if (HAS_PCH_IBX(dev_priv->dev)) {
3660 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003661 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003662 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003663
Daniel Vetter46edb022013-06-05 13:34:12 +02003664 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3665 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003666
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003667 WARN_ON(pll->refcount);
3668
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003669 goto found;
3670 }
3671
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003672 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3673 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003674
3675 /* Only want to check enabled timings first */
3676 if (pll->refcount == 0)
3677 continue;
3678
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003679 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3680 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003681 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003682 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003683 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003684
3685 goto found;
3686 }
3687 }
3688
3689 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003690 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3691 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003692 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003693 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3694 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003695 goto found;
3696 }
3697 }
3698
3699 return NULL;
3700
3701found:
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003702 if (pll->refcount == 0)
3703 pll->hw_state = crtc->config.dpll_hw_state;
3704
Daniel Vettera43f6e02013-06-07 23:10:32 +02003705 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003706 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3707 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003708
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003709 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003710
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003711 return pll;
3712}
3713
Daniel Vettera1520312013-05-03 11:49:50 +02003714static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003715{
3716 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003717 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003718 u32 temp;
3719
3720 temp = I915_READ(dslreg);
3721 udelay(500);
3722 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003723 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003724 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003725 }
3726}
3727
Jesse Barnesb074cec2013-04-25 12:55:02 -07003728static void ironlake_pfit_enable(struct intel_crtc *crtc)
3729{
3730 struct drm_device *dev = crtc->base.dev;
3731 struct drm_i915_private *dev_priv = dev->dev_private;
3732 int pipe = crtc->pipe;
3733
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003734 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003735 /* Force use of hard-coded filter coefficients
3736 * as some pre-programmed values are broken,
3737 * e.g. x201.
3738 */
3739 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3740 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3741 PF_PIPE_SEL_IVB(pipe));
3742 else
3743 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3744 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3745 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003746 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003747}
3748
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003749static void intel_enable_planes(struct drm_crtc *crtc)
3750{
3751 struct drm_device *dev = crtc->dev;
3752 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003753 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003754 struct intel_plane *intel_plane;
3755
Matt Roperaf2b6532014-04-01 15:22:32 -07003756 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3757 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003758 if (intel_plane->pipe == pipe)
3759 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003760 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003761}
3762
3763static void intel_disable_planes(struct drm_crtc *crtc)
3764{
3765 struct drm_device *dev = crtc->dev;
3766 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003767 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003768 struct intel_plane *intel_plane;
3769
Matt Roperaf2b6532014-04-01 15:22:32 -07003770 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3771 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003772 if (intel_plane->pipe == pipe)
3773 intel_plane_disable(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003774 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003775}
3776
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003777void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003778{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003779 struct drm_device *dev = crtc->base.dev;
3780 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03003781
3782 if (!crtc->config.ips_enabled)
3783 return;
3784
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003785 /* We can only enable IPS after we enable a plane and wait for a vblank */
3786 intel_wait_for_vblank(dev, crtc->pipe);
3787
Paulo Zanonid77e4532013-09-24 13:52:55 -03003788 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003789 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003790 mutex_lock(&dev_priv->rps.hw_lock);
3791 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3792 mutex_unlock(&dev_priv->rps.hw_lock);
3793 /* Quoting Art Runyan: "its not safe to expect any particular
3794 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003795 * mailbox." Moreover, the mailbox may return a bogus state,
3796 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003797 */
3798 } else {
3799 I915_WRITE(IPS_CTL, IPS_ENABLE);
3800 /* The bit only becomes 1 in the next vblank, so this wait here
3801 * is essentially intel_wait_for_vblank. If we don't have this
3802 * and don't wait for vblanks until the end of crtc_enable, then
3803 * the HW state readout code will complain that the expected
3804 * IPS_CTL value is not the one we read. */
3805 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3806 DRM_ERROR("Timed out waiting for IPS enable\n");
3807 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003808}
3809
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003810void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003811{
3812 struct drm_device *dev = crtc->base.dev;
3813 struct drm_i915_private *dev_priv = dev->dev_private;
3814
3815 if (!crtc->config.ips_enabled)
3816 return;
3817
3818 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003819 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003820 mutex_lock(&dev_priv->rps.hw_lock);
3821 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3822 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003823 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3824 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3825 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08003826 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003827 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08003828 POSTING_READ(IPS_CTL);
3829 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003830
3831 /* We need to wait for a vblank before we can disable the plane. */
3832 intel_wait_for_vblank(dev, crtc->pipe);
3833}
3834
3835/** Loads the palette/gamma unit for the CRTC with the prepared values */
3836static void intel_crtc_load_lut(struct drm_crtc *crtc)
3837{
3838 struct drm_device *dev = crtc->dev;
3839 struct drm_i915_private *dev_priv = dev->dev_private;
3840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3841 enum pipe pipe = intel_crtc->pipe;
3842 int palreg = PALETTE(pipe);
3843 int i;
3844 bool reenable_ips = false;
3845
3846 /* The clocks have to be on to load the palette. */
3847 if (!crtc->enabled || !intel_crtc->active)
3848 return;
3849
3850 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3851 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3852 assert_dsi_pll_enabled(dev_priv);
3853 else
3854 assert_pll_enabled(dev_priv, pipe);
3855 }
3856
3857 /* use legacy palette for Ironlake */
3858 if (HAS_PCH_SPLIT(dev))
3859 palreg = LGC_PALETTE(pipe);
3860
3861 /* Workaround : Do not read or write the pipe palette/gamma data while
3862 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3863 */
Paulo Zanoni41e6fc42014-01-08 17:26:31 -02003864 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03003865 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3866 GAMMA_MODE_MODE_SPLIT)) {
3867 hsw_disable_ips(intel_crtc);
3868 reenable_ips = true;
3869 }
3870
3871 for (i = 0; i < 256; i++) {
3872 I915_WRITE(palreg + 4 * i,
3873 (intel_crtc->lut_r[i] << 16) |
3874 (intel_crtc->lut_g[i] << 8) |
3875 intel_crtc->lut_b[i]);
3876 }
3877
3878 if (reenable_ips)
3879 hsw_enable_ips(intel_crtc);
3880}
3881
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003882static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3883{
3884 if (!enable && intel_crtc->overlay) {
3885 struct drm_device *dev = intel_crtc->base.dev;
3886 struct drm_i915_private *dev_priv = dev->dev_private;
3887
3888 mutex_lock(&dev->struct_mutex);
3889 dev_priv->mm.interruptible = false;
3890 (void) intel_overlay_switch_off(intel_crtc->overlay);
3891 dev_priv->mm.interruptible = true;
3892 mutex_unlock(&dev->struct_mutex);
3893 }
3894
3895 /* Let userspace switch the overlay on again. In most cases userspace
3896 * has to recompute where to put it anyway.
3897 */
3898}
3899
3900/**
3901 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3902 * cursor plane briefly if not already running after enabling the display
3903 * plane.
3904 * This workaround avoids occasional blank screens when self refresh is
3905 * enabled.
3906 */
3907static void
3908g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3909{
3910 u32 cntl = I915_READ(CURCNTR(pipe));
3911
3912 if ((cntl & CURSOR_MODE) == 0) {
3913 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3914
3915 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3916 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3917 intel_wait_for_vblank(dev_priv->dev, pipe);
3918 I915_WRITE(CURCNTR(pipe), cntl);
3919 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3920 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3921 }
3922}
3923
3924static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003925{
3926 struct drm_device *dev = crtc->dev;
3927 struct drm_i915_private *dev_priv = dev->dev_private;
3928 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3929 int pipe = intel_crtc->pipe;
3930 int plane = intel_crtc->plane;
3931
Ville Syrjäläf98551a2014-05-22 17:48:06 +03003932 drm_vblank_on(dev, pipe);
3933
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003934 intel_enable_primary_hw_plane(dev_priv, plane, pipe);
3935 intel_enable_planes(crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003936 /* The fixup needs to happen before cursor is enabled */
3937 if (IS_G4X(dev))
3938 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003939 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003940 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003941
3942 hsw_enable_ips(intel_crtc);
3943
3944 mutex_lock(&dev->struct_mutex);
3945 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02003946 intel_edp_psr_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003947 mutex_unlock(&dev->struct_mutex);
3948}
3949
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003950static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003951{
3952 struct drm_device *dev = crtc->dev;
3953 struct drm_i915_private *dev_priv = dev->dev_private;
3954 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3955 int pipe = intel_crtc->pipe;
3956 int plane = intel_crtc->plane;
3957
3958 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003959
3960 if (dev_priv->fbc.plane == plane)
3961 intel_disable_fbc(dev);
3962
3963 hsw_disable_ips(intel_crtc);
3964
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003965 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003966 intel_crtc_update_cursor(crtc, false);
3967 intel_disable_planes(crtc);
3968 intel_disable_primary_hw_plane(dev_priv, plane, pipe);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03003969
3970 drm_vblank_off(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003971}
3972
Jesse Barnesf67a5592011-01-05 10:31:48 -08003973static void ironlake_crtc_enable(struct drm_crtc *crtc)
3974{
3975 struct drm_device *dev = crtc->dev;
3976 struct drm_i915_private *dev_priv = dev->dev_private;
3977 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003978 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003979 int pipe = intel_crtc->pipe;
Daniel Vetter29407aa2014-04-24 23:55:08 +02003980 enum plane plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003981
Daniel Vetter08a48462012-07-02 11:43:47 +02003982 WARN_ON(!crtc->enabled);
3983
Jesse Barnesf67a5592011-01-05 10:31:48 -08003984 if (intel_crtc->active)
3985 return;
3986
Daniel Vetterb14b1052014-04-24 23:55:13 +02003987 if (intel_crtc->config.has_pch_encoder)
3988 intel_prepare_shared_dpll(intel_crtc);
3989
Daniel Vetter29407aa2014-04-24 23:55:08 +02003990 if (intel_crtc->config.has_dp_encoder)
3991 intel_dp_set_m_n(intel_crtc);
3992
3993 intel_set_pipe_timings(intel_crtc);
3994
3995 if (intel_crtc->config.has_pch_encoder) {
3996 intel_cpu_transcoder_set_m_n(intel_crtc,
3997 &intel_crtc->config.fdi_m_n);
3998 }
3999
4000 ironlake_set_pipeconf(crtc);
4001
4002 /* Set up the display plane register */
4003 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
4004 POSTING_READ(DSPCNTR(plane));
4005
4006 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4007 crtc->x, crtc->y);
4008
Jesse Barnesf67a5592011-01-05 10:31:48 -08004009 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004010
4011 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4012 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4013
Daniel Vetterf6736a12013-06-05 13:34:30 +02004014 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004015 if (encoder->pre_enable)
4016 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004017
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004018 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004019 /* Note: FDI PLL enabling _must_ be done before we enable the
4020 * cpu pipes, hence this is separate from all the other fdi/pch
4021 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004022 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004023 } else {
4024 assert_fdi_tx_disabled(dev_priv, pipe);
4025 assert_fdi_rx_disabled(dev_priv, pipe);
4026 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004027
Jesse Barnesb074cec2013-04-25 12:55:02 -07004028 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004029
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004030 /*
4031 * On ILK+ LUT must be loaded before the pipe is running but with
4032 * clocks enabled
4033 */
4034 intel_crtc_load_lut(crtc);
4035
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004036 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004037 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004038
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004039 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004040 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004041
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004042 for_each_encoder_on_crtc(dev, crtc, encoder)
4043 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004044
4045 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004046 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004047
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004048 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004049}
4050
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004051/* IPS only exists on ULT machines and is tied to pipe A. */
4052static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4053{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004054 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004055}
4056
Paulo Zanonie4916942013-09-20 16:21:19 -03004057/*
4058 * This implements the workaround described in the "notes" section of the mode
4059 * set sequence documentation. When going from no pipes or single pipe to
4060 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4061 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4062 */
4063static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4064{
4065 struct drm_device *dev = crtc->base.dev;
4066 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4067
4068 /* We want to get the other_active_crtc only if there's only 1 other
4069 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004070 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004071 if (!crtc_it->active || crtc_it == crtc)
4072 continue;
4073
4074 if (other_active_crtc)
4075 return;
4076
4077 other_active_crtc = crtc_it;
4078 }
4079 if (!other_active_crtc)
4080 return;
4081
4082 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4083 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4084}
4085
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004086static void haswell_crtc_enable(struct drm_crtc *crtc)
4087{
4088 struct drm_device *dev = crtc->dev;
4089 struct drm_i915_private *dev_priv = dev->dev_private;
4090 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4091 struct intel_encoder *encoder;
4092 int pipe = intel_crtc->pipe;
Daniel Vetter229fca92014-04-24 23:55:09 +02004093 enum plane plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004094
4095 WARN_ON(!crtc->enabled);
4096
4097 if (intel_crtc->active)
4098 return;
4099
Daniel Vetter229fca92014-04-24 23:55:09 +02004100 if (intel_crtc->config.has_dp_encoder)
4101 intel_dp_set_m_n(intel_crtc);
4102
4103 intel_set_pipe_timings(intel_crtc);
4104
4105 if (intel_crtc->config.has_pch_encoder) {
4106 intel_cpu_transcoder_set_m_n(intel_crtc,
4107 &intel_crtc->config.fdi_m_n);
4108 }
4109
4110 haswell_set_pipeconf(crtc);
4111
4112 intel_set_pipe_csc(crtc);
4113
4114 /* Set up the display plane register */
4115 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
4116 POSTING_READ(DSPCNTR(plane));
4117
4118 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4119 crtc->x, crtc->y);
4120
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004121 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004122
4123 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4124 if (intel_crtc->config.has_pch_encoder)
4125 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4126
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004127 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02004128 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004129
4130 for_each_encoder_on_crtc(dev, crtc, encoder)
4131 if (encoder->pre_enable)
4132 encoder->pre_enable(encoder);
4133
Paulo Zanoni1f544382012-10-24 11:32:00 -02004134 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004135
Jesse Barnesb074cec2013-04-25 12:55:02 -07004136 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004137
4138 /*
4139 * On ILK+ LUT must be loaded before the pipe is running but with
4140 * clocks enabled
4141 */
4142 intel_crtc_load_lut(crtc);
4143
Paulo Zanoni1f544382012-10-24 11:32:00 -02004144 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004145 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004146
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004147 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004148 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004149
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004150 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004151 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004152
Jani Nikula8807e552013-08-30 19:40:32 +03004153 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004154 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004155 intel_opregion_notify_encoder(encoder, true);
4156 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004157
Paulo Zanonie4916942013-09-20 16:21:19 -03004158 /* If we change the relative order between pipe/planes enabling, we need
4159 * to change the workaround. */
4160 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004161 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004162}
4163
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004164static void ironlake_pfit_disable(struct intel_crtc *crtc)
4165{
4166 struct drm_device *dev = crtc->base.dev;
4167 struct drm_i915_private *dev_priv = dev->dev_private;
4168 int pipe = crtc->pipe;
4169
4170 /* To avoid upsetting the power well on haswell only disable the pfit if
4171 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01004172 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004173 I915_WRITE(PF_CTL(pipe), 0);
4174 I915_WRITE(PF_WIN_POS(pipe), 0);
4175 I915_WRITE(PF_WIN_SZ(pipe), 0);
4176 }
4177}
4178
Jesse Barnes6be4a602010-09-10 10:26:01 -07004179static void ironlake_crtc_disable(struct drm_crtc *crtc)
4180{
4181 struct drm_device *dev = crtc->dev;
4182 struct drm_i915_private *dev_priv = dev->dev_private;
4183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004184 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004185 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004186 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004187
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004188 if (!intel_crtc->active)
4189 return;
4190
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004191 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004192
Daniel Vetterea9d7582012-07-10 10:42:52 +02004193 for_each_encoder_on_crtc(dev, crtc, encoder)
4194 encoder->disable(encoder);
4195
Daniel Vetterd925c592013-06-05 13:34:04 +02004196 if (intel_crtc->config.has_pch_encoder)
4197 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4198
Jesse Barnesb24e7172011-01-04 15:09:30 -08004199 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004200
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004201 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004202
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004203 for_each_encoder_on_crtc(dev, crtc, encoder)
4204 if (encoder->post_disable)
4205 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004206
Daniel Vetterd925c592013-06-05 13:34:04 +02004207 if (intel_crtc->config.has_pch_encoder) {
4208 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004209
Daniel Vetterd925c592013-06-05 13:34:04 +02004210 ironlake_disable_pch_transcoder(dev_priv, pipe);
4211 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004212
Daniel Vetterd925c592013-06-05 13:34:04 +02004213 if (HAS_PCH_CPT(dev)) {
4214 /* disable TRANS_DP_CTL */
4215 reg = TRANS_DP_CTL(pipe);
4216 temp = I915_READ(reg);
4217 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4218 TRANS_DP_PORT_SEL_MASK);
4219 temp |= TRANS_DP_PORT_SEL_NONE;
4220 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004221
Daniel Vetterd925c592013-06-05 13:34:04 +02004222 /* disable DPLL_SEL */
4223 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004224 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004225 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004226 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004227
4228 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004229 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004230
4231 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004232 }
4233
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004234 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004235 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004236
4237 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004238 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02004239 intel_edp_psr_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004240 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004241}
4242
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004243static void haswell_crtc_disable(struct drm_crtc *crtc)
4244{
4245 struct drm_device *dev = crtc->dev;
4246 struct drm_i915_private *dev_priv = dev->dev_private;
4247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4248 struct intel_encoder *encoder;
4249 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004250 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004251
4252 if (!intel_crtc->active)
4253 return;
4254
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004255 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004256
Jani Nikula8807e552013-08-30 19:40:32 +03004257 for_each_encoder_on_crtc(dev, crtc, encoder) {
4258 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004259 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004260 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004261
Paulo Zanoni86642812013-04-12 17:57:57 -03004262 if (intel_crtc->config.has_pch_encoder)
4263 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004264 intel_disable_pipe(dev_priv, pipe);
4265
Paulo Zanoniad80a812012-10-24 16:06:19 -02004266 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004267
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004268 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004269
Paulo Zanoni1f544382012-10-24 11:32:00 -02004270 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004271
4272 for_each_encoder_on_crtc(dev, crtc, encoder)
4273 if (encoder->post_disable)
4274 encoder->post_disable(encoder);
4275
Daniel Vetter88adfff2013-03-28 10:42:01 +01004276 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004277 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03004278 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004279 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004280 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004281
4282 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004283 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004284
4285 mutex_lock(&dev->struct_mutex);
4286 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02004287 intel_edp_psr_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004288 mutex_unlock(&dev->struct_mutex);
4289}
4290
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004291static void ironlake_crtc_off(struct drm_crtc *crtc)
4292{
4293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004294 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004295}
4296
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004297static void haswell_crtc_off(struct drm_crtc *crtc)
4298{
4299 intel_ddi_put_crtc_pll(crtc);
4300}
4301
Jesse Barnes2dd24552013-04-25 12:55:01 -07004302static void i9xx_pfit_enable(struct intel_crtc *crtc)
4303{
4304 struct drm_device *dev = crtc->base.dev;
4305 struct drm_i915_private *dev_priv = dev->dev_private;
4306 struct intel_crtc_config *pipe_config = &crtc->config;
4307
Daniel Vetter328d8e82013-05-08 10:36:31 +02004308 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004309 return;
4310
Daniel Vetterc0b03412013-05-28 12:05:54 +02004311 /*
4312 * The panel fitter should only be adjusted whilst the pipe is disabled,
4313 * according to register description and PRM.
4314 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004315 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4316 assert_pipe_disabled(dev_priv, crtc->pipe);
4317
Jesse Barnesb074cec2013-04-25 12:55:02 -07004318 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4319 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004320
4321 /* Border color in case we don't scale up to the full screen. Black by
4322 * default, change to something else for debugging. */
4323 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004324}
4325
Imre Deak77d22dc2014-03-05 16:20:52 +02004326#define for_each_power_domain(domain, mask) \
4327 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4328 if ((1 << (domain)) & (mask))
4329
Imre Deak319be8a2014-03-04 19:22:57 +02004330enum intel_display_power_domain
4331intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004332{
Imre Deak319be8a2014-03-04 19:22:57 +02004333 struct drm_device *dev = intel_encoder->base.dev;
4334 struct intel_digital_port *intel_dig_port;
4335
4336 switch (intel_encoder->type) {
4337 case INTEL_OUTPUT_UNKNOWN:
4338 /* Only DDI platforms should ever use this output type */
4339 WARN_ON_ONCE(!HAS_DDI(dev));
4340 case INTEL_OUTPUT_DISPLAYPORT:
4341 case INTEL_OUTPUT_HDMI:
4342 case INTEL_OUTPUT_EDP:
4343 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4344 switch (intel_dig_port->port) {
4345 case PORT_A:
4346 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4347 case PORT_B:
4348 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4349 case PORT_C:
4350 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4351 case PORT_D:
4352 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4353 default:
4354 WARN_ON_ONCE(1);
4355 return POWER_DOMAIN_PORT_OTHER;
4356 }
4357 case INTEL_OUTPUT_ANALOG:
4358 return POWER_DOMAIN_PORT_CRT;
4359 case INTEL_OUTPUT_DSI:
4360 return POWER_DOMAIN_PORT_DSI;
4361 default:
4362 return POWER_DOMAIN_PORT_OTHER;
4363 }
4364}
4365
4366static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4367{
4368 struct drm_device *dev = crtc->dev;
4369 struct intel_encoder *intel_encoder;
4370 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4371 enum pipe pipe = intel_crtc->pipe;
4372 bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
Imre Deak77d22dc2014-03-05 16:20:52 +02004373 unsigned long mask;
4374 enum transcoder transcoder;
4375
4376 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4377
4378 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4379 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4380 if (pfit_enabled)
4381 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4382
Imre Deak319be8a2014-03-04 19:22:57 +02004383 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4384 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4385
Imre Deak77d22dc2014-03-05 16:20:52 +02004386 return mask;
4387}
4388
4389void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4390 bool enable)
4391{
4392 if (dev_priv->power_domains.init_power_on == enable)
4393 return;
4394
4395 if (enable)
4396 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4397 else
4398 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4399
4400 dev_priv->power_domains.init_power_on = enable;
4401}
4402
4403static void modeset_update_crtc_power_domains(struct drm_device *dev)
4404{
4405 struct drm_i915_private *dev_priv = dev->dev_private;
4406 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4407 struct intel_crtc *crtc;
4408
4409 /*
4410 * First get all needed power domains, then put all unneeded, to avoid
4411 * any unnecessary toggling of the power wells.
4412 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004413 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004414 enum intel_display_power_domain domain;
4415
4416 if (!crtc->base.enabled)
4417 continue;
4418
Imre Deak319be8a2014-03-04 19:22:57 +02004419 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004420
4421 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4422 intel_display_power_get(dev_priv, domain);
4423 }
4424
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004425 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004426 enum intel_display_power_domain domain;
4427
4428 for_each_power_domain(domain, crtc->enabled_power_domains)
4429 intel_display_power_put(dev_priv, domain);
4430
4431 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4432 }
4433
4434 intel_display_set_init_power(dev_priv, false);
4435}
4436
Jesse Barnes586f49d2013-11-04 16:06:59 -08004437int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004438{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004439 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004440
Jesse Barnes586f49d2013-11-04 16:06:59 -08004441 /* Obtain SKU information */
4442 mutex_lock(&dev_priv->dpio_lock);
4443 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4444 CCK_FUSE_HPLL_FREQ_MASK;
4445 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004446
Jesse Barnes586f49d2013-11-04 16:06:59 -08004447 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08004448}
4449
4450/* Adjust CDclk dividers to allow high res or save power if possible */
4451static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4452{
4453 struct drm_i915_private *dev_priv = dev->dev_private;
4454 u32 val, cmd;
4455
Imre Deakd60c4472014-03-27 17:45:10 +02004456 WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
4457 dev_priv->vlv_cdclk_freq = cdclk;
4458
Jesse Barnes30a970c2013-11-04 13:48:12 -08004459 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
4460 cmd = 2;
4461 else if (cdclk == 266)
4462 cmd = 1;
4463 else
4464 cmd = 0;
4465
4466 mutex_lock(&dev_priv->rps.hw_lock);
4467 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4468 val &= ~DSPFREQGUAR_MASK;
4469 val |= (cmd << DSPFREQGUAR_SHIFT);
4470 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4471 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4472 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4473 50)) {
4474 DRM_ERROR("timed out waiting for CDclk change\n");
4475 }
4476 mutex_unlock(&dev_priv->rps.hw_lock);
4477
4478 if (cdclk == 400) {
4479 u32 divider, vco;
4480
4481 vco = valleyview_get_vco(dev_priv);
4482 divider = ((vco << 1) / cdclk) - 1;
4483
4484 mutex_lock(&dev_priv->dpio_lock);
4485 /* adjust cdclk divider */
4486 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4487 val &= ~0xf;
4488 val |= divider;
4489 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4490 mutex_unlock(&dev_priv->dpio_lock);
4491 }
4492
4493 mutex_lock(&dev_priv->dpio_lock);
4494 /* adjust self-refresh exit latency value */
4495 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4496 val &= ~0x7f;
4497
4498 /*
4499 * For high bandwidth configs, we set a higher latency in the bunit
4500 * so that the core display fetch happens in time to avoid underruns.
4501 */
4502 if (cdclk == 400)
4503 val |= 4500 / 250; /* 4.5 usec */
4504 else
4505 val |= 3000 / 250; /* 3.0 usec */
4506 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4507 mutex_unlock(&dev_priv->dpio_lock);
4508
4509 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4510 intel_i2c_reset(dev);
4511}
4512
Imre Deakd60c4472014-03-27 17:45:10 +02004513int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004514{
4515 int cur_cdclk, vco;
4516 int divider;
4517
4518 vco = valleyview_get_vco(dev_priv);
4519
4520 mutex_lock(&dev_priv->dpio_lock);
4521 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4522 mutex_unlock(&dev_priv->dpio_lock);
4523
4524 divider &= 0xf;
4525
4526 cur_cdclk = (vco << 1) / (divider + 1);
4527
4528 return cur_cdclk;
4529}
4530
4531static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4532 int max_pixclk)
4533{
Jesse Barnes30a970c2013-11-04 13:48:12 -08004534 /*
4535 * Really only a few cases to deal with, as only 4 CDclks are supported:
4536 * 200MHz
4537 * 267MHz
4538 * 320MHz
4539 * 400MHz
4540 * So we check to see whether we're above 90% of the lower bin and
4541 * adjust if needed.
4542 */
4543 if (max_pixclk > 288000) {
4544 return 400;
4545 } else if (max_pixclk > 240000) {
4546 return 320;
4547 } else
4548 return 266;
4549 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4550}
4551
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004552/* compute the max pixel clock for new configuration */
4553static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004554{
4555 struct drm_device *dev = dev_priv->dev;
4556 struct intel_crtc *intel_crtc;
4557 int max_pixclk = 0;
4558
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004559 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004560 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004561 max_pixclk = max(max_pixclk,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004562 intel_crtc->new_config->adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004563 }
4564
4565 return max_pixclk;
4566}
4567
4568static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004569 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004570{
4571 struct drm_i915_private *dev_priv = dev->dev_private;
4572 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004573 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004574
Imre Deakd60c4472014-03-27 17:45:10 +02004575 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4576 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004577 return;
4578
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004579 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004580 for_each_intel_crtc(dev, intel_crtc)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004581 if (intel_crtc->base.enabled)
4582 *prepare_pipes |= (1 << intel_crtc->pipe);
4583}
4584
4585static void valleyview_modeset_global_resources(struct drm_device *dev)
4586{
4587 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004588 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004589 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4590
Imre Deakd60c4472014-03-27 17:45:10 +02004591 if (req_cdclk != dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004592 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak77961eb2014-03-05 16:20:56 +02004593 modeset_update_crtc_power_domains(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004594}
4595
Jesse Barnes89b667f2013-04-18 14:51:36 -07004596static void valleyview_crtc_enable(struct drm_crtc *crtc)
4597{
4598 struct drm_device *dev = crtc->dev;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004599 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004600 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4601 struct intel_encoder *encoder;
4602 int pipe = intel_crtc->pipe;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004603 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03004604 bool is_dsi;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004605 u32 dspcntr;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004606
4607 WARN_ON(!crtc->enabled);
4608
4609 if (intel_crtc->active)
4610 return;
4611
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02004612 vlv_prepare_pll(intel_crtc);
4613
Daniel Vetter5b18e572014-04-24 23:55:06 +02004614 /* Set up the display plane register */
4615 dspcntr = DISPPLANE_GAMMA_ENABLE;
4616
4617 if (intel_crtc->config.has_dp_encoder)
4618 intel_dp_set_m_n(intel_crtc);
4619
4620 intel_set_pipe_timings(intel_crtc);
4621
4622 /* pipesrc and dspsize control the size that is scaled from,
4623 * which should always be the user's requested size.
4624 */
4625 I915_WRITE(DSPSIZE(plane),
4626 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4627 (intel_crtc->config.pipe_src_w - 1));
4628 I915_WRITE(DSPPOS(plane), 0);
4629
4630 i9xx_set_pipeconf(intel_crtc);
4631
4632 I915_WRITE(DSPCNTR(plane), dspcntr);
4633 POSTING_READ(DSPCNTR(plane));
4634
4635 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4636 crtc->x, crtc->y);
4637
Jesse Barnes89b667f2013-04-18 14:51:36 -07004638 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004639
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004640 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4641
Jesse Barnes89b667f2013-04-18 14:51:36 -07004642 for_each_encoder_on_crtc(dev, crtc, encoder)
4643 if (encoder->pre_pll_enable)
4644 encoder->pre_pll_enable(encoder);
4645
Jani Nikula23538ef2013-08-27 15:12:22 +03004646 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4647
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004648 if (!is_dsi) {
4649 if (IS_CHERRYVIEW(dev))
4650 chv_enable_pll(intel_crtc);
4651 else
4652 vlv_enable_pll(intel_crtc);
4653 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07004654
4655 for_each_encoder_on_crtc(dev, crtc, encoder)
4656 if (encoder->pre_enable)
4657 encoder->pre_enable(encoder);
4658
Jesse Barnes2dd24552013-04-25 12:55:01 -07004659 i9xx_pfit_enable(intel_crtc);
4660
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004661 intel_crtc_load_lut(crtc);
4662
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004663 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004664 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004665
Jani Nikula50049452013-07-30 12:20:32 +03004666 for_each_encoder_on_crtc(dev, crtc, encoder)
4667 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004668
4669 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004670
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004671 /* Underruns don't raise interrupts, so check manually. */
4672 i9xx_check_fifo_underruns(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004673}
4674
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004675static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4676{
4677 struct drm_device *dev = crtc->base.dev;
4678 struct drm_i915_private *dev_priv = dev->dev_private;
4679
4680 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4681 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4682}
4683
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004684static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004685{
4686 struct drm_device *dev = crtc->dev;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004687 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08004688 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004689 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004690 int pipe = intel_crtc->pipe;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004691 int plane = intel_crtc->plane;
4692 u32 dspcntr;
Jesse Barnes79e53942008-11-07 14:24:08 -08004693
Daniel Vetter08a48462012-07-02 11:43:47 +02004694 WARN_ON(!crtc->enabled);
4695
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004696 if (intel_crtc->active)
4697 return;
4698
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004699 i9xx_set_pll_dividers(intel_crtc);
4700
Daniel Vetter5b18e572014-04-24 23:55:06 +02004701 /* Set up the display plane register */
4702 dspcntr = DISPPLANE_GAMMA_ENABLE;
4703
4704 if (pipe == 0)
4705 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4706 else
4707 dspcntr |= DISPPLANE_SEL_PIPE_B;
4708
4709 if (intel_crtc->config.has_dp_encoder)
4710 intel_dp_set_m_n(intel_crtc);
4711
4712 intel_set_pipe_timings(intel_crtc);
4713
4714 /* pipesrc and dspsize control the size that is scaled from,
4715 * which should always be the user's requested size.
4716 */
4717 I915_WRITE(DSPSIZE(plane),
4718 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4719 (intel_crtc->config.pipe_src_w - 1));
4720 I915_WRITE(DSPPOS(plane), 0);
4721
4722 i9xx_set_pipeconf(intel_crtc);
4723
4724 I915_WRITE(DSPCNTR(plane), dspcntr);
4725 POSTING_READ(DSPCNTR(plane));
4726
4727 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4728 crtc->x, crtc->y);
4729
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004730 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004731
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004732 if (!IS_GEN2(dev))
4733 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4734
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004735 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004736 if (encoder->pre_enable)
4737 encoder->pre_enable(encoder);
4738
Daniel Vetterf6736a12013-06-05 13:34:30 +02004739 i9xx_enable_pll(intel_crtc);
4740
Jesse Barnes2dd24552013-04-25 12:55:01 -07004741 i9xx_pfit_enable(intel_crtc);
4742
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004743 intel_crtc_load_lut(crtc);
4744
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004745 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004746 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004747
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004748 for_each_encoder_on_crtc(dev, crtc, encoder)
4749 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004750
4751 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004752
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004753 /*
4754 * Gen2 reports pipe underruns whenever all planes are disabled.
4755 * So don't enable underrun reporting before at least some planes
4756 * are enabled.
4757 * FIXME: Need to fix the logic to work when we turn off all planes
4758 * but leave the pipe running.
4759 */
4760 if (IS_GEN2(dev))
4761 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4762
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004763 /* Underruns don't raise interrupts, so check manually. */
4764 i9xx_check_fifo_underruns(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004765}
4766
Daniel Vetter87476d62013-04-11 16:29:06 +02004767static void i9xx_pfit_disable(struct intel_crtc *crtc)
4768{
4769 struct drm_device *dev = crtc->base.dev;
4770 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004771
4772 if (!crtc->config.gmch_pfit.control)
4773 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004774
4775 assert_pipe_disabled(dev_priv, crtc->pipe);
4776
Daniel Vetter328d8e82013-05-08 10:36:31 +02004777 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4778 I915_READ(PFIT_CONTROL));
4779 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004780}
4781
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004782static void i9xx_crtc_disable(struct drm_crtc *crtc)
4783{
4784 struct drm_device *dev = crtc->dev;
4785 struct drm_i915_private *dev_priv = dev->dev_private;
4786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004787 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004788 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004789
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004790 if (!intel_crtc->active)
4791 return;
4792
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004793 /*
4794 * Gen2 reports pipe underruns whenever all planes are disabled.
4795 * So diasble underrun reporting before all the planes get disabled.
4796 * FIXME: Need to fix the logic to work when we turn off all planes
4797 * but leave the pipe running.
4798 */
4799 if (IS_GEN2(dev))
4800 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4801
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004802 intel_crtc_disable_planes(crtc);
4803
Daniel Vetterea9d7582012-07-10 10:42:52 +02004804 for_each_encoder_on_crtc(dev, crtc, encoder)
4805 encoder->disable(encoder);
4806
Ville Syrjälä6304cd92014-04-25 13:30:12 +03004807 /*
4808 * On gen2 planes are double buffered but the pipe isn't, so we must
4809 * wait for planes to fully turn off before disabling the pipe.
4810 */
4811 if (IS_GEN2(dev))
4812 intel_wait_for_vblank(dev, pipe);
4813
Jesse Barnesb24e7172011-01-04 15:09:30 -08004814 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004815
Daniel Vetter87476d62013-04-11 16:29:06 +02004816 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004817
Jesse Barnes89b667f2013-04-18 14:51:36 -07004818 for_each_encoder_on_crtc(dev, crtc, encoder)
4819 if (encoder->post_disable)
4820 encoder->post_disable(encoder);
4821
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03004822 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4823 if (IS_CHERRYVIEW(dev))
4824 chv_disable_pll(dev_priv, pipe);
4825 else if (IS_VALLEYVIEW(dev))
4826 vlv_disable_pll(dev_priv, pipe);
4827 else
4828 i9xx_disable_pll(dev_priv, pipe);
4829 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004830
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004831 if (!IS_GEN2(dev))
4832 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4833
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004834 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004835 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004836
Daniel Vetterefa96242014-04-24 23:55:02 +02004837 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004838 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02004839 intel_edp_psr_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02004840 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004841}
4842
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004843static void i9xx_crtc_off(struct drm_crtc *crtc)
4844{
4845}
4846
Daniel Vetter976f8a22012-07-08 22:34:21 +02004847static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4848 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004849{
4850 struct drm_device *dev = crtc->dev;
4851 struct drm_i915_master_private *master_priv;
4852 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4853 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004854
4855 if (!dev->primary->master)
4856 return;
4857
4858 master_priv = dev->primary->master->driver_priv;
4859 if (!master_priv->sarea_priv)
4860 return;
4861
Jesse Barnes79e53942008-11-07 14:24:08 -08004862 switch (pipe) {
4863 case 0:
4864 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4865 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4866 break;
4867 case 1:
4868 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4869 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4870 break;
4871 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004872 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004873 break;
4874 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004875}
4876
Daniel Vetter976f8a22012-07-08 22:34:21 +02004877/**
4878 * Sets the power management mode of the pipe and plane.
4879 */
4880void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004881{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004882 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004883 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02004884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004885 struct intel_encoder *intel_encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02004886 enum intel_display_power_domain domain;
4887 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004888 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004889
Daniel Vetter976f8a22012-07-08 22:34:21 +02004890 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4891 enable |= intel_encoder->connectors_active;
4892
Daniel Vetter0e572fe2014-04-24 23:55:42 +02004893 if (enable) {
4894 if (!intel_crtc->active) {
4895 /*
4896 * FIXME: DDI plls and relevant code isn't converted
4897 * yet, so do runtime PM for DPMS only for all other
4898 * platforms for now.
4899 */
4900 if (!HAS_DDI(dev)) {
4901 domains = get_crtc_power_domains(crtc);
4902 for_each_power_domain(domain, domains)
4903 intel_display_power_get(dev_priv, domain);
4904 intel_crtc->enabled_power_domains = domains;
4905 }
4906
4907 dev_priv->display.crtc_enable(crtc);
4908 }
4909 } else {
4910 if (intel_crtc->active) {
4911 dev_priv->display.crtc_disable(crtc);
4912
4913 if (!HAS_DDI(dev)) {
4914 domains = intel_crtc->enabled_power_domains;
4915 for_each_power_domain(domain, domains)
4916 intel_display_power_put(dev_priv, domain);
4917 intel_crtc->enabled_power_domains = 0;
4918 }
4919 }
4920 }
Daniel Vetter976f8a22012-07-08 22:34:21 +02004921
4922 intel_crtc_update_sarea(crtc, enable);
4923}
4924
Daniel Vetter976f8a22012-07-08 22:34:21 +02004925static void intel_crtc_disable(struct drm_crtc *crtc)
4926{
4927 struct drm_device *dev = crtc->dev;
4928 struct drm_connector *connector;
4929 struct drm_i915_private *dev_priv = dev->dev_private;
4930
4931 /* crtc should still be enabled when we disable it. */
4932 WARN_ON(!crtc->enabled);
4933
4934 dev_priv->display.crtc_disable(crtc);
4935 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004936 dev_priv->display.off(crtc);
4937
Chris Wilson931872f2012-01-16 23:01:13 +00004938 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004939 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004940 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004941
Matt Roperf4510a22014-04-01 15:22:40 -07004942 if (crtc->primary->fb) {
Chris Wilsoncdd59982010-09-08 16:30:16 +01004943 mutex_lock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07004944 intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004945 mutex_unlock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07004946 crtc->primary->fb = NULL;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004947 }
4948
4949 /* Update computed state. */
4950 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4951 if (!connector->encoder || !connector->encoder->crtc)
4952 continue;
4953
4954 if (connector->encoder->crtc != crtc)
4955 continue;
4956
4957 connector->dpms = DRM_MODE_DPMS_OFF;
4958 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004959 }
4960}
4961
Chris Wilsonea5b2132010-08-04 13:50:23 +01004962void intel_encoder_destroy(struct drm_encoder *encoder)
4963{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004964 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004965
Chris Wilsonea5b2132010-08-04 13:50:23 +01004966 drm_encoder_cleanup(encoder);
4967 kfree(intel_encoder);
4968}
4969
Damien Lespiau92373292013-08-08 22:28:57 +01004970/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004971 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4972 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004973static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004974{
4975 if (mode == DRM_MODE_DPMS_ON) {
4976 encoder->connectors_active = true;
4977
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004978 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004979 } else {
4980 encoder->connectors_active = false;
4981
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004982 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004983 }
4984}
4985
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004986/* Cross check the actual hw state with our own modeset state tracking (and it's
4987 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004988static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004989{
4990 if (connector->get_hw_state(connector)) {
4991 struct intel_encoder *encoder = connector->encoder;
4992 struct drm_crtc *crtc;
4993 bool encoder_enabled;
4994 enum pipe pipe;
4995
4996 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4997 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03004998 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004999
5000 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5001 "wrong connector dpms state\n");
5002 WARN(connector->base.encoder != &encoder->base,
5003 "active connector not linked to encoder\n");
5004 WARN(!encoder->connectors_active,
5005 "encoder->connectors_active not set\n");
5006
5007 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5008 WARN(!encoder_enabled, "encoder not enabled\n");
5009 if (WARN_ON(!encoder->base.crtc))
5010 return;
5011
5012 crtc = encoder->base.crtc;
5013
5014 WARN(!crtc->enabled, "crtc not enabled\n");
5015 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5016 WARN(pipe != to_intel_crtc(crtc)->pipe,
5017 "encoder active on the wrong pipe\n");
5018 }
5019}
5020
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005021/* Even simpler default implementation, if there's really no special case to
5022 * consider. */
5023void intel_connector_dpms(struct drm_connector *connector, int mode)
5024{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005025 /* All the simple cases only support two dpms states. */
5026 if (mode != DRM_MODE_DPMS_ON)
5027 mode = DRM_MODE_DPMS_OFF;
5028
5029 if (mode == connector->dpms)
5030 return;
5031
5032 connector->dpms = mode;
5033
5034 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01005035 if (connector->encoder)
5036 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005037
Daniel Vetterb9805142012-08-31 17:37:33 +02005038 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005039}
5040
Daniel Vetterf0947c32012-07-02 13:10:34 +02005041/* Simple connector->get_hw_state implementation for encoders that support only
5042 * one connector and no cloning and hence the encoder state determines the state
5043 * of the connector. */
5044bool intel_connector_get_hw_state(struct intel_connector *connector)
5045{
Daniel Vetter24929352012-07-02 20:28:59 +02005046 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005047 struct intel_encoder *encoder = connector->encoder;
5048
5049 return encoder->get_hw_state(encoder, &pipe);
5050}
5051
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005052static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5053 struct intel_crtc_config *pipe_config)
5054{
5055 struct drm_i915_private *dev_priv = dev->dev_private;
5056 struct intel_crtc *pipe_B_crtc =
5057 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5058
5059 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5060 pipe_name(pipe), pipe_config->fdi_lanes);
5061 if (pipe_config->fdi_lanes > 4) {
5062 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5063 pipe_name(pipe), pipe_config->fdi_lanes);
5064 return false;
5065 }
5066
Paulo Zanonibafb6552013-11-02 21:07:44 -07005067 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005068 if (pipe_config->fdi_lanes > 2) {
5069 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5070 pipe_config->fdi_lanes);
5071 return false;
5072 } else {
5073 return true;
5074 }
5075 }
5076
5077 if (INTEL_INFO(dev)->num_pipes == 2)
5078 return true;
5079
5080 /* Ivybridge 3 pipe is really complicated */
5081 switch (pipe) {
5082 case PIPE_A:
5083 return true;
5084 case PIPE_B:
5085 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5086 pipe_config->fdi_lanes > 2) {
5087 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5088 pipe_name(pipe), pipe_config->fdi_lanes);
5089 return false;
5090 }
5091 return true;
5092 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01005093 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005094 pipe_B_crtc->config.fdi_lanes <= 2) {
5095 if (pipe_config->fdi_lanes > 2) {
5096 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5097 pipe_name(pipe), pipe_config->fdi_lanes);
5098 return false;
5099 }
5100 } else {
5101 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5102 return false;
5103 }
5104 return true;
5105 default:
5106 BUG();
5107 }
5108}
5109
Daniel Vettere29c22c2013-02-21 00:00:16 +01005110#define RETRY 1
5111static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5112 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005113{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005114 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005115 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005116 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005117 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005118
Daniel Vettere29c22c2013-02-21 00:00:16 +01005119retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005120 /* FDI is a binary signal running at ~2.7GHz, encoding
5121 * each output octet as 10 bits. The actual frequency
5122 * is stored as a divider into a 100MHz clock, and the
5123 * mode pixel clock is stored in units of 1KHz.
5124 * Hence the bw of each lane in terms of the mode signal
5125 * is:
5126 */
5127 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5128
Damien Lespiau241bfc32013-09-25 16:45:37 +01005129 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005130
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005131 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005132 pipe_config->pipe_bpp);
5133
5134 pipe_config->fdi_lanes = lane;
5135
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005136 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005137 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005138
Daniel Vettere29c22c2013-02-21 00:00:16 +01005139 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5140 intel_crtc->pipe, pipe_config);
5141 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5142 pipe_config->pipe_bpp -= 2*3;
5143 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5144 pipe_config->pipe_bpp);
5145 needs_recompute = true;
5146 pipe_config->bw_constrained = true;
5147
5148 goto retry;
5149 }
5150
5151 if (needs_recompute)
5152 return RETRY;
5153
5154 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005155}
5156
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005157static void hsw_compute_ips_config(struct intel_crtc *crtc,
5158 struct intel_crtc_config *pipe_config)
5159{
Jani Nikulad330a952014-01-21 11:24:25 +02005160 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005161 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005162 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005163}
5164
Daniel Vettera43f6e02013-06-07 23:10:32 +02005165static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01005166 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005167{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005168 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005169 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005170
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005171 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005172 if (INTEL_INFO(dev)->gen < 4) {
5173 struct drm_i915_private *dev_priv = dev->dev_private;
5174 int clock_limit =
5175 dev_priv->display.get_display_clock_speed(dev);
5176
5177 /*
5178 * Enable pixel doubling when the dot clock
5179 * is > 90% of the (display) core speed.
5180 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005181 * GDG double wide on either pipe,
5182 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005183 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005184 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005185 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005186 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005187 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005188 }
5189
Damien Lespiau241bfc32013-09-25 16:45:37 +01005190 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005191 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005192 }
Chris Wilson89749352010-09-12 18:25:19 +01005193
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005194 /*
5195 * Pipe horizontal size must be even in:
5196 * - DVO ganged mode
5197 * - LVDS dual channel mode
5198 * - Double wide pipe
5199 */
5200 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5201 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5202 pipe_config->pipe_src_w &= ~1;
5203
Damien Lespiau8693a822013-05-03 18:48:11 +01005204 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5205 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005206 */
5207 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5208 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005209 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005210
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005211 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005212 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005213 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005214 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5215 * for lvds. */
5216 pipe_config->pipe_bpp = 8*3;
5217 }
5218
Damien Lespiauf5adf942013-06-24 18:29:34 +01005219 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005220 hsw_compute_ips_config(crtc, pipe_config);
5221
5222 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
5223 * clock survives for now. */
5224 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5225 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005226
Daniel Vetter877d48d2013-04-19 11:24:43 +02005227 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005228 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005229
Daniel Vettere29c22c2013-02-21 00:00:16 +01005230 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005231}
5232
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005233static int valleyview_get_display_clock_speed(struct drm_device *dev)
5234{
5235 return 400000; /* FIXME */
5236}
5237
Jesse Barnese70236a2009-09-21 10:42:27 -07005238static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005239{
Jesse Barnese70236a2009-09-21 10:42:27 -07005240 return 400000;
5241}
Jesse Barnes79e53942008-11-07 14:24:08 -08005242
Jesse Barnese70236a2009-09-21 10:42:27 -07005243static int i915_get_display_clock_speed(struct drm_device *dev)
5244{
5245 return 333000;
5246}
Jesse Barnes79e53942008-11-07 14:24:08 -08005247
Jesse Barnese70236a2009-09-21 10:42:27 -07005248static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5249{
5250 return 200000;
5251}
Jesse Barnes79e53942008-11-07 14:24:08 -08005252
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005253static int pnv_get_display_clock_speed(struct drm_device *dev)
5254{
5255 u16 gcfgc = 0;
5256
5257 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5258
5259 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5260 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5261 return 267000;
5262 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5263 return 333000;
5264 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5265 return 444000;
5266 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5267 return 200000;
5268 default:
5269 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5270 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5271 return 133000;
5272 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5273 return 167000;
5274 }
5275}
5276
Jesse Barnese70236a2009-09-21 10:42:27 -07005277static int i915gm_get_display_clock_speed(struct drm_device *dev)
5278{
5279 u16 gcfgc = 0;
5280
5281 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5282
5283 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005284 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005285 else {
5286 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5287 case GC_DISPLAY_CLOCK_333_MHZ:
5288 return 333000;
5289 default:
5290 case GC_DISPLAY_CLOCK_190_200_MHZ:
5291 return 190000;
5292 }
5293 }
5294}
Jesse Barnes79e53942008-11-07 14:24:08 -08005295
Jesse Barnese70236a2009-09-21 10:42:27 -07005296static int i865_get_display_clock_speed(struct drm_device *dev)
5297{
5298 return 266000;
5299}
5300
5301static int i855_get_display_clock_speed(struct drm_device *dev)
5302{
5303 u16 hpllcc = 0;
5304 /* Assume that the hardware is in the high speed state. This
5305 * should be the default.
5306 */
5307 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5308 case GC_CLOCK_133_200:
5309 case GC_CLOCK_100_200:
5310 return 200000;
5311 case GC_CLOCK_166_250:
5312 return 250000;
5313 case GC_CLOCK_100_133:
5314 return 133000;
5315 }
5316
5317 /* Shouldn't happen */
5318 return 0;
5319}
5320
5321static int i830_get_display_clock_speed(struct drm_device *dev)
5322{
5323 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005324}
5325
Zhenyu Wang2c072452009-06-05 15:38:42 +08005326static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005327intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005328{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005329 while (*num > DATA_LINK_M_N_MASK ||
5330 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005331 *num >>= 1;
5332 *den >>= 1;
5333 }
5334}
5335
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005336static void compute_m_n(unsigned int m, unsigned int n,
5337 uint32_t *ret_m, uint32_t *ret_n)
5338{
5339 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5340 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5341 intel_reduce_m_n_ratio(ret_m, ret_n);
5342}
5343
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005344void
5345intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5346 int pixel_clock, int link_clock,
5347 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005348{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005349 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005350
5351 compute_m_n(bits_per_pixel * pixel_clock,
5352 link_clock * nlanes * 8,
5353 &m_n->gmch_m, &m_n->gmch_n);
5354
5355 compute_m_n(pixel_clock, link_clock,
5356 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005357}
5358
Chris Wilsona7615032011-01-12 17:04:08 +00005359static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5360{
Jani Nikulad330a952014-01-21 11:24:25 +02005361 if (i915.panel_use_ssc >= 0)
5362 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005363 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005364 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005365}
5366
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005367static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5368{
5369 struct drm_device *dev = crtc->dev;
5370 struct drm_i915_private *dev_priv = dev->dev_private;
5371 int refclk;
5372
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005373 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005374 refclk = 100000;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005375 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005376 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005377 refclk = dev_priv->vbt.lvds_ssc_freq;
5378 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005379 } else if (!IS_GEN2(dev)) {
5380 refclk = 96000;
5381 } else {
5382 refclk = 48000;
5383 }
5384
5385 return refclk;
5386}
5387
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005388static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005389{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005390 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005391}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005392
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005393static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5394{
5395 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005396}
5397
Daniel Vetterf47709a2013-03-28 10:42:02 +01005398static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005399 intel_clock_t *reduced_clock)
5400{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005401 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005402 u32 fp, fp2 = 0;
5403
5404 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005405 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005406 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005407 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005408 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005409 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005410 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005411 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005412 }
5413
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005414 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005415
Daniel Vetterf47709a2013-03-28 10:42:02 +01005416 crtc->lowfreq_avail = false;
5417 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005418 reduced_clock && i915.powersave) {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005419 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005420 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005421 } else {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005422 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005423 }
5424}
5425
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005426static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5427 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005428{
5429 u32 reg_val;
5430
5431 /*
5432 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5433 * and set it to a reasonable value instead.
5434 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005435 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005436 reg_val &= 0xffffff00;
5437 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005438 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005439
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005440 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005441 reg_val &= 0x8cffffff;
5442 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005443 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005444
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005445 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005446 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005447 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005448
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005449 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005450 reg_val &= 0x00ffffff;
5451 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005452 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005453}
5454
Daniel Vetterb5518422013-05-03 11:49:48 +02005455static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5456 struct intel_link_m_n *m_n)
5457{
5458 struct drm_device *dev = crtc->base.dev;
5459 struct drm_i915_private *dev_priv = dev->dev_private;
5460 int pipe = crtc->pipe;
5461
Daniel Vettere3b95f12013-05-03 11:49:49 +02005462 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5463 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5464 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5465 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005466}
5467
5468static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5469 struct intel_link_m_n *m_n)
5470{
5471 struct drm_device *dev = crtc->base.dev;
5472 struct drm_i915_private *dev_priv = dev->dev_private;
5473 int pipe = crtc->pipe;
5474 enum transcoder transcoder = crtc->config.cpu_transcoder;
5475
5476 if (INTEL_INFO(dev)->gen >= 5) {
5477 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5478 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5479 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5480 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5481 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02005482 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5483 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5484 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5485 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005486 }
5487}
5488
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005489static void intel_dp_set_m_n(struct intel_crtc *crtc)
5490{
5491 if (crtc->config.has_pch_encoder)
5492 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5493 else
5494 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5495}
5496
Daniel Vetterf47709a2013-03-28 10:42:02 +01005497static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005498{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005499 u32 dpll, dpll_md;
5500
5501 /*
5502 * Enable DPIO clock input. We should never disable the reference
5503 * clock for pipe B, since VGA hotplug / manual detection depends
5504 * on it.
5505 */
5506 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5507 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5508 /* We should never disable this, set it here for state tracking */
5509 if (crtc->pipe == PIPE_B)
5510 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5511 dpll |= DPLL_VCO_ENABLE;
5512 crtc->config.dpll_hw_state.dpll = dpll;
5513
5514 dpll_md = (crtc->config.pixel_multiplier - 1)
5515 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5516 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5517}
5518
5519static void vlv_prepare_pll(struct intel_crtc *crtc)
5520{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005521 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005522 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005523 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005524 u32 mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005525 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005526 u32 coreclk, reg_val;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005527
Daniel Vetter09153002012-12-12 14:06:44 +01005528 mutex_lock(&dev_priv->dpio_lock);
5529
Daniel Vetterf47709a2013-03-28 10:42:02 +01005530 bestn = crtc->config.dpll.n;
5531 bestm1 = crtc->config.dpll.m1;
5532 bestm2 = crtc->config.dpll.m2;
5533 bestp1 = crtc->config.dpll.p1;
5534 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005535
Jesse Barnes89b667f2013-04-18 14:51:36 -07005536 /* See eDP HDMI DPIO driver vbios notes doc */
5537
5538 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005539 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005540 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005541
5542 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005543 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005544
5545 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005546 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005547 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005548 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005549
5550 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005551 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005552
5553 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005554 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5555 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5556 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005557 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07005558
5559 /*
5560 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5561 * but we don't support that).
5562 * Note: don't use the DAC post divider as it seems unstable.
5563 */
5564 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005565 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005566
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005567 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005568 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005569
Jesse Barnes89b667f2013-04-18 14:51:36 -07005570 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02005571 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03005572 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07005573 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005574 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03005575 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005576 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005577 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005578 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005579
Jesse Barnes89b667f2013-04-18 14:51:36 -07005580 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5581 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5582 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005583 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005584 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005585 0x0df40000);
5586 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005587 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005588 0x0df70000);
5589 } else { /* HDMI or VGA */
5590 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005591 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005592 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005593 0x0df70000);
5594 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005595 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005596 0x0df40000);
5597 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005598
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005599 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005600 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5601 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5602 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5603 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005604 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005605
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005606 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01005607 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07005608}
5609
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005610static void chv_update_pll(struct intel_crtc *crtc)
5611{
5612 struct drm_device *dev = crtc->base.dev;
5613 struct drm_i915_private *dev_priv = dev->dev_private;
5614 int pipe = crtc->pipe;
5615 int dpll_reg = DPLL(crtc->pipe);
5616 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ville Syrjälä580d3812014-04-09 13:29:00 +03005617 u32 loopfilter, intcoeff;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005618 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5619 int refclk;
5620
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005621 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5622 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5623 DPLL_VCO_ENABLE;
5624 if (pipe != PIPE_A)
5625 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5626
5627 crtc->config.dpll_hw_state.dpll_md =
5628 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005629
5630 bestn = crtc->config.dpll.n;
5631 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5632 bestm1 = crtc->config.dpll.m1;
5633 bestm2 = crtc->config.dpll.m2 >> 22;
5634 bestp1 = crtc->config.dpll.p1;
5635 bestp2 = crtc->config.dpll.p2;
5636
5637 /*
5638 * Enable Refclk and SSC
5639 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005640 I915_WRITE(dpll_reg,
5641 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5642
5643 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005644
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005645 /* p1 and p2 divider */
5646 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5647 5 << DPIO_CHV_S1_DIV_SHIFT |
5648 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5649 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5650 1 << DPIO_CHV_K_DIV_SHIFT);
5651
5652 /* Feedback post-divider - m2 */
5653 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5654
5655 /* Feedback refclk divider - n and m1 */
5656 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5657 DPIO_CHV_M1_DIV_BY_2 |
5658 1 << DPIO_CHV_N_DIV_SHIFT);
5659
5660 /* M2 fraction division */
5661 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5662
5663 /* M2 fraction division enable */
5664 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5665 DPIO_CHV_FRAC_DIV_EN |
5666 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5667
5668 /* Loop filter */
5669 refclk = i9xx_get_refclk(&crtc->base, 0);
5670 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5671 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5672 if (refclk == 100000)
5673 intcoeff = 11;
5674 else if (refclk == 38400)
5675 intcoeff = 10;
5676 else
5677 intcoeff = 9;
5678 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5679 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5680
5681 /* AFC Recal */
5682 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5683 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5684 DPIO_AFC_RECAL);
5685
5686 mutex_unlock(&dev_priv->dpio_lock);
5687}
5688
Daniel Vetterf47709a2013-03-28 10:42:02 +01005689static void i9xx_update_pll(struct intel_crtc *crtc,
5690 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005691 int num_connectors)
5692{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005693 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005694 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005695 u32 dpll;
5696 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005697 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005698
Daniel Vetterf47709a2013-03-28 10:42:02 +01005699 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305700
Daniel Vetterf47709a2013-03-28 10:42:02 +01005701 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5702 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005703
5704 dpll = DPLL_VGA_MODE_DIS;
5705
Daniel Vetterf47709a2013-03-28 10:42:02 +01005706 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005707 dpll |= DPLLB_MODE_LVDS;
5708 else
5709 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005710
Daniel Vetteref1b4602013-06-01 17:17:04 +02005711 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005712 dpll |= (crtc->config.pixel_multiplier - 1)
5713 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005714 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005715
5716 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005717 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005718
Daniel Vetterf47709a2013-03-28 10:42:02 +01005719 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005720 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005721
5722 /* compute bitmask from p1 value */
5723 if (IS_PINEVIEW(dev))
5724 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5725 else {
5726 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5727 if (IS_G4X(dev) && reduced_clock)
5728 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5729 }
5730 switch (clock->p2) {
5731 case 5:
5732 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5733 break;
5734 case 7:
5735 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5736 break;
5737 case 10:
5738 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5739 break;
5740 case 14:
5741 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5742 break;
5743 }
5744 if (INTEL_INFO(dev)->gen >= 4)
5745 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5746
Daniel Vetter09ede542013-04-30 14:01:45 +02005747 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005748 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005749 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005750 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5751 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5752 else
5753 dpll |= PLL_REF_INPUT_DREFCLK;
5754
5755 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005756 crtc->config.dpll_hw_state.dpll = dpll;
5757
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005758 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005759 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5760 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005761 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005762 }
5763}
5764
Daniel Vetterf47709a2013-03-28 10:42:02 +01005765static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005766 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005767 int num_connectors)
5768{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005769 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005770 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005771 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005772 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005773
Daniel Vetterf47709a2013-03-28 10:42:02 +01005774 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305775
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005776 dpll = DPLL_VGA_MODE_DIS;
5777
Daniel Vetterf47709a2013-03-28 10:42:02 +01005778 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005779 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5780 } else {
5781 if (clock->p1 == 2)
5782 dpll |= PLL_P1_DIVIDE_BY_TWO;
5783 else
5784 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5785 if (clock->p2 == 4)
5786 dpll |= PLL_P2_DIVIDE_BY_4;
5787 }
5788
Daniel Vetter4a33e482013-07-06 12:52:05 +02005789 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5790 dpll |= DPLL_DVO_2X_MODE;
5791
Daniel Vetterf47709a2013-03-28 10:42:02 +01005792 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005793 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5794 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5795 else
5796 dpll |= PLL_REF_INPUT_DREFCLK;
5797
5798 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005799 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005800}
5801
Daniel Vetter8a654f32013-06-01 17:16:22 +02005802static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005803{
5804 struct drm_device *dev = intel_crtc->base.dev;
5805 struct drm_i915_private *dev_priv = dev->dev_private;
5806 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005807 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005808 struct drm_display_mode *adjusted_mode =
5809 &intel_crtc->config.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005810 uint32_t crtc_vtotal, crtc_vblank_end;
5811 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005812
5813 /* We need to be careful not to changed the adjusted mode, for otherwise
5814 * the hw state checker will get angry at the mismatch. */
5815 crtc_vtotal = adjusted_mode->crtc_vtotal;
5816 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005817
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005818 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005819 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005820 crtc_vtotal -= 1;
5821 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005822
5823 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5824 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5825 else
5826 vsyncshift = adjusted_mode->crtc_hsync_start -
5827 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005828 if (vsyncshift < 0)
5829 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005830 }
5831
5832 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005833 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005834
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005835 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005836 (adjusted_mode->crtc_hdisplay - 1) |
5837 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005838 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005839 (adjusted_mode->crtc_hblank_start - 1) |
5840 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005841 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005842 (adjusted_mode->crtc_hsync_start - 1) |
5843 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5844
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005845 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005846 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005847 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005848 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005849 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005850 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005851 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005852 (adjusted_mode->crtc_vsync_start - 1) |
5853 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5854
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005855 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5856 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5857 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5858 * bits. */
5859 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5860 (pipe == PIPE_B || pipe == PIPE_C))
5861 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5862
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005863 /* pipesrc controls the size that is scaled from, which should
5864 * always be the user's requested size.
5865 */
5866 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005867 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5868 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005869}
5870
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005871static void intel_get_pipe_timings(struct intel_crtc *crtc,
5872 struct intel_crtc_config *pipe_config)
5873{
5874 struct drm_device *dev = crtc->base.dev;
5875 struct drm_i915_private *dev_priv = dev->dev_private;
5876 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5877 uint32_t tmp;
5878
5879 tmp = I915_READ(HTOTAL(cpu_transcoder));
5880 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5881 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5882 tmp = I915_READ(HBLANK(cpu_transcoder));
5883 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5884 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5885 tmp = I915_READ(HSYNC(cpu_transcoder));
5886 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5887 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5888
5889 tmp = I915_READ(VTOTAL(cpu_transcoder));
5890 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5891 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5892 tmp = I915_READ(VBLANK(cpu_transcoder));
5893 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5894 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5895 tmp = I915_READ(VSYNC(cpu_transcoder));
5896 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5897 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5898
5899 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5900 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5901 pipe_config->adjusted_mode.crtc_vtotal += 1;
5902 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5903 }
5904
5905 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005906 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5907 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5908
5909 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5910 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005911}
5912
Daniel Vetterf6a83282014-02-11 15:28:57 -08005913void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5914 struct intel_crtc_config *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03005915{
Daniel Vetterf6a83282014-02-11 15:28:57 -08005916 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5917 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
5918 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5919 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03005920
Daniel Vetterf6a83282014-02-11 15:28:57 -08005921 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5922 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5923 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5924 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03005925
Daniel Vetterf6a83282014-02-11 15:28:57 -08005926 mode->flags = pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03005927
Daniel Vetterf6a83282014-02-11 15:28:57 -08005928 mode->clock = pipe_config->adjusted_mode.crtc_clock;
5929 mode->flags |= pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03005930}
5931
Daniel Vetter84b046f2013-02-19 18:48:54 +01005932static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5933{
5934 struct drm_device *dev = intel_crtc->base.dev;
5935 struct drm_i915_private *dev_priv = dev->dev_private;
5936 uint32_t pipeconf;
5937
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005938 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005939
Daniel Vetter67c72a12013-09-24 11:46:14 +02005940 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5941 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5942 pipeconf |= PIPECONF_ENABLE;
5943
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005944 if (intel_crtc->config.double_wide)
5945 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005946
Daniel Vetterff9ce462013-04-24 14:57:17 +02005947 /* only g4x and later have fancy bpc/dither controls */
5948 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005949 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5950 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5951 pipeconf |= PIPECONF_DITHER_EN |
5952 PIPECONF_DITHER_TYPE_SP;
5953
5954 switch (intel_crtc->config.pipe_bpp) {
5955 case 18:
5956 pipeconf |= PIPECONF_6BPC;
5957 break;
5958 case 24:
5959 pipeconf |= PIPECONF_8BPC;
5960 break;
5961 case 30:
5962 pipeconf |= PIPECONF_10BPC;
5963 break;
5964 default:
5965 /* Case prevented by intel_choose_pipe_bpp_dither. */
5966 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005967 }
5968 }
5969
5970 if (HAS_PIPE_CXSR(dev)) {
5971 if (intel_crtc->lowfreq_avail) {
5972 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5973 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5974 } else {
5975 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005976 }
5977 }
5978
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02005979 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
5980 if (INTEL_INFO(dev)->gen < 4 ||
5981 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5982 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5983 else
5984 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
5985 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01005986 pipeconf |= PIPECONF_PROGRESSIVE;
5987
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005988 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5989 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005990
Daniel Vetter84b046f2013-02-19 18:48:54 +01005991 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5992 POSTING_READ(PIPECONF(intel_crtc->pipe));
5993}
5994
Eric Anholtf564048e2011-03-30 13:01:02 -07005995static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005996 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005997 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005998{
5999 struct drm_device *dev = crtc->dev;
6000 struct drm_i915_private *dev_priv = dev->dev_private;
6001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtc751ce42010-03-25 11:48:48 -07006002 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006003 intel_clock_t clock, reduced_clock;
Daniel Vettera16af7212013-04-30 14:01:44 +02006004 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006005 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006006 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006007 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08006008
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02006009 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01006010 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006011 case INTEL_OUTPUT_LVDS:
6012 is_lvds = true;
6013 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006014 case INTEL_OUTPUT_DSI:
6015 is_dsi = true;
6016 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006017 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006018
Eric Anholtc751ce42010-03-25 11:48:48 -07006019 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006020 }
6021
Jani Nikulaf2335332013-09-13 11:03:09 +03006022 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006023 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006024
Jani Nikulaf2335332013-09-13 11:03:09 +03006025 if (!intel_crtc->config.clock_set) {
6026 refclk = i9xx_get_refclk(crtc, num_connectors);
6027
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006028 /*
6029 * Returns a set of divisors for the desired target clock with
6030 * the given refclk, or FALSE. The returned values represent
6031 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6032 * 2) / p1 / p2.
6033 */
6034 limit = intel_limit(crtc, refclk);
6035 ok = dev_priv->display.find_dpll(limit, crtc,
6036 intel_crtc->config.port_clock,
6037 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006038 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006039 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6040 return -EINVAL;
6041 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006042
Jani Nikulaf2335332013-09-13 11:03:09 +03006043 if (is_lvds && dev_priv->lvds_downclock_avail) {
6044 /*
6045 * Ensure we match the reduced clock's P to the target
6046 * clock. If the clocks don't match, we can't switch
6047 * the display clock by using the FP0/FP1. In such case
6048 * we will disable the LVDS downclock feature.
6049 */
6050 has_reduced_clock =
6051 dev_priv->display.find_dpll(limit, crtc,
6052 dev_priv->lvds_downclock,
6053 refclk, &clock,
6054 &reduced_clock);
6055 }
6056 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01006057 intel_crtc->config.dpll.n = clock.n;
6058 intel_crtc->config.dpll.m1 = clock.m1;
6059 intel_crtc->config.dpll.m2 = clock.m2;
6060 intel_crtc->config.dpll.p1 = clock.p1;
6061 intel_crtc->config.dpll.p2 = clock.p2;
6062 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006063
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006064 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02006065 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306066 has_reduced_clock ? &reduced_clock : NULL,
6067 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006068 } else if (IS_CHERRYVIEW(dev)) {
6069 chv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006070 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03006071 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006072 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01006073 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006074 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006075 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006076 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006077
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006078 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006079}
6080
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006081static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6082 struct intel_crtc_config *pipe_config)
6083{
6084 struct drm_device *dev = crtc->base.dev;
6085 struct drm_i915_private *dev_priv = dev->dev_private;
6086 uint32_t tmp;
6087
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006088 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6089 return;
6090
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006091 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006092 if (!(tmp & PFIT_ENABLE))
6093 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006094
Daniel Vetter06922822013-07-11 13:35:40 +02006095 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006096 if (INTEL_INFO(dev)->gen < 4) {
6097 if (crtc->pipe != PIPE_B)
6098 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006099 } else {
6100 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6101 return;
6102 }
6103
Daniel Vetter06922822013-07-11 13:35:40 +02006104 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006105 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6106 if (INTEL_INFO(dev)->gen < 5)
6107 pipe_config->gmch_pfit.lvds_border_bits =
6108 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6109}
6110
Jesse Barnesacbec812013-09-20 11:29:32 -07006111static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6112 struct intel_crtc_config *pipe_config)
6113{
6114 struct drm_device *dev = crtc->base.dev;
6115 struct drm_i915_private *dev_priv = dev->dev_private;
6116 int pipe = pipe_config->cpu_transcoder;
6117 intel_clock_t clock;
6118 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006119 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006120
6121 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006122 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006123 mutex_unlock(&dev_priv->dpio_lock);
6124
6125 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6126 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6127 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6128 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6129 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6130
Ville Syrjäläf6466282013-10-14 14:50:31 +03006131 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006132
Ville Syrjäläf6466282013-10-14 14:50:31 +03006133 /* clock.dot is the fast clock */
6134 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006135}
6136
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006137static void i9xx_get_plane_config(struct intel_crtc *crtc,
6138 struct intel_plane_config *plane_config)
6139{
6140 struct drm_device *dev = crtc->base.dev;
6141 struct drm_i915_private *dev_priv = dev->dev_private;
6142 u32 val, base, offset;
6143 int pipe = crtc->pipe, plane = crtc->plane;
6144 int fourcc, pixel_format;
6145 int aligned_height;
6146
Dave Airlie66e514c2014-04-03 07:51:54 +10006147 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6148 if (!crtc->base.primary->fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006149 DRM_DEBUG_KMS("failed to alloc fb\n");
6150 return;
6151 }
6152
6153 val = I915_READ(DSPCNTR(plane));
6154
6155 if (INTEL_INFO(dev)->gen >= 4)
6156 if (val & DISPPLANE_TILED)
6157 plane_config->tiled = true;
6158
6159 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6160 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10006161 crtc->base.primary->fb->pixel_format = fourcc;
6162 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006163 drm_format_plane_cpp(fourcc, 0) * 8;
6164
6165 if (INTEL_INFO(dev)->gen >= 4) {
6166 if (plane_config->tiled)
6167 offset = I915_READ(DSPTILEOFF(plane));
6168 else
6169 offset = I915_READ(DSPLINOFF(plane));
6170 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6171 } else {
6172 base = I915_READ(DSPADDR(plane));
6173 }
6174 plane_config->base = base;
6175
6176 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006177 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6178 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006179
6180 val = I915_READ(DSPSTRIDE(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006181 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006182
Dave Airlie66e514c2014-04-03 07:51:54 +10006183 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006184 plane_config->tiled);
6185
Dave Airlie66e514c2014-04-03 07:51:54 +10006186 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006187 aligned_height, PAGE_SIZE);
6188
6189 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 +10006190 pipe, plane, crtc->base.primary->fb->width,
6191 crtc->base.primary->fb->height,
6192 crtc->base.primary->fb->bits_per_pixel, base,
6193 crtc->base.primary->fb->pitches[0],
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006194 plane_config->size);
6195
6196}
6197
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006198static void chv_crtc_clock_get(struct intel_crtc *crtc,
6199 struct intel_crtc_config *pipe_config)
6200{
6201 struct drm_device *dev = crtc->base.dev;
6202 struct drm_i915_private *dev_priv = dev->dev_private;
6203 int pipe = pipe_config->cpu_transcoder;
6204 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6205 intel_clock_t clock;
6206 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6207 int refclk = 100000;
6208
6209 mutex_lock(&dev_priv->dpio_lock);
6210 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6211 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6212 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6213 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6214 mutex_unlock(&dev_priv->dpio_lock);
6215
6216 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6217 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6218 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6219 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6220 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6221
6222 chv_clock(refclk, &clock);
6223
6224 /* clock.dot is the fast clock */
6225 pipe_config->port_clock = clock.dot / 5;
6226}
6227
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006228static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6229 struct intel_crtc_config *pipe_config)
6230{
6231 struct drm_device *dev = crtc->base.dev;
6232 struct drm_i915_private *dev_priv = dev->dev_private;
6233 uint32_t tmp;
6234
Imre Deakb5482bd2014-03-05 16:20:55 +02006235 if (!intel_display_power_enabled(dev_priv,
6236 POWER_DOMAIN_PIPE(crtc->pipe)))
6237 return false;
6238
Daniel Vettere143a212013-07-04 12:01:15 +02006239 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006240 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006241
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006242 tmp = I915_READ(PIPECONF(crtc->pipe));
6243 if (!(tmp & PIPECONF_ENABLE))
6244 return false;
6245
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006246 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6247 switch (tmp & PIPECONF_BPC_MASK) {
6248 case PIPECONF_6BPC:
6249 pipe_config->pipe_bpp = 18;
6250 break;
6251 case PIPECONF_8BPC:
6252 pipe_config->pipe_bpp = 24;
6253 break;
6254 case PIPECONF_10BPC:
6255 pipe_config->pipe_bpp = 30;
6256 break;
6257 default:
6258 break;
6259 }
6260 }
6261
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02006262 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6263 pipe_config->limited_color_range = true;
6264
Ville Syrjälä282740f2013-09-04 18:30:03 +03006265 if (INTEL_INFO(dev)->gen < 4)
6266 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6267
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006268 intel_get_pipe_timings(crtc, pipe_config);
6269
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006270 i9xx_get_pfit_config(crtc, pipe_config);
6271
Daniel Vetter6c49f242013-06-06 12:45:25 +02006272 if (INTEL_INFO(dev)->gen >= 4) {
6273 tmp = I915_READ(DPLL_MD(crtc->pipe));
6274 pipe_config->pixel_multiplier =
6275 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6276 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006277 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02006278 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6279 tmp = I915_READ(DPLL(crtc->pipe));
6280 pipe_config->pixel_multiplier =
6281 ((tmp & SDVO_MULTIPLIER_MASK)
6282 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6283 } else {
6284 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6285 * port and will be fixed up in the encoder->get_config
6286 * function. */
6287 pipe_config->pixel_multiplier = 1;
6288 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006289 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6290 if (!IS_VALLEYVIEW(dev)) {
6291 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6292 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03006293 } else {
6294 /* Mask out read-only status bits. */
6295 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6296 DPLL_PORTC_READY_MASK |
6297 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006298 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02006299
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006300 if (IS_CHERRYVIEW(dev))
6301 chv_crtc_clock_get(crtc, pipe_config);
6302 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07006303 vlv_crtc_clock_get(crtc, pipe_config);
6304 else
6305 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03006306
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006307 return true;
6308}
6309
Paulo Zanonidde86e22012-12-01 12:04:25 -02006310static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07006311{
6312 struct drm_i915_private *dev_priv = dev->dev_private;
6313 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006314 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006315 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006316 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006317 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006318 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07006319 bool has_ck505 = false;
6320 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006321
6322 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07006323 list_for_each_entry(encoder, &mode_config->encoder_list,
6324 base.head) {
6325 switch (encoder->type) {
6326 case INTEL_OUTPUT_LVDS:
6327 has_panel = true;
6328 has_lvds = true;
6329 break;
6330 case INTEL_OUTPUT_EDP:
6331 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03006332 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07006333 has_cpu_edp = true;
6334 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006335 }
6336 }
6337
Keith Packard99eb6a02011-09-26 14:29:12 -07006338 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006339 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07006340 can_ssc = has_ck505;
6341 } else {
6342 has_ck505 = false;
6343 can_ssc = true;
6344 }
6345
Imre Deak2de69052013-05-08 13:14:04 +03006346 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6347 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006348
6349 /* Ironlake: try to setup display ref clock before DPLL
6350 * enabling. This is only under driver's control after
6351 * PCH B stepping, previous chipset stepping should be
6352 * ignoring this setting.
6353 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006354 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006355
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006356 /* As we must carefully and slowly disable/enable each source in turn,
6357 * compute the final state we want first and check if we need to
6358 * make any changes at all.
6359 */
6360 final = val;
6361 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07006362 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006363 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07006364 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006365 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6366
6367 final &= ~DREF_SSC_SOURCE_MASK;
6368 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6369 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006370
Keith Packard199e5d72011-09-22 12:01:57 -07006371 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006372 final |= DREF_SSC_SOURCE_ENABLE;
6373
6374 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6375 final |= DREF_SSC1_ENABLE;
6376
6377 if (has_cpu_edp) {
6378 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6379 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6380 else
6381 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6382 } else
6383 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6384 } else {
6385 final |= DREF_SSC_SOURCE_DISABLE;
6386 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6387 }
6388
6389 if (final == val)
6390 return;
6391
6392 /* Always enable nonspread source */
6393 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6394
6395 if (has_ck505)
6396 val |= DREF_NONSPREAD_CK505_ENABLE;
6397 else
6398 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6399
6400 if (has_panel) {
6401 val &= ~DREF_SSC_SOURCE_MASK;
6402 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006403
Keith Packard199e5d72011-09-22 12:01:57 -07006404 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07006405 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006406 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006407 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02006408 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006409 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006410
6411 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006412 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006413 POSTING_READ(PCH_DREF_CONTROL);
6414 udelay(200);
6415
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006416 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006417
6418 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07006419 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07006420 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006421 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006422 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02006423 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006424 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07006425 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006426 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006427
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006428 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006429 POSTING_READ(PCH_DREF_CONTROL);
6430 udelay(200);
6431 } else {
6432 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6433
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006434 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07006435
6436 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006437 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006438
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006439 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006440 POSTING_READ(PCH_DREF_CONTROL);
6441 udelay(200);
6442
6443 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006444 val &= ~DREF_SSC_SOURCE_MASK;
6445 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006446
6447 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006448 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006449
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006450 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006451 POSTING_READ(PCH_DREF_CONTROL);
6452 udelay(200);
6453 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006454
6455 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006456}
6457
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006458static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02006459{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006460 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006461
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006462 tmp = I915_READ(SOUTH_CHICKEN2);
6463 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6464 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006465
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006466 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6467 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6468 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02006469
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006470 tmp = I915_READ(SOUTH_CHICKEN2);
6471 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6472 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006473
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006474 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6475 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6476 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006477}
6478
6479/* WaMPhyProgramming:hsw */
6480static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6481{
6482 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006483
6484 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6485 tmp &= ~(0xFF << 24);
6486 tmp |= (0x12 << 24);
6487 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6488
Paulo Zanonidde86e22012-12-01 12:04:25 -02006489 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6490 tmp |= (1 << 11);
6491 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6492
6493 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6494 tmp |= (1 << 11);
6495 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6496
Paulo Zanonidde86e22012-12-01 12:04:25 -02006497 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6498 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6499 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6500
6501 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6502 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6503 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6504
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006505 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6506 tmp &= ~(7 << 13);
6507 tmp |= (5 << 13);
6508 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006509
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006510 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6511 tmp &= ~(7 << 13);
6512 tmp |= (5 << 13);
6513 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006514
6515 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6516 tmp &= ~0xFF;
6517 tmp |= 0x1C;
6518 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6519
6520 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6521 tmp &= ~0xFF;
6522 tmp |= 0x1C;
6523 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6524
6525 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6526 tmp &= ~(0xFF << 16);
6527 tmp |= (0x1C << 16);
6528 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6529
6530 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6531 tmp &= ~(0xFF << 16);
6532 tmp |= (0x1C << 16);
6533 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6534
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006535 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6536 tmp |= (1 << 27);
6537 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006538
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006539 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6540 tmp |= (1 << 27);
6541 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006542
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006543 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6544 tmp &= ~(0xF << 28);
6545 tmp |= (4 << 28);
6546 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006547
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006548 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6549 tmp &= ~(0xF << 28);
6550 tmp |= (4 << 28);
6551 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006552}
6553
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006554/* Implements 3 different sequences from BSpec chapter "Display iCLK
6555 * Programming" based on the parameters passed:
6556 * - Sequence to enable CLKOUT_DP
6557 * - Sequence to enable CLKOUT_DP without spread
6558 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6559 */
6560static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6561 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006562{
6563 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006564 uint32_t reg, tmp;
6565
6566 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6567 with_spread = true;
6568 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6569 with_fdi, "LP PCH doesn't have FDI\n"))
6570 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006571
6572 mutex_lock(&dev_priv->dpio_lock);
6573
6574 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6575 tmp &= ~SBI_SSCCTL_DISABLE;
6576 tmp |= SBI_SSCCTL_PATHALT;
6577 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6578
6579 udelay(24);
6580
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006581 if (with_spread) {
6582 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6583 tmp &= ~SBI_SSCCTL_PATHALT;
6584 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006585
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006586 if (with_fdi) {
6587 lpt_reset_fdi_mphy(dev_priv);
6588 lpt_program_fdi_mphy(dev_priv);
6589 }
6590 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02006591
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006592 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6593 SBI_GEN0 : SBI_DBUFF0;
6594 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6595 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6596 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01006597
6598 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006599}
6600
Paulo Zanoni47701c32013-07-23 11:19:25 -03006601/* Sequence to disable CLKOUT_DP */
6602static void lpt_disable_clkout_dp(struct drm_device *dev)
6603{
6604 struct drm_i915_private *dev_priv = dev->dev_private;
6605 uint32_t reg, tmp;
6606
6607 mutex_lock(&dev_priv->dpio_lock);
6608
6609 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6610 SBI_GEN0 : SBI_DBUFF0;
6611 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6612 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6613 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6614
6615 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6616 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6617 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6618 tmp |= SBI_SSCCTL_PATHALT;
6619 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6620 udelay(32);
6621 }
6622 tmp |= SBI_SSCCTL_DISABLE;
6623 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6624 }
6625
6626 mutex_unlock(&dev_priv->dpio_lock);
6627}
6628
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006629static void lpt_init_pch_refclk(struct drm_device *dev)
6630{
6631 struct drm_mode_config *mode_config = &dev->mode_config;
6632 struct intel_encoder *encoder;
6633 bool has_vga = false;
6634
6635 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
6636 switch (encoder->type) {
6637 case INTEL_OUTPUT_ANALOG:
6638 has_vga = true;
6639 break;
6640 }
6641 }
6642
Paulo Zanoni47701c32013-07-23 11:19:25 -03006643 if (has_vga)
6644 lpt_enable_clkout_dp(dev, true, true);
6645 else
6646 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006647}
6648
Paulo Zanonidde86e22012-12-01 12:04:25 -02006649/*
6650 * Initialize reference clocks when the driver loads
6651 */
6652void intel_init_pch_refclk(struct drm_device *dev)
6653{
6654 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6655 ironlake_init_pch_refclk(dev);
6656 else if (HAS_PCH_LPT(dev))
6657 lpt_init_pch_refclk(dev);
6658}
6659
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006660static int ironlake_get_refclk(struct drm_crtc *crtc)
6661{
6662 struct drm_device *dev = crtc->dev;
6663 struct drm_i915_private *dev_priv = dev->dev_private;
6664 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006665 int num_connectors = 0;
6666 bool is_lvds = false;
6667
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02006668 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006669 switch (encoder->type) {
6670 case INTEL_OUTPUT_LVDS:
6671 is_lvds = true;
6672 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006673 }
6674 num_connectors++;
6675 }
6676
6677 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006678 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006679 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006680 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006681 }
6682
6683 return 120000;
6684}
6685
Daniel Vetter6ff93602013-04-19 11:24:36 +02006686static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03006687{
6688 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
6689 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6690 int pipe = intel_crtc->pipe;
6691 uint32_t val;
6692
Daniel Vetter78114072013-06-13 00:54:57 +02006693 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03006694
Daniel Vetter965e0c42013-03-27 00:44:57 +01006695 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03006696 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006697 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006698 break;
6699 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006700 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006701 break;
6702 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006703 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006704 break;
6705 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006706 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006707 break;
6708 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03006709 /* Case prevented by intel_choose_pipe_bpp_dither. */
6710 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03006711 }
6712
Daniel Vetterd8b32242013-04-25 17:54:44 +02006713 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03006714 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6715
Daniel Vetter6ff93602013-04-19 11:24:36 +02006716 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03006717 val |= PIPECONF_INTERLACED_ILK;
6718 else
6719 val |= PIPECONF_PROGRESSIVE;
6720
Daniel Vetter50f3b012013-03-27 00:44:56 +01006721 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006722 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006723
Paulo Zanonic8203562012-09-12 10:06:29 -03006724 I915_WRITE(PIPECONF(pipe), val);
6725 POSTING_READ(PIPECONF(pipe));
6726}
6727
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006728/*
6729 * Set up the pipe CSC unit.
6730 *
6731 * Currently only full range RGB to limited range RGB conversion
6732 * is supported, but eventually this should handle various
6733 * RGB<->YCbCr scenarios as well.
6734 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01006735static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006736{
6737 struct drm_device *dev = crtc->dev;
6738 struct drm_i915_private *dev_priv = dev->dev_private;
6739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6740 int pipe = intel_crtc->pipe;
6741 uint16_t coeff = 0x7800; /* 1.0 */
6742
6743 /*
6744 * TODO: Check what kind of values actually come out of the pipe
6745 * with these coeff/postoff values and adjust to get the best
6746 * accuracy. Perhaps we even need to take the bpc value into
6747 * consideration.
6748 */
6749
Daniel Vetter50f3b012013-03-27 00:44:56 +01006750 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006751 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6752
6753 /*
6754 * GY/GU and RY/RU should be the other way around according
6755 * to BSpec, but reality doesn't agree. Just set them up in
6756 * a way that results in the correct picture.
6757 */
6758 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6759 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6760
6761 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6762 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6763
6764 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6765 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6766
6767 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6768 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6769 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6770
6771 if (INTEL_INFO(dev)->gen > 6) {
6772 uint16_t postoff = 0;
6773
Daniel Vetter50f3b012013-03-27 00:44:56 +01006774 if (intel_crtc->config.limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02006775 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006776
6777 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6778 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6779 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6780
6781 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6782 } else {
6783 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6784
Daniel Vetter50f3b012013-03-27 00:44:56 +01006785 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006786 mode |= CSC_BLACK_SCREEN_OFFSET;
6787
6788 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6789 }
6790}
6791
Daniel Vetter6ff93602013-04-19 11:24:36 +02006792static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006793{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006794 struct drm_device *dev = crtc->dev;
6795 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006797 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006798 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006799 uint32_t val;
6800
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006801 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006802
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006803 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006804 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6805
Daniel Vetter6ff93602013-04-19 11:24:36 +02006806 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006807 val |= PIPECONF_INTERLACED_ILK;
6808 else
6809 val |= PIPECONF_PROGRESSIVE;
6810
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006811 I915_WRITE(PIPECONF(cpu_transcoder), val);
6812 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006813
6814 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6815 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006816
6817 if (IS_BROADWELL(dev)) {
6818 val = 0;
6819
6820 switch (intel_crtc->config.pipe_bpp) {
6821 case 18:
6822 val |= PIPEMISC_DITHER_6_BPC;
6823 break;
6824 case 24:
6825 val |= PIPEMISC_DITHER_8_BPC;
6826 break;
6827 case 30:
6828 val |= PIPEMISC_DITHER_10_BPC;
6829 break;
6830 case 36:
6831 val |= PIPEMISC_DITHER_12_BPC;
6832 break;
6833 default:
6834 /* Case prevented by pipe_config_set_bpp. */
6835 BUG();
6836 }
6837
6838 if (intel_crtc->config.dither)
6839 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6840
6841 I915_WRITE(PIPEMISC(pipe), val);
6842 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006843}
6844
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006845static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006846 intel_clock_t *clock,
6847 bool *has_reduced_clock,
6848 intel_clock_t *reduced_clock)
6849{
6850 struct drm_device *dev = crtc->dev;
6851 struct drm_i915_private *dev_priv = dev->dev_private;
6852 struct intel_encoder *intel_encoder;
6853 int refclk;
6854 const intel_limit_t *limit;
Daniel Vettera16af7212013-04-30 14:01:44 +02006855 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006856
6857 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6858 switch (intel_encoder->type) {
6859 case INTEL_OUTPUT_LVDS:
6860 is_lvds = true;
6861 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006862 }
6863 }
6864
6865 refclk = ironlake_get_refclk(crtc);
6866
6867 /*
6868 * Returns a set of divisors for the desired target clock with the given
6869 * refclk, or FALSE. The returned values represent the clock equation:
6870 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6871 */
6872 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006873 ret = dev_priv->display.find_dpll(limit, crtc,
6874 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006875 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006876 if (!ret)
6877 return false;
6878
6879 if (is_lvds && dev_priv->lvds_downclock_avail) {
6880 /*
6881 * Ensure we match the reduced clock's P to the target clock.
6882 * If the clocks don't match, we can't switch the display clock
6883 * by using the FP0/FP1. In such case we will disable the LVDS
6884 * downclock feature.
6885 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006886 *has_reduced_clock =
6887 dev_priv->display.find_dpll(limit, crtc,
6888 dev_priv->lvds_downclock,
6889 refclk, clock,
6890 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006891 }
6892
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006893 return true;
6894}
6895
Paulo Zanonid4b19312012-11-29 11:29:32 -02006896int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6897{
6898 /*
6899 * Account for spread spectrum to avoid
6900 * oversubscribing the link. Max center spread
6901 * is 2.5%; use 5% for safety's sake.
6902 */
6903 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02006904 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02006905}
6906
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006907static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006908{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006909 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006910}
6911
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006912static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006913 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006914 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006915{
6916 struct drm_crtc *crtc = &intel_crtc->base;
6917 struct drm_device *dev = crtc->dev;
6918 struct drm_i915_private *dev_priv = dev->dev_private;
6919 struct intel_encoder *intel_encoder;
6920 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006921 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006922 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006923
6924 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6925 switch (intel_encoder->type) {
6926 case INTEL_OUTPUT_LVDS:
6927 is_lvds = true;
6928 break;
6929 case INTEL_OUTPUT_SDVO:
6930 case INTEL_OUTPUT_HDMI:
6931 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006932 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006933 }
6934
6935 num_connectors++;
6936 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006937
Chris Wilsonc1858122010-12-03 21:35:48 +00006938 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006939 factor = 21;
6940 if (is_lvds) {
6941 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006942 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006943 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006944 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006945 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006946 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006947
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006948 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006949 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006950
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006951 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6952 *fp2 |= FP_CB_TUNE;
6953
Chris Wilson5eddb702010-09-11 13:48:45 +01006954 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006955
Eric Anholta07d6782011-03-30 13:01:08 -07006956 if (is_lvds)
6957 dpll |= DPLLB_MODE_LVDS;
6958 else
6959 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006960
Daniel Vetteref1b4602013-06-01 17:17:04 +02006961 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6962 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006963
6964 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006965 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006966 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006967 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006968
Eric Anholta07d6782011-03-30 13:01:08 -07006969 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006970 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006971 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006972 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006973
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006974 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006975 case 5:
6976 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6977 break;
6978 case 7:
6979 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6980 break;
6981 case 10:
6982 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6983 break;
6984 case 14:
6985 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6986 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006987 }
6988
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006989 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006990 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006991 else
6992 dpll |= PLL_REF_INPUT_DREFCLK;
6993
Daniel Vetter959e16d2013-06-05 13:34:21 +02006994 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006995}
6996
Jesse Barnes79e53942008-11-07 14:24:08 -08006997static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006998 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006999 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08007000{
7001 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007003 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007004 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007005 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007006 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01007007 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007008 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02007009 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007010
7011 for_each_encoder_on_crtc(dev, crtc, encoder) {
7012 switch (encoder->type) {
7013 case INTEL_OUTPUT_LVDS:
7014 is_lvds = true;
7015 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007016 }
7017
7018 num_connectors++;
7019 }
7020
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007021 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7022 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7023
Daniel Vetterff9a6752013-06-01 17:16:21 +02007024 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007025 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02007026 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007027 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7028 return -EINVAL;
7029 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007030 /* Compat-code for transition, will disappear. */
7031 if (!intel_crtc->config.clock_set) {
7032 intel_crtc->config.dpll.n = clock.n;
7033 intel_crtc->config.dpll.m1 = clock.m1;
7034 intel_crtc->config.dpll.m2 = clock.m2;
7035 intel_crtc->config.dpll.p1 = clock.p1;
7036 intel_crtc->config.dpll.p2 = clock.p2;
7037 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007038
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007039 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01007040 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007041 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007042 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007043 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007044
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007045 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007046 &fp, &reduced_clock,
7047 has_reduced_clock ? &fp2 : NULL);
7048
Daniel Vetter959e16d2013-06-05 13:34:21 +02007049 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007050 intel_crtc->config.dpll_hw_state.fp0 = fp;
7051 if (has_reduced_clock)
7052 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7053 else
7054 intel_crtc->config.dpll_hw_state.fp1 = fp;
7055
Daniel Vetterb89a1d32013-06-05 13:34:24 +02007056 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007057 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007058 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Daniel Vetter29407aa2014-04-24 23:55:08 +02007059 pipe_name(intel_crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007060 return -EINVAL;
7061 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007062 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02007063 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007064
Jani Nikulad330a952014-01-21 11:24:25 +02007065 if (is_lvds && has_reduced_clock && i915.powersave)
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007066 intel_crtc->lowfreq_avail = true;
7067 else
7068 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007069
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007070 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007071}
7072
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007073static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7074 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007075{
7076 struct drm_device *dev = crtc->base.dev;
7077 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007078 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007079
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007080 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7081 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7082 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7083 & ~TU_SIZE_MASK;
7084 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7085 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7086 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7087}
7088
7089static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7090 enum transcoder transcoder,
7091 struct intel_link_m_n *m_n)
7092{
7093 struct drm_device *dev = crtc->base.dev;
7094 struct drm_i915_private *dev_priv = dev->dev_private;
7095 enum pipe pipe = crtc->pipe;
7096
7097 if (INTEL_INFO(dev)->gen >= 5) {
7098 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7099 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7100 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7101 & ~TU_SIZE_MASK;
7102 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7103 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7104 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7105 } else {
7106 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7107 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7108 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7109 & ~TU_SIZE_MASK;
7110 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7111 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7112 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7113 }
7114}
7115
7116void intel_dp_get_m_n(struct intel_crtc *crtc,
7117 struct intel_crtc_config *pipe_config)
7118{
7119 if (crtc->config.has_pch_encoder)
7120 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7121 else
7122 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7123 &pipe_config->dp_m_n);
7124}
7125
Daniel Vetter72419202013-04-04 13:28:53 +02007126static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7127 struct intel_crtc_config *pipe_config)
7128{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007129 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7130 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02007131}
7132
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007133static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7134 struct intel_crtc_config *pipe_config)
7135{
7136 struct drm_device *dev = crtc->base.dev;
7137 struct drm_i915_private *dev_priv = dev->dev_private;
7138 uint32_t tmp;
7139
7140 tmp = I915_READ(PF_CTL(crtc->pipe));
7141
7142 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01007143 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007144 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7145 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02007146
7147 /* We currently do not free assignements of panel fitters on
7148 * ivb/hsw (since we don't use the higher upscaling modes which
7149 * differentiates them) so just WARN about this case for now. */
7150 if (IS_GEN7(dev)) {
7151 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7152 PF_PIPE_SEL_IVB(crtc->pipe));
7153 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007154 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007155}
7156
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007157static void ironlake_get_plane_config(struct intel_crtc *crtc,
7158 struct intel_plane_config *plane_config)
7159{
7160 struct drm_device *dev = crtc->base.dev;
7161 struct drm_i915_private *dev_priv = dev->dev_private;
7162 u32 val, base, offset;
7163 int pipe = crtc->pipe, plane = crtc->plane;
7164 int fourcc, pixel_format;
7165 int aligned_height;
7166
Dave Airlie66e514c2014-04-03 07:51:54 +10007167 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7168 if (!crtc->base.primary->fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007169 DRM_DEBUG_KMS("failed to alloc fb\n");
7170 return;
7171 }
7172
7173 val = I915_READ(DSPCNTR(plane));
7174
7175 if (INTEL_INFO(dev)->gen >= 4)
7176 if (val & DISPPLANE_TILED)
7177 plane_config->tiled = true;
7178
7179 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7180 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10007181 crtc->base.primary->fb->pixel_format = fourcc;
7182 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007183 drm_format_plane_cpp(fourcc, 0) * 8;
7184
7185 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7186 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7187 offset = I915_READ(DSPOFFSET(plane));
7188 } else {
7189 if (plane_config->tiled)
7190 offset = I915_READ(DSPTILEOFF(plane));
7191 else
7192 offset = I915_READ(DSPLINOFF(plane));
7193 }
7194 plane_config->base = base;
7195
7196 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007197 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7198 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007199
7200 val = I915_READ(DSPSTRIDE(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007201 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007202
Dave Airlie66e514c2014-04-03 07:51:54 +10007203 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007204 plane_config->tiled);
7205
Dave Airlie66e514c2014-04-03 07:51:54 +10007206 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007207 aligned_height, PAGE_SIZE);
7208
7209 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 +10007210 pipe, plane, crtc->base.primary->fb->width,
7211 crtc->base.primary->fb->height,
7212 crtc->base.primary->fb->bits_per_pixel, base,
7213 crtc->base.primary->fb->pitches[0],
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007214 plane_config->size);
7215}
7216
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007217static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7218 struct intel_crtc_config *pipe_config)
7219{
7220 struct drm_device *dev = crtc->base.dev;
7221 struct drm_i915_private *dev_priv = dev->dev_private;
7222 uint32_t tmp;
7223
Daniel Vettere143a212013-07-04 12:01:15 +02007224 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007225 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007226
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007227 tmp = I915_READ(PIPECONF(crtc->pipe));
7228 if (!(tmp & PIPECONF_ENABLE))
7229 return false;
7230
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007231 switch (tmp & PIPECONF_BPC_MASK) {
7232 case PIPECONF_6BPC:
7233 pipe_config->pipe_bpp = 18;
7234 break;
7235 case PIPECONF_8BPC:
7236 pipe_config->pipe_bpp = 24;
7237 break;
7238 case PIPECONF_10BPC:
7239 pipe_config->pipe_bpp = 30;
7240 break;
7241 case PIPECONF_12BPC:
7242 pipe_config->pipe_bpp = 36;
7243 break;
7244 default:
7245 break;
7246 }
7247
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007248 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7249 pipe_config->limited_color_range = true;
7250
Daniel Vetterab9412b2013-05-03 11:49:46 +02007251 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02007252 struct intel_shared_dpll *pll;
7253
Daniel Vetter88adfff2013-03-28 10:42:01 +01007254 pipe_config->has_pch_encoder = true;
7255
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007256 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7257 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7258 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007259
7260 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007261
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007262 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02007263 pipe_config->shared_dpll =
7264 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007265 } else {
7266 tmp = I915_READ(PCH_DPLL_SEL);
7267 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7268 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7269 else
7270 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7271 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02007272
7273 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7274
7275 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7276 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02007277
7278 tmp = pipe_config->dpll_hw_state.dpll;
7279 pipe_config->pixel_multiplier =
7280 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7281 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007282
7283 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007284 } else {
7285 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007286 }
7287
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007288 intel_get_pipe_timings(crtc, pipe_config);
7289
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007290 ironlake_get_pfit_config(crtc, pipe_config);
7291
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007292 return true;
7293}
7294
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007295static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7296{
7297 struct drm_device *dev = dev_priv->dev;
7298 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
7299 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007300
Damien Lespiaud3fcc802014-05-13 23:32:22 +01007301 for_each_intel_crtc(dev, crtc)
Paulo Zanoni798183c2013-12-06 20:29:01 -02007302 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007303 pipe_name(crtc->pipe));
7304
7305 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
7306 WARN(plls->spll_refcount, "SPLL enabled\n");
7307 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
7308 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
7309 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7310 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7311 "CPU PWM1 enabled\n");
7312 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7313 "CPU PWM2 enabled\n");
7314 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7315 "PCH PWM1 enabled\n");
7316 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7317 "Utility pin enabled\n");
7318 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7319
Paulo Zanoni9926ada2014-04-01 19:39:47 -03007320 /*
7321 * In theory we can still leave IRQs enabled, as long as only the HPD
7322 * interrupts remain enabled. We used to check for that, but since it's
7323 * gen-specific and since we only disable LCPLL after we fully disable
7324 * the interrupts, the check below should be enough.
7325 */
7326 WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007327}
7328
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007329static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7330{
7331 struct drm_device *dev = dev_priv->dev;
7332
7333 if (IS_HASWELL(dev)) {
7334 mutex_lock(&dev_priv->rps.hw_lock);
7335 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7336 val))
7337 DRM_ERROR("Failed to disable D_COMP\n");
7338 mutex_unlock(&dev_priv->rps.hw_lock);
7339 } else {
7340 I915_WRITE(D_COMP, val);
7341 }
7342 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007343}
7344
7345/*
7346 * This function implements pieces of two sequences from BSpec:
7347 * - Sequence for display software to disable LCPLL
7348 * - Sequence for display software to allow package C8+
7349 * The steps implemented here are just the steps that actually touch the LCPLL
7350 * register. Callers should take care of disabling all the display engine
7351 * functions, doing the mode unset, fixing interrupts, etc.
7352 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007353static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7354 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007355{
7356 uint32_t val;
7357
7358 assert_can_disable_lcpll(dev_priv);
7359
7360 val = I915_READ(LCPLL_CTL);
7361
7362 if (switch_to_fclk) {
7363 val |= LCPLL_CD_SOURCE_FCLK;
7364 I915_WRITE(LCPLL_CTL, val);
7365
7366 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7367 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7368 DRM_ERROR("Switching to FCLK failed\n");
7369
7370 val = I915_READ(LCPLL_CTL);
7371 }
7372
7373 val |= LCPLL_PLL_DISABLE;
7374 I915_WRITE(LCPLL_CTL, val);
7375 POSTING_READ(LCPLL_CTL);
7376
7377 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7378 DRM_ERROR("LCPLL still locked\n");
7379
7380 val = I915_READ(D_COMP);
7381 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007382 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007383 ndelay(100);
7384
7385 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
7386 DRM_ERROR("D_COMP RCOMP still in progress\n");
7387
7388 if (allow_power_down) {
7389 val = I915_READ(LCPLL_CTL);
7390 val |= LCPLL_POWER_DOWN_ALLOW;
7391 I915_WRITE(LCPLL_CTL, val);
7392 POSTING_READ(LCPLL_CTL);
7393 }
7394}
7395
7396/*
7397 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7398 * source.
7399 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007400static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007401{
7402 uint32_t val;
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007403 unsigned long irqflags;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007404
7405 val = I915_READ(LCPLL_CTL);
7406
7407 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7408 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7409 return;
7410
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007411 /*
7412 * Make sure we're not on PC8 state before disabling PC8, otherwise
7413 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7414 *
7415 * The other problem is that hsw_restore_lcpll() is called as part of
7416 * the runtime PM resume sequence, so we can't just call
7417 * gen6_gt_force_wake_get() because that function calls
7418 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7419 * while we are on the resume sequence. So to solve this problem we have
7420 * to call special forcewake code that doesn't touch runtime PM and
7421 * doesn't enable the forcewake delayed work.
7422 */
7423 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7424 if (dev_priv->uncore.forcewake_count++ == 0)
7425 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7426 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanoni215733f2013-08-19 13:18:07 -03007427
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007428 if (val & LCPLL_POWER_DOWN_ALLOW) {
7429 val &= ~LCPLL_POWER_DOWN_ALLOW;
7430 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02007431 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007432 }
7433
7434 val = I915_READ(D_COMP);
7435 val |= D_COMP_COMP_FORCE;
7436 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007437 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007438
7439 val = I915_READ(LCPLL_CTL);
7440 val &= ~LCPLL_PLL_DISABLE;
7441 I915_WRITE(LCPLL_CTL, val);
7442
7443 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7444 DRM_ERROR("LCPLL not locked yet\n");
7445
7446 if (val & LCPLL_CD_SOURCE_FCLK) {
7447 val = I915_READ(LCPLL_CTL);
7448 val &= ~LCPLL_CD_SOURCE_FCLK;
7449 I915_WRITE(LCPLL_CTL, val);
7450
7451 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7452 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7453 DRM_ERROR("Switching back to LCPLL failed\n");
7454 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03007455
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007456 /* See the big comment above. */
7457 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7458 if (--dev_priv->uncore.forcewake_count == 0)
7459 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7460 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007461}
7462
Paulo Zanoni765dab62014-03-07 20:08:18 -03007463/*
7464 * Package states C8 and deeper are really deep PC states that can only be
7465 * reached when all the devices on the system allow it, so even if the graphics
7466 * device allows PC8+, it doesn't mean the system will actually get to these
7467 * states. Our driver only allows PC8+ when going into runtime PM.
7468 *
7469 * The requirements for PC8+ are that all the outputs are disabled, the power
7470 * well is disabled and most interrupts are disabled, and these are also
7471 * requirements for runtime PM. When these conditions are met, we manually do
7472 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7473 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7474 * hang the machine.
7475 *
7476 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7477 * the state of some registers, so when we come back from PC8+ we need to
7478 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7479 * need to take care of the registers kept by RC6. Notice that this happens even
7480 * if we don't put the device in PCI D3 state (which is what currently happens
7481 * because of the runtime PM support).
7482 *
7483 * For more, read "Display Sequences for Package C8" on the hardware
7484 * documentation.
7485 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007486void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007487{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007488 struct drm_device *dev = dev_priv->dev;
7489 uint32_t val;
7490
Paulo Zanonic67a4702013-08-19 13:18:09 -03007491 DRM_DEBUG_KMS("Enabling package C8+\n");
7492
Paulo Zanonic67a4702013-08-19 13:18:09 -03007493 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7494 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7495 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7496 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7497 }
7498
7499 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007500 hsw_disable_lcpll(dev_priv, true, true);
7501}
7502
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007503void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007504{
7505 struct drm_device *dev = dev_priv->dev;
7506 uint32_t val;
7507
Paulo Zanonic67a4702013-08-19 13:18:09 -03007508 DRM_DEBUG_KMS("Disabling package C8+\n");
7509
7510 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007511 lpt_init_pch_refclk(dev);
7512
7513 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7514 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7515 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7516 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7517 }
7518
7519 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007520}
7521
Paulo Zanoni9a952a02014-03-07 20:12:34 -03007522static void snb_modeset_global_resources(struct drm_device *dev)
7523{
7524 modeset_update_crtc_power_domains(dev);
7525}
7526
Imre Deak4f074122013-10-16 17:25:51 +03007527static void haswell_modeset_global_resources(struct drm_device *dev)
7528{
Paulo Zanonida723562013-12-19 11:54:51 -02007529 modeset_update_crtc_power_domains(dev);
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02007530}
7531
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007532static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007533 int x, int y,
7534 struct drm_framebuffer *fb)
7535{
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007536 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007537
Paulo Zanoni566b7342013-11-25 15:27:08 -02007538 if (!intel_ddi_pll_select(intel_crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007539 return -EINVAL;
Paulo Zanoni566b7342013-11-25 15:27:08 -02007540 intel_ddi_pll_enable(intel_crtc);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007541
Daniel Vetter644cef32014-04-24 23:55:07 +02007542 intel_crtc->lowfreq_avail = false;
7543
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007544 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007545}
7546
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007547static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7548 struct intel_crtc_config *pipe_config)
7549{
7550 struct drm_device *dev = crtc->base.dev;
7551 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007552 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007553 uint32_t tmp;
7554
Imre Deakb5482bd2014-03-05 16:20:55 +02007555 if (!intel_display_power_enabled(dev_priv,
7556 POWER_DOMAIN_PIPE(crtc->pipe)))
7557 return false;
7558
Daniel Vettere143a212013-07-04 12:01:15 +02007559 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007560 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7561
Daniel Vettereccb1402013-05-22 00:50:22 +02007562 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7563 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7564 enum pipe trans_edp_pipe;
7565 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7566 default:
7567 WARN(1, "unknown pipe linked to edp transcoder\n");
7568 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7569 case TRANS_DDI_EDP_INPUT_A_ON:
7570 trans_edp_pipe = PIPE_A;
7571 break;
7572 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7573 trans_edp_pipe = PIPE_B;
7574 break;
7575 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7576 trans_edp_pipe = PIPE_C;
7577 break;
7578 }
7579
7580 if (trans_edp_pipe == crtc->pipe)
7581 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7582 }
7583
Imre Deakda7e29b2014-02-18 00:02:02 +02007584 if (!intel_display_power_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02007585 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03007586 return false;
7587
Daniel Vettereccb1402013-05-22 00:50:22 +02007588 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007589 if (!(tmp & PIPECONF_ENABLE))
7590 return false;
7591
Daniel Vetter88adfff2013-03-28 10:42:01 +01007592 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03007593 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01007594 * DDI E. So just check whether this pipe is wired to DDI E and whether
7595 * the PCH transcoder is on.
7596 */
Daniel Vettereccb1402013-05-22 00:50:22 +02007597 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01007598 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02007599 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01007600 pipe_config->has_pch_encoder = true;
7601
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007602 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7603 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7604 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007605
7606 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007607 }
7608
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007609 intel_get_pipe_timings(crtc, pipe_config);
7610
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007611 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Imre Deakda7e29b2014-02-18 00:02:02 +02007612 if (intel_display_power_enabled(dev_priv, pfit_domain))
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007613 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01007614
Jesse Barnese59150d2014-01-07 13:30:45 -08007615 if (IS_HASWELL(dev))
7616 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7617 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007618
Daniel Vetter6c49f242013-06-06 12:45:25 +02007619 pipe_config->pixel_multiplier = 1;
7620
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007621 return true;
7622}
7623
Jani Nikula1a915102013-10-16 12:34:48 +03007624static struct {
7625 int clock;
7626 u32 config;
7627} hdmi_audio_clock[] = {
7628 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7629 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7630 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7631 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7632 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7633 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7634 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7635 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7636 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7637 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7638};
7639
7640/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7641static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7642{
7643 int i;
7644
7645 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7646 if (mode->clock == hdmi_audio_clock[i].clock)
7647 break;
7648 }
7649
7650 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7651 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7652 i = 1;
7653 }
7654
7655 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7656 hdmi_audio_clock[i].clock,
7657 hdmi_audio_clock[i].config);
7658
7659 return hdmi_audio_clock[i].config;
7660}
7661
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007662static bool intel_eld_uptodate(struct drm_connector *connector,
7663 int reg_eldv, uint32_t bits_eldv,
7664 int reg_elda, uint32_t bits_elda,
7665 int reg_edid)
7666{
7667 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7668 uint8_t *eld = connector->eld;
7669 uint32_t i;
7670
7671 i = I915_READ(reg_eldv);
7672 i &= bits_eldv;
7673
7674 if (!eld[0])
7675 return !i;
7676
7677 if (!i)
7678 return false;
7679
7680 i = I915_READ(reg_elda);
7681 i &= ~bits_elda;
7682 I915_WRITE(reg_elda, i);
7683
7684 for (i = 0; i < eld[2]; i++)
7685 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7686 return false;
7687
7688 return true;
7689}
7690
Wu Fengguange0dac652011-09-05 14:25:34 +08007691static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007692 struct drm_crtc *crtc,
7693 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007694{
7695 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7696 uint8_t *eld = connector->eld;
7697 uint32_t eldv;
7698 uint32_t len;
7699 uint32_t i;
7700
7701 i = I915_READ(G4X_AUD_VID_DID);
7702
7703 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7704 eldv = G4X_ELDV_DEVCL_DEVBLC;
7705 else
7706 eldv = G4X_ELDV_DEVCTG;
7707
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007708 if (intel_eld_uptodate(connector,
7709 G4X_AUD_CNTL_ST, eldv,
7710 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7711 G4X_HDMIW_HDMIEDID))
7712 return;
7713
Wu Fengguange0dac652011-09-05 14:25:34 +08007714 i = I915_READ(G4X_AUD_CNTL_ST);
7715 i &= ~(eldv | G4X_ELD_ADDR);
7716 len = (i >> 9) & 0x1f; /* ELD buffer size */
7717 I915_WRITE(G4X_AUD_CNTL_ST, i);
7718
7719 if (!eld[0])
7720 return;
7721
7722 len = min_t(uint8_t, eld[2], len);
7723 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7724 for (i = 0; i < len; i++)
7725 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7726
7727 i = I915_READ(G4X_AUD_CNTL_ST);
7728 i |= eldv;
7729 I915_WRITE(G4X_AUD_CNTL_ST, i);
7730}
7731
Wang Xingchao83358c852012-08-16 22:43:37 +08007732static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007733 struct drm_crtc *crtc,
7734 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007735{
7736 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7737 uint8_t *eld = connector->eld;
Wang Xingchao83358c852012-08-16 22:43:37 +08007738 uint32_t eldv;
7739 uint32_t i;
7740 int len;
7741 int pipe = to_intel_crtc(crtc)->pipe;
7742 int tmp;
7743
7744 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7745 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7746 int aud_config = HSW_AUD_CFG(pipe);
7747 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7748
Wang Xingchao83358c852012-08-16 22:43:37 +08007749 /* Audio output enable */
7750 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7751 tmp = I915_READ(aud_cntrl_st2);
7752 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7753 I915_WRITE(aud_cntrl_st2, tmp);
Daniel Vetterc7905792014-04-16 16:56:09 +02007754 POSTING_READ(aud_cntrl_st2);
Wang Xingchao83358c852012-08-16 22:43:37 +08007755
Daniel Vetterc7905792014-04-16 16:56:09 +02007756 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Wang Xingchao83358c852012-08-16 22:43:37 +08007757
7758 /* Set ELD valid state */
7759 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007760 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007761 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7762 I915_WRITE(aud_cntrl_st2, tmp);
7763 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007764 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007765
7766 /* Enable HDMI mode */
7767 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007768 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007769 /* clear N_programing_enable and N_value_index */
7770 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7771 I915_WRITE(aud_config, tmp);
7772
7773 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7774
7775 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7776
7777 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7778 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7779 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7780 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007781 } else {
7782 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7783 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007784
7785 if (intel_eld_uptodate(connector,
7786 aud_cntrl_st2, eldv,
7787 aud_cntl_st, IBX_ELD_ADDRESS,
7788 hdmiw_hdmiedid))
7789 return;
7790
7791 i = I915_READ(aud_cntrl_st2);
7792 i &= ~eldv;
7793 I915_WRITE(aud_cntrl_st2, i);
7794
7795 if (!eld[0])
7796 return;
7797
7798 i = I915_READ(aud_cntl_st);
7799 i &= ~IBX_ELD_ADDRESS;
7800 I915_WRITE(aud_cntl_st, i);
7801 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7802 DRM_DEBUG_DRIVER("port num:%d\n", i);
7803
7804 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7805 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7806 for (i = 0; i < len; i++)
7807 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7808
7809 i = I915_READ(aud_cntrl_st2);
7810 i |= eldv;
7811 I915_WRITE(aud_cntrl_st2, i);
7812
7813}
7814
Wu Fengguange0dac652011-09-05 14:25:34 +08007815static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007816 struct drm_crtc *crtc,
7817 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007818{
7819 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7820 uint8_t *eld = connector->eld;
7821 uint32_t eldv;
7822 uint32_t i;
7823 int len;
7824 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007825 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007826 int aud_cntl_st;
7827 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007828 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007829
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007830 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007831 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7832 aud_config = IBX_AUD_CFG(pipe);
7833 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007834 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007835 } else if (IS_VALLEYVIEW(connector->dev)) {
7836 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7837 aud_config = VLV_AUD_CFG(pipe);
7838 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7839 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007840 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007841 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7842 aud_config = CPT_AUD_CFG(pipe);
7843 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007844 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007845 }
7846
Wang Xingchao9b138a82012-08-09 16:52:18 +08007847 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007848
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007849 if (IS_VALLEYVIEW(connector->dev)) {
7850 struct intel_encoder *intel_encoder;
7851 struct intel_digital_port *intel_dig_port;
7852
7853 intel_encoder = intel_attached_encoder(connector);
7854 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7855 i = intel_dig_port->port;
7856 } else {
7857 i = I915_READ(aud_cntl_st);
7858 i = (i >> 29) & DIP_PORT_SEL_MASK;
7859 /* DIP_Port_Select, 0x1 = PortB */
7860 }
7861
Wu Fengguange0dac652011-09-05 14:25:34 +08007862 if (!i) {
7863 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7864 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007865 eldv = IBX_ELD_VALIDB;
7866 eldv |= IBX_ELD_VALIDB << 4;
7867 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007868 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007869 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007870 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007871 }
7872
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007873 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7874 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7875 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007876 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007877 } else {
7878 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7879 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007880
7881 if (intel_eld_uptodate(connector,
7882 aud_cntrl_st2, eldv,
7883 aud_cntl_st, IBX_ELD_ADDRESS,
7884 hdmiw_hdmiedid))
7885 return;
7886
Wu Fengguange0dac652011-09-05 14:25:34 +08007887 i = I915_READ(aud_cntrl_st2);
7888 i &= ~eldv;
7889 I915_WRITE(aud_cntrl_st2, i);
7890
7891 if (!eld[0])
7892 return;
7893
Wu Fengguange0dac652011-09-05 14:25:34 +08007894 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007895 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007896 I915_WRITE(aud_cntl_st, i);
7897
7898 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7899 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7900 for (i = 0; i < len; i++)
7901 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7902
7903 i = I915_READ(aud_cntrl_st2);
7904 i |= eldv;
7905 I915_WRITE(aud_cntrl_st2, i);
7906}
7907
7908void intel_write_eld(struct drm_encoder *encoder,
7909 struct drm_display_mode *mode)
7910{
7911 struct drm_crtc *crtc = encoder->crtc;
7912 struct drm_connector *connector;
7913 struct drm_device *dev = encoder->dev;
7914 struct drm_i915_private *dev_priv = dev->dev_private;
7915
7916 connector = drm_select_eld(encoder, mode);
7917 if (!connector)
7918 return;
7919
7920 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7921 connector->base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03007922 connector->name,
Wu Fengguange0dac652011-09-05 14:25:34 +08007923 connector->encoder->base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +03007924 connector->encoder->name);
Wu Fengguange0dac652011-09-05 14:25:34 +08007925
7926 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7927
7928 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007929 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007930}
7931
Chris Wilson560b85b2010-08-07 11:01:38 +01007932static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7933{
7934 struct drm_device *dev = crtc->dev;
7935 struct drm_i915_private *dev_priv = dev->dev_private;
7936 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson4b0e3332014-05-30 16:35:26 +03007937 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01007938
Chris Wilson4b0e3332014-05-30 16:35:26 +03007939 if (base != intel_crtc->cursor_base) {
Chris Wilson560b85b2010-08-07 11:01:38 +01007940 /* On these chipsets we can only modify the base whilst
7941 * the cursor is disabled.
7942 */
Chris Wilson4b0e3332014-05-30 16:35:26 +03007943 if (intel_crtc->cursor_cntl) {
7944 I915_WRITE(_CURACNTR, 0);
7945 POSTING_READ(_CURACNTR);
7946 intel_crtc->cursor_cntl = 0;
7947 }
7948
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007949 I915_WRITE(_CURABASE, base);
Chris Wilson4b0e3332014-05-30 16:35:26 +03007950 POSTING_READ(_CURABASE);
7951 }
Chris Wilson560b85b2010-08-07 11:01:38 +01007952
Chris Wilson4b0e3332014-05-30 16:35:26 +03007953 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7954 cntl = 0;
7955 if (base)
7956 cntl = (CURSOR_ENABLE |
Chris Wilson560b85b2010-08-07 11:01:38 +01007957 CURSOR_GAMMA_ENABLE |
Chris Wilson4b0e3332014-05-30 16:35:26 +03007958 CURSOR_FORMAT_ARGB);
7959 if (intel_crtc->cursor_cntl != cntl) {
7960 I915_WRITE(_CURACNTR, cntl);
7961 POSTING_READ(_CURACNTR);
7962 intel_crtc->cursor_cntl = cntl;
7963 }
Chris Wilson560b85b2010-08-07 11:01:38 +01007964}
7965
7966static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7967{
7968 struct drm_device *dev = crtc->dev;
7969 struct drm_i915_private *dev_priv = dev->dev_private;
7970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7971 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03007972 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01007973
Chris Wilson4b0e3332014-05-30 16:35:26 +03007974 cntl = 0;
7975 if (base) {
7976 cntl = MCURSOR_GAMMA_ENABLE;
7977 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307978 case 64:
7979 cntl |= CURSOR_MODE_64_ARGB_AX;
7980 break;
7981 case 128:
7982 cntl |= CURSOR_MODE_128_ARGB_AX;
7983 break;
7984 case 256:
7985 cntl |= CURSOR_MODE_256_ARGB_AX;
7986 break;
7987 default:
7988 WARN_ON(1);
7989 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01007990 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03007991 cntl |= pipe << 28; /* Connect to correct pipe */
Chris Wilson560b85b2010-08-07 11:01:38 +01007992 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03007993 if (intel_crtc->cursor_cntl != cntl) {
7994 I915_WRITE(CURCNTR(pipe), cntl);
7995 POSTING_READ(CURCNTR(pipe));
7996 intel_crtc->cursor_cntl = cntl;
7997 }
7998
Chris Wilson560b85b2010-08-07 11:01:38 +01007999 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08008000 I915_WRITE(CURBASE(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01008001 POSTING_READ(CURBASE(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01008002}
8003
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008004static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
8005{
8006 struct drm_device *dev = crtc->dev;
8007 struct drm_i915_private *dev_priv = dev->dev_private;
8008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8009 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008010 uint32_t cntl;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008011
Chris Wilson4b0e3332014-05-30 16:35:26 +03008012 cntl = 0;
8013 if (base) {
8014 cntl = MCURSOR_GAMMA_ENABLE;
8015 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308016 case 64:
8017 cntl |= CURSOR_MODE_64_ARGB_AX;
8018 break;
8019 case 128:
8020 cntl |= CURSOR_MODE_128_ARGB_AX;
8021 break;
8022 case 256:
8023 cntl |= CURSOR_MODE_256_ARGB_AX;
8024 break;
8025 default:
8026 WARN_ON(1);
8027 return;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008028 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008029 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008030 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8031 cntl |= CURSOR_PIPE_CSC_ENABLE;
8032
8033 if (intel_crtc->cursor_cntl != cntl) {
8034 I915_WRITE(CURCNTR(pipe), cntl);
8035 POSTING_READ(CURCNTR(pipe));
8036 intel_crtc->cursor_cntl = cntl;
8037 }
8038
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008039 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008040 I915_WRITE(CURBASE(pipe), base);
8041 POSTING_READ(CURBASE(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008042}
8043
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008044/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008045static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8046 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008047{
8048 struct drm_device *dev = crtc->dev;
8049 struct drm_i915_private *dev_priv = dev->dev_private;
8050 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8051 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008052 int x = crtc->cursor_x;
8053 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008054 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008055
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008056 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008057 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008058
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008059 if (x >= intel_crtc->config.pipe_src_w)
8060 base = 0;
8061
8062 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008063 base = 0;
8064
8065 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008066 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008067 base = 0;
8068
8069 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8070 x = -x;
8071 }
8072 pos |= x << CURSOR_X_SHIFT;
8073
8074 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008075 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008076 base = 0;
8077
8078 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8079 y = -y;
8080 }
8081 pos |= y << CURSOR_Y_SHIFT;
8082
Chris Wilson4b0e3332014-05-30 16:35:26 +03008083 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008084 return;
8085
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008086 I915_WRITE(CURPOS(pipe), pos);
8087
8088 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008089 ivb_update_cursor(crtc, base);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008090 else if (IS_845G(dev) || IS_I865G(dev))
8091 i845_update_cursor(crtc, base);
8092 else
8093 i9xx_update_cursor(crtc, base);
Chris Wilson4b0e3332014-05-30 16:35:26 +03008094 intel_crtc->cursor_base = base;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008095}
8096
Matt Ropere3287952014-06-10 08:28:12 -07008097/*
8098 * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
8099 *
8100 * Note that the object's reference will be consumed if the update fails. If
8101 * the update succeeds, the reference of the old object (if any) will be
8102 * consumed.
8103 */
8104static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8105 struct drm_i915_gem_object *obj,
8106 uint32_t width, uint32_t height)
Jesse Barnes79e53942008-11-07 14:24:08 -08008107{
8108 struct drm_device *dev = crtc->dev;
8109 struct drm_i915_private *dev_priv = dev->dev_private;
8110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson64f962e2014-03-26 12:38:15 +00008111 unsigned old_width;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008112 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008113 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008114
Jesse Barnes79e53942008-11-07 14:24:08 -08008115 /* if we want to turn off the cursor ignore width and height */
Matt Ropere3287952014-06-10 08:28:12 -07008116 if (!obj) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008117 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008118 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00008119 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10008120 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008121 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08008122 }
8123
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308124 /* Check for which cursor types we support */
8125 if (!((width == 64 && height == 64) ||
8126 (width == 128 && height == 128 && !IS_GEN2(dev)) ||
8127 (width == 256 && height == 256 && !IS_GEN2(dev)))) {
8128 DRM_DEBUG("Cursor dimension not supported\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08008129 return -EINVAL;
8130 }
8131
Chris Wilson05394f32010-11-08 19:18:58 +00008132 if (obj->base.size < width * height * 4) {
Matt Ropere3287952014-06-10 08:28:12 -07008133 DRM_DEBUG_KMS("buffer is too small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10008134 ret = -ENOMEM;
8135 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008136 }
8137
Dave Airlie71acb5e2008-12-30 20:31:46 +10008138 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008139 mutex_lock(&dev->struct_mutex);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008140 if (!INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00008141 unsigned alignment;
8142
Chris Wilsond9e86c02010-11-10 16:40:20 +00008143 if (obj->tiling_mode) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008144 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Chris Wilsond9e86c02010-11-10 16:40:20 +00008145 ret = -EINVAL;
8146 goto fail_locked;
8147 }
8148
Chris Wilson693db182013-03-05 14:52:39 +00008149 /* Note that the w/a also requires 2 PTE of padding following
8150 * the bo. We currently fill all unused PTE with the shadow
8151 * page and so we should always have valid PTE following the
8152 * cursor preventing the VT-d warning.
8153 */
8154 alignment = 0;
8155 if (need_vtd_wa(dev))
8156 alignment = 64*1024;
8157
8158 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01008159 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008160 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01008161 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008162 }
8163
Chris Wilsond9e86c02010-11-10 16:40:20 +00008164 ret = i915_gem_object_put_fence(obj);
8165 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008166 DRM_DEBUG_KMS("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00008167 goto fail_unpin;
8168 }
8169
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008170 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008171 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01008172 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson00731152014-05-21 12:42:56 +01008173 ret = i915_gem_object_attach_phys(obj, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008174 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008175 DRM_DEBUG_KMS("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008176 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10008177 }
Chris Wilson00731152014-05-21 12:42:56 +01008178 addr = obj->phys_handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008179 }
8180
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008181 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04008182 I915_WRITE(CURSIZE, (height << 12) | width);
8183
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008184 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008185 if (intel_crtc->cursor_bo) {
Chris Wilson00731152014-05-21 12:42:56 +01008186 if (!INTEL_INFO(dev)->cursor_needs_physical)
Chris Wilsoncc98b412013-08-09 12:25:09 +01008187 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008188 }
Jesse Barnes80824002009-09-10 15:28:06 -07008189
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008190 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008191
Chris Wilson64f962e2014-03-26 12:38:15 +00008192 old_width = intel_crtc->cursor_width;
8193
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008194 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00008195 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008196 intel_crtc->cursor_width = width;
8197 intel_crtc->cursor_height = height;
8198
Chris Wilson64f962e2014-03-26 12:38:15 +00008199 if (intel_crtc->active) {
8200 if (old_width != width)
8201 intel_update_watermarks(crtc);
Ville Syrjäläf2f5f772013-09-17 18:33:44 +03008202 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Chris Wilson64f962e2014-03-26 12:38:15 +00008203 }
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008204
Jesse Barnes79e53942008-11-07 14:24:08 -08008205 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008206fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01008207 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008208fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10008209 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00008210fail:
Chris Wilson05394f32010-11-08 19:18:58 +00008211 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10008212 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008213}
8214
Jesse Barnes79e53942008-11-07 14:24:08 -08008215static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008216 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008217{
James Simmons72034252010-08-03 01:33:19 +01008218 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008220
James Simmons72034252010-08-03 01:33:19 +01008221 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008222 intel_crtc->lut_r[i] = red[i] >> 8;
8223 intel_crtc->lut_g[i] = green[i] >> 8;
8224 intel_crtc->lut_b[i] = blue[i] >> 8;
8225 }
8226
8227 intel_crtc_load_lut(crtc);
8228}
8229
Jesse Barnes79e53942008-11-07 14:24:08 -08008230/* VESA 640x480x72Hz mode to set on the pipe */
8231static struct drm_display_mode load_detect_mode = {
8232 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8233 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8234};
8235
Daniel Vettera8bb6812014-02-10 18:00:39 +01008236struct drm_framebuffer *
8237__intel_framebuffer_create(struct drm_device *dev,
8238 struct drm_mode_fb_cmd2 *mode_cmd,
8239 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008240{
8241 struct intel_framebuffer *intel_fb;
8242 int ret;
8243
8244 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8245 if (!intel_fb) {
8246 drm_gem_object_unreference_unlocked(&obj->base);
8247 return ERR_PTR(-ENOMEM);
8248 }
8249
8250 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008251 if (ret)
8252 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008253
8254 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008255err:
8256 drm_gem_object_unreference_unlocked(&obj->base);
8257 kfree(intel_fb);
8258
8259 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008260}
8261
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008262static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008263intel_framebuffer_create(struct drm_device *dev,
8264 struct drm_mode_fb_cmd2 *mode_cmd,
8265 struct drm_i915_gem_object *obj)
8266{
8267 struct drm_framebuffer *fb;
8268 int ret;
8269
8270 ret = i915_mutex_lock_interruptible(dev);
8271 if (ret)
8272 return ERR_PTR(ret);
8273 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8274 mutex_unlock(&dev->struct_mutex);
8275
8276 return fb;
8277}
8278
Chris Wilsond2dff872011-04-19 08:36:26 +01008279static u32
8280intel_framebuffer_pitch_for_width(int width, int bpp)
8281{
8282 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8283 return ALIGN(pitch, 64);
8284}
8285
8286static u32
8287intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8288{
8289 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
8290 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
8291}
8292
8293static struct drm_framebuffer *
8294intel_framebuffer_create_for_mode(struct drm_device *dev,
8295 struct drm_display_mode *mode,
8296 int depth, int bpp)
8297{
8298 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008299 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008300
8301 obj = i915_gem_alloc_object(dev,
8302 intel_framebuffer_size_for_mode(mode, bpp));
8303 if (obj == NULL)
8304 return ERR_PTR(-ENOMEM);
8305
8306 mode_cmd.width = mode->hdisplay;
8307 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008308 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8309 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008310 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008311
8312 return intel_framebuffer_create(dev, &mode_cmd, obj);
8313}
8314
8315static struct drm_framebuffer *
8316mode_fits_in_fbdev(struct drm_device *dev,
8317 struct drm_display_mode *mode)
8318{
Daniel Vetter4520f532013-10-09 09:18:51 +02008319#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008320 struct drm_i915_private *dev_priv = dev->dev_private;
8321 struct drm_i915_gem_object *obj;
8322 struct drm_framebuffer *fb;
8323
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008324 if (!dev_priv->fbdev)
8325 return NULL;
8326
8327 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008328 return NULL;
8329
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008330 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008331 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008332
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008333 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008334 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8335 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008336 return NULL;
8337
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008338 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008339 return NULL;
8340
8341 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008342#else
8343 return NULL;
8344#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008345}
8346
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008347bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008348 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008349 struct intel_load_detect_pipe *old,
8350 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008351{
8352 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008353 struct intel_encoder *intel_encoder =
8354 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008355 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008356 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008357 struct drm_crtc *crtc = NULL;
8358 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008359 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008360 struct drm_mode_config *config = &dev->mode_config;
8361 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008362
Chris Wilsond2dff872011-04-19 08:36:26 +01008363 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008364 connector->base.id, connector->name,
Jani Nikula8e329a02014-06-03 14:56:21 +03008365 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008366
Rob Clark51fd3712013-11-19 12:10:12 -05008367 drm_modeset_acquire_init(ctx, 0);
8368
8369retry:
8370 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8371 if (ret)
8372 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008373
Jesse Barnes79e53942008-11-07 14:24:08 -08008374 /*
8375 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008376 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008377 * - if the connector already has an assigned crtc, use it (but make
8378 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008379 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008380 * - try to find the first unused crtc that can drive this connector,
8381 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008382 */
8383
8384 /* See if we already have a CRTC for this connector */
8385 if (encoder->crtc) {
8386 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008387
Rob Clark51fd3712013-11-19 12:10:12 -05008388 ret = drm_modeset_lock(&crtc->mutex, ctx);
8389 if (ret)
8390 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01008391
Daniel Vetter24218aa2012-08-12 19:27:11 +02008392 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008393 old->load_detect_temp = false;
8394
8395 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008396 if (connector->dpms != DRM_MODE_DPMS_ON)
8397 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008398
Chris Wilson71731882011-04-19 23:10:58 +01008399 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008400 }
8401
8402 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008403 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008404 i++;
8405 if (!(encoder->possible_crtcs & (1 << i)))
8406 continue;
8407 if (!possible_crtc->enabled) {
8408 crtc = possible_crtc;
8409 break;
8410 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008411 }
8412
8413 /*
8414 * If we didn't find an unused CRTC, don't use any.
8415 */
8416 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008417 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05008418 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08008419 }
8420
Rob Clark51fd3712013-11-19 12:10:12 -05008421 ret = drm_modeset_lock(&crtc->mutex, ctx);
8422 if (ret)
8423 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02008424 intel_encoder->new_crtc = to_intel_crtc(crtc);
8425 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008426
8427 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008428 intel_crtc->new_enabled = true;
8429 intel_crtc->new_config = &intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008430 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008431 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008432 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008433
Chris Wilson64927112011-04-20 07:25:26 +01008434 if (!mode)
8435 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008436
Chris Wilsond2dff872011-04-19 08:36:26 +01008437 /* We need a framebuffer large enough to accommodate all accesses
8438 * that the plane may generate whilst we perform load detection.
8439 * We can not rely on the fbcon either being present (we get called
8440 * during its initialisation to detect all boot displays, or it may
8441 * not even exist) or that it is large enough to satisfy the
8442 * requested mode.
8443 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008444 fb = mode_fits_in_fbdev(dev, mode);
8445 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008446 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008447 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8448 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008449 } else
8450 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008451 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008452 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008453 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008454 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008455
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008456 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008457 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008458 if (old->release_fb)
8459 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008460 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008461 }
Chris Wilson71731882011-04-19 23:10:58 +01008462
Jesse Barnes79e53942008-11-07 14:24:08 -08008463 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008464 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008465 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008466
8467 fail:
8468 intel_crtc->new_enabled = crtc->enabled;
8469 if (intel_crtc->new_enabled)
8470 intel_crtc->new_config = &intel_crtc->config;
8471 else
8472 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05008473fail_unlock:
8474 if (ret == -EDEADLK) {
8475 drm_modeset_backoff(ctx);
8476 goto retry;
8477 }
8478
8479 drm_modeset_drop_locks(ctx);
8480 drm_modeset_acquire_fini(ctx);
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008481
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008482 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008483}
8484
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008485void intel_release_load_detect_pipe(struct drm_connector *connector,
Rob Clark51fd3712013-11-19 12:10:12 -05008486 struct intel_load_detect_pipe *old,
8487 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008488{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008489 struct intel_encoder *intel_encoder =
8490 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008491 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008492 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008494
Chris Wilsond2dff872011-04-19 08:36:26 +01008495 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008496 connector->base.id, connector->name,
Jani Nikula8e329a02014-06-03 14:56:21 +03008497 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008498
Chris Wilson8261b192011-04-19 23:18:09 +01008499 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008500 to_intel_connector(connector)->new_encoder = NULL;
8501 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008502 intel_crtc->new_enabled = false;
8503 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008504 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01008505
Daniel Vetter36206362012-12-10 20:42:17 +01008506 if (old->release_fb) {
8507 drm_framebuffer_unregister_private(old->release_fb);
8508 drm_framebuffer_unreference(old->release_fb);
8509 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008510
Rob Clark51fd3712013-11-19 12:10:12 -05008511 goto unlock;
Chris Wilson0622a532011-04-21 09:32:11 +01008512 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008513 }
8514
Eric Anholtc751ce42010-03-25 11:48:48 -07008515 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008516 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8517 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01008518
Rob Clark51fd3712013-11-19 12:10:12 -05008519unlock:
8520 drm_modeset_drop_locks(ctx);
8521 drm_modeset_acquire_fini(ctx);
Jesse Barnes79e53942008-11-07 14:24:08 -08008522}
8523
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008524static int i9xx_pll_refclk(struct drm_device *dev,
8525 const struct intel_crtc_config *pipe_config)
8526{
8527 struct drm_i915_private *dev_priv = dev->dev_private;
8528 u32 dpll = pipe_config->dpll_hw_state.dpll;
8529
8530 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008531 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008532 else if (HAS_PCH_SPLIT(dev))
8533 return 120000;
8534 else if (!IS_GEN2(dev))
8535 return 96000;
8536 else
8537 return 48000;
8538}
8539
Jesse Barnes79e53942008-11-07 14:24:08 -08008540/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008541static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8542 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08008543{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008544 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008545 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008546 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008547 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008548 u32 fp;
8549 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008550 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08008551
8552 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03008553 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008554 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008555 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008556
8557 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008558 if (IS_PINEVIEW(dev)) {
8559 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8560 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008561 } else {
8562 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8563 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8564 }
8565
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008566 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008567 if (IS_PINEVIEW(dev))
8568 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8569 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008570 else
8571 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008572 DPLL_FPA01_P1_POST_DIV_SHIFT);
8573
8574 switch (dpll & DPLL_MODE_MASK) {
8575 case DPLLB_MODE_DAC_SERIAL:
8576 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8577 5 : 10;
8578 break;
8579 case DPLLB_MODE_LVDS:
8580 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8581 7 : 14;
8582 break;
8583 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008584 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008585 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008586 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008587 }
8588
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008589 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008590 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008591 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008592 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008593 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008594 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008595 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008596
8597 if (is_lvds) {
8598 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8599 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008600
8601 if (lvds & LVDS_CLKB_POWER_UP)
8602 clock.p2 = 7;
8603 else
8604 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008605 } else {
8606 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8607 clock.p1 = 2;
8608 else {
8609 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8610 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8611 }
8612 if (dpll & PLL_P2_DIVIDE_BY_4)
8613 clock.p2 = 4;
8614 else
8615 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008616 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008617
8618 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008619 }
8620
Ville Syrjälä18442d02013-09-13 16:00:08 +03008621 /*
8622 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008623 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03008624 * encoder's get_config() function.
8625 */
8626 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008627}
8628
Ville Syrjälä6878da02013-09-13 15:59:11 +03008629int intel_dotclock_calculate(int link_freq,
8630 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008631{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008632 /*
8633 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008634 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008635 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008636 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008637 *
8638 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008639 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008640 */
8641
Ville Syrjälä6878da02013-09-13 15:59:11 +03008642 if (!m_n->link_n)
8643 return 0;
8644
8645 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8646}
8647
Ville Syrjälä18442d02013-09-13 16:00:08 +03008648static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8649 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008650{
8651 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008652
8653 /* read out port_clock from the DPLL */
8654 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008655
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008656 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008657 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008658 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008659 * agree once we know their relationship in the encoder's
8660 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008661 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008662 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008663 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8664 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008665}
8666
8667/** Returns the currently programmed mode of the given pipe. */
8668struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8669 struct drm_crtc *crtc)
8670{
Jesse Barnes548f2452011-02-17 10:40:53 -08008671 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008672 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008673 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008674 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008675 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008676 int htot = I915_READ(HTOTAL(cpu_transcoder));
8677 int hsync = I915_READ(HSYNC(cpu_transcoder));
8678 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8679 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008680 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008681
8682 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8683 if (!mode)
8684 return NULL;
8685
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008686 /*
8687 * Construct a pipe_config sufficient for getting the clock info
8688 * back out of crtc_clock_get.
8689 *
8690 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8691 * to use a real value here instead.
8692 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008693 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008694 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008695 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8696 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8697 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008698 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8699
Ville Syrjälä773ae032013-09-23 17:48:20 +03008700 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008701 mode->hdisplay = (htot & 0xffff) + 1;
8702 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8703 mode->hsync_start = (hsync & 0xffff) + 1;
8704 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8705 mode->vdisplay = (vtot & 0xffff) + 1;
8706 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8707 mode->vsync_start = (vsync & 0xffff) + 1;
8708 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8709
8710 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008711
8712 return mode;
8713}
8714
Daniel Vetter3dec0092010-08-20 21:40:52 +02008715static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07008716{
8717 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008718 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008719 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8720 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008721 int dpll_reg = DPLL(pipe);
8722 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008723
Eric Anholtbad720f2009-10-22 16:11:14 -07008724 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008725 return;
8726
8727 if (!dev_priv->lvds_downclock_avail)
8728 return;
8729
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008730 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008731 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008732 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008733
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008734 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008735
8736 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8737 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008738 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008739
Jesse Barnes652c3932009-08-17 13:31:43 -07008740 dpll = I915_READ(dpll_reg);
8741 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008742 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008743 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008744}
8745
8746static void intel_decrease_pllclock(struct drm_crtc *crtc)
8747{
8748 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008749 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008750 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008751
Eric Anholtbad720f2009-10-22 16:11:14 -07008752 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008753 return;
8754
8755 if (!dev_priv->lvds_downclock_avail)
8756 return;
8757
8758 /*
8759 * Since this is called by a timer, we should never get here in
8760 * the manual case.
8761 */
8762 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008763 int pipe = intel_crtc->pipe;
8764 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008765 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008766
Zhao Yakui44d98a62009-10-09 11:39:40 +08008767 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008768
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008769 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008770
Chris Wilson074b5e12012-05-02 12:07:06 +01008771 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008772 dpll |= DISPLAY_RATE_SELECT_FPA1;
8773 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008774 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008775 dpll = I915_READ(dpll_reg);
8776 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008777 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008778 }
8779
8780}
8781
Chris Wilsonf047e392012-07-21 12:31:41 +01008782void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008783{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008784 struct drm_i915_private *dev_priv = dev->dev_private;
8785
Chris Wilsonf62a0072014-02-21 17:55:39 +00008786 if (dev_priv->mm.busy)
8787 return;
8788
Paulo Zanoni43694d62014-03-07 20:08:08 -03008789 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008790 i915_update_gfx_val(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00008791 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01008792}
8793
8794void intel_mark_idle(struct drm_device *dev)
8795{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008796 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008797 struct drm_crtc *crtc;
8798
Chris Wilsonf62a0072014-02-21 17:55:39 +00008799 if (!dev_priv->mm.busy)
8800 return;
8801
8802 dev_priv->mm.busy = false;
8803
Jani Nikulad330a952014-01-21 11:24:25 +02008804 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008805 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00008806
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008807 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07008808 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00008809 continue;
8810
8811 intel_decrease_pllclock(crtc);
8812 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008813
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008814 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008815 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008816
8817out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03008818 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008819}
8820
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -07008821
Chris Wilsonc65355b2013-06-06 16:53:41 -03008822void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01008823 struct intel_engine_cs *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008824{
8825 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008826 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008827
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -07008828 intel_edp_psr_exit(dev, true);
8829
Jani Nikulad330a952014-01-21 11:24:25 +02008830 if (!i915.powersave)
Jesse Barnes652c3932009-08-17 13:31:43 -07008831 return;
8832
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008833 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07008834 if (!crtc->primary->fb)
Jesse Barnes652c3932009-08-17 13:31:43 -07008835 continue;
8836
Matt Roperf4510a22014-04-01 15:22:40 -07008837 if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
Chris Wilsonc65355b2013-06-06 16:53:41 -03008838 continue;
8839
8840 intel_increase_pllclock(crtc);
8841 if (ring && intel_fbc_enabled(dev))
8842 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008843 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008844}
8845
Jesse Barnes79e53942008-11-07 14:24:08 -08008846static void intel_crtc_destroy(struct drm_crtc *crtc)
8847{
8848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008849 struct drm_device *dev = crtc->dev;
8850 struct intel_unpin_work *work;
8851 unsigned long flags;
8852
8853 spin_lock_irqsave(&dev->event_lock, flags);
8854 work = intel_crtc->unpin_work;
8855 intel_crtc->unpin_work = NULL;
8856 spin_unlock_irqrestore(&dev->event_lock, flags);
8857
8858 if (work) {
8859 cancel_work_sync(&work->work);
8860 kfree(work);
8861 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008862
8863 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008864
Jesse Barnes79e53942008-11-07 14:24:08 -08008865 kfree(intel_crtc);
8866}
8867
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008868static void intel_unpin_work_fn(struct work_struct *__work)
8869{
8870 struct intel_unpin_work *work =
8871 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008872 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008873
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008874 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008875 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008876 drm_gem_object_unreference(&work->pending_flip_obj->base);
8877 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008878
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008879 intel_update_fbc(dev);
8880 mutex_unlock(&dev->struct_mutex);
8881
8882 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8883 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8884
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008885 kfree(work);
8886}
8887
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008888static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008889 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008890{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008891 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008892 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8893 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008894 unsigned long flags;
8895
8896 /* Ignore early vblank irqs */
8897 if (intel_crtc == NULL)
8898 return;
8899
8900 spin_lock_irqsave(&dev->event_lock, flags);
8901 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008902
8903 /* Ensure we don't miss a work->pending update ... */
8904 smp_rmb();
8905
8906 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008907 spin_unlock_irqrestore(&dev->event_lock, flags);
8908 return;
8909 }
8910
Chris Wilsone7d841c2012-12-03 11:36:30 +00008911 /* and that the unpin work is consistent wrt ->pending. */
8912 smp_rmb();
8913
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008914 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008915
Rob Clark45a066e2012-10-08 14:50:40 -05008916 if (work->event)
8917 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008918
Daniel Vetter87b6b102014-05-15 15:33:46 +02008919 drm_crtc_vblank_put(crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008920
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008921 spin_unlock_irqrestore(&dev->event_lock, flags);
8922
Daniel Vetter2c10d572012-12-20 21:24:07 +01008923 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008924
8925 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008926
8927 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008928}
8929
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008930void intel_finish_page_flip(struct drm_device *dev, int pipe)
8931{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008932 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008933 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8934
Mario Kleiner49b14a52010-12-09 07:00:07 +01008935 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008936}
8937
8938void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8939{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008940 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008941 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8942
Mario Kleiner49b14a52010-12-09 07:00:07 +01008943 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008944}
8945
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03008946/* Is 'a' after or equal to 'b'? */
8947static bool g4x_flip_count_after_eq(u32 a, u32 b)
8948{
8949 return !((a - b) & 0x80000000);
8950}
8951
8952static bool page_flip_finished(struct intel_crtc *crtc)
8953{
8954 struct drm_device *dev = crtc->base.dev;
8955 struct drm_i915_private *dev_priv = dev->dev_private;
8956
8957 /*
8958 * The relevant registers doen't exist on pre-ctg.
8959 * As the flip done interrupt doesn't trigger for mmio
8960 * flips on gmch platforms, a flip count check isn't
8961 * really needed there. But since ctg has the registers,
8962 * include it in the check anyway.
8963 */
8964 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
8965 return true;
8966
8967 /*
8968 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
8969 * used the same base address. In that case the mmio flip might
8970 * have completed, but the CS hasn't even executed the flip yet.
8971 *
8972 * A flip count check isn't enough as the CS might have updated
8973 * the base address just after start of vblank, but before we
8974 * managed to process the interrupt. This means we'd complete the
8975 * CS flip too soon.
8976 *
8977 * Combining both checks should get us a good enough result. It may
8978 * still happen that the CS flip has been executed, but has not
8979 * yet actually completed. But in case the base address is the same
8980 * anyway, we don't really care.
8981 */
8982 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
8983 crtc->unpin_work->gtt_offset &&
8984 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
8985 crtc->unpin_work->flip_count);
8986}
8987
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008988void intel_prepare_page_flip(struct drm_device *dev, int plane)
8989{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008990 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008991 struct intel_crtc *intel_crtc =
8992 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8993 unsigned long flags;
8994
Chris Wilsone7d841c2012-12-03 11:36:30 +00008995 /* NB: An MMIO update of the plane base pointer will also
8996 * generate a page-flip completion irq, i.e. every modeset
8997 * is also accompanied by a spurious intel_prepare_page_flip().
8998 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008999 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009000 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009001 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009002 spin_unlock_irqrestore(&dev->event_lock, flags);
9003}
9004
Robin Schroereba905b2014-05-18 02:24:50 +02009005static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009006{
9007 /* Ensure that the work item is consistent when activating it ... */
9008 smp_wmb();
9009 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9010 /* and that it is marked active as soon as the irq could fire. */
9011 smp_wmb();
9012}
9013
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009014static int intel_gen2_queue_flip(struct drm_device *dev,
9015 struct drm_crtc *crtc,
9016 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009017 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009018 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009019 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009020{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009021 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009022 u32 flip_mask;
9023 int ret;
9024
Daniel Vetter6d90c952012-04-26 23:28:05 +02009025 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009026 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009027 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009028
9029 /* Can't queue multiple flips, so wait for the previous
9030 * one to finish before executing the next.
9031 */
9032 if (intel_crtc->plane)
9033 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9034 else
9035 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009036 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9037 intel_ring_emit(ring, MI_NOOP);
9038 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9039 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9040 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009041 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009042 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009043
9044 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009045 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009046 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009047}
9048
9049static int intel_gen3_queue_flip(struct drm_device *dev,
9050 struct drm_crtc *crtc,
9051 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009052 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009053 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009054 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009055{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009056 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009057 u32 flip_mask;
9058 int ret;
9059
Daniel Vetter6d90c952012-04-26 23:28:05 +02009060 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009061 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009062 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009063
9064 if (intel_crtc->plane)
9065 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9066 else
9067 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009068 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9069 intel_ring_emit(ring, MI_NOOP);
9070 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9071 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9072 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009073 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009074 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009075
Chris Wilsone7d841c2012-12-03 11:36:30 +00009076 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009077 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009078 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009079}
9080
9081static int intel_gen4_queue_flip(struct drm_device *dev,
9082 struct drm_crtc *crtc,
9083 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009084 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009085 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009086 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009087{
9088 struct drm_i915_private *dev_priv = dev->dev_private;
9089 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9090 uint32_t pf, pipesrc;
9091 int ret;
9092
Daniel Vetter6d90c952012-04-26 23:28:05 +02009093 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009094 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009095 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009096
9097 /* i965+ uses the linear or tiled offsets from the
9098 * Display Registers (which do not change across a page-flip)
9099 * so we need only reprogram the base address.
9100 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009101 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9102 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9103 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009104 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009105 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009106
9107 /* XXX Enabling the panel-fitter across page-flip is so far
9108 * untested on non-native modes, so ignore it for now.
9109 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9110 */
9111 pf = 0;
9112 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009113 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009114
9115 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009116 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009117 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009118}
9119
9120static int intel_gen6_queue_flip(struct drm_device *dev,
9121 struct drm_crtc *crtc,
9122 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009123 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009124 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009125 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009126{
9127 struct drm_i915_private *dev_priv = dev->dev_private;
9128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9129 uint32_t pf, pipesrc;
9130 int ret;
9131
Daniel Vetter6d90c952012-04-26 23:28:05 +02009132 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009133 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009134 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009135
Daniel Vetter6d90c952012-04-26 23:28:05 +02009136 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9137 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9138 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009139 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009140
Chris Wilson99d9acd2012-04-17 20:37:00 +01009141 /* Contrary to the suggestions in the documentation,
9142 * "Enable Panel Fitter" does not seem to be required when page
9143 * flipping with a non-native mode, and worse causes a normal
9144 * modeset to fail.
9145 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9146 */
9147 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009148 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009149 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009150
9151 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009152 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009153 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009154}
9155
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009156static int intel_gen7_queue_flip(struct drm_device *dev,
9157 struct drm_crtc *crtc,
9158 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009159 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009160 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009161 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009162{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009164 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009165 int len, ret;
9166
Robin Schroereba905b2014-05-18 02:24:50 +02009167 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009168 case PLANE_A:
9169 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9170 break;
9171 case PLANE_B:
9172 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9173 break;
9174 case PLANE_C:
9175 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9176 break;
9177 default:
9178 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009179 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009180 }
9181
Chris Wilsonffe74d72013-08-26 20:58:12 +01009182 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009183 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009184 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009185 /*
9186 * On Gen 8, SRM is now taking an extra dword to accommodate
9187 * 48bits addresses, and we need a NOOP for the batch size to
9188 * stay even.
9189 */
9190 if (IS_GEN8(dev))
9191 len += 2;
9192 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009193
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009194 /*
9195 * BSpec MI_DISPLAY_FLIP for IVB:
9196 * "The full packet must be contained within the same cache line."
9197 *
9198 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9199 * cacheline, if we ever start emitting more commands before
9200 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9201 * then do the cacheline alignment, and finally emit the
9202 * MI_DISPLAY_FLIP.
9203 */
9204 ret = intel_ring_cacheline_align(ring);
9205 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009206 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009207
Chris Wilsonffe74d72013-08-26 20:58:12 +01009208 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009209 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009210 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009211
Chris Wilsonffe74d72013-08-26 20:58:12 +01009212 /* Unmask the flip-done completion message. Note that the bspec says that
9213 * we should do this for both the BCS and RCS, and that we must not unmask
9214 * more than one flip event at any time (or ensure that one flip message
9215 * can be sent by waiting for flip-done prior to queueing new flips).
9216 * Experimentation says that BCS works despite DERRMR masking all
9217 * flip-done completion events and that unmasking all planes at once
9218 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9219 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9220 */
9221 if (ring->id == RCS) {
9222 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9223 intel_ring_emit(ring, DERRMR);
9224 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9225 DERRMR_PIPEB_PRI_FLIP_DONE |
9226 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009227 if (IS_GEN8(dev))
9228 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9229 MI_SRM_LRM_GLOBAL_GTT);
9230 else
9231 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9232 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009233 intel_ring_emit(ring, DERRMR);
9234 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009235 if (IS_GEN8(dev)) {
9236 intel_ring_emit(ring, 0);
9237 intel_ring_emit(ring, MI_NOOP);
9238 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009239 }
9240
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009241 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009242 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009243 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009244 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009245
9246 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009247 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009248 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009249}
9250
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009251static int intel_default_queue_flip(struct drm_device *dev,
9252 struct drm_crtc *crtc,
9253 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009254 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009255 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009256 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009257{
9258 return -ENODEV;
9259}
9260
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009261static int intel_crtc_page_flip(struct drm_crtc *crtc,
9262 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009263 struct drm_pending_vblank_event *event,
9264 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009265{
9266 struct drm_device *dev = crtc->dev;
9267 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009268 struct drm_framebuffer *old_fb = crtc->primary->fb;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02009269 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009270 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9271 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009272 struct intel_engine_cs *ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009273 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01009274 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009275
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009276 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009277 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009278 return -EINVAL;
9279
9280 /*
9281 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9282 * Note that pitch changes could also affect these register.
9283 */
9284 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009285 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9286 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009287 return -EINVAL;
9288
Chris Wilsonf900db42014-02-20 09:26:13 +00009289 if (i915_terminally_wedged(&dev_priv->gpu_error))
9290 goto out_hang;
9291
Daniel Vetterb14c5672013-09-19 12:18:32 +02009292 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009293 if (work == NULL)
9294 return -ENOMEM;
9295
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -07009296 /* Exit PSR early in page flip */
9297 intel_edp_psr_exit(dev, true);
9298
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009299 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009300 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02009301 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009302 INIT_WORK(&work->work, intel_unpin_work_fn);
9303
Daniel Vetter87b6b102014-05-15 15:33:46 +02009304 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009305 if (ret)
9306 goto free_work;
9307
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009308 /* We borrow the event spin lock for protecting unpin_work */
9309 spin_lock_irqsave(&dev->event_lock, flags);
9310 if (intel_crtc->unpin_work) {
9311 spin_unlock_irqrestore(&dev->event_lock, flags);
9312 kfree(work);
Daniel Vetter87b6b102014-05-15 15:33:46 +02009313 drm_crtc_vblank_put(crtc);
Chris Wilson468f0b42010-05-27 13:18:13 +01009314
9315 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009316 return -EBUSY;
9317 }
9318 intel_crtc->unpin_work = work;
9319 spin_unlock_irqrestore(&dev->event_lock, flags);
9320
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009321 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9322 flush_workqueue(dev_priv->wq);
9323
Chris Wilson79158102012-05-23 11:13:58 +01009324 ret = i915_mutex_lock_interruptible(dev);
9325 if (ret)
9326 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009327
Jesse Barnes75dfca82010-02-10 15:09:44 -08009328 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00009329 drm_gem_object_reference(&work->old_fb_obj->base);
9330 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009331
Matt Roperf4510a22014-04-01 15:22:40 -07009332 crtc->primary->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01009333
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009334 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009335
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01009336 work->enable_stall_check = true;
9337
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009338 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02009339 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009340
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009341 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
9342 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(intel_crtc->pipe)) + 1;
9343
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009344 if (IS_VALLEYVIEW(dev)) {
9345 ring = &dev_priv->ring[BCS];
9346 } else if (INTEL_INFO(dev)->gen >= 7) {
9347 ring = obj->ring;
9348 if (ring == NULL || ring->id != RCS)
9349 ring = &dev_priv->ring[BCS];
9350 } else {
9351 ring = &dev_priv->ring[RCS];
9352 }
9353
9354 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009355 if (ret)
9356 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009357
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009358 work->gtt_offset =
9359 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9360
9361 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring, page_flip_flags);
9362 if (ret)
9363 goto cleanup_unpin;
9364
Chris Wilson7782de32011-07-08 12:22:41 +01009365 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03009366 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009367 mutex_unlock(&dev->struct_mutex);
9368
Jesse Barnese5510fa2010-07-01 16:48:37 -07009369 trace_i915_flip_request(intel_crtc->plane, obj);
9370
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009371 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01009372
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009373cleanup_unpin:
9374 intel_unpin_fb_obj(obj);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009375cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009376 atomic_dec(&intel_crtc->unpin_work_count);
Matt Roperf4510a22014-04-01 15:22:40 -07009377 crtc->primary->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00009378 drm_gem_object_unreference(&work->old_fb_obj->base);
9379 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01009380 mutex_unlock(&dev->struct_mutex);
9381
Chris Wilson79158102012-05-23 11:13:58 +01009382cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01009383 spin_lock_irqsave(&dev->event_lock, flags);
9384 intel_crtc->unpin_work = NULL;
9385 spin_unlock_irqrestore(&dev->event_lock, flags);
9386
Daniel Vetter87b6b102014-05-15 15:33:46 +02009387 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009388free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01009389 kfree(work);
9390
Chris Wilsonf900db42014-02-20 09:26:13 +00009391 if (ret == -EIO) {
9392out_hang:
9393 intel_crtc_wait_for_pending_flips(crtc);
9394 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9395 if (ret == 0 && event)
9396 drm_send_vblank_event(dev, intel_crtc->pipe, event);
9397 }
Chris Wilson96b099f2010-06-07 14:03:04 +01009398 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009399}
9400
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009401static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009402 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9403 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009404};
9405
Daniel Vetter9a935852012-07-05 22:34:27 +02009406/**
9407 * intel_modeset_update_staged_output_state
9408 *
9409 * Updates the staged output configuration state, e.g. after we've read out the
9410 * current hw state.
9411 */
9412static void intel_modeset_update_staged_output_state(struct drm_device *dev)
9413{
Ville Syrjälä76688512014-01-10 11:28:06 +02009414 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009415 struct intel_encoder *encoder;
9416 struct intel_connector *connector;
9417
9418 list_for_each_entry(connector, &dev->mode_config.connector_list,
9419 base.head) {
9420 connector->new_encoder =
9421 to_intel_encoder(connector->base.encoder);
9422 }
9423
9424 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9425 base.head) {
9426 encoder->new_crtc =
9427 to_intel_crtc(encoder->base.crtc);
9428 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009429
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009430 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009431 crtc->new_enabled = crtc->base.enabled;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009432
9433 if (crtc->new_enabled)
9434 crtc->new_config = &crtc->config;
9435 else
9436 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +02009437 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009438}
9439
9440/**
9441 * intel_modeset_commit_output_state
9442 *
9443 * This function copies the stage display pipe configuration to the real one.
9444 */
9445static void intel_modeset_commit_output_state(struct drm_device *dev)
9446{
Ville Syrjälä76688512014-01-10 11:28:06 +02009447 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009448 struct intel_encoder *encoder;
9449 struct intel_connector *connector;
9450
9451 list_for_each_entry(connector, &dev->mode_config.connector_list,
9452 base.head) {
9453 connector->base.encoder = &connector->new_encoder->base;
9454 }
9455
9456 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9457 base.head) {
9458 encoder->base.crtc = &encoder->new_crtc->base;
9459 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009460
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009461 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009462 crtc->base.enabled = crtc->new_enabled;
9463 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009464}
9465
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009466static void
Robin Schroereba905b2014-05-18 02:24:50 +02009467connected_sink_compute_bpp(struct intel_connector *connector,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009468 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009469{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009470 int bpp = pipe_config->pipe_bpp;
9471
9472 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9473 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03009474 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009475
9476 /* Don't use an invalid EDID bpc value */
9477 if (connector->base.display_info.bpc &&
9478 connector->base.display_info.bpc * 3 < bpp) {
9479 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9480 bpp, connector->base.display_info.bpc*3);
9481 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9482 }
9483
9484 /* Clamp bpp to 8 on screens without EDID 1.4 */
9485 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9486 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9487 bpp);
9488 pipe_config->pipe_bpp = 24;
9489 }
9490}
9491
9492static int
9493compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9494 struct drm_framebuffer *fb,
9495 struct intel_crtc_config *pipe_config)
9496{
9497 struct drm_device *dev = crtc->base.dev;
9498 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009499 int bpp;
9500
Daniel Vetterd42264b2013-03-28 16:38:08 +01009501 switch (fb->pixel_format) {
9502 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009503 bpp = 8*3; /* since we go through a colormap */
9504 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009505 case DRM_FORMAT_XRGB1555:
9506 case DRM_FORMAT_ARGB1555:
9507 /* checked in intel_framebuffer_init already */
9508 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9509 return -EINVAL;
9510 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009511 bpp = 6*3; /* min is 18bpp */
9512 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009513 case DRM_FORMAT_XBGR8888:
9514 case DRM_FORMAT_ABGR8888:
9515 /* checked in intel_framebuffer_init already */
9516 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9517 return -EINVAL;
9518 case DRM_FORMAT_XRGB8888:
9519 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009520 bpp = 8*3;
9521 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009522 case DRM_FORMAT_XRGB2101010:
9523 case DRM_FORMAT_ARGB2101010:
9524 case DRM_FORMAT_XBGR2101010:
9525 case DRM_FORMAT_ABGR2101010:
9526 /* checked in intel_framebuffer_init already */
9527 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01009528 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009529 bpp = 10*3;
9530 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01009531 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009532 default:
9533 DRM_DEBUG_KMS("unsupported depth\n");
9534 return -EINVAL;
9535 }
9536
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009537 pipe_config->pipe_bpp = bpp;
9538
9539 /* Clamp display bpp to EDID value */
9540 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009541 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02009542 if (!connector->new_encoder ||
9543 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009544 continue;
9545
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009546 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009547 }
9548
9549 return bpp;
9550}
9551
Daniel Vetter644db712013-09-19 14:53:58 +02009552static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9553{
9554 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9555 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01009556 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02009557 mode->crtc_hdisplay, mode->crtc_hsync_start,
9558 mode->crtc_hsync_end, mode->crtc_htotal,
9559 mode->crtc_vdisplay, mode->crtc_vsync_start,
9560 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9561}
9562
Daniel Vetterc0b03412013-05-28 12:05:54 +02009563static void intel_dump_pipe_config(struct intel_crtc *crtc,
9564 struct intel_crtc_config *pipe_config,
9565 const char *context)
9566{
9567 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9568 context, pipe_name(crtc->pipe));
9569
9570 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9571 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9572 pipe_config->pipe_bpp, pipe_config->dither);
9573 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9574 pipe_config->has_pch_encoder,
9575 pipe_config->fdi_lanes,
9576 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9577 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9578 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009579 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9580 pipe_config->has_dp_encoder,
9581 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9582 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9583 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009584 DRM_DEBUG_KMS("requested mode:\n");
9585 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9586 DRM_DEBUG_KMS("adjusted mode:\n");
9587 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02009588 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009589 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009590 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9591 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009592 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9593 pipe_config->gmch_pfit.control,
9594 pipe_config->gmch_pfit.pgm_ratios,
9595 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009596 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02009597 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009598 pipe_config->pch_pfit.size,
9599 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009600 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03009601 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009602}
9603
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009604static bool encoders_cloneable(const struct intel_encoder *a,
9605 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009606{
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009607 /* masks could be asymmetric, so check both ways */
9608 return a == b || (a->cloneable & (1 << b->type) &&
9609 b->cloneable & (1 << a->type));
9610}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009611
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009612static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9613 struct intel_encoder *encoder)
9614{
9615 struct drm_device *dev = crtc->base.dev;
9616 struct intel_encoder *source_encoder;
9617
9618 list_for_each_entry(source_encoder,
9619 &dev->mode_config.encoder_list, base.head) {
9620 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009621 continue;
9622
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009623 if (!encoders_cloneable(encoder, source_encoder))
9624 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009625 }
9626
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009627 return true;
9628}
9629
9630static bool check_encoder_cloning(struct intel_crtc *crtc)
9631{
9632 struct drm_device *dev = crtc->base.dev;
9633 struct intel_encoder *encoder;
9634
9635 list_for_each_entry(encoder,
9636 &dev->mode_config.encoder_list, base.head) {
9637 if (encoder->new_crtc != crtc)
9638 continue;
9639
9640 if (!check_single_encoder_cloning(crtc, encoder))
9641 return false;
9642 }
9643
9644 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009645}
9646
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009647static struct intel_crtc_config *
9648intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009649 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009650 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02009651{
9652 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02009653 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009654 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01009655 int plane_bpp, ret = -EINVAL;
9656 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02009657
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009658 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009659 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9660 return ERR_PTR(-EINVAL);
9661 }
9662
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009663 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9664 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02009665 return ERR_PTR(-ENOMEM);
9666
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009667 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9668 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009669
Daniel Vettere143a212013-07-04 12:01:15 +02009670 pipe_config->cpu_transcoder =
9671 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009672 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009673
Imre Deak2960bc92013-07-30 13:36:32 +03009674 /*
9675 * Sanitize sync polarity flags based on requested ones. If neither
9676 * positive or negative polarity is requested, treat this as meaning
9677 * negative polarity.
9678 */
9679 if (!(pipe_config->adjusted_mode.flags &
9680 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9681 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9682
9683 if (!(pipe_config->adjusted_mode.flags &
9684 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9685 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9686
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009687 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9688 * plane pixel format and any sink constraints into account. Returns the
9689 * source plane bpp so that dithering can be selected on mismatches
9690 * after encoders and crtc also have had their say. */
9691 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9692 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009693 if (plane_bpp < 0)
9694 goto fail;
9695
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03009696 /*
9697 * Determine the real pipe dimensions. Note that stereo modes can
9698 * increase the actual pipe size due to the frame doubling and
9699 * insertion of additional space for blanks between the frame. This
9700 * is stored in the crtc timings. We use the requested mode to do this
9701 * computation to clearly distinguish it from the adjusted mode, which
9702 * can be changed by the connectors in the below retry loop.
9703 */
9704 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9705 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9706 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9707
Daniel Vettere29c22c2013-02-21 00:00:16 +01009708encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02009709 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02009710 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02009711 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009712
Daniel Vetter135c81b2013-07-21 21:37:09 +02009713 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01009714 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02009715
Daniel Vetter7758a112012-07-08 19:40:39 +02009716 /* Pass our mode to the connectors and the CRTC to give them a chance to
9717 * adjust it according to limitations or connector properties, and also
9718 * a chance to reject the mode entirely.
9719 */
9720 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9721 base.head) {
9722
9723 if (&encoder->new_crtc->base != crtc)
9724 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01009725
Daniel Vetterefea6e82013-07-21 21:36:59 +02009726 if (!(encoder->compute_config(encoder, pipe_config))) {
9727 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02009728 goto fail;
9729 }
9730 }
9731
Daniel Vetterff9a6752013-06-01 17:16:21 +02009732 /* Set default port clock if not overwritten by the encoder. Needs to be
9733 * done afterwards in case the encoder adjusts the mode. */
9734 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01009735 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9736 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009737
Daniel Vettera43f6e02013-06-07 23:10:32 +02009738 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009739 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02009740 DRM_DEBUG_KMS("CRTC fixup failed\n");
9741 goto fail;
9742 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01009743
9744 if (ret == RETRY) {
9745 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9746 ret = -EINVAL;
9747 goto fail;
9748 }
9749
9750 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9751 retry = false;
9752 goto encoder_retry;
9753 }
9754
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009755 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9756 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9757 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9758
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009759 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02009760fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009761 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009762 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02009763}
9764
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009765/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9766 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9767static void
9768intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9769 unsigned *prepare_pipes, unsigned *disable_pipes)
9770{
9771 struct intel_crtc *intel_crtc;
9772 struct drm_device *dev = crtc->dev;
9773 struct intel_encoder *encoder;
9774 struct intel_connector *connector;
9775 struct drm_crtc *tmp_crtc;
9776
9777 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9778
9779 /* Check which crtcs have changed outputs connected to them, these need
9780 * to be part of the prepare_pipes mask. We don't (yet) support global
9781 * modeset across multiple crtcs, so modeset_pipes will only have one
9782 * bit set at most. */
9783 list_for_each_entry(connector, &dev->mode_config.connector_list,
9784 base.head) {
9785 if (connector->base.encoder == &connector->new_encoder->base)
9786 continue;
9787
9788 if (connector->base.encoder) {
9789 tmp_crtc = connector->base.encoder->crtc;
9790
9791 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9792 }
9793
9794 if (connector->new_encoder)
9795 *prepare_pipes |=
9796 1 << connector->new_encoder->new_crtc->pipe;
9797 }
9798
9799 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9800 base.head) {
9801 if (encoder->base.crtc == &encoder->new_crtc->base)
9802 continue;
9803
9804 if (encoder->base.crtc) {
9805 tmp_crtc = encoder->base.crtc;
9806
9807 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9808 }
9809
9810 if (encoder->new_crtc)
9811 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9812 }
9813
Ville Syrjälä76688512014-01-10 11:28:06 +02009814 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009815 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009816 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009817 continue;
9818
Ville Syrjälä76688512014-01-10 11:28:06 +02009819 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009820 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +02009821 else
9822 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009823 }
9824
9825
9826 /* set_mode is also used to update properties on life display pipes. */
9827 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +02009828 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009829 *prepare_pipes |= 1 << intel_crtc->pipe;
9830
Daniel Vetterb6c51642013-04-12 18:48:43 +02009831 /*
9832 * For simplicity do a full modeset on any pipe where the output routing
9833 * changed. We could be more clever, but that would require us to be
9834 * more careful with calling the relevant encoder->mode_set functions.
9835 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009836 if (*prepare_pipes)
9837 *modeset_pipes = *prepare_pipes;
9838
9839 /* ... and mask these out. */
9840 *modeset_pipes &= ~(*disable_pipes);
9841 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02009842
9843 /*
9844 * HACK: We don't (yet) fully support global modesets. intel_set_config
9845 * obies this rule, but the modeset restore mode of
9846 * intel_modeset_setup_hw_state does not.
9847 */
9848 *modeset_pipes &= 1 << intel_crtc->pipe;
9849 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02009850
9851 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9852 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009853}
9854
Daniel Vetterea9d7582012-07-10 10:42:52 +02009855static bool intel_crtc_in_use(struct drm_crtc *crtc)
9856{
9857 struct drm_encoder *encoder;
9858 struct drm_device *dev = crtc->dev;
9859
9860 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9861 if (encoder->crtc == crtc)
9862 return true;
9863
9864 return false;
9865}
9866
9867static void
9868intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9869{
9870 struct intel_encoder *intel_encoder;
9871 struct intel_crtc *intel_crtc;
9872 struct drm_connector *connector;
9873
9874 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9875 base.head) {
9876 if (!intel_encoder->base.crtc)
9877 continue;
9878
9879 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9880
9881 if (prepare_pipes & (1 << intel_crtc->pipe))
9882 intel_encoder->connectors_active = false;
9883 }
9884
9885 intel_modeset_commit_output_state(dev);
9886
Ville Syrjälä76688512014-01-10 11:28:06 +02009887 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009888 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009889 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009890 WARN_ON(intel_crtc->new_config &&
9891 intel_crtc->new_config != &intel_crtc->config);
9892 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009893 }
9894
9895 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9896 if (!connector->encoder || !connector->encoder->crtc)
9897 continue;
9898
9899 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9900
9901 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02009902 struct drm_property *dpms_property =
9903 dev->mode_config.dpms_property;
9904
Daniel Vetterea9d7582012-07-10 10:42:52 +02009905 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05009906 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02009907 dpms_property,
9908 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009909
9910 intel_encoder = to_intel_encoder(connector->encoder);
9911 intel_encoder->connectors_active = true;
9912 }
9913 }
9914
9915}
9916
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009917static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009918{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009919 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009920
9921 if (clock1 == clock2)
9922 return true;
9923
9924 if (!clock1 || !clock2)
9925 return false;
9926
9927 diff = abs(clock1 - clock2);
9928
9929 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9930 return true;
9931
9932 return false;
9933}
9934
Daniel Vetter25c5b262012-07-08 22:08:04 +02009935#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9936 list_for_each_entry((intel_crtc), \
9937 &(dev)->mode_config.crtc_list, \
9938 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02009939 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02009940
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009941static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009942intel_pipe_config_compare(struct drm_device *dev,
9943 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009944 struct intel_crtc_config *pipe_config)
9945{
Daniel Vetter66e985c2013-06-05 13:34:20 +02009946#define PIPE_CONF_CHECK_X(name) \
9947 if (current_config->name != pipe_config->name) { \
9948 DRM_ERROR("mismatch in " #name " " \
9949 "(expected 0x%08x, found 0x%08x)\n", \
9950 current_config->name, \
9951 pipe_config->name); \
9952 return false; \
9953 }
9954
Daniel Vetter08a24032013-04-19 11:25:34 +02009955#define PIPE_CONF_CHECK_I(name) \
9956 if (current_config->name != pipe_config->name) { \
9957 DRM_ERROR("mismatch in " #name " " \
9958 "(expected %i, found %i)\n", \
9959 current_config->name, \
9960 pipe_config->name); \
9961 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01009962 }
9963
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009964#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9965 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07009966 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009967 "(expected %i, found %i)\n", \
9968 current_config->name & (mask), \
9969 pipe_config->name & (mask)); \
9970 return false; \
9971 }
9972
Ville Syrjälä5e550652013-09-06 23:29:07 +03009973#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9974 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9975 DRM_ERROR("mismatch in " #name " " \
9976 "(expected %i, found %i)\n", \
9977 current_config->name, \
9978 pipe_config->name); \
9979 return false; \
9980 }
9981
Daniel Vetterbb760062013-06-06 14:55:52 +02009982#define PIPE_CONF_QUIRK(quirk) \
9983 ((current_config->quirks | pipe_config->quirks) & (quirk))
9984
Daniel Vettereccb1402013-05-22 00:50:22 +02009985 PIPE_CONF_CHECK_I(cpu_transcoder);
9986
Daniel Vetter08a24032013-04-19 11:25:34 +02009987 PIPE_CONF_CHECK_I(has_pch_encoder);
9988 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02009989 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9990 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9991 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9992 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9993 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02009994
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009995 PIPE_CONF_CHECK_I(has_dp_encoder);
9996 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9997 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9998 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9999 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10000 PIPE_CONF_CHECK_I(dp_m_n.tu);
10001
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010002 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10003 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10004 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10005 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10006 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10007 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10008
10009 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10010 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10011 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10012 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10013 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10014 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10015
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010016 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020010017 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020010018 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10019 IS_VALLEYVIEW(dev))
10020 PIPE_CONF_CHECK_I(limited_color_range);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010021
Daniel Vetter9ed109a2014-04-24 23:54:52 +020010022 PIPE_CONF_CHECK_I(has_audio);
10023
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010024 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10025 DRM_MODE_FLAG_INTERLACE);
10026
Daniel Vetterbb760062013-06-06 14:55:52 +020010027 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10028 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10029 DRM_MODE_FLAG_PHSYNC);
10030 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10031 DRM_MODE_FLAG_NHSYNC);
10032 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10033 DRM_MODE_FLAG_PVSYNC);
10034 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10035 DRM_MODE_FLAG_NVSYNC);
10036 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010037
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010038 PIPE_CONF_CHECK_I(pipe_src_w);
10039 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010040
Daniel Vetter99535992014-04-13 12:00:33 +020010041 /*
10042 * FIXME: BIOS likes to set up a cloned config with lvds+external
10043 * screen. Since we don't yet re-compute the pipe config when moving
10044 * just the lvds port away to another pipe the sw tracking won't match.
10045 *
10046 * Proper atomic modesets with recomputed global state will fix this.
10047 * Until then just don't check gmch state for inherited modes.
10048 */
10049 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10050 PIPE_CONF_CHECK_I(gmch_pfit.control);
10051 /* pfit ratios are autocomputed by the hw on gen4+ */
10052 if (INTEL_INFO(dev)->gen < 4)
10053 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10054 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10055 }
10056
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010057 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10058 if (current_config->pch_pfit.enabled) {
10059 PIPE_CONF_CHECK_I(pch_pfit.pos);
10060 PIPE_CONF_CHECK_I(pch_pfit.size);
10061 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010062
Jesse Barnese59150d2014-01-07 13:30:45 -080010063 /* BDW+ don't expose a synchronous way to read the state */
10064 if (IS_HASWELL(dev))
10065 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010066
Ville Syrjälä282740f2013-09-04 18:30:03 +030010067 PIPE_CONF_CHECK_I(double_wide);
10068
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010069 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010070 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020010071 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010072 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10073 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010074
Ville Syrjälä42571ae2013-09-06 23:29:00 +030010075 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10076 PIPE_CONF_CHECK_I(pipe_bpp);
10077
Jesse Barnesa9a7e982014-01-20 14:18:04 -080010078 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10079 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030010080
Daniel Vetter66e985c2013-06-05 13:34:20 +020010081#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020010082#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010083#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030010084#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020010085#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010086
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010087 return true;
10088}
10089
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010090static void
10091check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010092{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010093 struct intel_connector *connector;
10094
10095 list_for_each_entry(connector, &dev->mode_config.connector_list,
10096 base.head) {
10097 /* This also checks the encoder/connector hw state with the
10098 * ->get_hw_state callbacks. */
10099 intel_connector_check_state(connector);
10100
10101 WARN(&connector->new_encoder->base != connector->base.encoder,
10102 "connector's staged encoder doesn't match current encoder\n");
10103 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010104}
10105
10106static void
10107check_encoder_state(struct drm_device *dev)
10108{
10109 struct intel_encoder *encoder;
10110 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010111
10112 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10113 base.head) {
10114 bool enabled = false;
10115 bool active = false;
10116 enum pipe pipe, tracked_pipe;
10117
10118 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10119 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030010120 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010121
10122 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10123 "encoder's stage crtc doesn't match current crtc\n");
10124 WARN(encoder->connectors_active && !encoder->base.crtc,
10125 "encoder's active_connectors set, but no crtc\n");
10126
10127 list_for_each_entry(connector, &dev->mode_config.connector_list,
10128 base.head) {
10129 if (connector->base.encoder != &encoder->base)
10130 continue;
10131 enabled = true;
10132 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10133 active = true;
10134 }
10135 WARN(!!encoder->base.crtc != enabled,
10136 "encoder's enabled state mismatch "
10137 "(expected %i, found %i)\n",
10138 !!encoder->base.crtc, enabled);
10139 WARN(active && !encoder->base.crtc,
10140 "active encoder with no crtc\n");
10141
10142 WARN(encoder->connectors_active != active,
10143 "encoder's computed active state doesn't match tracked active state "
10144 "(expected %i, found %i)\n", active, encoder->connectors_active);
10145
10146 active = encoder->get_hw_state(encoder, &pipe);
10147 WARN(active != encoder->connectors_active,
10148 "encoder's hw state doesn't match sw tracking "
10149 "(expected %i, found %i)\n",
10150 encoder->connectors_active, active);
10151
10152 if (!encoder->base.crtc)
10153 continue;
10154
10155 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10156 WARN(active && pipe != tracked_pipe,
10157 "active encoder's pipe doesn't match"
10158 "(expected %i, found %i)\n",
10159 tracked_pipe, pipe);
10160
10161 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010162}
10163
10164static void
10165check_crtc_state(struct drm_device *dev)
10166{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010167 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010168 struct intel_crtc *crtc;
10169 struct intel_encoder *encoder;
10170 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010171
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010172 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010173 bool enabled = false;
10174 bool active = false;
10175
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010176 memset(&pipe_config, 0, sizeof(pipe_config));
10177
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010178 DRM_DEBUG_KMS("[CRTC:%d]\n",
10179 crtc->base.base.id);
10180
10181 WARN(crtc->active && !crtc->base.enabled,
10182 "active crtc, but not enabled in sw tracking\n");
10183
10184 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10185 base.head) {
10186 if (encoder->base.crtc != &crtc->base)
10187 continue;
10188 enabled = true;
10189 if (encoder->connectors_active)
10190 active = true;
10191 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010192
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010193 WARN(active != crtc->active,
10194 "crtc's computed active state doesn't match tracked active state "
10195 "(expected %i, found %i)\n", active, crtc->active);
10196 WARN(enabled != crtc->base.enabled,
10197 "crtc's computed enabled state doesn't match tracked enabled state "
10198 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10199
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010200 active = dev_priv->display.get_pipe_config(crtc,
10201 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020010202
10203 /* hw state is inconsistent with the pipe A quirk */
10204 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
10205 active = crtc->active;
10206
Daniel Vetter6c49f242013-06-06 12:45:25 +020010207 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10208 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030010209 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020010210 if (encoder->base.crtc != &crtc->base)
10211 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010010212 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020010213 encoder->get_config(encoder, &pipe_config);
10214 }
10215
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010216 WARN(crtc->active != active,
10217 "crtc active state doesn't match with hw state "
10218 "(expected %i, found %i)\n", crtc->active, active);
10219
Daniel Vetterc0b03412013-05-28 12:05:54 +020010220 if (active &&
10221 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10222 WARN(1, "pipe state doesn't match!\n");
10223 intel_dump_pipe_config(crtc, &pipe_config,
10224 "[hw state]");
10225 intel_dump_pipe_config(crtc, &crtc->config,
10226 "[sw state]");
10227 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010228 }
10229}
10230
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010231static void
10232check_shared_dpll_state(struct drm_device *dev)
10233{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010234 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010235 struct intel_crtc *crtc;
10236 struct intel_dpll_hw_state dpll_hw_state;
10237 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020010238
10239 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10240 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10241 int enabled_crtcs = 0, active_crtcs = 0;
10242 bool active;
10243
10244 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10245
10246 DRM_DEBUG_KMS("%s\n", pll->name);
10247
10248 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10249
10250 WARN(pll->active > pll->refcount,
10251 "more active pll users than references: %i vs %i\n",
10252 pll->active, pll->refcount);
10253 WARN(pll->active && !pll->on,
10254 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +020010255 WARN(pll->on && !pll->active,
10256 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010257 WARN(pll->on != active,
10258 "pll on state mismatch (expected %i, found %i)\n",
10259 pll->on, active);
10260
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010261 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020010262 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10263 enabled_crtcs++;
10264 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10265 active_crtcs++;
10266 }
10267 WARN(pll->active != active_crtcs,
10268 "pll active crtcs mismatch (expected %i, found %i)\n",
10269 pll->active, active_crtcs);
10270 WARN(pll->refcount != enabled_crtcs,
10271 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10272 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010273
10274 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10275 sizeof(dpll_hw_state)),
10276 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010277 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010278}
10279
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010280void
10281intel_modeset_check_state(struct drm_device *dev)
10282{
10283 check_connector_state(dev);
10284 check_encoder_state(dev);
10285 check_crtc_state(dev);
10286 check_shared_dpll_state(dev);
10287}
10288
Ville Syrjälä18442d02013-09-13 16:00:08 +030010289void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10290 int dotclock)
10291{
10292 /*
10293 * FDI already provided one idea for the dotclock.
10294 * Yell if the encoder disagrees.
10295 */
Damien Lespiau241bfc32013-09-25 16:45:37 +010010296 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010297 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +010010298 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010299}
10300
Ville Syrjälä80715b22014-05-15 20:23:23 +030010301static void update_scanline_offset(struct intel_crtc *crtc)
10302{
10303 struct drm_device *dev = crtc->base.dev;
10304
10305 /*
10306 * The scanline counter increments at the leading edge of hsync.
10307 *
10308 * On most platforms it starts counting from vtotal-1 on the
10309 * first active line. That means the scanline counter value is
10310 * always one less than what we would expect. Ie. just after
10311 * start of vblank, which also occurs at start of hsync (on the
10312 * last active line), the scanline counter will read vblank_start-1.
10313 *
10314 * On gen2 the scanline counter starts counting from 1 instead
10315 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10316 * to keep the value positive), instead of adding one.
10317 *
10318 * On HSW+ the behaviour of the scanline counter depends on the output
10319 * type. For DP ports it behaves like most other platforms, but on HDMI
10320 * there's an extra 1 line difference. So we need to add two instead of
10321 * one to the value.
10322 */
10323 if (IS_GEN2(dev)) {
10324 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10325 int vtotal;
10326
10327 vtotal = mode->crtc_vtotal;
10328 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10329 vtotal /= 2;
10330
10331 crtc->scanline_offset = vtotal - 1;
10332 } else if (HAS_DDI(dev) &&
10333 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
10334 crtc->scanline_offset = 2;
10335 } else
10336 crtc->scanline_offset = 1;
10337}
10338
Daniel Vetterf30da182013-04-11 20:22:50 +020010339static int __intel_set_mode(struct drm_crtc *crtc,
10340 struct drm_display_mode *mode,
10341 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +020010342{
10343 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010344 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010345 struct drm_display_mode *saved_mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010346 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010347 struct intel_crtc *intel_crtc;
10348 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010349 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020010350
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010351 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010352 if (!saved_mode)
10353 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020010354
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010355 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +020010356 &prepare_pipes, &disable_pipes);
10357
Tim Gardner3ac18232012-12-07 07:54:26 -070010358 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010359
Daniel Vetter25c5b262012-07-08 22:08:04 +020010360 /* Hack: Because we don't (yet) support global modeset on multiple
10361 * crtcs, we don't keep track of the new mode for more than one crtc.
10362 * Hence simply check whether any bit is set in modeset_pipes in all the
10363 * pieces of code that are not yet converted to deal with mutliple crtcs
10364 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010365 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010366 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010367 if (IS_ERR(pipe_config)) {
10368 ret = PTR_ERR(pipe_config);
10369 pipe_config = NULL;
10370
Tim Gardner3ac18232012-12-07 07:54:26 -070010371 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010372 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020010373 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10374 "[modeset]");
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010375 to_intel_crtc(crtc)->new_config = pipe_config;
Daniel Vettera6778b32012-07-02 09:56:42 +020010376 }
10377
Jesse Barnes30a970c2013-11-04 13:48:12 -080010378 /*
10379 * See if the config requires any additional preparation, e.g.
10380 * to adjust global state with pipes off. We need to do this
10381 * here so we can get the modeset_pipe updated config for the new
10382 * mode set on this crtc. For other crtcs we need to use the
10383 * adjusted_mode bits in the crtc directly.
10384 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020010385 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020010386 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080010387
Ville Syrjäläc164f832013-11-05 22:34:12 +020010388 /* may have added more to prepare_pipes than we should */
10389 prepare_pipes &= ~disable_pipes;
10390 }
10391
Daniel Vetter460da9162013-03-27 00:44:51 +010010392 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10393 intel_crtc_disable(&intel_crtc->base);
10394
Daniel Vetterea9d7582012-07-10 10:42:52 +020010395 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10396 if (intel_crtc->base.enabled)
10397 dev_priv->display.crtc_disable(&intel_crtc->base);
10398 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010399
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020010400 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10401 * to set it here already despite that we pass it down the callchain.
10402 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010403 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020010404 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010405 /* mode_set/enable/disable functions rely on a correct pipe
10406 * config. */
10407 to_intel_crtc(crtc)->config = *pipe_config;
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010408 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020010409
10410 /*
10411 * Calculate and store various constants which
10412 * are later needed by vblank and swap-completion
10413 * timestamping. They are derived from true hwmode.
10414 */
10415 drm_calc_timestamping_constants(crtc,
10416 &pipe_config->adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010417 }
Daniel Vetter7758a112012-07-08 19:40:39 +020010418
Daniel Vetterea9d7582012-07-10 10:42:52 +020010419 /* Only after disabling all output pipelines that will be changed can we
10420 * update the the output configuration. */
10421 intel_modeset_update_state(dev, prepare_pipes);
10422
Daniel Vetter47fab732012-10-26 10:58:18 +020010423 if (dev_priv->display.modeset_global_resources)
10424 dev_priv->display.modeset_global_resources(dev);
10425
Daniel Vettera6778b32012-07-02 09:56:42 +020010426 /* Set up the DPLL and any encoders state that needs to adjust or depend
10427 * on the DPLL.
10428 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010429 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Daniel Vetter4c107942014-04-24 23:55:05 +020010430 struct drm_framebuffer *old_fb;
10431
10432 mutex_lock(&dev->struct_mutex);
10433 ret = intel_pin_and_fence_fb_obj(dev,
10434 to_intel_framebuffer(fb)->obj,
10435 NULL);
10436 if (ret != 0) {
10437 DRM_ERROR("pin & fence failed\n");
10438 mutex_unlock(&dev->struct_mutex);
10439 goto done;
10440 }
10441 old_fb = crtc->primary->fb;
10442 if (old_fb)
10443 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
10444 mutex_unlock(&dev->struct_mutex);
10445
10446 crtc->primary->fb = fb;
10447 crtc->x = x;
10448 crtc->y = y;
10449
Daniel Vetter4271b752014-04-24 23:55:00 +020010450 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
10451 x, y, fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010452 if (ret)
10453 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +020010454 }
10455
10456 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030010457 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10458 update_scanline_offset(intel_crtc);
10459
Daniel Vetter25c5b262012-07-08 22:08:04 +020010460 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030010461 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010462
Daniel Vettera6778b32012-07-02 09:56:42 +020010463 /* FIXME: add subpixel order */
10464done:
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010465 if (ret && crtc->enabled)
Tim Gardner3ac18232012-12-07 07:54:26 -070010466 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010467
Tim Gardner3ac18232012-12-07 07:54:26 -070010468out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010469 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -070010470 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020010471 return ret;
10472}
10473
Damien Lespiaue7457a92013-08-08 22:28:59 +010010474static int intel_set_mode(struct drm_crtc *crtc,
10475 struct drm_display_mode *mode,
10476 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020010477{
10478 int ret;
10479
10480 ret = __intel_set_mode(crtc, mode, x, y, fb);
10481
10482 if (ret == 0)
10483 intel_modeset_check_state(crtc->dev);
10484
10485 return ret;
10486}
10487
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010488void intel_crtc_restore_mode(struct drm_crtc *crtc)
10489{
Matt Roperf4510a22014-04-01 15:22:40 -070010490 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010491}
10492
Daniel Vetter25c5b262012-07-08 22:08:04 +020010493#undef for_each_intel_crtc_masked
10494
Daniel Vetterd9e55602012-07-04 22:16:09 +020010495static void intel_set_config_free(struct intel_set_config *config)
10496{
10497 if (!config)
10498 return;
10499
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010500 kfree(config->save_connector_encoders);
10501 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020010502 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020010503 kfree(config);
10504}
10505
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010506static int intel_set_config_save_state(struct drm_device *dev,
10507 struct intel_set_config *config)
10508{
Ville Syrjälä76688512014-01-10 11:28:06 +020010509 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010510 struct drm_encoder *encoder;
10511 struct drm_connector *connector;
10512 int count;
10513
Ville Syrjälä76688512014-01-10 11:28:06 +020010514 config->save_crtc_enabled =
10515 kcalloc(dev->mode_config.num_crtc,
10516 sizeof(bool), GFP_KERNEL);
10517 if (!config->save_crtc_enabled)
10518 return -ENOMEM;
10519
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010520 config->save_encoder_crtcs =
10521 kcalloc(dev->mode_config.num_encoder,
10522 sizeof(struct drm_crtc *), GFP_KERNEL);
10523 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010524 return -ENOMEM;
10525
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010526 config->save_connector_encoders =
10527 kcalloc(dev->mode_config.num_connector,
10528 sizeof(struct drm_encoder *), GFP_KERNEL);
10529 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010530 return -ENOMEM;
10531
10532 /* Copy data. Note that driver private data is not affected.
10533 * Should anything bad happen only the expected state is
10534 * restored, not the drivers personal bookkeeping.
10535 */
10536 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010537 for_each_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010538 config->save_crtc_enabled[count++] = crtc->enabled;
10539 }
10540
10541 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010542 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010543 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010544 }
10545
10546 count = 0;
10547 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010548 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010549 }
10550
10551 return 0;
10552}
10553
10554static void intel_set_config_restore_state(struct drm_device *dev,
10555 struct intel_set_config *config)
10556{
Ville Syrjälä76688512014-01-10 11:28:06 +020010557 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010558 struct intel_encoder *encoder;
10559 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010560 int count;
10561
10562 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010563 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010564 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010565
10566 if (crtc->new_enabled)
10567 crtc->new_config = &crtc->config;
10568 else
10569 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010570 }
10571
10572 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010573 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10574 encoder->new_crtc =
10575 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010576 }
10577
10578 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010579 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10580 connector->new_encoder =
10581 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010582 }
10583}
10584
Imre Deake3de42b2013-05-03 19:44:07 +020010585static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010010586is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020010587{
10588 int i;
10589
Chris Wilson2e57f472013-07-17 12:14:40 +010010590 if (set->num_connectors == 0)
10591 return false;
10592
10593 if (WARN_ON(set->connectors == NULL))
10594 return false;
10595
10596 for (i = 0; i < set->num_connectors; i++)
10597 if (set->connectors[i]->encoder &&
10598 set->connectors[i]->encoder->crtc == set->crtc &&
10599 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020010600 return true;
10601
10602 return false;
10603}
10604
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010605static void
10606intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10607 struct intel_set_config *config)
10608{
10609
10610 /* We should be able to check here if the fb has the same properties
10611 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010010612 if (is_crtc_connector_off(set)) {
10613 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070010614 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070010615 /*
10616 * If we have no fb, we can only flip as long as the crtc is
10617 * active, otherwise we need a full mode set. The crtc may
10618 * be active if we've only disabled the primary plane, or
10619 * in fastboot situations.
10620 */
Matt Roperf4510a22014-04-01 15:22:40 -070010621 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010622 struct intel_crtc *intel_crtc =
10623 to_intel_crtc(set->crtc);
10624
Matt Roper3b150f02014-05-29 08:06:53 -070010625 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010626 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10627 config->fb_changed = true;
10628 } else {
10629 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10630 config->mode_changed = true;
10631 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010632 } else if (set->fb == NULL) {
10633 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010010634 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070010635 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010636 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010637 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010638 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010639 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010640 }
10641
Daniel Vetter835c5872012-07-10 18:11:08 +020010642 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010643 config->fb_changed = true;
10644
10645 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10646 DRM_DEBUG_KMS("modes are different, full mode set\n");
10647 drm_mode_debug_printmodeline(&set->crtc->mode);
10648 drm_mode_debug_printmodeline(set->mode);
10649 config->mode_changed = true;
10650 }
Chris Wilsona1d95702013-08-13 18:48:47 +010010651
10652 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10653 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010654}
10655
Daniel Vetter2e431052012-07-04 22:42:15 +020010656static int
Daniel Vetter9a935852012-07-05 22:34:27 +020010657intel_modeset_stage_output_state(struct drm_device *dev,
10658 struct drm_mode_set *set,
10659 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020010660{
Daniel Vetter9a935852012-07-05 22:34:27 +020010661 struct intel_connector *connector;
10662 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020010663 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030010664 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020010665
Damien Lespiau9abdda72013-02-13 13:29:23 +000010666 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020010667 * of connectors. For paranoia, double-check this. */
10668 WARN_ON(!set->fb && (set->num_connectors != 0));
10669 WARN_ON(set->fb && (set->num_connectors == 0));
10670
Daniel Vetter9a935852012-07-05 22:34:27 +020010671 list_for_each_entry(connector, &dev->mode_config.connector_list,
10672 base.head) {
10673 /* Otherwise traverse passed in connector list and get encoders
10674 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010675 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010676 if (set->connectors[ro] == &connector->base) {
10677 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +020010678 break;
10679 }
10680 }
10681
Daniel Vetter9a935852012-07-05 22:34:27 +020010682 /* If we disable the crtc, disable all its connectors. Also, if
10683 * the connector is on the changing crtc but not on the new
10684 * connector list, disable it. */
10685 if ((!set->fb || ro == set->num_connectors) &&
10686 connector->base.encoder &&
10687 connector->base.encoder->crtc == set->crtc) {
10688 connector->new_encoder = NULL;
10689
10690 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
10691 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010692 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020010693 }
10694
10695
10696 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +020010697 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010698 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020010699 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010700 }
10701 /* connector->new_encoder is now updated for all connectors. */
10702
10703 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +020010704 list_for_each_entry(connector, &dev->mode_config.connector_list,
10705 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010706 struct drm_crtc *new_crtc;
10707
Daniel Vetter9a935852012-07-05 22:34:27 +020010708 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020010709 continue;
10710
Daniel Vetter9a935852012-07-05 22:34:27 +020010711 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020010712
10713 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010714 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020010715 new_crtc = set->crtc;
10716 }
10717
10718 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010010719 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
10720 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010721 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020010722 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010723 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
10724
10725 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
10726 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010727 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020010728 new_crtc->base.id);
10729 }
10730
10731 /* Check for any encoders that needs to be disabled. */
10732 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10733 base.head) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010734 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010735 list_for_each_entry(connector,
10736 &dev->mode_config.connector_list,
10737 base.head) {
10738 if (connector->new_encoder == encoder) {
10739 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010740 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020010741 }
10742 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010743
10744 if (num_connectors == 0)
10745 encoder->new_crtc = NULL;
10746 else if (num_connectors > 1)
10747 return -EINVAL;
10748
Daniel Vetter9a935852012-07-05 22:34:27 +020010749 /* Only now check for crtc changes so we don't miss encoders
10750 * that will be disabled. */
10751 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020010752 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010753 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020010754 }
10755 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010756 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010757
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010758 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010759 crtc->new_enabled = false;
10760
10761 list_for_each_entry(encoder,
10762 &dev->mode_config.encoder_list,
10763 base.head) {
10764 if (encoder->new_crtc == crtc) {
10765 crtc->new_enabled = true;
10766 break;
10767 }
10768 }
10769
10770 if (crtc->new_enabled != crtc->base.enabled) {
10771 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
10772 crtc->new_enabled ? "en" : "dis");
10773 config->mode_changed = true;
10774 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010775
10776 if (crtc->new_enabled)
10777 crtc->new_config = &crtc->config;
10778 else
10779 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010780 }
10781
Daniel Vetter2e431052012-07-04 22:42:15 +020010782 return 0;
10783}
10784
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010785static void disable_crtc_nofb(struct intel_crtc *crtc)
10786{
10787 struct drm_device *dev = crtc->base.dev;
10788 struct intel_encoder *encoder;
10789 struct intel_connector *connector;
10790
10791 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
10792 pipe_name(crtc->pipe));
10793
10794 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10795 if (connector->new_encoder &&
10796 connector->new_encoder->new_crtc == crtc)
10797 connector->new_encoder = NULL;
10798 }
10799
10800 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10801 if (encoder->new_crtc == crtc)
10802 encoder->new_crtc = NULL;
10803 }
10804
10805 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010806 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010807}
10808
Daniel Vetter2e431052012-07-04 22:42:15 +020010809static int intel_crtc_set_config(struct drm_mode_set *set)
10810{
10811 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020010812 struct drm_mode_set save_set;
10813 struct intel_set_config *config;
10814 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020010815
Daniel Vetter8d3e3752012-07-05 16:09:09 +020010816 BUG_ON(!set);
10817 BUG_ON(!set->crtc);
10818 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020010819
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010010820 /* Enforce sane interface api - has been abused by the fb helper. */
10821 BUG_ON(!set->mode && set->fb);
10822 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020010823
Daniel Vetter2e431052012-07-04 22:42:15 +020010824 if (set->fb) {
10825 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
10826 set->crtc->base.id, set->fb->base.id,
10827 (int)set->num_connectors, set->x, set->y);
10828 } else {
10829 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020010830 }
10831
10832 dev = set->crtc->dev;
10833
10834 ret = -ENOMEM;
10835 config = kzalloc(sizeof(*config), GFP_KERNEL);
10836 if (!config)
10837 goto out_config;
10838
10839 ret = intel_set_config_save_state(dev, config);
10840 if (ret)
10841 goto out_config;
10842
10843 save_set.crtc = set->crtc;
10844 save_set.mode = &set->crtc->mode;
10845 save_set.x = set->crtc->x;
10846 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070010847 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020010848
10849 /* Compute whether we need a full modeset, only an fb base update or no
10850 * change at all. In the future we might also check whether only the
10851 * mode changed, e.g. for LVDS where we only change the panel fitter in
10852 * such cases. */
10853 intel_set_config_compute_mode_changes(set, config);
10854
Daniel Vetter9a935852012-07-05 22:34:27 +020010855 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020010856 if (ret)
10857 goto fail;
10858
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010859 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010860 ret = intel_set_mode(set->crtc, set->mode,
10861 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010862 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070010863 struct drm_i915_private *dev_priv = dev->dev_private;
10864 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
10865
Ville Syrjälä4878cae2013-02-18 19:08:48 +020010866 intel_crtc_wait_for_pending_flips(set->crtc);
10867
Daniel Vetter4f660f42012-07-02 09:47:37 +020010868 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020010869 set->x, set->y, set->fb);
Matt Roper3b150f02014-05-29 08:06:53 -070010870
10871 /*
10872 * We need to make sure the primary plane is re-enabled if it
10873 * has previously been turned off.
10874 */
10875 if (!intel_crtc->primary_enabled && ret == 0) {
10876 WARN_ON(!intel_crtc->active);
10877 intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
10878 intel_crtc->pipe);
10879 }
10880
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010881 /*
10882 * In the fastboot case this may be our only check of the
10883 * state after boot. It would be better to only do it on
10884 * the first update, but we don't have a nice way of doing that
10885 * (and really, set_config isn't used much for high freq page
10886 * flipping, so increasing its cost here shouldn't be a big
10887 * deal).
10888 */
Jani Nikulad330a952014-01-21 11:24:25 +020010889 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010890 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020010891 }
10892
Chris Wilson2d05eae2013-05-03 17:36:25 +010010893 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020010894 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10895 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020010896fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010010897 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010898
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010899 /*
10900 * HACK: if the pipe was on, but we didn't have a framebuffer,
10901 * force the pipe off to avoid oopsing in the modeset code
10902 * due to fb==NULL. This should only happen during boot since
10903 * we don't yet reconstruct the FB from the hardware state.
10904 */
10905 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
10906 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
10907
Chris Wilson2d05eae2013-05-03 17:36:25 +010010908 /* Try to restore the config */
10909 if (config->mode_changed &&
10910 intel_set_mode(save_set.crtc, save_set.mode,
10911 save_set.x, save_set.y, save_set.fb))
10912 DRM_ERROR("failed to restore config after modeset failure\n");
10913 }
Daniel Vetter50f56112012-07-02 09:35:43 +020010914
Daniel Vetterd9e55602012-07-04 22:16:09 +020010915out_config:
10916 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010917 return ret;
10918}
10919
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010920static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010921 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020010922 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010923 .destroy = intel_crtc_destroy,
10924 .page_flip = intel_crtc_page_flip,
10925};
10926
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010927static void intel_cpu_pll_init(struct drm_device *dev)
10928{
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010929 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010930 intel_ddi_pll_init(dev);
10931}
10932
Daniel Vetter53589012013-06-05 13:34:16 +020010933static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10934 struct intel_shared_dpll *pll,
10935 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010936{
Daniel Vetter53589012013-06-05 13:34:16 +020010937 uint32_t val;
10938
10939 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020010940 hw_state->dpll = val;
10941 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10942 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020010943
10944 return val & DPLL_VCO_ENABLE;
10945}
10946
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010947static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10948 struct intel_shared_dpll *pll)
10949{
10950 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10951 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10952}
10953
Daniel Vettere7b903d2013-06-05 13:34:14 +020010954static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10955 struct intel_shared_dpll *pll)
10956{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010957 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020010958 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020010959
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010960 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10961
10962 /* Wait for the clocks to stabilize. */
10963 POSTING_READ(PCH_DPLL(pll->id));
10964 udelay(150);
10965
10966 /* The pixel multiplier can only be updated once the
10967 * DPLL is enabled and the clocks are stable.
10968 *
10969 * So write it again.
10970 */
10971 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10972 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010973 udelay(200);
10974}
10975
10976static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10977 struct intel_shared_dpll *pll)
10978{
10979 struct drm_device *dev = dev_priv->dev;
10980 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010981
10982 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010983 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020010984 if (intel_crtc_to_shared_dpll(crtc) == pll)
10985 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
10986 }
10987
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010988 I915_WRITE(PCH_DPLL(pll->id), 0);
10989 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010990 udelay(200);
10991}
10992
Daniel Vetter46edb022013-06-05 13:34:12 +020010993static char *ibx_pch_dpll_names[] = {
10994 "PCH DPLL A",
10995 "PCH DPLL B",
10996};
10997
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010998static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010999{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011000 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011001 int i;
11002
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011003 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011004
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011005 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020011006 dev_priv->shared_dplls[i].id = i;
11007 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011008 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011009 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11010 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020011011 dev_priv->shared_dplls[i].get_hw_state =
11012 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011013 }
11014}
11015
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011016static void intel_shared_dpll_init(struct drm_device *dev)
11017{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011018 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011019
11020 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
11021 ibx_pch_dpll_init(dev);
11022 else
11023 dev_priv->num_shared_dpll = 0;
11024
11025 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011026}
11027
Matt Roper465c1202014-05-29 08:06:54 -070011028static int
11029intel_primary_plane_disable(struct drm_plane *plane)
11030{
11031 struct drm_device *dev = plane->dev;
11032 struct drm_i915_private *dev_priv = dev->dev_private;
11033 struct intel_plane *intel_plane = to_intel_plane(plane);
11034 struct intel_crtc *intel_crtc;
11035
11036 if (!plane->fb)
11037 return 0;
11038
11039 BUG_ON(!plane->crtc);
11040
11041 intel_crtc = to_intel_crtc(plane->crtc);
11042
11043 /*
11044 * Even though we checked plane->fb above, it's still possible that
11045 * the primary plane has been implicitly disabled because the crtc
11046 * coordinates given weren't visible, or because we detected
11047 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11048 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11049 * In either case, we need to unpin the FB and let the fb pointer get
11050 * updated, but otherwise we don't need to touch the hardware.
11051 */
11052 if (!intel_crtc->primary_enabled)
11053 goto disable_unpin;
11054
11055 intel_crtc_wait_for_pending_flips(plane->crtc);
11056 intel_disable_primary_hw_plane(dev_priv, intel_plane->plane,
11057 intel_plane->pipe);
11058
11059disable_unpin:
11060 intel_unpin_fb_obj(to_intel_framebuffer(plane->fb)->obj);
11061 plane->fb = NULL;
11062
11063 return 0;
11064}
11065
11066static int
11067intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11068 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11069 unsigned int crtc_w, unsigned int crtc_h,
11070 uint32_t src_x, uint32_t src_y,
11071 uint32_t src_w, uint32_t src_h)
11072{
11073 struct drm_device *dev = crtc->dev;
11074 struct drm_i915_private *dev_priv = dev->dev_private;
11075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11076 struct intel_plane *intel_plane = to_intel_plane(plane);
11077 struct drm_rect dest = {
11078 /* integer pixels */
11079 .x1 = crtc_x,
11080 .y1 = crtc_y,
11081 .x2 = crtc_x + crtc_w,
11082 .y2 = crtc_y + crtc_h,
11083 };
11084 struct drm_rect src = {
11085 /* 16.16 fixed point */
11086 .x1 = src_x,
11087 .y1 = src_y,
11088 .x2 = src_x + src_w,
11089 .y2 = src_y + src_h,
11090 };
11091 const struct drm_rect clip = {
11092 /* integer pixels */
11093 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11094 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
11095 };
11096 bool visible;
11097 int ret;
11098
11099 ret = drm_plane_helper_check_update(plane, crtc, fb,
11100 &src, &dest, &clip,
11101 DRM_PLANE_HELPER_NO_SCALING,
11102 DRM_PLANE_HELPER_NO_SCALING,
11103 false, true, &visible);
11104
11105 if (ret)
11106 return ret;
11107
11108 /*
11109 * If the CRTC isn't enabled, we're just pinning the framebuffer,
11110 * updating the fb pointer, and returning without touching the
11111 * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
11112 * turn on the display with all planes setup as desired.
11113 */
11114 if (!crtc->enabled) {
11115 /*
11116 * If we already called setplane while the crtc was disabled,
11117 * we may have an fb pinned; unpin it.
11118 */
11119 if (plane->fb)
11120 intel_unpin_fb_obj(to_intel_framebuffer(plane->fb)->obj);
11121
11122 /* Pin and return without programming hardware */
11123 return intel_pin_and_fence_fb_obj(dev,
11124 to_intel_framebuffer(fb)->obj,
11125 NULL);
11126 }
11127
11128 intel_crtc_wait_for_pending_flips(crtc);
11129
11130 /*
11131 * If clipping results in a non-visible primary plane, we'll disable
11132 * the primary plane. Note that this is a bit different than what
11133 * happens if userspace explicitly disables the plane by passing fb=0
11134 * because plane->fb still gets set and pinned.
11135 */
11136 if (!visible) {
11137 /*
11138 * Try to pin the new fb first so that we can bail out if we
11139 * fail.
11140 */
11141 if (plane->fb != fb) {
11142 ret = intel_pin_and_fence_fb_obj(dev,
11143 to_intel_framebuffer(fb)->obj,
11144 NULL);
11145 if (ret)
11146 return ret;
11147 }
11148
11149 if (intel_crtc->primary_enabled)
11150 intel_disable_primary_hw_plane(dev_priv,
11151 intel_plane->plane,
11152 intel_plane->pipe);
11153
11154
11155 if (plane->fb != fb)
11156 if (plane->fb)
11157 intel_unpin_fb_obj(to_intel_framebuffer(plane->fb)->obj);
11158
11159 return 0;
11160 }
11161
11162 ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
11163 if (ret)
11164 return ret;
11165
11166 if (!intel_crtc->primary_enabled)
11167 intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
11168 intel_crtc->pipe);
11169
11170 return 0;
11171}
11172
Matt Roper3d7d6512014-06-10 08:28:13 -070011173/* Common destruction function for both primary and cursor planes */
11174static void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070011175{
11176 struct intel_plane *intel_plane = to_intel_plane(plane);
11177 drm_plane_cleanup(plane);
11178 kfree(intel_plane);
11179}
11180
11181static const struct drm_plane_funcs intel_primary_plane_funcs = {
11182 .update_plane = intel_primary_plane_setplane,
11183 .disable_plane = intel_primary_plane_disable,
Matt Roper3d7d6512014-06-10 08:28:13 -070011184 .destroy = intel_plane_destroy,
Matt Roper465c1202014-05-29 08:06:54 -070011185};
11186
11187static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11188 int pipe)
11189{
11190 struct intel_plane *primary;
11191 const uint32_t *intel_primary_formats;
11192 int num_formats;
11193
11194 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11195 if (primary == NULL)
11196 return NULL;
11197
11198 primary->can_scale = false;
11199 primary->max_downscale = 1;
11200 primary->pipe = pipe;
11201 primary->plane = pipe;
11202 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11203 primary->plane = !pipe;
11204
11205 if (INTEL_INFO(dev)->gen <= 3) {
11206 intel_primary_formats = intel_primary_formats_gen2;
11207 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11208 } else {
11209 intel_primary_formats = intel_primary_formats_gen4;
11210 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11211 }
11212
11213 drm_universal_plane_init(dev, &primary->base, 0,
11214 &intel_primary_plane_funcs,
11215 intel_primary_formats, num_formats,
11216 DRM_PLANE_TYPE_PRIMARY);
11217 return &primary->base;
11218}
11219
Matt Roper3d7d6512014-06-10 08:28:13 -070011220static int
11221intel_cursor_plane_disable(struct drm_plane *plane)
11222{
11223 if (!plane->fb)
11224 return 0;
11225
11226 BUG_ON(!plane->crtc);
11227
11228 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11229}
11230
11231static int
11232intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
11233 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11234 unsigned int crtc_w, unsigned int crtc_h,
11235 uint32_t src_x, uint32_t src_y,
11236 uint32_t src_w, uint32_t src_h)
11237{
11238 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11239 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11240 struct drm_i915_gem_object *obj = intel_fb->obj;
11241 struct drm_rect dest = {
11242 /* integer pixels */
11243 .x1 = crtc_x,
11244 .y1 = crtc_y,
11245 .x2 = crtc_x + crtc_w,
11246 .y2 = crtc_y + crtc_h,
11247 };
11248 struct drm_rect src = {
11249 /* 16.16 fixed point */
11250 .x1 = src_x,
11251 .y1 = src_y,
11252 .x2 = src_x + src_w,
11253 .y2 = src_y + src_h,
11254 };
11255 const struct drm_rect clip = {
11256 /* integer pixels */
11257 .x2 = intel_crtc->config.pipe_src_w,
11258 .y2 = intel_crtc->config.pipe_src_h,
11259 };
11260 bool visible;
11261 int ret;
11262
11263 ret = drm_plane_helper_check_update(plane, crtc, fb,
11264 &src, &dest, &clip,
11265 DRM_PLANE_HELPER_NO_SCALING,
11266 DRM_PLANE_HELPER_NO_SCALING,
11267 true, true, &visible);
11268 if (ret)
11269 return ret;
11270
11271 crtc->cursor_x = crtc_x;
11272 crtc->cursor_y = crtc_y;
11273 if (fb != crtc->cursor->fb) {
11274 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
11275 } else {
11276 intel_crtc_update_cursor(crtc, visible);
11277 return 0;
11278 }
11279}
11280static const struct drm_plane_funcs intel_cursor_plane_funcs = {
11281 .update_plane = intel_cursor_plane_update,
11282 .disable_plane = intel_cursor_plane_disable,
11283 .destroy = intel_plane_destroy,
11284};
11285
11286static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
11287 int pipe)
11288{
11289 struct intel_plane *cursor;
11290
11291 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
11292 if (cursor == NULL)
11293 return NULL;
11294
11295 cursor->can_scale = false;
11296 cursor->max_downscale = 1;
11297 cursor->pipe = pipe;
11298 cursor->plane = pipe;
11299
11300 drm_universal_plane_init(dev, &cursor->base, 0,
11301 &intel_cursor_plane_funcs,
11302 intel_cursor_formats,
11303 ARRAY_SIZE(intel_cursor_formats),
11304 DRM_PLANE_TYPE_CURSOR);
11305 return &cursor->base;
11306}
11307
Hannes Ederb358d0a2008-12-18 21:18:47 +010011308static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080011309{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011310 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080011311 struct intel_crtc *intel_crtc;
Matt Roper3d7d6512014-06-10 08:28:13 -070011312 struct drm_plane *primary = NULL;
11313 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070011314 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080011315
Daniel Vetter955382f2013-09-19 14:05:45 +020011316 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080011317 if (intel_crtc == NULL)
11318 return;
11319
Matt Roper465c1202014-05-29 08:06:54 -070011320 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070011321 if (!primary)
11322 goto fail;
11323
11324 cursor = intel_cursor_plane_create(dev, pipe);
11325 if (!cursor)
11326 goto fail;
11327
Matt Roper465c1202014-05-29 08:06:54 -070011328 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070011329 cursor, &intel_crtc_funcs);
11330 if (ret)
11331 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011332
11333 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080011334 for (i = 0; i < 256; i++) {
11335 intel_crtc->lut_r[i] = i;
11336 intel_crtc->lut_g[i] = i;
11337 intel_crtc->lut_b[i] = i;
11338 }
11339
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020011340 /*
11341 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020011342 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020011343 */
Jesse Barnes80824002009-09-10 15:28:06 -070011344 intel_crtc->pipe = pipe;
11345 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010011346 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080011347 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010011348 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070011349 }
11350
Chris Wilson4b0e3332014-05-30 16:35:26 +030011351 intel_crtc->cursor_base = ~0;
11352 intel_crtc->cursor_cntl = ~0;
11353
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030011354 init_waitqueue_head(&intel_crtc->vbl_wait);
11355
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080011356 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
11357 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
11358 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
11359 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
11360
Jesse Barnes79e53942008-11-07 14:24:08 -080011361 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020011362
11363 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070011364 return;
11365
11366fail:
11367 if (primary)
11368 drm_plane_cleanup(primary);
11369 if (cursor)
11370 drm_plane_cleanup(cursor);
11371 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080011372}
11373
Jesse Barnes752aa882013-10-31 18:55:49 +020011374enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
11375{
11376 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011377 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020011378
Rob Clark51fd3712013-11-19 12:10:12 -050011379 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020011380
11381 if (!encoder)
11382 return INVALID_PIPE;
11383
11384 return to_intel_crtc(encoder->crtc)->pipe;
11385}
11386
Carl Worth08d7b3d2009-04-29 14:43:54 -070011387int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000011388 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070011389{
Carl Worth08d7b3d2009-04-29 14:43:54 -070011390 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020011391 struct drm_mode_object *drmmode_obj;
11392 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011393
Daniel Vetter1cff8f62012-04-24 09:55:08 +020011394 if (!drm_core_check_feature(dev, DRIVER_MODESET))
11395 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011396
Daniel Vetterc05422d2009-08-11 16:05:30 +020011397 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
11398 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070011399
Daniel Vetterc05422d2009-08-11 16:05:30 +020011400 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070011401 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030011402 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011403 }
11404
Daniel Vetterc05422d2009-08-11 16:05:30 +020011405 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
11406 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011407
Daniel Vetterc05422d2009-08-11 16:05:30 +020011408 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011409}
11410
Daniel Vetter66a92782012-07-12 20:08:18 +020011411static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080011412{
Daniel Vetter66a92782012-07-12 20:08:18 +020011413 struct drm_device *dev = encoder->base.dev;
11414 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080011415 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080011416 int entry = 0;
11417
Daniel Vetter66a92782012-07-12 20:08:18 +020011418 list_for_each_entry(source_encoder,
11419 &dev->mode_config.encoder_list, base.head) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011420 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020011421 index_mask |= (1 << entry);
11422
Jesse Barnes79e53942008-11-07 14:24:08 -080011423 entry++;
11424 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010011425
Jesse Barnes79e53942008-11-07 14:24:08 -080011426 return index_mask;
11427}
11428
Chris Wilson4d302442010-12-14 19:21:29 +000011429static bool has_edp_a(struct drm_device *dev)
11430{
11431 struct drm_i915_private *dev_priv = dev->dev_private;
11432
11433 if (!IS_MOBILE(dev))
11434 return false;
11435
11436 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
11437 return false;
11438
Damien Lespiaue3589902014-02-07 19:12:50 +000011439 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000011440 return false;
11441
11442 return true;
11443}
11444
Damien Lespiauba0fbca2014-01-08 14:18:23 +000011445const char *intel_output_name(int output)
11446{
11447 static const char *names[] = {
11448 [INTEL_OUTPUT_UNUSED] = "Unused",
11449 [INTEL_OUTPUT_ANALOG] = "Analog",
11450 [INTEL_OUTPUT_DVO] = "DVO",
11451 [INTEL_OUTPUT_SDVO] = "SDVO",
11452 [INTEL_OUTPUT_LVDS] = "LVDS",
11453 [INTEL_OUTPUT_TVOUT] = "TV",
11454 [INTEL_OUTPUT_HDMI] = "HDMI",
11455 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
11456 [INTEL_OUTPUT_EDP] = "eDP",
11457 [INTEL_OUTPUT_DSI] = "DSI",
11458 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
11459 };
11460
11461 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
11462 return "Invalid";
11463
11464 return names[output];
11465}
11466
Jesse Barnes79e53942008-11-07 14:24:08 -080011467static void intel_setup_outputs(struct drm_device *dev)
11468{
Eric Anholt725e30a2009-01-22 13:01:02 -080011469 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010011470 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011471 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080011472
Daniel Vetterc9093352013-06-06 22:22:47 +020011473 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011474
Jesse Barnes27da3bd2014-04-04 16:12:07 -070011475 if (!IS_ULT(dev) && !IS_CHERRYVIEW(dev) && dev_priv->vbt.int_crt_support)
Paulo Zanoni79935fc2012-11-20 13:27:40 -020011476 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011477
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011478 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030011479 int found;
11480
11481 /* Haswell uses DDI functions to detect digital outputs */
11482 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
11483 /* DDI A only supports eDP */
11484 if (found)
11485 intel_ddi_init(dev, PORT_A);
11486
11487 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
11488 * register */
11489 found = I915_READ(SFUSE_STRAP);
11490
11491 if (found & SFUSE_STRAP_DDIB_DETECTED)
11492 intel_ddi_init(dev, PORT_B);
11493 if (found & SFUSE_STRAP_DDIC_DETECTED)
11494 intel_ddi_init(dev, PORT_C);
11495 if (found & SFUSE_STRAP_DDID_DETECTED)
11496 intel_ddi_init(dev, PORT_D);
11497 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011498 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020011499 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020011500
11501 if (has_edp_a(dev))
11502 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011503
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011504 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080011505 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010011506 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011507 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011508 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011509 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011510 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011511 }
11512
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011513 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011514 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011515
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011516 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011517 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011518
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011519 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011520 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011521
Daniel Vetter270b3042012-10-27 15:52:05 +020011522 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011523 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070011524 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030011525 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
11526 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
11527 PORT_B);
11528 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
11529 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
11530 }
11531
Jesse Barnes6f6005a2013-08-09 09:34:35 -070011532 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
11533 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
11534 PORT_C);
11535 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020011536 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Jesse Barnes6f6005a2013-08-09 09:34:35 -070011537 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053011538
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030011539 if (IS_CHERRYVIEW(dev)) {
11540 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
11541 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
11542 PORT_D);
11543 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
11544 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
11545 }
11546 }
11547
Jani Nikula3cfca972013-08-27 15:12:26 +030011548 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080011549 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080011550 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080011551
Paulo Zanonie2debe92013-02-18 19:00:27 -030011552 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011553 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011554 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011555 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
11556 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011557 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011558 }
Ma Ling27185ae2009-08-24 13:50:23 +080011559
Imre Deake7281ea2013-05-08 13:14:08 +030011560 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011561 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080011562 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040011563
11564 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040011565
Paulo Zanonie2debe92013-02-18 19:00:27 -030011566 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011567 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011568 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011569 }
Ma Ling27185ae2009-08-24 13:50:23 +080011570
Paulo Zanonie2debe92013-02-18 19:00:27 -030011571 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080011572
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011573 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
11574 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011575 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011576 }
Imre Deake7281ea2013-05-08 13:14:08 +030011577 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011578 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080011579 }
Ma Ling27185ae2009-08-24 13:50:23 +080011580
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011581 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030011582 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011583 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070011584 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080011585 intel_dvo_init(dev);
11586
Zhenyu Wang103a1962009-11-27 11:44:36 +080011587 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080011588 intel_tv_init(dev);
11589
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070011590 intel_edp_psr_init(dev);
11591
Chris Wilson4ef69c72010-09-09 15:14:28 +010011592 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
11593 encoder->base.possible_crtcs = encoder->crtc_mask;
11594 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020011595 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080011596 }
Chris Wilson47356eb2011-01-11 17:06:04 +000011597
Paulo Zanonidde86e22012-12-01 12:04:25 -020011598 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020011599
11600 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011601}
11602
11603static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
11604{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030011605 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080011606 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080011607
Daniel Vetteref2d6332014-02-10 18:00:38 +010011608 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030011609 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010011610 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030011611 drm_gem_object_unreference(&intel_fb->obj->base);
11612 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080011613 kfree(intel_fb);
11614}
11615
11616static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000011617 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080011618 unsigned int *handle)
11619{
11620 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011621 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080011622
Chris Wilson05394f32010-11-08 19:18:58 +000011623 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080011624}
11625
11626static const struct drm_framebuffer_funcs intel_fb_funcs = {
11627 .destroy = intel_user_framebuffer_destroy,
11628 .create_handle = intel_user_framebuffer_create_handle,
11629};
11630
Daniel Vetterb5ea6422014-03-02 21:18:00 +010011631static int intel_framebuffer_init(struct drm_device *dev,
11632 struct intel_framebuffer *intel_fb,
11633 struct drm_mode_fb_cmd2 *mode_cmd,
11634 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080011635{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080011636 int aligned_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010011637 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080011638 int ret;
11639
Daniel Vetterdd4916c2013-10-09 21:23:51 +020011640 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
11641
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011642 if (obj->tiling_mode == I915_TILING_Y) {
11643 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010011644 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011645 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011646
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011647 if (mode_cmd->pitches[0] & 63) {
11648 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
11649 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010011650 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011651 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011652
Chris Wilsona35cdaa2013-06-25 17:26:45 +010011653 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
11654 pitch_limit = 32*1024;
11655 } else if (INTEL_INFO(dev)->gen >= 4) {
11656 if (obj->tiling_mode)
11657 pitch_limit = 16*1024;
11658 else
11659 pitch_limit = 32*1024;
11660 } else if (INTEL_INFO(dev)->gen >= 3) {
11661 if (obj->tiling_mode)
11662 pitch_limit = 8*1024;
11663 else
11664 pitch_limit = 16*1024;
11665 } else
11666 /* XXX DSPC is limited to 4k tiled */
11667 pitch_limit = 8*1024;
11668
11669 if (mode_cmd->pitches[0] > pitch_limit) {
11670 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
11671 obj->tiling_mode ? "tiled" : "linear",
11672 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011673 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011674 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011675
11676 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011677 mode_cmd->pitches[0] != obj->stride) {
11678 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
11679 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011680 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011681 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011682
Ville Syrjälä57779d02012-10-31 17:50:14 +020011683 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011684 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020011685 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020011686 case DRM_FORMAT_RGB565:
11687 case DRM_FORMAT_XRGB8888:
11688 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020011689 break;
11690 case DRM_FORMAT_XRGB1555:
11691 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011692 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011693 DRM_DEBUG("unsupported pixel format: %s\n",
11694 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011695 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011696 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020011697 break;
11698 case DRM_FORMAT_XBGR8888:
11699 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020011700 case DRM_FORMAT_XRGB2101010:
11701 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020011702 case DRM_FORMAT_XBGR2101010:
11703 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011704 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011705 DRM_DEBUG("unsupported pixel format: %s\n",
11706 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011707 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011708 }
Jesse Barnesb5626742011-06-24 12:19:27 -070011709 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020011710 case DRM_FORMAT_YUYV:
11711 case DRM_FORMAT_UYVY:
11712 case DRM_FORMAT_YVYU:
11713 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011714 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011715 DRM_DEBUG("unsupported pixel format: %s\n",
11716 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011717 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011718 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011719 break;
11720 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011721 DRM_DEBUG("unsupported pixel format: %s\n",
11722 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010011723 return -EINVAL;
11724 }
11725
Ville Syrjälä90f9a332012-10-31 17:50:19 +020011726 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
11727 if (mode_cmd->offsets[0] != 0)
11728 return -EINVAL;
11729
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080011730 aligned_height = intel_align_height(dev, mode_cmd->height,
11731 obj->tiling_mode);
Daniel Vetter53155c02013-10-09 21:55:33 +020011732 /* FIXME drm helper for size checks (especially planar formats)? */
11733 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
11734 return -EINVAL;
11735
Daniel Vetterc7d73f62012-12-13 23:38:38 +010011736 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
11737 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020011738 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010011739
Jesse Barnes79e53942008-11-07 14:24:08 -080011740 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
11741 if (ret) {
11742 DRM_ERROR("framebuffer init failed %d\n", ret);
11743 return ret;
11744 }
11745
Jesse Barnes79e53942008-11-07 14:24:08 -080011746 return 0;
11747}
11748
Jesse Barnes79e53942008-11-07 14:24:08 -080011749static struct drm_framebuffer *
11750intel_user_framebuffer_create(struct drm_device *dev,
11751 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011752 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080011753{
Chris Wilson05394f32010-11-08 19:18:58 +000011754 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080011755
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011756 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
11757 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000011758 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010011759 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080011760
Chris Wilsond2dff872011-04-19 08:36:26 +010011761 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080011762}
11763
Daniel Vetter4520f532013-10-09 09:18:51 +020011764#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020011765static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020011766{
11767}
11768#endif
11769
Jesse Barnes79e53942008-11-07 14:24:08 -080011770static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080011771 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020011772 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080011773};
11774
Jesse Barnese70236a2009-09-21 10:42:27 -070011775/* Set up chip specific display functions */
11776static void intel_init_display(struct drm_device *dev)
11777{
11778 struct drm_i915_private *dev_priv = dev->dev_private;
11779
Daniel Vetteree9300b2013-06-03 22:40:22 +020011780 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
11781 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030011782 else if (IS_CHERRYVIEW(dev))
11783 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020011784 else if (IS_VALLEYVIEW(dev))
11785 dev_priv->display.find_dpll = vlv_find_best_dpll;
11786 else if (IS_PINEVIEW(dev))
11787 dev_priv->display.find_dpll = pnv_find_best_dpll;
11788 else
11789 dev_priv->display.find_dpll = i9xx_find_best_dpll;
11790
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011791 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011792 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080011793 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030011794 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020011795 dev_priv->display.crtc_enable = haswell_crtc_enable;
11796 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011797 dev_priv->display.off = haswell_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011798 dev_priv->display.update_primary_plane =
11799 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030011800 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011801 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080011802 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070011803 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020011804 dev_priv->display.crtc_enable = ironlake_crtc_enable;
11805 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011806 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011807 dev_priv->display.update_primary_plane =
11808 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070011809 } else if (IS_VALLEYVIEW(dev)) {
11810 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080011811 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Jesse Barnes89b667f2013-04-18 14:51:36 -070011812 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
11813 dev_priv->display.crtc_enable = valleyview_crtc_enable;
11814 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11815 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011816 dev_priv->display.update_primary_plane =
11817 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070011818 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011819 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080011820 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070011821 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020011822 dev_priv->display.crtc_enable = i9xx_crtc_enable;
11823 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011824 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011825 dev_priv->display.update_primary_plane =
11826 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070011827 }
Jesse Barnese70236a2009-09-21 10:42:27 -070011828
Jesse Barnese70236a2009-09-21 10:42:27 -070011829 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070011830 if (IS_VALLEYVIEW(dev))
11831 dev_priv->display.get_display_clock_speed =
11832 valleyview_get_display_clock_speed;
11833 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070011834 dev_priv->display.get_display_clock_speed =
11835 i945_get_display_clock_speed;
11836 else if (IS_I915G(dev))
11837 dev_priv->display.get_display_clock_speed =
11838 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020011839 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070011840 dev_priv->display.get_display_clock_speed =
11841 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020011842 else if (IS_PINEVIEW(dev))
11843 dev_priv->display.get_display_clock_speed =
11844 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070011845 else if (IS_I915GM(dev))
11846 dev_priv->display.get_display_clock_speed =
11847 i915gm_get_display_clock_speed;
11848 else if (IS_I865G(dev))
11849 dev_priv->display.get_display_clock_speed =
11850 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020011851 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070011852 dev_priv->display.get_display_clock_speed =
11853 i855_get_display_clock_speed;
11854 else /* 852, 830 */
11855 dev_priv->display.get_display_clock_speed =
11856 i830_get_display_clock_speed;
11857
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080011858 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010011859 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070011860 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011861 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080011862 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070011863 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011864 dev_priv->display.write_eld = ironlake_write_eld;
Paulo Zanoni9a952a02014-03-07 20:12:34 -030011865 dev_priv->display.modeset_global_resources =
11866 snb_modeset_global_resources;
Jesse Barnes357555c2011-04-28 15:09:55 -070011867 } else if (IS_IVYBRIDGE(dev)) {
11868 /* FIXME: detect B0+ stepping and use auto training */
11869 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011870 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020011871 dev_priv->display.modeset_global_resources =
11872 ivb_modeset_global_resources;
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070011873 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030011874 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080011875 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020011876 dev_priv->display.modeset_global_resources =
11877 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020011878 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070011879 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080011880 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080011881 } else if (IS_VALLEYVIEW(dev)) {
11882 dev_priv->display.modeset_global_resources =
11883 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040011884 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070011885 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011886
11887 /* Default just returns -ENODEV to indicate unsupported */
11888 dev_priv->display.queue_flip = intel_default_queue_flip;
11889
11890 switch (INTEL_INFO(dev)->gen) {
11891 case 2:
11892 dev_priv->display.queue_flip = intel_gen2_queue_flip;
11893 break;
11894
11895 case 3:
11896 dev_priv->display.queue_flip = intel_gen3_queue_flip;
11897 break;
11898
11899 case 4:
11900 case 5:
11901 dev_priv->display.queue_flip = intel_gen4_queue_flip;
11902 break;
11903
11904 case 6:
11905 dev_priv->display.queue_flip = intel_gen6_queue_flip;
11906 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011907 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070011908 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011909 dev_priv->display.queue_flip = intel_gen7_queue_flip;
11910 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011911 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020011912
11913 intel_panel_init_backlight_funcs(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011914}
11915
Jesse Barnesb690e962010-07-19 13:53:12 -070011916/*
11917 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
11918 * resume, or other times. This quirk makes sure that's the case for
11919 * affected systems.
11920 */
Akshay Joshi0206e352011-08-16 15:34:10 -040011921static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070011922{
11923 struct drm_i915_private *dev_priv = dev->dev_private;
11924
11925 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011926 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070011927}
11928
Keith Packard435793d2011-07-12 14:56:22 -070011929/*
11930 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
11931 */
11932static void quirk_ssc_force_disable(struct drm_device *dev)
11933{
11934 struct drm_i915_private *dev_priv = dev->dev_private;
11935 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011936 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070011937}
11938
Carsten Emde4dca20e2012-03-15 15:56:26 +010011939/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010011940 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
11941 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010011942 */
11943static void quirk_invert_brightness(struct drm_device *dev)
11944{
11945 struct drm_i915_private *dev_priv = dev->dev_private;
11946 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011947 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070011948}
11949
11950struct intel_quirk {
11951 int device;
11952 int subsystem_vendor;
11953 int subsystem_device;
11954 void (*hook)(struct drm_device *dev);
11955};
11956
Egbert Eich5f85f1762012-10-14 15:46:38 +020011957/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
11958struct intel_dmi_quirk {
11959 void (*hook)(struct drm_device *dev);
11960 const struct dmi_system_id (*dmi_id_list)[];
11961};
11962
11963static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
11964{
11965 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
11966 return 1;
11967}
11968
11969static const struct intel_dmi_quirk intel_dmi_quirks[] = {
11970 {
11971 .dmi_id_list = &(const struct dmi_system_id[]) {
11972 {
11973 .callback = intel_dmi_reverse_brightness,
11974 .ident = "NCR Corporation",
11975 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
11976 DMI_MATCH(DMI_PRODUCT_NAME, ""),
11977 },
11978 },
11979 { } /* terminating entry */
11980 },
11981 .hook = quirk_invert_brightness,
11982 },
11983};
11984
Ben Widawskyc43b5632012-04-16 14:07:40 -070011985static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070011986 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040011987 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070011988
Jesse Barnesb690e962010-07-19 13:53:12 -070011989 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
11990 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
11991
Jesse Barnesb690e962010-07-19 13:53:12 -070011992 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
11993 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
11994
Keith Packard435793d2011-07-12 14:56:22 -070011995 /* Lenovo U160 cannot use SSC on LVDS */
11996 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020011997
11998 /* Sony Vaio Y cannot use SSC on LVDS */
11999 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010012000
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010012001 /* Acer Aspire 5734Z must invert backlight brightness */
12002 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12003
12004 /* Acer/eMachines G725 */
12005 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12006
12007 /* Acer/eMachines e725 */
12008 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12009
12010 /* Acer/Packard Bell NCL20 */
12011 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12012
12013 /* Acer Aspire 4736Z */
12014 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020012015
12016 /* Acer Aspire 5336 */
12017 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -070012018};
12019
12020static void intel_init_quirks(struct drm_device *dev)
12021{
12022 struct pci_dev *d = dev->pdev;
12023 int i;
12024
12025 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12026 struct intel_quirk *q = &intel_quirks[i];
12027
12028 if (d->device == q->device &&
12029 (d->subsystem_vendor == q->subsystem_vendor ||
12030 q->subsystem_vendor == PCI_ANY_ID) &&
12031 (d->subsystem_device == q->subsystem_device ||
12032 q->subsystem_device == PCI_ANY_ID))
12033 q->hook(dev);
12034 }
Egbert Eich5f85f1762012-10-14 15:46:38 +020012035 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12036 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12037 intel_dmi_quirks[i].hook(dev);
12038 }
Jesse Barnesb690e962010-07-19 13:53:12 -070012039}
12040
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012041/* Disable the VGA plane that we never use */
12042static void i915_disable_vga(struct drm_device *dev)
12043{
12044 struct drm_i915_private *dev_priv = dev->dev_private;
12045 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012046 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012047
Ville Syrjälä2b37c612014-01-22 21:32:38 +020012048 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012049 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070012050 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012051 sr1 = inb(VGA_SR_DATA);
12052 outb(sr1 | 1<<5, VGA_SR_DATA);
12053 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12054 udelay(300);
12055
12056 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
12057 POSTING_READ(vga_reg);
12058}
12059
Daniel Vetterf8175862012-04-10 15:50:11 +020012060void intel_modeset_init_hw(struct drm_device *dev)
12061{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030012062 intel_prepare_ddi(dev);
12063
Daniel Vetterf8175862012-04-10 15:50:11 +020012064 intel_init_clock_gating(dev);
12065
Jesse Barnes5382f5f352013-12-16 16:34:24 -080012066 intel_reset_dpio(dev);
Jesse Barnes40e9cf62013-10-03 11:35:46 -070012067
Daniel Vetter8090c6b2012-06-24 16:42:32 +020012068 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020012069}
12070
Imre Deak7d708ee2013-04-17 14:04:50 +030012071void intel_modeset_suspend_hw(struct drm_device *dev)
12072{
12073 intel_suspend_hw(dev);
12074}
12075
Jesse Barnes79e53942008-11-07 14:24:08 -080012076void intel_modeset_init(struct drm_device *dev)
12077{
Jesse Barnes652c3932009-08-17 13:31:43 -070012078 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000012079 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012080 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080012081 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080012082
12083 drm_mode_config_init(dev);
12084
12085 dev->mode_config.min_width = 0;
12086 dev->mode_config.min_height = 0;
12087
Dave Airlie019d96c2011-09-29 16:20:42 +010012088 dev->mode_config.preferred_depth = 24;
12089 dev->mode_config.prefer_shadow = 1;
12090
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020012091 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080012092
Jesse Barnesb690e962010-07-19 13:53:12 -070012093 intel_init_quirks(dev);
12094
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030012095 intel_init_pm(dev);
12096
Ben Widawskye3c74752013-04-05 13:12:39 -070012097 if (INTEL_INFO(dev)->num_pipes == 0)
12098 return;
12099
Jesse Barnese70236a2009-09-21 10:42:27 -070012100 intel_init_display(dev);
12101
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012102 if (IS_GEN2(dev)) {
12103 dev->mode_config.max_width = 2048;
12104 dev->mode_config.max_height = 2048;
12105 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070012106 dev->mode_config.max_width = 4096;
12107 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080012108 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012109 dev->mode_config.max_width = 8192;
12110 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080012111 }
Damien Lespiau068be562014-03-28 14:17:49 +000012112
12113 if (IS_GEN2(dev)) {
12114 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12115 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12116 } else {
12117 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12118 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12119 }
12120
Ben Widawsky5d4545a2013-01-17 12:45:15 -080012121 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080012122
Zhao Yakui28c97732009-10-09 11:39:41 +080012123 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012124 INTEL_INFO(dev)->num_pipes,
12125 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080012126
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012127 for_each_pipe(pipe) {
12128 intel_crtc_init(dev, pipe);
Damien Lespiau1fe47782014-03-03 17:31:47 +000012129 for_each_sprite(pipe, sprite) {
12130 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012131 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030012132 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000012133 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012134 }
Jesse Barnes79e53942008-11-07 14:24:08 -080012135 }
12136
Jesse Barnesf42bb702013-12-16 16:34:23 -080012137 intel_init_dpio(dev);
Jesse Barnes5382f5f352013-12-16 16:34:24 -080012138 intel_reset_dpio(dev);
Jesse Barnesf42bb702013-12-16 16:34:23 -080012139
Paulo Zanoni79f689a2012-10-05 12:05:52 -030012140 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012141 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012142
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012143 /* Just disable it once at startup */
12144 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012145 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000012146
12147 /* Just in case the BIOS is doing something questionable. */
12148 intel_disable_fbc(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012149
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012150 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012151 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012152 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012153
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012154 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080012155 if (!crtc->active)
12156 continue;
12157
Jesse Barnes46f297f2014-03-07 08:57:48 -080012158 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080012159 * Note that reserving the BIOS fb up front prevents us
12160 * from stuffing other stolen allocations like the ring
12161 * on top. This prevents some ugliness at boot time, and
12162 * can even allow for smooth boot transitions if the BIOS
12163 * fb is large enough for the active pipe configuration.
12164 */
12165 if (dev_priv->display.get_plane_config) {
12166 dev_priv->display.get_plane_config(crtc,
12167 &crtc->plane_config);
12168 /*
12169 * If the fb is shared between multiple heads, we'll
12170 * just get the first one.
12171 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080012172 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012173 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080012174 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010012175}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080012176
Daniel Vetter7fad7982012-07-04 17:51:47 +020012177static void intel_enable_pipe_a(struct drm_device *dev)
12178{
12179 struct intel_connector *connector;
12180 struct drm_connector *crt = NULL;
12181 struct intel_load_detect_pipe load_detect_temp;
Rob Clark51fd3712013-11-19 12:10:12 -050012182 struct drm_modeset_acquire_ctx ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020012183
12184 /* We can't just switch on the pipe A, we need to set things up with a
12185 * proper mode and output configuration. As a gross hack, enable pipe A
12186 * by enabling the load detect pipe once. */
12187 list_for_each_entry(connector,
12188 &dev->mode_config.connector_list,
12189 base.head) {
12190 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
12191 crt = &connector->base;
12192 break;
12193 }
12194 }
12195
12196 if (!crt)
12197 return;
12198
Rob Clark51fd3712013-11-19 12:10:12 -050012199 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
12200 intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020012201
12202
12203}
12204
Daniel Vetterfa555832012-10-10 23:14:00 +020012205static bool
12206intel_check_plane_mapping(struct intel_crtc *crtc)
12207{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012208 struct drm_device *dev = crtc->base.dev;
12209 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020012210 u32 reg, val;
12211
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012212 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020012213 return true;
12214
12215 reg = DSPCNTR(!crtc->plane);
12216 val = I915_READ(reg);
12217
12218 if ((val & DISPLAY_PLANE_ENABLE) &&
12219 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
12220 return false;
12221
12222 return true;
12223}
12224
Daniel Vetter24929352012-07-02 20:28:59 +020012225static void intel_sanitize_crtc(struct intel_crtc *crtc)
12226{
12227 struct drm_device *dev = crtc->base.dev;
12228 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020012229 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020012230
Daniel Vetter24929352012-07-02 20:28:59 +020012231 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020012232 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020012233 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
12234
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030012235 /* restore vblank interrupts to correct state */
12236 if (crtc->active)
12237 drm_vblank_on(dev, crtc->pipe);
12238 else
12239 drm_vblank_off(dev, crtc->pipe);
12240
Daniel Vetter24929352012-07-02 20:28:59 +020012241 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020012242 * disable the crtc (and hence change the state) if it is wrong. Note
12243 * that gen4+ has a fixed plane -> pipe mapping. */
12244 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020012245 struct intel_connector *connector;
12246 bool plane;
12247
Daniel Vetter24929352012-07-02 20:28:59 +020012248 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
12249 crtc->base.base.id);
12250
12251 /* Pipe has the wrong plane attached and the plane is active.
12252 * Temporarily change the plane mapping and disable everything
12253 * ... */
12254 plane = crtc->plane;
12255 crtc->plane = !plane;
12256 dev_priv->display.crtc_disable(&crtc->base);
12257 crtc->plane = plane;
12258
12259 /* ... and break all links. */
12260 list_for_each_entry(connector, &dev->mode_config.connector_list,
12261 base.head) {
12262 if (connector->encoder->base.crtc != &crtc->base)
12263 continue;
12264
Egbert Eich7f1950f2014-04-25 10:56:22 +020012265 connector->base.dpms = DRM_MODE_DPMS_OFF;
12266 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020012267 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020012268 /* multiple connectors may have the same encoder:
12269 * handle them and break crtc link separately */
12270 list_for_each_entry(connector, &dev->mode_config.connector_list,
12271 base.head)
12272 if (connector->encoder->base.crtc == &crtc->base) {
12273 connector->encoder->base.crtc = NULL;
12274 connector->encoder->connectors_active = false;
12275 }
Daniel Vetter24929352012-07-02 20:28:59 +020012276
12277 WARN_ON(crtc->active);
12278 crtc->base.enabled = false;
12279 }
Daniel Vetter24929352012-07-02 20:28:59 +020012280
Daniel Vetter7fad7982012-07-04 17:51:47 +020012281 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
12282 crtc->pipe == PIPE_A && !crtc->active) {
12283 /* BIOS forgot to enable pipe A, this mostly happens after
12284 * resume. Force-enable the pipe to fix this, the update_dpms
12285 * call below we restore the pipe to the right state, but leave
12286 * the required bits on. */
12287 intel_enable_pipe_a(dev);
12288 }
12289
Daniel Vetter24929352012-07-02 20:28:59 +020012290 /* Adjust the state of the output pipe according to whether we
12291 * have active connectors/encoders. */
12292 intel_crtc_update_dpms(&crtc->base);
12293
12294 if (crtc->active != crtc->base.enabled) {
12295 struct intel_encoder *encoder;
12296
12297 /* This can happen either due to bugs in the get_hw_state
12298 * functions or because the pipe is force-enabled due to the
12299 * pipe A quirk. */
12300 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
12301 crtc->base.base.id,
12302 crtc->base.enabled ? "enabled" : "disabled",
12303 crtc->active ? "enabled" : "disabled");
12304
12305 crtc->base.enabled = crtc->active;
12306
12307 /* Because we only establish the connector -> encoder ->
12308 * crtc links if something is active, this means the
12309 * crtc is now deactivated. Break the links. connector
12310 * -> encoder links are only establish when things are
12311 * actually up, hence no need to break them. */
12312 WARN_ON(crtc->active);
12313
12314 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
12315 WARN_ON(encoder->connectors_active);
12316 encoder->base.crtc = NULL;
12317 }
12318 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020012319
12320 if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010012321 /*
12322 * We start out with underrun reporting disabled to avoid races.
12323 * For correct bookkeeping mark this on active crtcs.
12324 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020012325 * Also on gmch platforms we dont have any hardware bits to
12326 * disable the underrun reporting. Which means we need to start
12327 * out with underrun reporting disabled also on inactive pipes,
12328 * since otherwise we'll complain about the garbage we read when
12329 * e.g. coming up after runtime pm.
12330 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010012331 * No protection against concurrent access is required - at
12332 * worst a fifo underrun happens which also sets this to false.
12333 */
12334 crtc->cpu_fifo_underrun_disabled = true;
12335 crtc->pch_fifo_underrun_disabled = true;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012336
12337 update_scanline_offset(crtc);
Daniel Vetter4cc31482014-03-24 00:01:41 +010012338 }
Daniel Vetter24929352012-07-02 20:28:59 +020012339}
12340
12341static void intel_sanitize_encoder(struct intel_encoder *encoder)
12342{
12343 struct intel_connector *connector;
12344 struct drm_device *dev = encoder->base.dev;
12345
12346 /* We need to check both for a crtc link (meaning that the
12347 * encoder is active and trying to read from a pipe) and the
12348 * pipe itself being active. */
12349 bool has_active_crtc = encoder->base.crtc &&
12350 to_intel_crtc(encoder->base.crtc)->active;
12351
12352 if (encoder->connectors_active && !has_active_crtc) {
12353 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
12354 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030012355 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012356
12357 /* Connector is active, but has no active pipe. This is
12358 * fallout from our resume register restoring. Disable
12359 * the encoder manually again. */
12360 if (encoder->base.crtc) {
12361 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
12362 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030012363 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012364 encoder->disable(encoder);
12365 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020012366 encoder->base.crtc = NULL;
12367 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020012368
12369 /* Inconsistent output/port/pipe state happens presumably due to
12370 * a bug in one of the get_hw_state functions. Or someplace else
12371 * in our code, like the register restore mess on resume. Clamp
12372 * things to off as a safer default. */
12373 list_for_each_entry(connector,
12374 &dev->mode_config.connector_list,
12375 base.head) {
12376 if (connector->encoder != encoder)
12377 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020012378 connector->base.dpms = DRM_MODE_DPMS_OFF;
12379 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020012380 }
12381 }
12382 /* Enabled encoders without active connectors will be fixed in
12383 * the crtc fixup. */
12384}
12385
Imre Deak04098752014-02-18 00:02:16 +020012386void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012387{
12388 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012389 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012390
Imre Deak04098752014-02-18 00:02:16 +020012391 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
12392 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
12393 i915_disable_vga(dev);
12394 }
12395}
12396
12397void i915_redisable_vga(struct drm_device *dev)
12398{
12399 struct drm_i915_private *dev_priv = dev->dev_private;
12400
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030012401 /* This function can be called both from intel_modeset_setup_hw_state or
12402 * at a very early point in our resume sequence, where the power well
12403 * structures are not yet restored. Since this function is at a very
12404 * paranoid "someone might have enabled VGA while we were not looking"
12405 * level, just check if the power well is enabled instead of trying to
12406 * follow the "don't touch the power well if we don't need it" policy
12407 * the rest of the driver uses. */
Imre Deak04098752014-02-18 00:02:16 +020012408 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030012409 return;
12410
Imre Deak04098752014-02-18 00:02:16 +020012411 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012412}
12413
Ville Syrjälä98ec7732014-04-30 17:43:01 +030012414static bool primary_get_hw_state(struct intel_crtc *crtc)
12415{
12416 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
12417
12418 if (!crtc->active)
12419 return false;
12420
12421 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
12422}
12423
Daniel Vetter30e984d2013-06-05 13:34:17 +020012424static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020012425{
12426 struct drm_i915_private *dev_priv = dev->dev_private;
12427 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020012428 struct intel_crtc *crtc;
12429 struct intel_encoder *encoder;
12430 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020012431 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020012432
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012433 for_each_intel_crtc(dev, crtc) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010012434 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020012435
Daniel Vetter99535992014-04-13 12:00:33 +020012436 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
12437
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012438 crtc->active = dev_priv->display.get_pipe_config(crtc,
12439 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020012440
12441 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030012442 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020012443
12444 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
12445 crtc->base.base.id,
12446 crtc->active ? "enabled" : "disabled");
12447 }
12448
Daniel Vetter53589012013-06-05 13:34:16 +020012449 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012450 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030012451 intel_ddi_setup_hw_pll_state(dev);
12452
Daniel Vetter53589012013-06-05 13:34:16 +020012453 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12454 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12455
12456 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
12457 pll->active = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012458 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020012459 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12460 pll->active++;
12461 }
12462 pll->refcount = pll->active;
12463
Daniel Vetter35c95372013-07-17 06:55:04 +020012464 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
12465 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020012466 }
12467
Daniel Vetter24929352012-07-02 20:28:59 +020012468 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12469 base.head) {
12470 pipe = 0;
12471
12472 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012473 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12474 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012475 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020012476 } else {
12477 encoder->base.crtc = NULL;
12478 }
12479
12480 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010012481 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020012482 encoder->base.base.id,
Jani Nikula8e329a02014-06-03 14:56:21 +030012483 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020012484 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010012485 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020012486 }
12487
12488 list_for_each_entry(connector, &dev->mode_config.connector_list,
12489 base.head) {
12490 if (connector->get_hw_state(connector)) {
12491 connector->base.dpms = DRM_MODE_DPMS_ON;
12492 connector->encoder->connectors_active = true;
12493 connector->base.encoder = &connector->encoder->base;
12494 } else {
12495 connector->base.dpms = DRM_MODE_DPMS_OFF;
12496 connector->base.encoder = NULL;
12497 }
12498 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
12499 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012500 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020012501 connector->base.encoder ? "enabled" : "disabled");
12502 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020012503}
12504
12505/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
12506 * and i915 state tracking structures. */
12507void intel_modeset_setup_hw_state(struct drm_device *dev,
12508 bool force_restore)
12509{
12510 struct drm_i915_private *dev_priv = dev->dev_private;
12511 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020012512 struct intel_crtc *crtc;
12513 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020012514 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020012515
12516 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020012517
Jesse Barnesbabea612013-06-26 18:57:38 +030012518 /*
12519 * Now that we have the config, copy it to each CRTC struct
12520 * Note that this could go away if we move to using crtc_config
12521 * checking everywhere.
12522 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012523 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020012524 if (crtc->active && i915.fastboot) {
Daniel Vetterf6a83282014-02-11 15:28:57 -080012525 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030012526 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
12527 crtc->base.base.id);
12528 drm_mode_debug_printmodeline(&crtc->base.mode);
12529 }
12530 }
12531
Daniel Vetter24929352012-07-02 20:28:59 +020012532 /* HW state is read out, now we need to sanitize this mess. */
12533 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12534 base.head) {
12535 intel_sanitize_encoder(encoder);
12536 }
12537
12538 for_each_pipe(pipe) {
12539 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12540 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012541 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020012542 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012543
Daniel Vetter35c95372013-07-17 06:55:04 +020012544 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12545 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12546
12547 if (!pll->on || pll->active)
12548 continue;
12549
12550 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
12551
12552 pll->disable(dev_priv, pll);
12553 pll->on = false;
12554 }
12555
Ville Syrjälä96f90c52013-12-05 15:51:38 +020012556 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030012557 ilk_wm_get_hw_state(dev);
12558
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012559 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030012560 i915_redisable_vga(dev);
12561
Daniel Vetterf30da182013-04-11 20:22:50 +020012562 /*
12563 * We need to use raw interfaces for restoring state to avoid
12564 * checking (bogus) intermediate states.
12565 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012566 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070012567 struct drm_crtc *crtc =
12568 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020012569
12570 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
Matt Roperf4510a22014-04-01 15:22:40 -070012571 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012572 }
12573 } else {
12574 intel_modeset_update_staged_output_state(dev);
12575 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012576
12577 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010012578}
12579
12580void intel_modeset_gem_init(struct drm_device *dev)
12581{
Jesse Barnes484b41d2014-03-07 08:57:55 -080012582 struct drm_crtc *c;
12583 struct intel_framebuffer *fb;
12584
Imre Deakae484342014-03-31 15:10:44 +030012585 mutex_lock(&dev->struct_mutex);
12586 intel_init_gt_powersave(dev);
12587 mutex_unlock(&dev->struct_mutex);
12588
Chris Wilson1833b132012-05-09 11:56:28 +010012589 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020012590
12591 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080012592
12593 /*
12594 * Make sure any fbs we allocated at startup are properly
12595 * pinned & fenced. When we do the allocation it's too early
12596 * for this.
12597 */
12598 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012599 for_each_crtc(dev, c) {
Dave Airlie66e514c2014-04-03 07:51:54 +100012600 if (!c->primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -080012601 continue;
12602
Dave Airlie66e514c2014-04-03 07:51:54 +100012603 fb = to_intel_framebuffer(c->primary->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -080012604 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
12605 DRM_ERROR("failed to pin boot fb on pipe %d\n",
12606 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100012607 drm_framebuffer_unreference(c->primary->fb);
12608 c->primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -080012609 }
12610 }
12611 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012612}
12613
Imre Deak4932e2c2014-02-11 17:12:48 +020012614void intel_connector_unregister(struct intel_connector *intel_connector)
12615{
12616 struct drm_connector *connector = &intel_connector->base;
12617
12618 intel_panel_destroy_backlight(connector);
12619 drm_sysfs_connector_remove(connector);
12620}
12621
Jesse Barnes79e53942008-11-07 14:24:08 -080012622void intel_modeset_cleanup(struct drm_device *dev)
12623{
Jesse Barnes652c3932009-08-17 13:31:43 -070012624 struct drm_i915_private *dev_priv = dev->dev_private;
12625 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030012626 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070012627
Daniel Vetterfd0c0642013-04-24 11:13:35 +020012628 /*
12629 * Interrupts and polling as the first thing to avoid creating havoc.
12630 * Too much stuff here (turning of rps, connectors, ...) would
12631 * experience fancy races otherwise.
12632 */
12633 drm_irq_uninstall(dev);
12634 cancel_work_sync(&dev_priv->hotplug_work);
12635 /*
12636 * Due to the hpd irq storm handling the hotplug work can re-arm the
12637 * poll handlers. Hence disable polling after hpd handling is shut down.
12638 */
Keith Packardf87ea762010-10-03 19:36:26 -070012639 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020012640
Jesse Barnes652c3932009-08-17 13:31:43 -070012641 mutex_lock(&dev->struct_mutex);
12642
Jesse Barnes723bfd72010-10-07 16:01:13 -070012643 intel_unregister_dsm_handler();
12644
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012645 for_each_crtc(dev, crtc) {
Jesse Barnes652c3932009-08-17 13:31:43 -070012646 /* Skip inactive CRTCs */
Matt Roperf4510a22014-04-01 15:22:40 -070012647 if (!crtc->primary->fb)
Jesse Barnes652c3932009-08-17 13:31:43 -070012648 continue;
12649
Daniel Vetter3dec0092010-08-20 21:40:52 +020012650 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070012651 }
12652
Chris Wilson973d04f2011-07-08 12:22:37 +010012653 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070012654
Daniel Vetter8090c6b2012-06-24 16:42:32 +020012655 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000012656
Daniel Vetter930ebb42012-06-29 23:32:16 +020012657 ironlake_teardown_rc6(dev);
12658
Kristian Høgsberg69341a52009-11-11 12:19:17 -050012659 mutex_unlock(&dev->struct_mutex);
12660
Chris Wilson1630fe72011-07-08 12:22:42 +010012661 /* flush any delayed tasks or pending work */
12662 flush_scheduled_work();
12663
Jani Nikuladb31af12013-11-08 16:48:53 +020012664 /* destroy the backlight and sysfs files before encoders/connectors */
12665 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020012666 struct intel_connector *intel_connector;
12667
12668 intel_connector = to_intel_connector(connector);
12669 intel_connector->unregister(intel_connector);
Jani Nikuladb31af12013-11-08 16:48:53 +020012670 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030012671
Jesse Barnes79e53942008-11-07 14:24:08 -080012672 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010012673
12674 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030012675
12676 mutex_lock(&dev->struct_mutex);
12677 intel_cleanup_gt_powersave(dev);
12678 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012679}
12680
Dave Airlie28d52042009-09-21 14:33:58 +100012681/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080012682 * Return which encoder is currently attached for connector.
12683 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010012684struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080012685{
Chris Wilsondf0e9242010-09-09 16:20:55 +010012686 return &intel_attached_encoder(connector)->base;
12687}
Jesse Barnes79e53942008-11-07 14:24:08 -080012688
Chris Wilsondf0e9242010-09-09 16:20:55 +010012689void intel_connector_attach_encoder(struct intel_connector *connector,
12690 struct intel_encoder *encoder)
12691{
12692 connector->encoder = encoder;
12693 drm_mode_connector_attach_encoder(&connector->base,
12694 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080012695}
Dave Airlie28d52042009-09-21 14:33:58 +100012696
12697/*
12698 * set vga decode state - true == enable VGA decode
12699 */
12700int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
12701{
12702 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000012703 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100012704 u16 gmch_ctrl;
12705
Chris Wilson75fa0412014-02-07 18:37:02 -020012706 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
12707 DRM_ERROR("failed to read control word\n");
12708 return -EIO;
12709 }
12710
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020012711 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
12712 return 0;
12713
Dave Airlie28d52042009-09-21 14:33:58 +100012714 if (state)
12715 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
12716 else
12717 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020012718
12719 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
12720 DRM_ERROR("failed to write control word\n");
12721 return -EIO;
12722 }
12723
Dave Airlie28d52042009-09-21 14:33:58 +100012724 return 0;
12725}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012726
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012727struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012728
12729 u32 power_well_driver;
12730
Chris Wilson63b66e52013-08-08 15:12:06 +020012731 int num_transcoders;
12732
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012733 struct intel_cursor_error_state {
12734 u32 control;
12735 u32 position;
12736 u32 base;
12737 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010012738 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012739
12740 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020012741 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012742 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030012743 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010012744 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012745
12746 struct intel_plane_error_state {
12747 u32 control;
12748 u32 stride;
12749 u32 size;
12750 u32 pos;
12751 u32 addr;
12752 u32 surface;
12753 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010012754 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020012755
12756 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020012757 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020012758 enum transcoder cpu_transcoder;
12759
12760 u32 conf;
12761
12762 u32 htotal;
12763 u32 hblank;
12764 u32 hsync;
12765 u32 vtotal;
12766 u32 vblank;
12767 u32 vsync;
12768 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012769};
12770
12771struct intel_display_error_state *
12772intel_display_capture_error_state(struct drm_device *dev)
12773{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012774 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012775 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020012776 int transcoders[] = {
12777 TRANSCODER_A,
12778 TRANSCODER_B,
12779 TRANSCODER_C,
12780 TRANSCODER_EDP,
12781 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012782 int i;
12783
Chris Wilson63b66e52013-08-08 15:12:06 +020012784 if (INTEL_INFO(dev)->num_pipes == 0)
12785 return NULL;
12786
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012787 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012788 if (error == NULL)
12789 return NULL;
12790
Imre Deak190be112013-11-25 17:15:31 +020012791 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012792 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
12793
Damien Lespiau52331302012-08-15 19:23:25 +010012794 for_each_pipe(i) {
Imre Deakddf9c532013-11-27 22:02:02 +020012795 error->pipe[i].power_domain_on =
Imre Deakda7e29b2014-02-18 00:02:02 +020012796 intel_display_power_enabled_sw(dev_priv,
12797 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020012798 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012799 continue;
12800
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030012801 error->cursor[i].control = I915_READ(CURCNTR(i));
12802 error->cursor[i].position = I915_READ(CURPOS(i));
12803 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012804
12805 error->plane[i].control = I915_READ(DSPCNTR(i));
12806 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012807 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030012808 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012809 error->plane[i].pos = I915_READ(DSPPOS(i));
12810 }
Paulo Zanonica291362013-03-06 20:03:14 -030012811 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
12812 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012813 if (INTEL_INFO(dev)->gen >= 4) {
12814 error->plane[i].surface = I915_READ(DSPSURF(i));
12815 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
12816 }
12817
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012818 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030012819
12820 if (!HAS_PCH_SPLIT(dev))
12821 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020012822 }
12823
12824 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
12825 if (HAS_DDI(dev_priv->dev))
12826 error->num_transcoders++; /* Account for eDP. */
12827
12828 for (i = 0; i < error->num_transcoders; i++) {
12829 enum transcoder cpu_transcoder = transcoders[i];
12830
Imre Deakddf9c532013-11-27 22:02:02 +020012831 error->transcoder[i].power_domain_on =
Imre Deakda7e29b2014-02-18 00:02:02 +020012832 intel_display_power_enabled_sw(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020012833 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020012834 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012835 continue;
12836
Chris Wilson63b66e52013-08-08 15:12:06 +020012837 error->transcoder[i].cpu_transcoder = cpu_transcoder;
12838
12839 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
12840 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
12841 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
12842 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
12843 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
12844 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
12845 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012846 }
12847
12848 return error;
12849}
12850
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012851#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
12852
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012853void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012854intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012855 struct drm_device *dev,
12856 struct intel_display_error_state *error)
12857{
12858 int i;
12859
Chris Wilson63b66e52013-08-08 15:12:06 +020012860 if (!error)
12861 return;
12862
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012863 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020012864 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012865 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012866 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010012867 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012868 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020012869 err_printf(m, " Power: %s\n",
12870 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012871 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030012872 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012873
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012874 err_printf(m, "Plane [%d]:\n", i);
12875 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
12876 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012877 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012878 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
12879 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012880 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030012881 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012882 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012883 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012884 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
12885 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012886 }
12887
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012888 err_printf(m, "Cursor [%d]:\n", i);
12889 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
12890 err_printf(m, " POS: %08x\n", error->cursor[i].position);
12891 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012892 }
Chris Wilson63b66e52013-08-08 15:12:06 +020012893
12894 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010012895 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020012896 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020012897 err_printf(m, " Power: %s\n",
12898 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020012899 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
12900 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
12901 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
12902 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
12903 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
12904 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
12905 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
12906 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012907}