blob: ae24829f799f6b6f91fbb9784f0128b58eb90d2b [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 Barnesa0c4da242012-06-15 11:55:13 -0700368 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-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 Barnesa0c4da242012-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 Barnesa0c4da242012-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 Barnesa0c4da242012-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 Barnesa0c4da242012-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 Barnesa0c4da242012-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 Barnesa0c4da242012-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 Barnesa0c4da242012-06-15 11:55:13 -0700798 }
799 }
800 }
801 }
802 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700803
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300804 return found;
Jesse Barnesa0c4da242012-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
Daniel Vettera071fa02014-06-18 23:28:09 +02002354 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002355 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002356
2357 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2358 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002359
2360out_unref_obj:
2361 drm_gem_object_unreference(&obj->base);
2362 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002363 return false;
2364}
2365
2366static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2367 struct intel_plane_config *plane_config)
2368{
2369 struct drm_device *dev = intel_crtc->base.dev;
2370 struct drm_crtc *c;
2371 struct intel_crtc *i;
2372 struct intel_framebuffer *fb;
2373
Dave Airlie66e514c2014-04-03 07:51:54 +10002374 if (!intel_crtc->base.primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002375 return;
2376
2377 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2378 return;
2379
Dave Airlie66e514c2014-04-03 07:51:54 +10002380 kfree(intel_crtc->base.primary->fb);
2381 intel_crtc->base.primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002382
2383 /*
2384 * Failed to alloc the obj, check to see if we should share
2385 * an fb with another CRTC instead
2386 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002387 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002388 i = to_intel_crtc(c);
2389
2390 if (c == &intel_crtc->base)
2391 continue;
2392
Dave Airlie66e514c2014-04-03 07:51:54 +10002393 if (!i->active || !c->primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002394 continue;
2395
Dave Airlie66e514c2014-04-03 07:51:54 +10002396 fb = to_intel_framebuffer(c->primary->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002397 if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
Dave Airlie66e514c2014-04-03 07:51:54 +10002398 drm_framebuffer_reference(c->primary->fb);
2399 intel_crtc->base.primary->fb = c->primary->fb;
Daniel Vettera071fa02014-06-18 23:28:09 +02002400 fb->obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002401 break;
2402 }
2403 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002404}
2405
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002406static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2407 struct drm_framebuffer *fb,
2408 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002409{
2410 struct drm_device *dev = crtc->dev;
2411 struct drm_i915_private *dev_priv = dev->dev_private;
2412 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2413 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002414 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002415 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002416 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002417 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002418 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002419
Jesse Barnes81255562010-08-02 12:07:50 -07002420 intel_fb = to_intel_framebuffer(fb);
2421 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002422
Chris Wilson5eddb702010-09-11 13:48:45 +01002423 reg = DSPCNTR(plane);
2424 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002425 /* Mask out pixel format bits in case we change it */
2426 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002427 switch (fb->pixel_format) {
2428 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002429 dspcntr |= DISPPLANE_8BPP;
2430 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002431 case DRM_FORMAT_XRGB1555:
2432 case DRM_FORMAT_ARGB1555:
2433 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002434 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002435 case DRM_FORMAT_RGB565:
2436 dspcntr |= DISPPLANE_BGRX565;
2437 break;
2438 case DRM_FORMAT_XRGB8888:
2439 case DRM_FORMAT_ARGB8888:
2440 dspcntr |= DISPPLANE_BGRX888;
2441 break;
2442 case DRM_FORMAT_XBGR8888:
2443 case DRM_FORMAT_ABGR8888:
2444 dspcntr |= DISPPLANE_RGBX888;
2445 break;
2446 case DRM_FORMAT_XRGB2101010:
2447 case DRM_FORMAT_ARGB2101010:
2448 dspcntr |= DISPPLANE_BGRX101010;
2449 break;
2450 case DRM_FORMAT_XBGR2101010:
2451 case DRM_FORMAT_ABGR2101010:
2452 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002453 break;
2454 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002455 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002456 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002457
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002458 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002459 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002460 dspcntr |= DISPPLANE_TILED;
2461 else
2462 dspcntr &= ~DISPPLANE_TILED;
2463 }
2464
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002465 if (IS_G4X(dev))
2466 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2467
Chris Wilson5eddb702010-09-11 13:48:45 +01002468 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002469
Daniel Vettere506a0c2012-07-05 12:17:29 +02002470 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002471
Daniel Vetterc2c75132012-07-05 12:17:30 +02002472 if (INTEL_INFO(dev)->gen >= 4) {
2473 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002474 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2475 fb->bits_per_pixel / 8,
2476 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002477 linear_offset -= intel_crtc->dspaddr_offset;
2478 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002479 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002480 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002481
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002482 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2483 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2484 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002485 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002486 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002487 I915_WRITE(DSPSURF(plane),
2488 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002489 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002490 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002491 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002492 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002493 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002494}
2495
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002496static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2497 struct drm_framebuffer *fb,
2498 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002499{
2500 struct drm_device *dev = crtc->dev;
2501 struct drm_i915_private *dev_priv = dev->dev_private;
2502 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2503 struct intel_framebuffer *intel_fb;
2504 struct drm_i915_gem_object *obj;
2505 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002506 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002507 u32 dspcntr;
2508 u32 reg;
2509
Jesse Barnes17638cd2011-06-24 12:19:23 -07002510 intel_fb = to_intel_framebuffer(fb);
2511 obj = intel_fb->obj;
2512
2513 reg = DSPCNTR(plane);
2514 dspcntr = I915_READ(reg);
2515 /* Mask out pixel format bits in case we change it */
2516 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002517 switch (fb->pixel_format) {
2518 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002519 dspcntr |= DISPPLANE_8BPP;
2520 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002521 case DRM_FORMAT_RGB565:
2522 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002523 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002524 case DRM_FORMAT_XRGB8888:
2525 case DRM_FORMAT_ARGB8888:
2526 dspcntr |= DISPPLANE_BGRX888;
2527 break;
2528 case DRM_FORMAT_XBGR8888:
2529 case DRM_FORMAT_ABGR8888:
2530 dspcntr |= DISPPLANE_RGBX888;
2531 break;
2532 case DRM_FORMAT_XRGB2101010:
2533 case DRM_FORMAT_ARGB2101010:
2534 dspcntr |= DISPPLANE_BGRX101010;
2535 break;
2536 case DRM_FORMAT_XBGR2101010:
2537 case DRM_FORMAT_ABGR2101010:
2538 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002539 break;
2540 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002541 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002542 }
2543
2544 if (obj->tiling_mode != I915_TILING_NONE)
2545 dspcntr |= DISPPLANE_TILED;
2546 else
2547 dspcntr &= ~DISPPLANE_TILED;
2548
Ville Syrjäläb42c6002013-11-03 13:47:27 +02002549 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002550 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2551 else
2552 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002553
2554 I915_WRITE(reg, dspcntr);
2555
Daniel Vettere506a0c2012-07-05 12:17:29 +02002556 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002557 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002558 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2559 fb->bits_per_pixel / 8,
2560 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002561 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002562
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002563 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2564 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2565 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002566 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002567 I915_WRITE(DSPSURF(plane),
2568 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002569 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002570 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2571 } else {
2572 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2573 I915_WRITE(DSPLINOFF(plane), linear_offset);
2574 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002575 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002576}
2577
2578/* Assume fb object is pinned & idle & fenced and just update base pointers */
2579static int
2580intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2581 int x, int y, enum mode_set_atomic state)
2582{
2583 struct drm_device *dev = crtc->dev;
2584 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002585
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002586 if (dev_priv->display.disable_fbc)
2587 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002588 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002589
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002590 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2591
2592 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07002593}
2594
Ville Syrjälä96a02912013-02-18 19:08:49 +02002595void intel_display_handle_reset(struct drm_device *dev)
2596{
2597 struct drm_i915_private *dev_priv = dev->dev_private;
2598 struct drm_crtc *crtc;
2599
2600 /*
2601 * Flips in the rings have been nuked by the reset,
2602 * so complete all pending flips so that user space
2603 * will get its events and not get stuck.
2604 *
2605 * Also update the base address of all primary
2606 * planes to the the last fb to make sure we're
2607 * showing the correct fb after a reset.
2608 *
2609 * Need to make two loops over the crtcs so that we
2610 * don't try to grab a crtc mutex before the
2611 * pending_flip_queue really got woken up.
2612 */
2613
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002614 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002615 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2616 enum plane plane = intel_crtc->plane;
2617
2618 intel_prepare_page_flip(dev, plane);
2619 intel_finish_page_flip_plane(dev, plane);
2620 }
2621
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002622 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2624
Rob Clark51fd3712013-11-19 12:10:12 -05002625 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002626 /*
2627 * FIXME: Once we have proper support for primary planes (and
2628 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002629 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002630 */
Matt Roperf4510a22014-04-01 15:22:40 -07002631 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002632 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002633 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002634 crtc->x,
2635 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05002636 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002637 }
2638}
2639
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002640static int
Chris Wilson14667a42012-04-03 17:58:35 +01002641intel_finish_fb(struct drm_framebuffer *old_fb)
2642{
2643 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2644 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2645 bool was_interruptible = dev_priv->mm.interruptible;
2646 int ret;
2647
Chris Wilson14667a42012-04-03 17:58:35 +01002648 /* Big Hammer, we also need to ensure that any pending
2649 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2650 * current scanout is retired before unpinning the old
2651 * framebuffer.
2652 *
2653 * This should only fail upon a hung GPU, in which case we
2654 * can safely continue.
2655 */
2656 dev_priv->mm.interruptible = false;
2657 ret = i915_gem_object_finish_gpu(obj);
2658 dev_priv->mm.interruptible = was_interruptible;
2659
2660 return ret;
2661}
2662
Chris Wilson7d5e3792014-03-04 13:15:08 +00002663static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2664{
2665 struct drm_device *dev = crtc->dev;
2666 struct drm_i915_private *dev_priv = dev->dev_private;
2667 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2668 unsigned long flags;
2669 bool pending;
2670
2671 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2672 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2673 return false;
2674
2675 spin_lock_irqsave(&dev->event_lock, flags);
2676 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2677 spin_unlock_irqrestore(&dev->event_lock, flags);
2678
2679 return pending;
2680}
2681
Chris Wilson14667a42012-04-03 17:58:35 +01002682static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002683intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002684 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002685{
2686 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002687 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002688 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02002689 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002690 struct drm_framebuffer *old_fb;
Daniel Vettera071fa02014-06-18 23:28:09 +02002691 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002692 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002693
Chris Wilson7d5e3792014-03-04 13:15:08 +00002694 if (intel_crtc_has_pending_flip(crtc)) {
2695 DRM_ERROR("pipe is still busy with an old pageflip\n");
2696 return -EBUSY;
2697 }
2698
Jesse Barnes79e53942008-11-07 14:24:08 -08002699 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002700 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002701 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002702 return 0;
2703 }
2704
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002705 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002706 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2707 plane_name(intel_crtc->plane),
2708 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002709 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002710 }
2711
Daniel Vettera071fa02014-06-18 23:28:09 +02002712 old_fb = crtc->primary->fb;
2713
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002714 mutex_lock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +02002715 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2716 if (ret == 0)
2717 i915_gem_track_fb(to_intel_framebuffer(old_fb)->obj, obj,
2718 INTEL_FRONTBUFFER_PRIMARY(pipe));
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002719 mutex_unlock(&dev->struct_mutex);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002720 if (ret != 0) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002721 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002722 return ret;
2723 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002724
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002725 /*
2726 * Update pipe size and adjust fitter if needed: the reason for this is
2727 * that in compute_mode_changes we check the native mode (not the pfit
2728 * mode) to see if we can flip rather than do a full mode set. In the
2729 * fastboot case, we'll flip, but if we don't update the pipesrc and
2730 * pfit state, we'll end up with a big fb scanned out into the wrong
2731 * sized surface.
2732 *
2733 * To fix this properly, we need to hoist the checks up into
2734 * compute_mode_changes (or above), check the actual pfit state and
2735 * whether the platform allows pfit disable with pipe active, and only
2736 * then update the pipesrc and pfit state, even on the flip path.
2737 */
Jani Nikulad330a952014-01-21 11:24:25 +02002738 if (i915.fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002739 const struct drm_display_mode *adjusted_mode =
2740 &intel_crtc->config.adjusted_mode;
2741
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002742 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002743 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2744 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002745 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002746 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2747 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2748 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2749 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2750 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2751 }
Jesse Barnes0637d602013-12-19 10:48:01 -08002752 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2753 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002754 }
2755
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002756 dev_priv->display.update_primary_plane(crtc, fb, x, y);
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002757
Matt Roperf4510a22014-04-01 15:22:40 -07002758 crtc->primary->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002759 crtc->x = x;
2760 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002761
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002762 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002763 if (intel_crtc->active && old_fb != fb)
2764 intel_wait_for_vblank(dev, intel_crtc->pipe);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002765 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002766 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002767 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002768 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002769
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002770 mutex_lock(&dev->struct_mutex);
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002771 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002772 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002773
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002774 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002775}
2776
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002777static void intel_fdi_normal_train(struct drm_crtc *crtc)
2778{
2779 struct drm_device *dev = crtc->dev;
2780 struct drm_i915_private *dev_priv = dev->dev_private;
2781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2782 int pipe = intel_crtc->pipe;
2783 u32 reg, temp;
2784
2785 /* enable normal train */
2786 reg = FDI_TX_CTL(pipe);
2787 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002788 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002789 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2790 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002791 } else {
2792 temp &= ~FDI_LINK_TRAIN_NONE;
2793 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002794 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002795 I915_WRITE(reg, temp);
2796
2797 reg = FDI_RX_CTL(pipe);
2798 temp = I915_READ(reg);
2799 if (HAS_PCH_CPT(dev)) {
2800 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2801 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2802 } else {
2803 temp &= ~FDI_LINK_TRAIN_NONE;
2804 temp |= FDI_LINK_TRAIN_NONE;
2805 }
2806 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2807
2808 /* wait one idle pattern time */
2809 POSTING_READ(reg);
2810 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002811
2812 /* IVB wants error correction enabled */
2813 if (IS_IVYBRIDGE(dev))
2814 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2815 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002816}
2817
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002818static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002819{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002820 return crtc->base.enabled && crtc->active &&
2821 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002822}
2823
Daniel Vetter01a415f2012-10-27 15:58:40 +02002824static void ivb_modeset_global_resources(struct drm_device *dev)
2825{
2826 struct drm_i915_private *dev_priv = dev->dev_private;
2827 struct intel_crtc *pipe_B_crtc =
2828 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2829 struct intel_crtc *pipe_C_crtc =
2830 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2831 uint32_t temp;
2832
Daniel Vetter1e833f42013-02-19 22:31:57 +01002833 /*
2834 * When everything is off disable fdi C so that we could enable fdi B
2835 * with all lanes. Note that we don't care about enabled pipes without
2836 * an enabled pch encoder.
2837 */
2838 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2839 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002840 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2841 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2842
2843 temp = I915_READ(SOUTH_CHICKEN1);
2844 temp &= ~FDI_BC_BIFURCATION_SELECT;
2845 DRM_DEBUG_KMS("disabling fdi C rx\n");
2846 I915_WRITE(SOUTH_CHICKEN1, temp);
2847 }
2848}
2849
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002850/* The FDI link training functions for ILK/Ibexpeak. */
2851static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2852{
2853 struct drm_device *dev = crtc->dev;
2854 struct drm_i915_private *dev_priv = dev->dev_private;
2855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2856 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002857 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002858
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03002859 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002860 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002861
Adam Jacksone1a44742010-06-25 15:32:14 -04002862 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2863 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002864 reg = FDI_RX_IMR(pipe);
2865 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002866 temp &= ~FDI_RX_SYMBOL_LOCK;
2867 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002868 I915_WRITE(reg, temp);
2869 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002870 udelay(150);
2871
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002872 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002873 reg = FDI_TX_CTL(pipe);
2874 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002875 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2876 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002877 temp &= ~FDI_LINK_TRAIN_NONE;
2878 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002879 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002880
Chris Wilson5eddb702010-09-11 13:48:45 +01002881 reg = FDI_RX_CTL(pipe);
2882 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002883 temp &= ~FDI_LINK_TRAIN_NONE;
2884 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002885 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2886
2887 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002888 udelay(150);
2889
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002890 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002891 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2892 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2893 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002894
Chris Wilson5eddb702010-09-11 13:48:45 +01002895 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002896 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002897 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002898 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2899
2900 if ((temp & FDI_RX_BIT_LOCK)) {
2901 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002902 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002903 break;
2904 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002905 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002906 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002907 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002908
2909 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002910 reg = FDI_TX_CTL(pipe);
2911 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002912 temp &= ~FDI_LINK_TRAIN_NONE;
2913 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002914 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002915
Chris Wilson5eddb702010-09-11 13:48:45 +01002916 reg = FDI_RX_CTL(pipe);
2917 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002918 temp &= ~FDI_LINK_TRAIN_NONE;
2919 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002920 I915_WRITE(reg, temp);
2921
2922 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002923 udelay(150);
2924
Chris Wilson5eddb702010-09-11 13:48:45 +01002925 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002926 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002927 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002928 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2929
2930 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002931 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002932 DRM_DEBUG_KMS("FDI train 2 done.\n");
2933 break;
2934 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002935 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002936 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002937 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002938
2939 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002940
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002941}
2942
Akshay Joshi0206e352011-08-16 15:34:10 -04002943static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002944 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2945 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2946 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2947 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2948};
2949
2950/* The FDI link training functions for SNB/Cougarpoint. */
2951static void gen6_fdi_link_train(struct drm_crtc *crtc)
2952{
2953 struct drm_device *dev = crtc->dev;
2954 struct drm_i915_private *dev_priv = dev->dev_private;
2955 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2956 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002957 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002958
Adam Jacksone1a44742010-06-25 15:32:14 -04002959 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2960 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002961 reg = FDI_RX_IMR(pipe);
2962 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002963 temp &= ~FDI_RX_SYMBOL_LOCK;
2964 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002965 I915_WRITE(reg, temp);
2966
2967 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002968 udelay(150);
2969
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002970 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002971 reg = FDI_TX_CTL(pipe);
2972 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002973 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2974 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002975 temp &= ~FDI_LINK_TRAIN_NONE;
2976 temp |= FDI_LINK_TRAIN_PATTERN_1;
2977 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2978 /* SNB-B */
2979 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002980 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002981
Daniel Vetterd74cf322012-10-26 10:58:13 +02002982 I915_WRITE(FDI_RX_MISC(pipe),
2983 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2984
Chris Wilson5eddb702010-09-11 13:48:45 +01002985 reg = FDI_RX_CTL(pipe);
2986 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002987 if (HAS_PCH_CPT(dev)) {
2988 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2989 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2990 } else {
2991 temp &= ~FDI_LINK_TRAIN_NONE;
2992 temp |= FDI_LINK_TRAIN_PATTERN_1;
2993 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002994 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2995
2996 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002997 udelay(150);
2998
Akshay Joshi0206e352011-08-16 15:34:10 -04002999 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003000 reg = FDI_TX_CTL(pipe);
3001 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003002 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3003 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003004 I915_WRITE(reg, temp);
3005
3006 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003007 udelay(500);
3008
Sean Paulfa37d392012-03-02 12:53:39 -05003009 for (retry = 0; retry < 5; retry++) {
3010 reg = FDI_RX_IIR(pipe);
3011 temp = I915_READ(reg);
3012 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3013 if (temp & FDI_RX_BIT_LOCK) {
3014 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3015 DRM_DEBUG_KMS("FDI train 1 done.\n");
3016 break;
3017 }
3018 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003019 }
Sean Paulfa37d392012-03-02 12:53:39 -05003020 if (retry < 5)
3021 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003022 }
3023 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003024 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003025
3026 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003027 reg = FDI_TX_CTL(pipe);
3028 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003029 temp &= ~FDI_LINK_TRAIN_NONE;
3030 temp |= FDI_LINK_TRAIN_PATTERN_2;
3031 if (IS_GEN6(dev)) {
3032 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3033 /* SNB-B */
3034 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3035 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003036 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003037
Chris Wilson5eddb702010-09-11 13:48:45 +01003038 reg = FDI_RX_CTL(pipe);
3039 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003040 if (HAS_PCH_CPT(dev)) {
3041 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3042 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3043 } else {
3044 temp &= ~FDI_LINK_TRAIN_NONE;
3045 temp |= FDI_LINK_TRAIN_PATTERN_2;
3046 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003047 I915_WRITE(reg, temp);
3048
3049 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003050 udelay(150);
3051
Akshay Joshi0206e352011-08-16 15:34:10 -04003052 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003053 reg = FDI_TX_CTL(pipe);
3054 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003055 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3056 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003057 I915_WRITE(reg, temp);
3058
3059 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003060 udelay(500);
3061
Sean Paulfa37d392012-03-02 12:53:39 -05003062 for (retry = 0; retry < 5; retry++) {
3063 reg = FDI_RX_IIR(pipe);
3064 temp = I915_READ(reg);
3065 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3066 if (temp & FDI_RX_SYMBOL_LOCK) {
3067 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3068 DRM_DEBUG_KMS("FDI train 2 done.\n");
3069 break;
3070 }
3071 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003072 }
Sean Paulfa37d392012-03-02 12:53:39 -05003073 if (retry < 5)
3074 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003075 }
3076 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003077 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003078
3079 DRM_DEBUG_KMS("FDI train done.\n");
3080}
3081
Jesse Barnes357555c2011-04-28 15:09:55 -07003082/* Manual link training for Ivy Bridge A0 parts */
3083static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3084{
3085 struct drm_device *dev = crtc->dev;
3086 struct drm_i915_private *dev_priv = dev->dev_private;
3087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3088 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003089 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003090
3091 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3092 for train result */
3093 reg = FDI_RX_IMR(pipe);
3094 temp = I915_READ(reg);
3095 temp &= ~FDI_RX_SYMBOL_LOCK;
3096 temp &= ~FDI_RX_BIT_LOCK;
3097 I915_WRITE(reg, temp);
3098
3099 POSTING_READ(reg);
3100 udelay(150);
3101
Daniel Vetter01a415f2012-10-27 15:58:40 +02003102 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3103 I915_READ(FDI_RX_IIR(pipe)));
3104
Jesse Barnes139ccd32013-08-19 11:04:55 -07003105 /* Try each vswing and preemphasis setting twice before moving on */
3106 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3107 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003108 reg = FDI_TX_CTL(pipe);
3109 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003110 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3111 temp &= ~FDI_TX_ENABLE;
3112 I915_WRITE(reg, temp);
3113
3114 reg = FDI_RX_CTL(pipe);
3115 temp = I915_READ(reg);
3116 temp &= ~FDI_LINK_TRAIN_AUTO;
3117 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3118 temp &= ~FDI_RX_ENABLE;
3119 I915_WRITE(reg, temp);
3120
3121 /* enable CPU FDI TX and PCH FDI RX */
3122 reg = FDI_TX_CTL(pipe);
3123 temp = I915_READ(reg);
3124 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3125 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3126 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003127 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003128 temp |= snb_b_fdi_train_param[j/2];
3129 temp |= FDI_COMPOSITE_SYNC;
3130 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3131
3132 I915_WRITE(FDI_RX_MISC(pipe),
3133 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3134
3135 reg = FDI_RX_CTL(pipe);
3136 temp = I915_READ(reg);
3137 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3138 temp |= FDI_COMPOSITE_SYNC;
3139 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3140
3141 POSTING_READ(reg);
3142 udelay(1); /* should be 0.5us */
3143
3144 for (i = 0; i < 4; i++) {
3145 reg = FDI_RX_IIR(pipe);
3146 temp = I915_READ(reg);
3147 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3148
3149 if (temp & FDI_RX_BIT_LOCK ||
3150 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3151 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3152 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3153 i);
3154 break;
3155 }
3156 udelay(1); /* should be 0.5us */
3157 }
3158 if (i == 4) {
3159 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3160 continue;
3161 }
3162
3163 /* Train 2 */
3164 reg = FDI_TX_CTL(pipe);
3165 temp = I915_READ(reg);
3166 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3167 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3168 I915_WRITE(reg, temp);
3169
3170 reg = FDI_RX_CTL(pipe);
3171 temp = I915_READ(reg);
3172 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3173 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003174 I915_WRITE(reg, temp);
3175
3176 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003177 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003178
Jesse Barnes139ccd32013-08-19 11:04:55 -07003179 for (i = 0; i < 4; i++) {
3180 reg = FDI_RX_IIR(pipe);
3181 temp = I915_READ(reg);
3182 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003183
Jesse Barnes139ccd32013-08-19 11:04:55 -07003184 if (temp & FDI_RX_SYMBOL_LOCK ||
3185 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3186 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3187 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3188 i);
3189 goto train_done;
3190 }
3191 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003192 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003193 if (i == 4)
3194 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003195 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003196
Jesse Barnes139ccd32013-08-19 11:04:55 -07003197train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003198 DRM_DEBUG_KMS("FDI train done.\n");
3199}
3200
Daniel Vetter88cefb62012-08-12 19:27:14 +02003201static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003202{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003203 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003204 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003205 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003206 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003207
Jesse Barnesc64e3112010-09-10 11:27:03 -07003208
Jesse Barnes0e23b992010-09-10 11:10:00 -07003209 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003210 reg = FDI_RX_CTL(pipe);
3211 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003212 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3213 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003214 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003215 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3216
3217 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003218 udelay(200);
3219
3220 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003221 temp = I915_READ(reg);
3222 I915_WRITE(reg, temp | FDI_PCDCLK);
3223
3224 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003225 udelay(200);
3226
Paulo Zanoni20749732012-11-23 15:30:38 -02003227 /* Enable CPU FDI TX PLL, always on for Ironlake */
3228 reg = FDI_TX_CTL(pipe);
3229 temp = I915_READ(reg);
3230 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3231 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003232
Paulo Zanoni20749732012-11-23 15:30:38 -02003233 POSTING_READ(reg);
3234 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003235 }
3236}
3237
Daniel Vetter88cefb62012-08-12 19:27:14 +02003238static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3239{
3240 struct drm_device *dev = intel_crtc->base.dev;
3241 struct drm_i915_private *dev_priv = dev->dev_private;
3242 int pipe = intel_crtc->pipe;
3243 u32 reg, temp;
3244
3245 /* Switch from PCDclk to Rawclk */
3246 reg = FDI_RX_CTL(pipe);
3247 temp = I915_READ(reg);
3248 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3249
3250 /* Disable CPU FDI TX PLL */
3251 reg = FDI_TX_CTL(pipe);
3252 temp = I915_READ(reg);
3253 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3254
3255 POSTING_READ(reg);
3256 udelay(100);
3257
3258 reg = FDI_RX_CTL(pipe);
3259 temp = I915_READ(reg);
3260 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3261
3262 /* Wait for the clocks to turn off. */
3263 POSTING_READ(reg);
3264 udelay(100);
3265}
3266
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003267static void ironlake_fdi_disable(struct drm_crtc *crtc)
3268{
3269 struct drm_device *dev = crtc->dev;
3270 struct drm_i915_private *dev_priv = dev->dev_private;
3271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3272 int pipe = intel_crtc->pipe;
3273 u32 reg, temp;
3274
3275 /* disable CPU FDI tx and PCH FDI rx */
3276 reg = FDI_TX_CTL(pipe);
3277 temp = I915_READ(reg);
3278 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3279 POSTING_READ(reg);
3280
3281 reg = FDI_RX_CTL(pipe);
3282 temp = I915_READ(reg);
3283 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003284 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003285 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3286
3287 POSTING_READ(reg);
3288 udelay(100);
3289
3290 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003291 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003292 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003293
3294 /* still set train pattern 1 */
3295 reg = FDI_TX_CTL(pipe);
3296 temp = I915_READ(reg);
3297 temp &= ~FDI_LINK_TRAIN_NONE;
3298 temp |= FDI_LINK_TRAIN_PATTERN_1;
3299 I915_WRITE(reg, temp);
3300
3301 reg = FDI_RX_CTL(pipe);
3302 temp = I915_READ(reg);
3303 if (HAS_PCH_CPT(dev)) {
3304 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3305 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3306 } else {
3307 temp &= ~FDI_LINK_TRAIN_NONE;
3308 temp |= FDI_LINK_TRAIN_PATTERN_1;
3309 }
3310 /* BPC in FDI rx is consistent with that in PIPECONF */
3311 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003312 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003313 I915_WRITE(reg, temp);
3314
3315 POSTING_READ(reg);
3316 udelay(100);
3317}
3318
Chris Wilson5dce5b932014-01-20 10:17:36 +00003319bool intel_has_pending_fb_unpin(struct drm_device *dev)
3320{
3321 struct intel_crtc *crtc;
3322
3323 /* Note that we don't need to be called with mode_config.lock here
3324 * as our list of CRTC objects is static for the lifetime of the
3325 * device and so cannot disappear as we iterate. Similarly, we can
3326 * happily treat the predicates as racy, atomic checks as userspace
3327 * cannot claim and pin a new fb without at least acquring the
3328 * struct_mutex and so serialising with us.
3329 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003330 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003331 if (atomic_read(&crtc->unpin_work_count) == 0)
3332 continue;
3333
3334 if (crtc->unpin_work)
3335 intel_wait_for_vblank(dev, crtc->pipe);
3336
3337 return true;
3338 }
3339
3340 return false;
3341}
3342
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003343void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003344{
Chris Wilson0f911282012-04-17 10:05:38 +01003345 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003346 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003347
Matt Roperf4510a22014-04-01 15:22:40 -07003348 if (crtc->primary->fb == NULL)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003349 return;
3350
Daniel Vetter2c10d572012-12-20 21:24:07 +01003351 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3352
Daniel Vettereed6d672014-05-19 16:09:35 +02003353 WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3354 !intel_crtc_has_pending_flip(crtc),
3355 60*HZ) == 0);
Chris Wilson5bb61642012-09-27 21:25:58 +01003356
Chris Wilson0f911282012-04-17 10:05:38 +01003357 mutex_lock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07003358 intel_finish_fb(crtc->primary->fb);
Chris Wilson0f911282012-04-17 10:05:38 +01003359 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003360}
3361
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003362/* Program iCLKIP clock to the desired frequency */
3363static void lpt_program_iclkip(struct drm_crtc *crtc)
3364{
3365 struct drm_device *dev = crtc->dev;
3366 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003367 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003368 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3369 u32 temp;
3370
Daniel Vetter09153002012-12-12 14:06:44 +01003371 mutex_lock(&dev_priv->dpio_lock);
3372
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003373 /* It is necessary to ungate the pixclk gate prior to programming
3374 * the divisors, and gate it back when it is done.
3375 */
3376 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3377
3378 /* Disable SSCCTL */
3379 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003380 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3381 SBI_SSCCTL_DISABLE,
3382 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003383
3384 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003385 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003386 auxdiv = 1;
3387 divsel = 0x41;
3388 phaseinc = 0x20;
3389 } else {
3390 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003391 * but the adjusted_mode->crtc_clock in in KHz. To get the
3392 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003393 * convert the virtual clock precision to KHz here for higher
3394 * precision.
3395 */
3396 u32 iclk_virtual_root_freq = 172800 * 1000;
3397 u32 iclk_pi_range = 64;
3398 u32 desired_divisor, msb_divisor_value, pi_value;
3399
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003400 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003401 msb_divisor_value = desired_divisor / iclk_pi_range;
3402 pi_value = desired_divisor % iclk_pi_range;
3403
3404 auxdiv = 0;
3405 divsel = msb_divisor_value - 2;
3406 phaseinc = pi_value;
3407 }
3408
3409 /* This should not happen with any sane values */
3410 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3411 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3412 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3413 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3414
3415 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 +03003416 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003417 auxdiv,
3418 divsel,
3419 phasedir,
3420 phaseinc);
3421
3422 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003423 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003424 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3425 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3426 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3427 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3428 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3429 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003430 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003431
3432 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003433 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003434 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3435 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003436 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003437
3438 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003439 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003440 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003441 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003442
3443 /* Wait for initialization time */
3444 udelay(24);
3445
3446 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003447
3448 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003449}
3450
Daniel Vetter275f01b22013-05-03 11:49:47 +02003451static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3452 enum pipe pch_transcoder)
3453{
3454 struct drm_device *dev = crtc->base.dev;
3455 struct drm_i915_private *dev_priv = dev->dev_private;
3456 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3457
3458 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3459 I915_READ(HTOTAL(cpu_transcoder)));
3460 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3461 I915_READ(HBLANK(cpu_transcoder)));
3462 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3463 I915_READ(HSYNC(cpu_transcoder)));
3464
3465 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3466 I915_READ(VTOTAL(cpu_transcoder)));
3467 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3468 I915_READ(VBLANK(cpu_transcoder)));
3469 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3470 I915_READ(VSYNC(cpu_transcoder)));
3471 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3472 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3473}
3474
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003475static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3476{
3477 struct drm_i915_private *dev_priv = dev->dev_private;
3478 uint32_t temp;
3479
3480 temp = I915_READ(SOUTH_CHICKEN1);
3481 if (temp & FDI_BC_BIFURCATION_SELECT)
3482 return;
3483
3484 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3485 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3486
3487 temp |= FDI_BC_BIFURCATION_SELECT;
3488 DRM_DEBUG_KMS("enabling fdi C rx\n");
3489 I915_WRITE(SOUTH_CHICKEN1, temp);
3490 POSTING_READ(SOUTH_CHICKEN1);
3491}
3492
3493static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3494{
3495 struct drm_device *dev = intel_crtc->base.dev;
3496 struct drm_i915_private *dev_priv = dev->dev_private;
3497
3498 switch (intel_crtc->pipe) {
3499 case PIPE_A:
3500 break;
3501 case PIPE_B:
3502 if (intel_crtc->config.fdi_lanes > 2)
3503 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3504 else
3505 cpt_enable_fdi_bc_bifurcation(dev);
3506
3507 break;
3508 case PIPE_C:
3509 cpt_enable_fdi_bc_bifurcation(dev);
3510
3511 break;
3512 default:
3513 BUG();
3514 }
3515}
3516
Jesse Barnesf67a5592011-01-05 10:31:48 -08003517/*
3518 * Enable PCH resources required for PCH ports:
3519 * - PCH PLLs
3520 * - FDI training & RX/TX
3521 * - update transcoder timings
3522 * - DP transcoding bits
3523 * - transcoder
3524 */
3525static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003526{
3527 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003528 struct drm_i915_private *dev_priv = dev->dev_private;
3529 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3530 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003531 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003532
Daniel Vetterab9412b2013-05-03 11:49:46 +02003533 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003534
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003535 if (IS_IVYBRIDGE(dev))
3536 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3537
Daniel Vettercd986ab2012-10-26 10:58:12 +02003538 /* Write the TU size bits before fdi link training, so that error
3539 * detection works. */
3540 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3541 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3542
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003543 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003544 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003545
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003546 /* We need to program the right clock selection before writing the pixel
3547 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003548 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003549 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003550
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003551 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003552 temp |= TRANS_DPLL_ENABLE(pipe);
3553 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003554 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003555 temp |= sel;
3556 else
3557 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003558 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003559 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003560
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003561 /* XXX: pch pll's can be enabled any time before we enable the PCH
3562 * transcoder, and we actually should do this to not upset any PCH
3563 * transcoder that already use the clock when we share it.
3564 *
3565 * Note that enable_shared_dpll tries to do the right thing, but
3566 * get_shared_dpll unconditionally resets the pll - we need that to have
3567 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02003568 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003569
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003570 /* set transcoder timing, panel must allow it */
3571 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003572 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003573
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003574 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003575
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003576 /* For PCH DP, enable TRANS_DP_CTL */
3577 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003578 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3579 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003580 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003581 reg = TRANS_DP_CTL(pipe);
3582 temp = I915_READ(reg);
3583 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003584 TRANS_DP_SYNC_MASK |
3585 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003586 temp |= (TRANS_DP_OUTPUT_ENABLE |
3587 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003588 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003589
3590 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003591 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003592 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003593 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003594
3595 switch (intel_trans_dp_port_sel(crtc)) {
3596 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003597 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003598 break;
3599 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003600 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003601 break;
3602 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003603 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003604 break;
3605 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003606 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003607 }
3608
Chris Wilson5eddb702010-09-11 13:48:45 +01003609 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003610 }
3611
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003612 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003613}
3614
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003615static void lpt_pch_enable(struct drm_crtc *crtc)
3616{
3617 struct drm_device *dev = crtc->dev;
3618 struct drm_i915_private *dev_priv = dev->dev_private;
3619 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003620 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003621
Daniel Vetterab9412b2013-05-03 11:49:46 +02003622 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003623
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003624 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003625
Paulo Zanoni0540e482012-10-31 18:12:40 -02003626 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003627 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003628
Paulo Zanoni937bb612012-10-31 18:12:47 -02003629 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003630}
3631
Daniel Vettere2b78262013-06-07 23:10:03 +02003632static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003633{
Daniel Vettere2b78262013-06-07 23:10:03 +02003634 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003635
3636 if (pll == NULL)
3637 return;
3638
3639 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003640 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003641 return;
3642 }
3643
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003644 if (--pll->refcount == 0) {
3645 WARN_ON(pll->on);
3646 WARN_ON(pll->active);
3647 }
3648
Daniel Vettera43f6e02013-06-07 23:10:32 +02003649 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003650}
3651
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003652static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003653{
Daniel Vettere2b78262013-06-07 23:10:03 +02003654 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3655 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3656 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003657
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003658 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003659 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3660 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003661 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003662 }
3663
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003664 if (HAS_PCH_IBX(dev_priv->dev)) {
3665 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003666 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003667 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003668
Daniel Vetter46edb022013-06-05 13:34:12 +02003669 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3670 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003671
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003672 WARN_ON(pll->refcount);
3673
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003674 goto found;
3675 }
3676
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003677 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3678 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003679
3680 /* Only want to check enabled timings first */
3681 if (pll->refcount == 0)
3682 continue;
3683
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003684 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3685 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003686 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003687 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003688 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003689
3690 goto found;
3691 }
3692 }
3693
3694 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003695 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3696 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003697 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003698 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3699 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003700 goto found;
3701 }
3702 }
3703
3704 return NULL;
3705
3706found:
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003707 if (pll->refcount == 0)
3708 pll->hw_state = crtc->config.dpll_hw_state;
3709
Daniel Vettera43f6e02013-06-07 23:10:32 +02003710 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003711 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3712 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003713
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003714 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003715
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003716 return pll;
3717}
3718
Daniel Vettera1520312013-05-03 11:49:50 +02003719static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003720{
3721 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003722 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003723 u32 temp;
3724
3725 temp = I915_READ(dslreg);
3726 udelay(500);
3727 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003728 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003729 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003730 }
3731}
3732
Jesse Barnesb074cec2013-04-25 12:55:02 -07003733static void ironlake_pfit_enable(struct intel_crtc *crtc)
3734{
3735 struct drm_device *dev = crtc->base.dev;
3736 struct drm_i915_private *dev_priv = dev->dev_private;
3737 int pipe = crtc->pipe;
3738
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003739 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003740 /* Force use of hard-coded filter coefficients
3741 * as some pre-programmed values are broken,
3742 * e.g. x201.
3743 */
3744 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3745 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3746 PF_PIPE_SEL_IVB(pipe));
3747 else
3748 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3749 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3750 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003751 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003752}
3753
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003754static void intel_enable_planes(struct drm_crtc *crtc)
3755{
3756 struct drm_device *dev = crtc->dev;
3757 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003758 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003759 struct intel_plane *intel_plane;
3760
Matt Roperaf2b6532014-04-01 15:22:32 -07003761 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3762 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003763 if (intel_plane->pipe == pipe)
3764 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003765 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003766}
3767
3768static void intel_disable_planes(struct drm_crtc *crtc)
3769{
3770 struct drm_device *dev = crtc->dev;
3771 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003772 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003773 struct intel_plane *intel_plane;
3774
Matt Roperaf2b6532014-04-01 15:22:32 -07003775 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3776 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003777 if (intel_plane->pipe == pipe)
3778 intel_plane_disable(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003779 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003780}
3781
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003782void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003783{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003784 struct drm_device *dev = crtc->base.dev;
3785 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03003786
3787 if (!crtc->config.ips_enabled)
3788 return;
3789
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003790 /* We can only enable IPS after we enable a plane and wait for a vblank */
3791 intel_wait_for_vblank(dev, crtc->pipe);
3792
Paulo Zanonid77e4532013-09-24 13:52:55 -03003793 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003794 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003795 mutex_lock(&dev_priv->rps.hw_lock);
3796 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3797 mutex_unlock(&dev_priv->rps.hw_lock);
3798 /* Quoting Art Runyan: "its not safe to expect any particular
3799 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003800 * mailbox." Moreover, the mailbox may return a bogus state,
3801 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003802 */
3803 } else {
3804 I915_WRITE(IPS_CTL, IPS_ENABLE);
3805 /* The bit only becomes 1 in the next vblank, so this wait here
3806 * is essentially intel_wait_for_vblank. If we don't have this
3807 * and don't wait for vblanks until the end of crtc_enable, then
3808 * the HW state readout code will complain that the expected
3809 * IPS_CTL value is not the one we read. */
3810 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3811 DRM_ERROR("Timed out waiting for IPS enable\n");
3812 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003813}
3814
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003815void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003816{
3817 struct drm_device *dev = crtc->base.dev;
3818 struct drm_i915_private *dev_priv = dev->dev_private;
3819
3820 if (!crtc->config.ips_enabled)
3821 return;
3822
3823 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003824 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003825 mutex_lock(&dev_priv->rps.hw_lock);
3826 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3827 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003828 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3829 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3830 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08003831 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003832 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08003833 POSTING_READ(IPS_CTL);
3834 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003835
3836 /* We need to wait for a vblank before we can disable the plane. */
3837 intel_wait_for_vblank(dev, crtc->pipe);
3838}
3839
3840/** Loads the palette/gamma unit for the CRTC with the prepared values */
3841static void intel_crtc_load_lut(struct drm_crtc *crtc)
3842{
3843 struct drm_device *dev = crtc->dev;
3844 struct drm_i915_private *dev_priv = dev->dev_private;
3845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3846 enum pipe pipe = intel_crtc->pipe;
3847 int palreg = PALETTE(pipe);
3848 int i;
3849 bool reenable_ips = false;
3850
3851 /* The clocks have to be on to load the palette. */
3852 if (!crtc->enabled || !intel_crtc->active)
3853 return;
3854
3855 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3856 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3857 assert_dsi_pll_enabled(dev_priv);
3858 else
3859 assert_pll_enabled(dev_priv, pipe);
3860 }
3861
3862 /* use legacy palette for Ironlake */
3863 if (HAS_PCH_SPLIT(dev))
3864 palreg = LGC_PALETTE(pipe);
3865
3866 /* Workaround : Do not read or write the pipe palette/gamma data while
3867 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3868 */
Paulo Zanoni41e6fc42014-01-08 17:26:31 -02003869 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03003870 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3871 GAMMA_MODE_MODE_SPLIT)) {
3872 hsw_disable_ips(intel_crtc);
3873 reenable_ips = true;
3874 }
3875
3876 for (i = 0; i < 256; i++) {
3877 I915_WRITE(palreg + 4 * i,
3878 (intel_crtc->lut_r[i] << 16) |
3879 (intel_crtc->lut_g[i] << 8) |
3880 intel_crtc->lut_b[i]);
3881 }
3882
3883 if (reenable_ips)
3884 hsw_enable_ips(intel_crtc);
3885}
3886
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003887static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3888{
3889 if (!enable && intel_crtc->overlay) {
3890 struct drm_device *dev = intel_crtc->base.dev;
3891 struct drm_i915_private *dev_priv = dev->dev_private;
3892
3893 mutex_lock(&dev->struct_mutex);
3894 dev_priv->mm.interruptible = false;
3895 (void) intel_overlay_switch_off(intel_crtc->overlay);
3896 dev_priv->mm.interruptible = true;
3897 mutex_unlock(&dev->struct_mutex);
3898 }
3899
3900 /* Let userspace switch the overlay on again. In most cases userspace
3901 * has to recompute where to put it anyway.
3902 */
3903}
3904
3905/**
3906 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3907 * cursor plane briefly if not already running after enabling the display
3908 * plane.
3909 * This workaround avoids occasional blank screens when self refresh is
3910 * enabled.
3911 */
3912static void
3913g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3914{
3915 u32 cntl = I915_READ(CURCNTR(pipe));
3916
3917 if ((cntl & CURSOR_MODE) == 0) {
3918 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3919
3920 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3921 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3922 intel_wait_for_vblank(dev_priv->dev, pipe);
3923 I915_WRITE(CURCNTR(pipe), cntl);
3924 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3925 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3926 }
3927}
3928
3929static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003930{
3931 struct drm_device *dev = crtc->dev;
3932 struct drm_i915_private *dev_priv = dev->dev_private;
3933 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3934 int pipe = intel_crtc->pipe;
3935 int plane = intel_crtc->plane;
3936
Ville Syrjäläf98551a2014-05-22 17:48:06 +03003937 drm_vblank_on(dev, pipe);
3938
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003939 intel_enable_primary_hw_plane(dev_priv, plane, pipe);
3940 intel_enable_planes(crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003941 /* The fixup needs to happen before cursor is enabled */
3942 if (IS_G4X(dev))
3943 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003944 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003945 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003946
3947 hsw_enable_ips(intel_crtc);
3948
3949 mutex_lock(&dev->struct_mutex);
3950 intel_update_fbc(dev);
3951 mutex_unlock(&dev->struct_mutex);
3952}
3953
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003954static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003955{
3956 struct drm_device *dev = crtc->dev;
3957 struct drm_i915_private *dev_priv = dev->dev_private;
3958 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3959 int pipe = intel_crtc->pipe;
3960 int plane = intel_crtc->plane;
3961
3962 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003963
3964 if (dev_priv->fbc.plane == plane)
3965 intel_disable_fbc(dev);
3966
3967 hsw_disable_ips(intel_crtc);
3968
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003969 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003970 intel_crtc_update_cursor(crtc, false);
3971 intel_disable_planes(crtc);
3972 intel_disable_primary_hw_plane(dev_priv, plane, pipe);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03003973
3974 drm_vblank_off(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003975}
3976
Jesse Barnesf67a5592011-01-05 10:31:48 -08003977static void ironlake_crtc_enable(struct drm_crtc *crtc)
3978{
3979 struct drm_device *dev = crtc->dev;
3980 struct drm_i915_private *dev_priv = dev->dev_private;
3981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003982 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003983 int pipe = intel_crtc->pipe;
Daniel Vetter29407aa2014-04-24 23:55:08 +02003984 enum plane plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003985
Daniel Vetter08a48462012-07-02 11:43:47 +02003986 WARN_ON(!crtc->enabled);
3987
Jesse Barnesf67a5592011-01-05 10:31:48 -08003988 if (intel_crtc->active)
3989 return;
3990
Daniel Vetterb14b1052014-04-24 23:55:13 +02003991 if (intel_crtc->config.has_pch_encoder)
3992 intel_prepare_shared_dpll(intel_crtc);
3993
Daniel Vetter29407aa2014-04-24 23:55:08 +02003994 if (intel_crtc->config.has_dp_encoder)
3995 intel_dp_set_m_n(intel_crtc);
3996
3997 intel_set_pipe_timings(intel_crtc);
3998
3999 if (intel_crtc->config.has_pch_encoder) {
4000 intel_cpu_transcoder_set_m_n(intel_crtc,
4001 &intel_crtc->config.fdi_m_n);
4002 }
4003
4004 ironlake_set_pipeconf(crtc);
4005
4006 /* Set up the display plane register */
4007 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
4008 POSTING_READ(DSPCNTR(plane));
4009
4010 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4011 crtc->x, crtc->y);
4012
Jesse Barnesf67a5592011-01-05 10:31:48 -08004013 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004014
4015 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4016 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
4017
Daniel Vetterf6736a12013-06-05 13:34:30 +02004018 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004019 if (encoder->pre_enable)
4020 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004021
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004022 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004023 /* Note: FDI PLL enabling _must_ be done before we enable the
4024 * cpu pipes, hence this is separate from all the other fdi/pch
4025 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004026 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004027 } else {
4028 assert_fdi_tx_disabled(dev_priv, pipe);
4029 assert_fdi_rx_disabled(dev_priv, pipe);
4030 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004031
Jesse Barnesb074cec2013-04-25 12:55:02 -07004032 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004033
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004034 /*
4035 * On ILK+ LUT must be loaded before the pipe is running but with
4036 * clocks enabled
4037 */
4038 intel_crtc_load_lut(crtc);
4039
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004040 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004041 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004042
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004043 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004044 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004045
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004046 for_each_encoder_on_crtc(dev, crtc, encoder)
4047 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004048
4049 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004050 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004051
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004052 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004053}
4054
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004055/* IPS only exists on ULT machines and is tied to pipe A. */
4056static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4057{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004058 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004059}
4060
Paulo Zanonie4916942013-09-20 16:21:19 -03004061/*
4062 * This implements the workaround described in the "notes" section of the mode
4063 * set sequence documentation. When going from no pipes or single pipe to
4064 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4065 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4066 */
4067static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4068{
4069 struct drm_device *dev = crtc->base.dev;
4070 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4071
4072 /* We want to get the other_active_crtc only if there's only 1 other
4073 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004074 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004075 if (!crtc_it->active || crtc_it == crtc)
4076 continue;
4077
4078 if (other_active_crtc)
4079 return;
4080
4081 other_active_crtc = crtc_it;
4082 }
4083 if (!other_active_crtc)
4084 return;
4085
4086 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4087 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4088}
4089
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004090static void haswell_crtc_enable(struct drm_crtc *crtc)
4091{
4092 struct drm_device *dev = crtc->dev;
4093 struct drm_i915_private *dev_priv = dev->dev_private;
4094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4095 struct intel_encoder *encoder;
4096 int pipe = intel_crtc->pipe;
Daniel Vetter229fca92014-04-24 23:55:09 +02004097 enum plane plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004098
4099 WARN_ON(!crtc->enabled);
4100
4101 if (intel_crtc->active)
4102 return;
4103
Daniel Vetter229fca92014-04-24 23:55:09 +02004104 if (intel_crtc->config.has_dp_encoder)
4105 intel_dp_set_m_n(intel_crtc);
4106
4107 intel_set_pipe_timings(intel_crtc);
4108
4109 if (intel_crtc->config.has_pch_encoder) {
4110 intel_cpu_transcoder_set_m_n(intel_crtc,
4111 &intel_crtc->config.fdi_m_n);
4112 }
4113
4114 haswell_set_pipeconf(crtc);
4115
4116 intel_set_pipe_csc(crtc);
4117
4118 /* Set up the display plane register */
4119 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
4120 POSTING_READ(DSPCNTR(plane));
4121
4122 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4123 crtc->x, crtc->y);
4124
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004125 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004126
4127 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4128 if (intel_crtc->config.has_pch_encoder)
4129 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4130
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004131 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02004132 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004133
4134 for_each_encoder_on_crtc(dev, crtc, encoder)
4135 if (encoder->pre_enable)
4136 encoder->pre_enable(encoder);
4137
Paulo Zanoni1f544382012-10-24 11:32:00 -02004138 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004139
Jesse Barnesb074cec2013-04-25 12:55:02 -07004140 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004141
4142 /*
4143 * On ILK+ LUT must be loaded before the pipe is running but with
4144 * clocks enabled
4145 */
4146 intel_crtc_load_lut(crtc);
4147
Paulo Zanoni1f544382012-10-24 11:32:00 -02004148 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004149 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004150
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004151 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004152 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004153
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004154 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004155 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004156
Jani Nikula8807e552013-08-30 19:40:32 +03004157 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004158 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004159 intel_opregion_notify_encoder(encoder, true);
4160 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004161
Paulo Zanonie4916942013-09-20 16:21:19 -03004162 /* If we change the relative order between pipe/planes enabling, we need
4163 * to change the workaround. */
4164 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004165 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004166}
4167
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004168static void ironlake_pfit_disable(struct intel_crtc *crtc)
4169{
4170 struct drm_device *dev = crtc->base.dev;
4171 struct drm_i915_private *dev_priv = dev->dev_private;
4172 int pipe = crtc->pipe;
4173
4174 /* To avoid upsetting the power well on haswell only disable the pfit if
4175 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01004176 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004177 I915_WRITE(PF_CTL(pipe), 0);
4178 I915_WRITE(PF_WIN_POS(pipe), 0);
4179 I915_WRITE(PF_WIN_SZ(pipe), 0);
4180 }
4181}
4182
Jesse Barnes6be4a602010-09-10 10:26:01 -07004183static void ironlake_crtc_disable(struct drm_crtc *crtc)
4184{
4185 struct drm_device *dev = crtc->dev;
4186 struct drm_i915_private *dev_priv = dev->dev_private;
4187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004188 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004189 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004190 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004191
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004192 if (!intel_crtc->active)
4193 return;
4194
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004195 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004196
Daniel Vetterea9d7582012-07-10 10:42:52 +02004197 for_each_encoder_on_crtc(dev, crtc, encoder)
4198 encoder->disable(encoder);
4199
Daniel Vetterd925c592013-06-05 13:34:04 +02004200 if (intel_crtc->config.has_pch_encoder)
4201 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4202
Jesse Barnesb24e7172011-01-04 15:09:30 -08004203 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004204
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004205 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004206
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004207 for_each_encoder_on_crtc(dev, crtc, encoder)
4208 if (encoder->post_disable)
4209 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004210
Daniel Vetterd925c592013-06-05 13:34:04 +02004211 if (intel_crtc->config.has_pch_encoder) {
4212 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004213
Daniel Vetterd925c592013-06-05 13:34:04 +02004214 ironlake_disable_pch_transcoder(dev_priv, pipe);
4215 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004216
Daniel Vetterd925c592013-06-05 13:34:04 +02004217 if (HAS_PCH_CPT(dev)) {
4218 /* disable TRANS_DP_CTL */
4219 reg = TRANS_DP_CTL(pipe);
4220 temp = I915_READ(reg);
4221 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4222 TRANS_DP_PORT_SEL_MASK);
4223 temp |= TRANS_DP_PORT_SEL_NONE;
4224 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004225
Daniel Vetterd925c592013-06-05 13:34:04 +02004226 /* disable DPLL_SEL */
4227 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004228 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004229 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004230 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004231
4232 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004233 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004234
4235 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004236 }
4237
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004238 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004239 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004240
4241 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004242 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004243 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004244}
4245
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004246static void haswell_crtc_disable(struct drm_crtc *crtc)
4247{
4248 struct drm_device *dev = crtc->dev;
4249 struct drm_i915_private *dev_priv = dev->dev_private;
4250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4251 struct intel_encoder *encoder;
4252 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004253 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004254
4255 if (!intel_crtc->active)
4256 return;
4257
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004258 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004259
Jani Nikula8807e552013-08-30 19:40:32 +03004260 for_each_encoder_on_crtc(dev, crtc, encoder) {
4261 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004262 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004263 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004264
Paulo Zanoni86642812013-04-12 17:57:57 -03004265 if (intel_crtc->config.has_pch_encoder)
4266 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004267 intel_disable_pipe(dev_priv, pipe);
4268
Paulo Zanoniad80a812012-10-24 16:06:19 -02004269 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004270
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004271 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004272
Paulo Zanoni1f544382012-10-24 11:32:00 -02004273 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004274
4275 for_each_encoder_on_crtc(dev, crtc, encoder)
4276 if (encoder->post_disable)
4277 encoder->post_disable(encoder);
4278
Daniel Vetter88adfff2013-03-28 10:42:01 +01004279 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004280 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03004281 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004282 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004283 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004284
4285 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004286 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004287
4288 mutex_lock(&dev->struct_mutex);
4289 intel_update_fbc(dev);
4290 mutex_unlock(&dev->struct_mutex);
4291}
4292
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004293static void ironlake_crtc_off(struct drm_crtc *crtc)
4294{
4295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004296 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004297}
4298
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004299static void haswell_crtc_off(struct drm_crtc *crtc)
4300{
4301 intel_ddi_put_crtc_pll(crtc);
4302}
4303
Jesse Barnes2dd24552013-04-25 12:55:01 -07004304static void i9xx_pfit_enable(struct intel_crtc *crtc)
4305{
4306 struct drm_device *dev = crtc->base.dev;
4307 struct drm_i915_private *dev_priv = dev->dev_private;
4308 struct intel_crtc_config *pipe_config = &crtc->config;
4309
Daniel Vetter328d8e82013-05-08 10:36:31 +02004310 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004311 return;
4312
Daniel Vetterc0b03412013-05-28 12:05:54 +02004313 /*
4314 * The panel fitter should only be adjusted whilst the pipe is disabled,
4315 * according to register description and PRM.
4316 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004317 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4318 assert_pipe_disabled(dev_priv, crtc->pipe);
4319
Jesse Barnesb074cec2013-04-25 12:55:02 -07004320 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4321 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004322
4323 /* Border color in case we don't scale up to the full screen. Black by
4324 * default, change to something else for debugging. */
4325 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004326}
4327
Imre Deak77d22dc2014-03-05 16:20:52 +02004328#define for_each_power_domain(domain, mask) \
4329 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4330 if ((1 << (domain)) & (mask))
4331
Imre Deak319be8a2014-03-04 19:22:57 +02004332enum intel_display_power_domain
4333intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004334{
Imre Deak319be8a2014-03-04 19:22:57 +02004335 struct drm_device *dev = intel_encoder->base.dev;
4336 struct intel_digital_port *intel_dig_port;
4337
4338 switch (intel_encoder->type) {
4339 case INTEL_OUTPUT_UNKNOWN:
4340 /* Only DDI platforms should ever use this output type */
4341 WARN_ON_ONCE(!HAS_DDI(dev));
4342 case INTEL_OUTPUT_DISPLAYPORT:
4343 case INTEL_OUTPUT_HDMI:
4344 case INTEL_OUTPUT_EDP:
4345 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4346 switch (intel_dig_port->port) {
4347 case PORT_A:
4348 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4349 case PORT_B:
4350 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4351 case PORT_C:
4352 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4353 case PORT_D:
4354 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4355 default:
4356 WARN_ON_ONCE(1);
4357 return POWER_DOMAIN_PORT_OTHER;
4358 }
4359 case INTEL_OUTPUT_ANALOG:
4360 return POWER_DOMAIN_PORT_CRT;
4361 case INTEL_OUTPUT_DSI:
4362 return POWER_DOMAIN_PORT_DSI;
4363 default:
4364 return POWER_DOMAIN_PORT_OTHER;
4365 }
4366}
4367
4368static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4369{
4370 struct drm_device *dev = crtc->dev;
4371 struct intel_encoder *intel_encoder;
4372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4373 enum pipe pipe = intel_crtc->pipe;
4374 bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
Imre Deak77d22dc2014-03-05 16:20:52 +02004375 unsigned long mask;
4376 enum transcoder transcoder;
4377
4378 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4379
4380 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4381 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4382 if (pfit_enabled)
4383 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4384
Imre Deak319be8a2014-03-04 19:22:57 +02004385 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4386 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4387
Imre Deak77d22dc2014-03-05 16:20:52 +02004388 return mask;
4389}
4390
4391void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4392 bool enable)
4393{
4394 if (dev_priv->power_domains.init_power_on == enable)
4395 return;
4396
4397 if (enable)
4398 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4399 else
4400 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4401
4402 dev_priv->power_domains.init_power_on = enable;
4403}
4404
4405static void modeset_update_crtc_power_domains(struct drm_device *dev)
4406{
4407 struct drm_i915_private *dev_priv = dev->dev_private;
4408 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4409 struct intel_crtc *crtc;
4410
4411 /*
4412 * First get all needed power domains, then put all unneeded, to avoid
4413 * any unnecessary toggling of the power wells.
4414 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004415 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004416 enum intel_display_power_domain domain;
4417
4418 if (!crtc->base.enabled)
4419 continue;
4420
Imre Deak319be8a2014-03-04 19:22:57 +02004421 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004422
4423 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4424 intel_display_power_get(dev_priv, domain);
4425 }
4426
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004427 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004428 enum intel_display_power_domain domain;
4429
4430 for_each_power_domain(domain, crtc->enabled_power_domains)
4431 intel_display_power_put(dev_priv, domain);
4432
4433 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4434 }
4435
4436 intel_display_set_init_power(dev_priv, false);
4437}
4438
Jesse Barnes586f49d2013-11-04 16:06:59 -08004439int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004440{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004441 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004442
Jesse Barnes586f49d2013-11-04 16:06:59 -08004443 /* Obtain SKU information */
4444 mutex_lock(&dev_priv->dpio_lock);
4445 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4446 CCK_FUSE_HPLL_FREQ_MASK;
4447 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004448
Jesse Barnes586f49d2013-11-04 16:06:59 -08004449 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08004450}
4451
4452/* Adjust CDclk dividers to allow high res or save power if possible */
4453static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4454{
4455 struct drm_i915_private *dev_priv = dev->dev_private;
4456 u32 val, cmd;
4457
Imre Deakd60c4472014-03-27 17:45:10 +02004458 WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
4459 dev_priv->vlv_cdclk_freq = cdclk;
4460
Jesse Barnes30a970c2013-11-04 13:48:12 -08004461 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
4462 cmd = 2;
4463 else if (cdclk == 266)
4464 cmd = 1;
4465 else
4466 cmd = 0;
4467
4468 mutex_lock(&dev_priv->rps.hw_lock);
4469 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4470 val &= ~DSPFREQGUAR_MASK;
4471 val |= (cmd << DSPFREQGUAR_SHIFT);
4472 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4473 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4474 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4475 50)) {
4476 DRM_ERROR("timed out waiting for CDclk change\n");
4477 }
4478 mutex_unlock(&dev_priv->rps.hw_lock);
4479
4480 if (cdclk == 400) {
4481 u32 divider, vco;
4482
4483 vco = valleyview_get_vco(dev_priv);
4484 divider = ((vco << 1) / cdclk) - 1;
4485
4486 mutex_lock(&dev_priv->dpio_lock);
4487 /* adjust cdclk divider */
4488 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4489 val &= ~0xf;
4490 val |= divider;
4491 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4492 mutex_unlock(&dev_priv->dpio_lock);
4493 }
4494
4495 mutex_lock(&dev_priv->dpio_lock);
4496 /* adjust self-refresh exit latency value */
4497 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4498 val &= ~0x7f;
4499
4500 /*
4501 * For high bandwidth configs, we set a higher latency in the bunit
4502 * so that the core display fetch happens in time to avoid underruns.
4503 */
4504 if (cdclk == 400)
4505 val |= 4500 / 250; /* 4.5 usec */
4506 else
4507 val |= 3000 / 250; /* 3.0 usec */
4508 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4509 mutex_unlock(&dev_priv->dpio_lock);
4510
4511 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4512 intel_i2c_reset(dev);
4513}
4514
Imre Deakd60c4472014-03-27 17:45:10 +02004515int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004516{
4517 int cur_cdclk, vco;
4518 int divider;
4519
4520 vco = valleyview_get_vco(dev_priv);
4521
4522 mutex_lock(&dev_priv->dpio_lock);
4523 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4524 mutex_unlock(&dev_priv->dpio_lock);
4525
4526 divider &= 0xf;
4527
4528 cur_cdclk = (vco << 1) / (divider + 1);
4529
4530 return cur_cdclk;
4531}
4532
4533static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4534 int max_pixclk)
4535{
Jesse Barnes30a970c2013-11-04 13:48:12 -08004536 /*
4537 * Really only a few cases to deal with, as only 4 CDclks are supported:
4538 * 200MHz
4539 * 267MHz
4540 * 320MHz
4541 * 400MHz
4542 * So we check to see whether we're above 90% of the lower bin and
4543 * adjust if needed.
4544 */
4545 if (max_pixclk > 288000) {
4546 return 400;
4547 } else if (max_pixclk > 240000) {
4548 return 320;
4549 } else
4550 return 266;
4551 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4552}
4553
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004554/* compute the max pixel clock for new configuration */
4555static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004556{
4557 struct drm_device *dev = dev_priv->dev;
4558 struct intel_crtc *intel_crtc;
4559 int max_pixclk = 0;
4560
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004561 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004562 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004563 max_pixclk = max(max_pixclk,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004564 intel_crtc->new_config->adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004565 }
4566
4567 return max_pixclk;
4568}
4569
4570static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004571 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004572{
4573 struct drm_i915_private *dev_priv = dev->dev_private;
4574 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004575 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004576
Imre Deakd60c4472014-03-27 17:45:10 +02004577 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4578 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004579 return;
4580
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004581 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004582 for_each_intel_crtc(dev, intel_crtc)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004583 if (intel_crtc->base.enabled)
4584 *prepare_pipes |= (1 << intel_crtc->pipe);
4585}
4586
4587static void valleyview_modeset_global_resources(struct drm_device *dev)
4588{
4589 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004590 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004591 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4592
Imre Deakd60c4472014-03-27 17:45:10 +02004593 if (req_cdclk != dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004594 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak77961eb2014-03-05 16:20:56 +02004595 modeset_update_crtc_power_domains(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004596}
4597
Jesse Barnes89b667f2013-04-18 14:51:36 -07004598static void valleyview_crtc_enable(struct drm_crtc *crtc)
4599{
4600 struct drm_device *dev = crtc->dev;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004601 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004602 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4603 struct intel_encoder *encoder;
4604 int pipe = intel_crtc->pipe;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004605 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03004606 bool is_dsi;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004607 u32 dspcntr;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004608
4609 WARN_ON(!crtc->enabled);
4610
4611 if (intel_crtc->active)
4612 return;
4613
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02004614 vlv_prepare_pll(intel_crtc);
4615
Daniel Vetter5b18e572014-04-24 23:55:06 +02004616 /* Set up the display plane register */
4617 dspcntr = DISPPLANE_GAMMA_ENABLE;
4618
4619 if (intel_crtc->config.has_dp_encoder)
4620 intel_dp_set_m_n(intel_crtc);
4621
4622 intel_set_pipe_timings(intel_crtc);
4623
4624 /* pipesrc and dspsize control the size that is scaled from,
4625 * which should always be the user's requested size.
4626 */
4627 I915_WRITE(DSPSIZE(plane),
4628 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4629 (intel_crtc->config.pipe_src_w - 1));
4630 I915_WRITE(DSPPOS(plane), 0);
4631
4632 i9xx_set_pipeconf(intel_crtc);
4633
4634 I915_WRITE(DSPCNTR(plane), dspcntr);
4635 POSTING_READ(DSPCNTR(plane));
4636
4637 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4638 crtc->x, crtc->y);
4639
Jesse Barnes89b667f2013-04-18 14:51:36 -07004640 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004641
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004642 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4643
Jesse Barnes89b667f2013-04-18 14:51:36 -07004644 for_each_encoder_on_crtc(dev, crtc, encoder)
4645 if (encoder->pre_pll_enable)
4646 encoder->pre_pll_enable(encoder);
4647
Jani Nikula23538ef2013-08-27 15:12:22 +03004648 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4649
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004650 if (!is_dsi) {
4651 if (IS_CHERRYVIEW(dev))
4652 chv_enable_pll(intel_crtc);
4653 else
4654 vlv_enable_pll(intel_crtc);
4655 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07004656
4657 for_each_encoder_on_crtc(dev, crtc, encoder)
4658 if (encoder->pre_enable)
4659 encoder->pre_enable(encoder);
4660
Jesse Barnes2dd24552013-04-25 12:55:01 -07004661 i9xx_pfit_enable(intel_crtc);
4662
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004663 intel_crtc_load_lut(crtc);
4664
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004665 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004666 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004667
Jani Nikula50049452013-07-30 12:20:32 +03004668 for_each_encoder_on_crtc(dev, crtc, encoder)
4669 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004670
4671 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004672
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004673 /* Underruns don't raise interrupts, so check manually. */
4674 i9xx_check_fifo_underruns(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004675}
4676
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004677static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4678{
4679 struct drm_device *dev = crtc->base.dev;
4680 struct drm_i915_private *dev_priv = dev->dev_private;
4681
4682 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4683 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4684}
4685
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004686static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004687{
4688 struct drm_device *dev = crtc->dev;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004689 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08004690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004691 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004692 int pipe = intel_crtc->pipe;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004693 int plane = intel_crtc->plane;
4694 u32 dspcntr;
Jesse Barnes79e53942008-11-07 14:24:08 -08004695
Daniel Vetter08a48462012-07-02 11:43:47 +02004696 WARN_ON(!crtc->enabled);
4697
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004698 if (intel_crtc->active)
4699 return;
4700
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004701 i9xx_set_pll_dividers(intel_crtc);
4702
Daniel Vetter5b18e572014-04-24 23:55:06 +02004703 /* Set up the display plane register */
4704 dspcntr = DISPPLANE_GAMMA_ENABLE;
4705
4706 if (pipe == 0)
4707 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4708 else
4709 dspcntr |= DISPPLANE_SEL_PIPE_B;
4710
4711 if (intel_crtc->config.has_dp_encoder)
4712 intel_dp_set_m_n(intel_crtc);
4713
4714 intel_set_pipe_timings(intel_crtc);
4715
4716 /* pipesrc and dspsize control the size that is scaled from,
4717 * which should always be the user's requested size.
4718 */
4719 I915_WRITE(DSPSIZE(plane),
4720 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4721 (intel_crtc->config.pipe_src_w - 1));
4722 I915_WRITE(DSPPOS(plane), 0);
4723
4724 i9xx_set_pipeconf(intel_crtc);
4725
4726 I915_WRITE(DSPCNTR(plane), dspcntr);
4727 POSTING_READ(DSPCNTR(plane));
4728
4729 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4730 crtc->x, crtc->y);
4731
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004732 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004733
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004734 if (!IS_GEN2(dev))
4735 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4736
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004737 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004738 if (encoder->pre_enable)
4739 encoder->pre_enable(encoder);
4740
Daniel Vetterf6736a12013-06-05 13:34:30 +02004741 i9xx_enable_pll(intel_crtc);
4742
Jesse Barnes2dd24552013-04-25 12:55:01 -07004743 i9xx_pfit_enable(intel_crtc);
4744
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004745 intel_crtc_load_lut(crtc);
4746
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004747 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004748 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004749
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004750 for_each_encoder_on_crtc(dev, crtc, encoder)
4751 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004752
4753 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004754
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004755 /*
4756 * Gen2 reports pipe underruns whenever all planes are disabled.
4757 * So don't enable underrun reporting before at least some planes
4758 * are enabled.
4759 * FIXME: Need to fix the logic to work when we turn off all planes
4760 * but leave the pipe running.
4761 */
4762 if (IS_GEN2(dev))
4763 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4764
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004765 /* Underruns don't raise interrupts, so check manually. */
4766 i9xx_check_fifo_underruns(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004767}
4768
Daniel Vetter87476d62013-04-11 16:29:06 +02004769static void i9xx_pfit_disable(struct intel_crtc *crtc)
4770{
4771 struct drm_device *dev = crtc->base.dev;
4772 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004773
4774 if (!crtc->config.gmch_pfit.control)
4775 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004776
4777 assert_pipe_disabled(dev_priv, crtc->pipe);
4778
Daniel Vetter328d8e82013-05-08 10:36:31 +02004779 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4780 I915_READ(PFIT_CONTROL));
4781 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004782}
4783
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004784static void i9xx_crtc_disable(struct drm_crtc *crtc)
4785{
4786 struct drm_device *dev = crtc->dev;
4787 struct drm_i915_private *dev_priv = dev->dev_private;
4788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004789 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004790 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004791
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004792 if (!intel_crtc->active)
4793 return;
4794
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004795 /*
4796 * Gen2 reports pipe underruns whenever all planes are disabled.
4797 * So diasble underrun reporting before all the planes get disabled.
4798 * FIXME: Need to fix the logic to work when we turn off all planes
4799 * but leave the pipe running.
4800 */
4801 if (IS_GEN2(dev))
4802 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4803
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004804 intel_crtc_disable_planes(crtc);
4805
Daniel Vetterea9d7582012-07-10 10:42:52 +02004806 for_each_encoder_on_crtc(dev, crtc, encoder)
4807 encoder->disable(encoder);
4808
Ville Syrjälä6304cd92014-04-25 13:30:12 +03004809 /*
4810 * On gen2 planes are double buffered but the pipe isn't, so we must
4811 * wait for planes to fully turn off before disabling the pipe.
4812 */
4813 if (IS_GEN2(dev))
4814 intel_wait_for_vblank(dev, pipe);
4815
Jesse Barnesb24e7172011-01-04 15:09:30 -08004816 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004817
Daniel Vetter87476d62013-04-11 16:29:06 +02004818 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004819
Jesse Barnes89b667f2013-04-18 14:51:36 -07004820 for_each_encoder_on_crtc(dev, crtc, encoder)
4821 if (encoder->post_disable)
4822 encoder->post_disable(encoder);
4823
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03004824 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4825 if (IS_CHERRYVIEW(dev))
4826 chv_disable_pll(dev_priv, pipe);
4827 else if (IS_VALLEYVIEW(dev))
4828 vlv_disable_pll(dev_priv, pipe);
4829 else
4830 i9xx_disable_pll(dev_priv, pipe);
4831 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004832
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004833 if (!IS_GEN2(dev))
4834 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4835
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004836 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004837 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004838
Daniel Vetterefa96242014-04-24 23:55:02 +02004839 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004840 intel_update_fbc(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02004841 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004842}
4843
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004844static void i9xx_crtc_off(struct drm_crtc *crtc)
4845{
4846}
4847
Daniel Vetter976f8a22012-07-08 22:34:21 +02004848static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4849 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004850{
4851 struct drm_device *dev = crtc->dev;
4852 struct drm_i915_master_private *master_priv;
4853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4854 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004855
4856 if (!dev->primary->master)
4857 return;
4858
4859 master_priv = dev->primary->master->driver_priv;
4860 if (!master_priv->sarea_priv)
4861 return;
4862
Jesse Barnes79e53942008-11-07 14:24:08 -08004863 switch (pipe) {
4864 case 0:
4865 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4866 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4867 break;
4868 case 1:
4869 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4870 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4871 break;
4872 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004873 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004874 break;
4875 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004876}
4877
Daniel Vetter976f8a22012-07-08 22:34:21 +02004878/**
4879 * Sets the power management mode of the pipe and plane.
4880 */
4881void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004882{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004883 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004884 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02004885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02004886 struct intel_encoder *intel_encoder;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02004887 enum intel_display_power_domain domain;
4888 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004889 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004890
Daniel Vetter976f8a22012-07-08 22:34:21 +02004891 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4892 enable |= intel_encoder->connectors_active;
4893
Daniel Vetter0e572fe2014-04-24 23:55:42 +02004894 if (enable) {
4895 if (!intel_crtc->active) {
4896 /*
4897 * FIXME: DDI plls and relevant code isn't converted
4898 * yet, so do runtime PM for DPMS only for all other
4899 * platforms for now.
4900 */
4901 if (!HAS_DDI(dev)) {
4902 domains = get_crtc_power_domains(crtc);
4903 for_each_power_domain(domain, domains)
4904 intel_display_power_get(dev_priv, domain);
4905 intel_crtc->enabled_power_domains = domains;
4906 }
4907
4908 dev_priv->display.crtc_enable(crtc);
4909 }
4910 } else {
4911 if (intel_crtc->active) {
4912 dev_priv->display.crtc_disable(crtc);
4913
4914 if (!HAS_DDI(dev)) {
4915 domains = intel_crtc->enabled_power_domains;
4916 for_each_power_domain(domain, domains)
4917 intel_display_power_put(dev_priv, domain);
4918 intel_crtc->enabled_power_domains = 0;
4919 }
4920 }
4921 }
Daniel Vetter976f8a22012-07-08 22:34:21 +02004922
4923 intel_crtc_update_sarea(crtc, enable);
4924}
4925
Daniel Vetter976f8a22012-07-08 22:34:21 +02004926static void intel_crtc_disable(struct drm_crtc *crtc)
4927{
4928 struct drm_device *dev = crtc->dev;
4929 struct drm_connector *connector;
4930 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettera071fa02014-06-18 23:28:09 +02004931 struct drm_i915_gem_object *old_obj;
4932 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004933
4934 /* crtc should still be enabled when we disable it. */
4935 WARN_ON(!crtc->enabled);
4936
4937 dev_priv->display.crtc_disable(crtc);
4938 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004939 dev_priv->display.off(crtc);
4940
Chris Wilson931872f2012-01-16 23:01:13 +00004941 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Daniel Vettera071fa02014-06-18 23:28:09 +02004942 assert_cursor_disabled(dev_priv, pipe);
4943 assert_pipe_disabled(dev->dev_private, pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004944
Matt Roperf4510a22014-04-01 15:22:40 -07004945 if (crtc->primary->fb) {
Daniel Vettera071fa02014-06-18 23:28:09 +02004946 old_obj = to_intel_framebuffer(crtc->primary->fb)->obj;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004947 mutex_lock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +02004948 intel_unpin_fb_obj(old_obj);
4949 i915_gem_track_fb(old_obj, NULL,
4950 INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilsoncdd59982010-09-08 16:30:16 +01004951 mutex_unlock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07004952 crtc->primary->fb = NULL;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004953 }
4954
4955 /* Update computed state. */
4956 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4957 if (!connector->encoder || !connector->encoder->crtc)
4958 continue;
4959
4960 if (connector->encoder->crtc != crtc)
4961 continue;
4962
4963 connector->dpms = DRM_MODE_DPMS_OFF;
4964 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004965 }
4966}
4967
Chris Wilsonea5b2132010-08-04 13:50:23 +01004968void intel_encoder_destroy(struct drm_encoder *encoder)
4969{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004970 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004971
Chris Wilsonea5b2132010-08-04 13:50:23 +01004972 drm_encoder_cleanup(encoder);
4973 kfree(intel_encoder);
4974}
4975
Damien Lespiau92373292013-08-08 22:28:57 +01004976/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004977 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4978 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004979static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004980{
4981 if (mode == DRM_MODE_DPMS_ON) {
4982 encoder->connectors_active = true;
4983
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004984 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004985 } else {
4986 encoder->connectors_active = false;
4987
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004988 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004989 }
4990}
4991
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004992/* Cross check the actual hw state with our own modeset state tracking (and it's
4993 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004994static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004995{
4996 if (connector->get_hw_state(connector)) {
4997 struct intel_encoder *encoder = connector->encoder;
4998 struct drm_crtc *crtc;
4999 bool encoder_enabled;
5000 enum pipe pipe;
5001
5002 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5003 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005004 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005005
5006 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5007 "wrong connector dpms state\n");
5008 WARN(connector->base.encoder != &encoder->base,
5009 "active connector not linked to encoder\n");
5010 WARN(!encoder->connectors_active,
5011 "encoder->connectors_active not set\n");
5012
5013 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5014 WARN(!encoder_enabled, "encoder not enabled\n");
5015 if (WARN_ON(!encoder->base.crtc))
5016 return;
5017
5018 crtc = encoder->base.crtc;
5019
5020 WARN(!crtc->enabled, "crtc not enabled\n");
5021 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5022 WARN(pipe != to_intel_crtc(crtc)->pipe,
5023 "encoder active on the wrong pipe\n");
5024 }
5025}
5026
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005027/* Even simpler default implementation, if there's really no special case to
5028 * consider. */
5029void intel_connector_dpms(struct drm_connector *connector, int mode)
5030{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005031 /* All the simple cases only support two dpms states. */
5032 if (mode != DRM_MODE_DPMS_ON)
5033 mode = DRM_MODE_DPMS_OFF;
5034
5035 if (mode == connector->dpms)
5036 return;
5037
5038 connector->dpms = mode;
5039
5040 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01005041 if (connector->encoder)
5042 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005043
Daniel Vetterb9805142012-08-31 17:37:33 +02005044 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005045}
5046
Daniel Vetterf0947c32012-07-02 13:10:34 +02005047/* Simple connector->get_hw_state implementation for encoders that support only
5048 * one connector and no cloning and hence the encoder state determines the state
5049 * of the connector. */
5050bool intel_connector_get_hw_state(struct intel_connector *connector)
5051{
Daniel Vetter24929352012-07-02 20:28:59 +02005052 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005053 struct intel_encoder *encoder = connector->encoder;
5054
5055 return encoder->get_hw_state(encoder, &pipe);
5056}
5057
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005058static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5059 struct intel_crtc_config *pipe_config)
5060{
5061 struct drm_i915_private *dev_priv = dev->dev_private;
5062 struct intel_crtc *pipe_B_crtc =
5063 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5064
5065 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5066 pipe_name(pipe), pipe_config->fdi_lanes);
5067 if (pipe_config->fdi_lanes > 4) {
5068 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5069 pipe_name(pipe), pipe_config->fdi_lanes);
5070 return false;
5071 }
5072
Paulo Zanonibafb6552013-11-02 21:07:44 -07005073 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005074 if (pipe_config->fdi_lanes > 2) {
5075 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5076 pipe_config->fdi_lanes);
5077 return false;
5078 } else {
5079 return true;
5080 }
5081 }
5082
5083 if (INTEL_INFO(dev)->num_pipes == 2)
5084 return true;
5085
5086 /* Ivybridge 3 pipe is really complicated */
5087 switch (pipe) {
5088 case PIPE_A:
5089 return true;
5090 case PIPE_B:
5091 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5092 pipe_config->fdi_lanes > 2) {
5093 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5094 pipe_name(pipe), pipe_config->fdi_lanes);
5095 return false;
5096 }
5097 return true;
5098 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01005099 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005100 pipe_B_crtc->config.fdi_lanes <= 2) {
5101 if (pipe_config->fdi_lanes > 2) {
5102 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5103 pipe_name(pipe), pipe_config->fdi_lanes);
5104 return false;
5105 }
5106 } else {
5107 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5108 return false;
5109 }
5110 return true;
5111 default:
5112 BUG();
5113 }
5114}
5115
Daniel Vettere29c22c2013-02-21 00:00:16 +01005116#define RETRY 1
5117static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5118 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005119{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005120 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005121 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005122 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005123 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005124
Daniel Vettere29c22c2013-02-21 00:00:16 +01005125retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005126 /* FDI is a binary signal running at ~2.7GHz, encoding
5127 * each output octet as 10 bits. The actual frequency
5128 * is stored as a divider into a 100MHz clock, and the
5129 * mode pixel clock is stored in units of 1KHz.
5130 * Hence the bw of each lane in terms of the mode signal
5131 * is:
5132 */
5133 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5134
Damien Lespiau241bfc32013-09-25 16:45:37 +01005135 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005136
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005137 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005138 pipe_config->pipe_bpp);
5139
5140 pipe_config->fdi_lanes = lane;
5141
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005142 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005143 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005144
Daniel Vettere29c22c2013-02-21 00:00:16 +01005145 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5146 intel_crtc->pipe, pipe_config);
5147 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5148 pipe_config->pipe_bpp -= 2*3;
5149 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5150 pipe_config->pipe_bpp);
5151 needs_recompute = true;
5152 pipe_config->bw_constrained = true;
5153
5154 goto retry;
5155 }
5156
5157 if (needs_recompute)
5158 return RETRY;
5159
5160 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005161}
5162
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005163static void hsw_compute_ips_config(struct intel_crtc *crtc,
5164 struct intel_crtc_config *pipe_config)
5165{
Jani Nikulad330a952014-01-21 11:24:25 +02005166 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005167 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005168 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005169}
5170
Daniel Vettera43f6e02013-06-07 23:10:32 +02005171static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01005172 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005173{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005174 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005175 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005176
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005177 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005178 if (INTEL_INFO(dev)->gen < 4) {
5179 struct drm_i915_private *dev_priv = dev->dev_private;
5180 int clock_limit =
5181 dev_priv->display.get_display_clock_speed(dev);
5182
5183 /*
5184 * Enable pixel doubling when the dot clock
5185 * is > 90% of the (display) core speed.
5186 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005187 * GDG double wide on either pipe,
5188 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005189 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005190 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005191 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005192 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005193 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005194 }
5195
Damien Lespiau241bfc32013-09-25 16:45:37 +01005196 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005197 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005198 }
Chris Wilson89749352010-09-12 18:25:19 +01005199
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005200 /*
5201 * Pipe horizontal size must be even in:
5202 * - DVO ganged mode
5203 * - LVDS dual channel mode
5204 * - Double wide pipe
5205 */
5206 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5207 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5208 pipe_config->pipe_src_w &= ~1;
5209
Damien Lespiau8693a822013-05-03 18:48:11 +01005210 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5211 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005212 */
5213 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5214 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005215 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005216
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005217 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005218 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005219 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005220 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5221 * for lvds. */
5222 pipe_config->pipe_bpp = 8*3;
5223 }
5224
Damien Lespiauf5adf942013-06-24 18:29:34 +01005225 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005226 hsw_compute_ips_config(crtc, pipe_config);
5227
5228 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
5229 * clock survives for now. */
5230 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5231 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005232
Daniel Vetter877d48d2013-04-19 11:24:43 +02005233 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005234 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005235
Daniel Vettere29c22c2013-02-21 00:00:16 +01005236 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005237}
5238
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005239static int valleyview_get_display_clock_speed(struct drm_device *dev)
5240{
5241 return 400000; /* FIXME */
5242}
5243
Jesse Barnese70236a2009-09-21 10:42:27 -07005244static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005245{
Jesse Barnese70236a2009-09-21 10:42:27 -07005246 return 400000;
5247}
Jesse Barnes79e53942008-11-07 14:24:08 -08005248
Jesse Barnese70236a2009-09-21 10:42:27 -07005249static int i915_get_display_clock_speed(struct drm_device *dev)
5250{
5251 return 333000;
5252}
Jesse Barnes79e53942008-11-07 14:24:08 -08005253
Jesse Barnese70236a2009-09-21 10:42:27 -07005254static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5255{
5256 return 200000;
5257}
Jesse Barnes79e53942008-11-07 14:24:08 -08005258
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005259static int pnv_get_display_clock_speed(struct drm_device *dev)
5260{
5261 u16 gcfgc = 0;
5262
5263 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5264
5265 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5266 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5267 return 267000;
5268 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5269 return 333000;
5270 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5271 return 444000;
5272 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5273 return 200000;
5274 default:
5275 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5276 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5277 return 133000;
5278 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5279 return 167000;
5280 }
5281}
5282
Jesse Barnese70236a2009-09-21 10:42:27 -07005283static int i915gm_get_display_clock_speed(struct drm_device *dev)
5284{
5285 u16 gcfgc = 0;
5286
5287 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5288
5289 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005290 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005291 else {
5292 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5293 case GC_DISPLAY_CLOCK_333_MHZ:
5294 return 333000;
5295 default:
5296 case GC_DISPLAY_CLOCK_190_200_MHZ:
5297 return 190000;
5298 }
5299 }
5300}
Jesse Barnes79e53942008-11-07 14:24:08 -08005301
Jesse Barnese70236a2009-09-21 10:42:27 -07005302static int i865_get_display_clock_speed(struct drm_device *dev)
5303{
5304 return 266000;
5305}
5306
5307static int i855_get_display_clock_speed(struct drm_device *dev)
5308{
5309 u16 hpllcc = 0;
5310 /* Assume that the hardware is in the high speed state. This
5311 * should be the default.
5312 */
5313 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5314 case GC_CLOCK_133_200:
5315 case GC_CLOCK_100_200:
5316 return 200000;
5317 case GC_CLOCK_166_250:
5318 return 250000;
5319 case GC_CLOCK_100_133:
5320 return 133000;
5321 }
5322
5323 /* Shouldn't happen */
5324 return 0;
5325}
5326
5327static int i830_get_display_clock_speed(struct drm_device *dev)
5328{
5329 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005330}
5331
Zhenyu Wang2c072452009-06-05 15:38:42 +08005332static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005333intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005334{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005335 while (*num > DATA_LINK_M_N_MASK ||
5336 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005337 *num >>= 1;
5338 *den >>= 1;
5339 }
5340}
5341
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005342static void compute_m_n(unsigned int m, unsigned int n,
5343 uint32_t *ret_m, uint32_t *ret_n)
5344{
5345 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5346 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5347 intel_reduce_m_n_ratio(ret_m, ret_n);
5348}
5349
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005350void
5351intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5352 int pixel_clock, int link_clock,
5353 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005354{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005355 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005356
5357 compute_m_n(bits_per_pixel * pixel_clock,
5358 link_clock * nlanes * 8,
5359 &m_n->gmch_m, &m_n->gmch_n);
5360
5361 compute_m_n(pixel_clock, link_clock,
5362 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005363}
5364
Chris Wilsona7615032011-01-12 17:04:08 +00005365static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5366{
Jani Nikulad330a952014-01-21 11:24:25 +02005367 if (i915.panel_use_ssc >= 0)
5368 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005369 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005370 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005371}
5372
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005373static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5374{
5375 struct drm_device *dev = crtc->dev;
5376 struct drm_i915_private *dev_priv = dev->dev_private;
5377 int refclk;
5378
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005379 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005380 refclk = 100000;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005381 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005382 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005383 refclk = dev_priv->vbt.lvds_ssc_freq;
5384 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005385 } else if (!IS_GEN2(dev)) {
5386 refclk = 96000;
5387 } else {
5388 refclk = 48000;
5389 }
5390
5391 return refclk;
5392}
5393
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005394static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005395{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005396 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005397}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005398
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005399static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5400{
5401 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005402}
5403
Daniel Vetterf47709a2013-03-28 10:42:02 +01005404static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005405 intel_clock_t *reduced_clock)
5406{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005407 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005408 u32 fp, fp2 = 0;
5409
5410 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005411 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005412 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005413 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005414 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005415 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005416 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005417 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005418 }
5419
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005420 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005421
Daniel Vetterf47709a2013-03-28 10:42:02 +01005422 crtc->lowfreq_avail = false;
5423 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005424 reduced_clock && i915.powersave) {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005425 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005426 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005427 } else {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005428 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005429 }
5430}
5431
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005432static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5433 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005434{
5435 u32 reg_val;
5436
5437 /*
5438 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5439 * and set it to a reasonable value instead.
5440 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005441 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005442 reg_val &= 0xffffff00;
5443 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005444 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005445
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005446 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005447 reg_val &= 0x8cffffff;
5448 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005449 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005450
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005451 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005452 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005453 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005454
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005455 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005456 reg_val &= 0x00ffffff;
5457 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005458 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005459}
5460
Daniel Vetterb5518422013-05-03 11:49:48 +02005461static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5462 struct intel_link_m_n *m_n)
5463{
5464 struct drm_device *dev = crtc->base.dev;
5465 struct drm_i915_private *dev_priv = dev->dev_private;
5466 int pipe = crtc->pipe;
5467
Daniel Vettere3b95f12013-05-03 11:49:49 +02005468 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5469 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5470 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5471 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005472}
5473
5474static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5475 struct intel_link_m_n *m_n)
5476{
5477 struct drm_device *dev = crtc->base.dev;
5478 struct drm_i915_private *dev_priv = dev->dev_private;
5479 int pipe = crtc->pipe;
5480 enum transcoder transcoder = crtc->config.cpu_transcoder;
5481
5482 if (INTEL_INFO(dev)->gen >= 5) {
5483 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5484 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5485 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5486 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5487 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02005488 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5489 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5490 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5491 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005492 }
5493}
5494
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005495static void intel_dp_set_m_n(struct intel_crtc *crtc)
5496{
5497 if (crtc->config.has_pch_encoder)
5498 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5499 else
5500 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5501}
5502
Daniel Vetterf47709a2013-03-28 10:42:02 +01005503static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005504{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005505 u32 dpll, dpll_md;
5506
5507 /*
5508 * Enable DPIO clock input. We should never disable the reference
5509 * clock for pipe B, since VGA hotplug / manual detection depends
5510 * on it.
5511 */
5512 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5513 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5514 /* We should never disable this, set it here for state tracking */
5515 if (crtc->pipe == PIPE_B)
5516 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5517 dpll |= DPLL_VCO_ENABLE;
5518 crtc->config.dpll_hw_state.dpll = dpll;
5519
5520 dpll_md = (crtc->config.pixel_multiplier - 1)
5521 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5522 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5523}
5524
5525static void vlv_prepare_pll(struct intel_crtc *crtc)
5526{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005527 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005528 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005529 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005530 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005531 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005532 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005533
Daniel Vetter09153002012-12-12 14:06:44 +01005534 mutex_lock(&dev_priv->dpio_lock);
5535
Daniel Vetterf47709a2013-03-28 10:42:02 +01005536 bestn = crtc->config.dpll.n;
5537 bestm1 = crtc->config.dpll.m1;
5538 bestm2 = crtc->config.dpll.m2;
5539 bestp1 = crtc->config.dpll.p1;
5540 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005541
Jesse Barnes89b667f2013-04-18 14:51:36 -07005542 /* See eDP HDMI DPIO driver vbios notes doc */
5543
5544 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005545 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005546 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005547
5548 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005549 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005550
5551 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005552 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005553 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005554 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005555
5556 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005557 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005558
5559 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005560 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5561 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5562 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005563 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07005564
5565 /*
5566 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5567 * but we don't support that).
5568 * Note: don't use the DAC post divider as it seems unstable.
5569 */
5570 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005571 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005572
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005573 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005574 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005575
Jesse Barnes89b667f2013-04-18 14:51:36 -07005576 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02005577 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03005578 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07005579 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005580 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03005581 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005582 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005583 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005584 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005585
Jesse Barnes89b667f2013-04-18 14:51:36 -07005586 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5587 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5588 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005589 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005590 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005591 0x0df40000);
5592 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005593 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005594 0x0df70000);
5595 } else { /* HDMI or VGA */
5596 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005597 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005598 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005599 0x0df70000);
5600 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005601 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005602 0x0df40000);
5603 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005604
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005605 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005606 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5607 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5608 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5609 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005610 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005611
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005612 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01005613 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005614}
5615
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005616static void chv_update_pll(struct intel_crtc *crtc)
5617{
5618 struct drm_device *dev = crtc->base.dev;
5619 struct drm_i915_private *dev_priv = dev->dev_private;
5620 int pipe = crtc->pipe;
5621 int dpll_reg = DPLL(crtc->pipe);
5622 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ville Syrjälä580d3812014-04-09 13:29:00 +03005623 u32 loopfilter, intcoeff;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005624 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5625 int refclk;
5626
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005627 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5628 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5629 DPLL_VCO_ENABLE;
5630 if (pipe != PIPE_A)
5631 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5632
5633 crtc->config.dpll_hw_state.dpll_md =
5634 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005635
5636 bestn = crtc->config.dpll.n;
5637 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5638 bestm1 = crtc->config.dpll.m1;
5639 bestm2 = crtc->config.dpll.m2 >> 22;
5640 bestp1 = crtc->config.dpll.p1;
5641 bestp2 = crtc->config.dpll.p2;
5642
5643 /*
5644 * Enable Refclk and SSC
5645 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005646 I915_WRITE(dpll_reg,
5647 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5648
5649 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005650
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005651 /* p1 and p2 divider */
5652 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5653 5 << DPIO_CHV_S1_DIV_SHIFT |
5654 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5655 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5656 1 << DPIO_CHV_K_DIV_SHIFT);
5657
5658 /* Feedback post-divider - m2 */
5659 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5660
5661 /* Feedback refclk divider - n and m1 */
5662 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5663 DPIO_CHV_M1_DIV_BY_2 |
5664 1 << DPIO_CHV_N_DIV_SHIFT);
5665
5666 /* M2 fraction division */
5667 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5668
5669 /* M2 fraction division enable */
5670 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5671 DPIO_CHV_FRAC_DIV_EN |
5672 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5673
5674 /* Loop filter */
5675 refclk = i9xx_get_refclk(&crtc->base, 0);
5676 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5677 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5678 if (refclk == 100000)
5679 intcoeff = 11;
5680 else if (refclk == 38400)
5681 intcoeff = 10;
5682 else
5683 intcoeff = 9;
5684 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5685 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5686
5687 /* AFC Recal */
5688 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5689 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5690 DPIO_AFC_RECAL);
5691
5692 mutex_unlock(&dev_priv->dpio_lock);
5693}
5694
Daniel Vetterf47709a2013-03-28 10:42:02 +01005695static void i9xx_update_pll(struct intel_crtc *crtc,
5696 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005697 int num_connectors)
5698{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005699 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005700 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005701 u32 dpll;
5702 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005703 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005704
Daniel Vetterf47709a2013-03-28 10:42:02 +01005705 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305706
Daniel Vetterf47709a2013-03-28 10:42:02 +01005707 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5708 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005709
5710 dpll = DPLL_VGA_MODE_DIS;
5711
Daniel Vetterf47709a2013-03-28 10:42:02 +01005712 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005713 dpll |= DPLLB_MODE_LVDS;
5714 else
5715 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005716
Daniel Vetteref1b4602013-06-01 17:17:04 +02005717 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005718 dpll |= (crtc->config.pixel_multiplier - 1)
5719 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005720 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005721
5722 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005723 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005724
Daniel Vetterf47709a2013-03-28 10:42:02 +01005725 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005726 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005727
5728 /* compute bitmask from p1 value */
5729 if (IS_PINEVIEW(dev))
5730 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5731 else {
5732 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5733 if (IS_G4X(dev) && reduced_clock)
5734 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5735 }
5736 switch (clock->p2) {
5737 case 5:
5738 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5739 break;
5740 case 7:
5741 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5742 break;
5743 case 10:
5744 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5745 break;
5746 case 14:
5747 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5748 break;
5749 }
5750 if (INTEL_INFO(dev)->gen >= 4)
5751 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5752
Daniel Vetter09ede542013-04-30 14:01:45 +02005753 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005754 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005755 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005756 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5757 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5758 else
5759 dpll |= PLL_REF_INPUT_DREFCLK;
5760
5761 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005762 crtc->config.dpll_hw_state.dpll = dpll;
5763
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005764 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005765 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5766 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005767 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005768 }
5769}
5770
Daniel Vetterf47709a2013-03-28 10:42:02 +01005771static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005772 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005773 int num_connectors)
5774{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005775 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005776 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005777 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005778 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005779
Daniel Vetterf47709a2013-03-28 10:42:02 +01005780 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305781
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005782 dpll = DPLL_VGA_MODE_DIS;
5783
Daniel Vetterf47709a2013-03-28 10:42:02 +01005784 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005785 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5786 } else {
5787 if (clock->p1 == 2)
5788 dpll |= PLL_P1_DIVIDE_BY_TWO;
5789 else
5790 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5791 if (clock->p2 == 4)
5792 dpll |= PLL_P2_DIVIDE_BY_4;
5793 }
5794
Daniel Vetter4a33e482013-07-06 12:52:05 +02005795 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5796 dpll |= DPLL_DVO_2X_MODE;
5797
Daniel Vetterf47709a2013-03-28 10:42:02 +01005798 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005799 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5800 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5801 else
5802 dpll |= PLL_REF_INPUT_DREFCLK;
5803
5804 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005805 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005806}
5807
Daniel Vetter8a654f32013-06-01 17:16:22 +02005808static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005809{
5810 struct drm_device *dev = intel_crtc->base.dev;
5811 struct drm_i915_private *dev_priv = dev->dev_private;
5812 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005813 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005814 struct drm_display_mode *adjusted_mode =
5815 &intel_crtc->config.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005816 uint32_t crtc_vtotal, crtc_vblank_end;
5817 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005818
5819 /* We need to be careful not to changed the adjusted mode, for otherwise
5820 * the hw state checker will get angry at the mismatch. */
5821 crtc_vtotal = adjusted_mode->crtc_vtotal;
5822 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005823
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005824 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005825 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005826 crtc_vtotal -= 1;
5827 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005828
5829 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5830 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5831 else
5832 vsyncshift = adjusted_mode->crtc_hsync_start -
5833 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005834 if (vsyncshift < 0)
5835 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005836 }
5837
5838 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005839 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005840
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005841 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005842 (adjusted_mode->crtc_hdisplay - 1) |
5843 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005844 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005845 (adjusted_mode->crtc_hblank_start - 1) |
5846 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005847 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005848 (adjusted_mode->crtc_hsync_start - 1) |
5849 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5850
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005851 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005852 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005853 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005854 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005855 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005856 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005857 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005858 (adjusted_mode->crtc_vsync_start - 1) |
5859 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5860
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005861 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5862 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5863 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5864 * bits. */
5865 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5866 (pipe == PIPE_B || pipe == PIPE_C))
5867 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5868
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005869 /* pipesrc controls the size that is scaled from, which should
5870 * always be the user's requested size.
5871 */
5872 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005873 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5874 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005875}
5876
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005877static void intel_get_pipe_timings(struct intel_crtc *crtc,
5878 struct intel_crtc_config *pipe_config)
5879{
5880 struct drm_device *dev = crtc->base.dev;
5881 struct drm_i915_private *dev_priv = dev->dev_private;
5882 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5883 uint32_t tmp;
5884
5885 tmp = I915_READ(HTOTAL(cpu_transcoder));
5886 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5887 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5888 tmp = I915_READ(HBLANK(cpu_transcoder));
5889 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5890 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5891 tmp = I915_READ(HSYNC(cpu_transcoder));
5892 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5893 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5894
5895 tmp = I915_READ(VTOTAL(cpu_transcoder));
5896 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5897 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5898 tmp = I915_READ(VBLANK(cpu_transcoder));
5899 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5900 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5901 tmp = I915_READ(VSYNC(cpu_transcoder));
5902 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5903 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5904
5905 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5906 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5907 pipe_config->adjusted_mode.crtc_vtotal += 1;
5908 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5909 }
5910
5911 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005912 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5913 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5914
5915 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5916 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005917}
5918
Daniel Vetterf6a83282014-02-11 15:28:57 -08005919void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5920 struct intel_crtc_config *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03005921{
Daniel Vetterf6a83282014-02-11 15:28:57 -08005922 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5923 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
5924 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5925 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03005926
Daniel Vetterf6a83282014-02-11 15:28:57 -08005927 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5928 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5929 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5930 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03005931
Daniel Vetterf6a83282014-02-11 15:28:57 -08005932 mode->flags = pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03005933
Daniel Vetterf6a83282014-02-11 15:28:57 -08005934 mode->clock = pipe_config->adjusted_mode.crtc_clock;
5935 mode->flags |= pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03005936}
5937
Daniel Vetter84b046f2013-02-19 18:48:54 +01005938static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5939{
5940 struct drm_device *dev = intel_crtc->base.dev;
5941 struct drm_i915_private *dev_priv = dev->dev_private;
5942 uint32_t pipeconf;
5943
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005944 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005945
Daniel Vetter67c72a12013-09-24 11:46:14 +02005946 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5947 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5948 pipeconf |= PIPECONF_ENABLE;
5949
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005950 if (intel_crtc->config.double_wide)
5951 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005952
Daniel Vetterff9ce462013-04-24 14:57:17 +02005953 /* only g4x and later have fancy bpc/dither controls */
5954 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005955 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5956 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5957 pipeconf |= PIPECONF_DITHER_EN |
5958 PIPECONF_DITHER_TYPE_SP;
5959
5960 switch (intel_crtc->config.pipe_bpp) {
5961 case 18:
5962 pipeconf |= PIPECONF_6BPC;
5963 break;
5964 case 24:
5965 pipeconf |= PIPECONF_8BPC;
5966 break;
5967 case 30:
5968 pipeconf |= PIPECONF_10BPC;
5969 break;
5970 default:
5971 /* Case prevented by intel_choose_pipe_bpp_dither. */
5972 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005973 }
5974 }
5975
5976 if (HAS_PIPE_CXSR(dev)) {
5977 if (intel_crtc->lowfreq_avail) {
5978 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5979 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5980 } else {
5981 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005982 }
5983 }
5984
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02005985 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
5986 if (INTEL_INFO(dev)->gen < 4 ||
5987 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5988 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5989 else
5990 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
5991 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01005992 pipeconf |= PIPECONF_PROGRESSIVE;
5993
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005994 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5995 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005996
Daniel Vetter84b046f2013-02-19 18:48:54 +01005997 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5998 POSTING_READ(PIPECONF(intel_crtc->pipe));
5999}
6000
Eric Anholtf564048e2011-03-30 13:01:02 -07006001static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07006002 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006003 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006004{
6005 struct drm_device *dev = crtc->dev;
6006 struct drm_i915_private *dev_priv = dev->dev_private;
6007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtc751ce42010-03-25 11:48:48 -07006008 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006009 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02006010 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006011 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006012 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006013 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08006014
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02006015 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01006016 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006017 case INTEL_OUTPUT_LVDS:
6018 is_lvds = true;
6019 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006020 case INTEL_OUTPUT_DSI:
6021 is_dsi = true;
6022 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006023 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006024
Eric Anholtc751ce42010-03-25 11:48:48 -07006025 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006026 }
6027
Jani Nikulaf2335332013-09-13 11:03:09 +03006028 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006029 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006030
Jani Nikulaf2335332013-09-13 11:03:09 +03006031 if (!intel_crtc->config.clock_set) {
6032 refclk = i9xx_get_refclk(crtc, num_connectors);
6033
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006034 /*
6035 * Returns a set of divisors for the desired target clock with
6036 * the given refclk, or FALSE. The returned values represent
6037 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6038 * 2) / p1 / p2.
6039 */
6040 limit = intel_limit(crtc, refclk);
6041 ok = dev_priv->display.find_dpll(limit, crtc,
6042 intel_crtc->config.port_clock,
6043 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006044 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006045 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6046 return -EINVAL;
6047 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006048
Jani Nikulaf2335332013-09-13 11:03:09 +03006049 if (is_lvds && dev_priv->lvds_downclock_avail) {
6050 /*
6051 * Ensure we match the reduced clock's P to the target
6052 * clock. If the clocks don't match, we can't switch
6053 * the display clock by using the FP0/FP1. In such case
6054 * we will disable the LVDS downclock feature.
6055 */
6056 has_reduced_clock =
6057 dev_priv->display.find_dpll(limit, crtc,
6058 dev_priv->lvds_downclock,
6059 refclk, &clock,
6060 &reduced_clock);
6061 }
6062 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01006063 intel_crtc->config.dpll.n = clock.n;
6064 intel_crtc->config.dpll.m1 = clock.m1;
6065 intel_crtc->config.dpll.m2 = clock.m2;
6066 intel_crtc->config.dpll.p1 = clock.p1;
6067 intel_crtc->config.dpll.p2 = clock.p2;
6068 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006069
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006070 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02006071 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306072 has_reduced_clock ? &reduced_clock : NULL,
6073 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006074 } else if (IS_CHERRYVIEW(dev)) {
6075 chv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006076 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03006077 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006078 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01006079 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006080 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006081 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006082 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006083
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006084 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006085}
6086
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006087static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6088 struct intel_crtc_config *pipe_config)
6089{
6090 struct drm_device *dev = crtc->base.dev;
6091 struct drm_i915_private *dev_priv = dev->dev_private;
6092 uint32_t tmp;
6093
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006094 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6095 return;
6096
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006097 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006098 if (!(tmp & PFIT_ENABLE))
6099 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006100
Daniel Vetter06922822013-07-11 13:35:40 +02006101 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006102 if (INTEL_INFO(dev)->gen < 4) {
6103 if (crtc->pipe != PIPE_B)
6104 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006105 } else {
6106 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6107 return;
6108 }
6109
Daniel Vetter06922822013-07-11 13:35:40 +02006110 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006111 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6112 if (INTEL_INFO(dev)->gen < 5)
6113 pipe_config->gmch_pfit.lvds_border_bits =
6114 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6115}
6116
Jesse Barnesacbec812013-09-20 11:29:32 -07006117static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6118 struct intel_crtc_config *pipe_config)
6119{
6120 struct drm_device *dev = crtc->base.dev;
6121 struct drm_i915_private *dev_priv = dev->dev_private;
6122 int pipe = pipe_config->cpu_transcoder;
6123 intel_clock_t clock;
6124 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006125 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006126
6127 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006128 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006129 mutex_unlock(&dev_priv->dpio_lock);
6130
6131 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6132 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6133 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6134 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6135 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6136
Ville Syrjäläf6466282013-10-14 14:50:31 +03006137 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006138
Ville Syrjäläf6466282013-10-14 14:50:31 +03006139 /* clock.dot is the fast clock */
6140 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006141}
6142
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006143static void i9xx_get_plane_config(struct intel_crtc *crtc,
6144 struct intel_plane_config *plane_config)
6145{
6146 struct drm_device *dev = crtc->base.dev;
6147 struct drm_i915_private *dev_priv = dev->dev_private;
6148 u32 val, base, offset;
6149 int pipe = crtc->pipe, plane = crtc->plane;
6150 int fourcc, pixel_format;
6151 int aligned_height;
6152
Dave Airlie66e514c2014-04-03 07:51:54 +10006153 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6154 if (!crtc->base.primary->fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006155 DRM_DEBUG_KMS("failed to alloc fb\n");
6156 return;
6157 }
6158
6159 val = I915_READ(DSPCNTR(plane));
6160
6161 if (INTEL_INFO(dev)->gen >= 4)
6162 if (val & DISPPLANE_TILED)
6163 plane_config->tiled = true;
6164
6165 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6166 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10006167 crtc->base.primary->fb->pixel_format = fourcc;
6168 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006169 drm_format_plane_cpp(fourcc, 0) * 8;
6170
6171 if (INTEL_INFO(dev)->gen >= 4) {
6172 if (plane_config->tiled)
6173 offset = I915_READ(DSPTILEOFF(plane));
6174 else
6175 offset = I915_READ(DSPLINOFF(plane));
6176 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6177 } else {
6178 base = I915_READ(DSPADDR(plane));
6179 }
6180 plane_config->base = base;
6181
6182 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006183 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6184 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006185
6186 val = I915_READ(DSPSTRIDE(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006187 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006188
Dave Airlie66e514c2014-04-03 07:51:54 +10006189 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006190 plane_config->tiled);
6191
Dave Airlie66e514c2014-04-03 07:51:54 +10006192 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006193 aligned_height, PAGE_SIZE);
6194
6195 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 +10006196 pipe, plane, crtc->base.primary->fb->width,
6197 crtc->base.primary->fb->height,
6198 crtc->base.primary->fb->bits_per_pixel, base,
6199 crtc->base.primary->fb->pitches[0],
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006200 plane_config->size);
6201
6202}
6203
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006204static void chv_crtc_clock_get(struct intel_crtc *crtc,
6205 struct intel_crtc_config *pipe_config)
6206{
6207 struct drm_device *dev = crtc->base.dev;
6208 struct drm_i915_private *dev_priv = dev->dev_private;
6209 int pipe = pipe_config->cpu_transcoder;
6210 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6211 intel_clock_t clock;
6212 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6213 int refclk = 100000;
6214
6215 mutex_lock(&dev_priv->dpio_lock);
6216 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6217 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6218 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6219 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6220 mutex_unlock(&dev_priv->dpio_lock);
6221
6222 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6223 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6224 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6225 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6226 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6227
6228 chv_clock(refclk, &clock);
6229
6230 /* clock.dot is the fast clock */
6231 pipe_config->port_clock = clock.dot / 5;
6232}
6233
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006234static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6235 struct intel_crtc_config *pipe_config)
6236{
6237 struct drm_device *dev = crtc->base.dev;
6238 struct drm_i915_private *dev_priv = dev->dev_private;
6239 uint32_t tmp;
6240
Imre Deakb5482bd2014-03-05 16:20:55 +02006241 if (!intel_display_power_enabled(dev_priv,
6242 POWER_DOMAIN_PIPE(crtc->pipe)))
6243 return false;
6244
Daniel Vettere143a212013-07-04 12:01:15 +02006245 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006246 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006247
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006248 tmp = I915_READ(PIPECONF(crtc->pipe));
6249 if (!(tmp & PIPECONF_ENABLE))
6250 return false;
6251
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006252 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6253 switch (tmp & PIPECONF_BPC_MASK) {
6254 case PIPECONF_6BPC:
6255 pipe_config->pipe_bpp = 18;
6256 break;
6257 case PIPECONF_8BPC:
6258 pipe_config->pipe_bpp = 24;
6259 break;
6260 case PIPECONF_10BPC:
6261 pipe_config->pipe_bpp = 30;
6262 break;
6263 default:
6264 break;
6265 }
6266 }
6267
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02006268 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6269 pipe_config->limited_color_range = true;
6270
Ville Syrjälä282740f2013-09-04 18:30:03 +03006271 if (INTEL_INFO(dev)->gen < 4)
6272 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6273
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006274 intel_get_pipe_timings(crtc, pipe_config);
6275
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006276 i9xx_get_pfit_config(crtc, pipe_config);
6277
Daniel Vetter6c49f242013-06-06 12:45:25 +02006278 if (INTEL_INFO(dev)->gen >= 4) {
6279 tmp = I915_READ(DPLL_MD(crtc->pipe));
6280 pipe_config->pixel_multiplier =
6281 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6282 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006283 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02006284 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6285 tmp = I915_READ(DPLL(crtc->pipe));
6286 pipe_config->pixel_multiplier =
6287 ((tmp & SDVO_MULTIPLIER_MASK)
6288 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6289 } else {
6290 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6291 * port and will be fixed up in the encoder->get_config
6292 * function. */
6293 pipe_config->pixel_multiplier = 1;
6294 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006295 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6296 if (!IS_VALLEYVIEW(dev)) {
6297 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6298 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03006299 } else {
6300 /* Mask out read-only status bits. */
6301 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6302 DPLL_PORTC_READY_MASK |
6303 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006304 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02006305
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006306 if (IS_CHERRYVIEW(dev))
6307 chv_crtc_clock_get(crtc, pipe_config);
6308 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07006309 vlv_crtc_clock_get(crtc, pipe_config);
6310 else
6311 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03006312
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006313 return true;
6314}
6315
Paulo Zanonidde86e22012-12-01 12:04:25 -02006316static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07006317{
6318 struct drm_i915_private *dev_priv = dev->dev_private;
6319 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006320 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006321 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006322 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006323 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006324 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07006325 bool has_ck505 = false;
6326 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006327
6328 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07006329 list_for_each_entry(encoder, &mode_config->encoder_list,
6330 base.head) {
6331 switch (encoder->type) {
6332 case INTEL_OUTPUT_LVDS:
6333 has_panel = true;
6334 has_lvds = true;
6335 break;
6336 case INTEL_OUTPUT_EDP:
6337 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03006338 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07006339 has_cpu_edp = true;
6340 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006341 }
6342 }
6343
Keith Packard99eb6a02011-09-26 14:29:12 -07006344 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006345 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07006346 can_ssc = has_ck505;
6347 } else {
6348 has_ck505 = false;
6349 can_ssc = true;
6350 }
6351
Imre Deak2de69052013-05-08 13:14:04 +03006352 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6353 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006354
6355 /* Ironlake: try to setup display ref clock before DPLL
6356 * enabling. This is only under driver's control after
6357 * PCH B stepping, previous chipset stepping should be
6358 * ignoring this setting.
6359 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006360 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006361
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006362 /* As we must carefully and slowly disable/enable each source in turn,
6363 * compute the final state we want first and check if we need to
6364 * make any changes at all.
6365 */
6366 final = val;
6367 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07006368 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006369 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07006370 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006371 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6372
6373 final &= ~DREF_SSC_SOURCE_MASK;
6374 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6375 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006376
Keith Packard199e5d72011-09-22 12:01:57 -07006377 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006378 final |= DREF_SSC_SOURCE_ENABLE;
6379
6380 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6381 final |= DREF_SSC1_ENABLE;
6382
6383 if (has_cpu_edp) {
6384 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6385 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6386 else
6387 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6388 } else
6389 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6390 } else {
6391 final |= DREF_SSC_SOURCE_DISABLE;
6392 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6393 }
6394
6395 if (final == val)
6396 return;
6397
6398 /* Always enable nonspread source */
6399 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6400
6401 if (has_ck505)
6402 val |= DREF_NONSPREAD_CK505_ENABLE;
6403 else
6404 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6405
6406 if (has_panel) {
6407 val &= ~DREF_SSC_SOURCE_MASK;
6408 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006409
Keith Packard199e5d72011-09-22 12:01:57 -07006410 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07006411 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006412 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006413 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02006414 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006415 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006416
6417 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006418 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006419 POSTING_READ(PCH_DREF_CONTROL);
6420 udelay(200);
6421
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006422 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006423
6424 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07006425 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07006426 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006427 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006428 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02006429 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006430 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07006431 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006432 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006433
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006434 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006435 POSTING_READ(PCH_DREF_CONTROL);
6436 udelay(200);
6437 } else {
6438 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6439
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006440 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07006441
6442 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006443 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006444
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006445 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006446 POSTING_READ(PCH_DREF_CONTROL);
6447 udelay(200);
6448
6449 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006450 val &= ~DREF_SSC_SOURCE_MASK;
6451 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006452
6453 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006454 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006455
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006456 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006457 POSTING_READ(PCH_DREF_CONTROL);
6458 udelay(200);
6459 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006460
6461 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006462}
6463
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006464static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02006465{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006466 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006467
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006468 tmp = I915_READ(SOUTH_CHICKEN2);
6469 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6470 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006471
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006472 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6473 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6474 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02006475
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006476 tmp = I915_READ(SOUTH_CHICKEN2);
6477 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6478 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006479
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006480 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6481 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6482 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006483}
6484
6485/* WaMPhyProgramming:hsw */
6486static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6487{
6488 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006489
6490 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6491 tmp &= ~(0xFF << 24);
6492 tmp |= (0x12 << 24);
6493 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6494
Paulo Zanonidde86e22012-12-01 12:04:25 -02006495 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6496 tmp |= (1 << 11);
6497 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6498
6499 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6500 tmp |= (1 << 11);
6501 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6502
Paulo Zanonidde86e22012-12-01 12:04:25 -02006503 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6504 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6505 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6506
6507 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6508 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6509 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6510
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006511 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6512 tmp &= ~(7 << 13);
6513 tmp |= (5 << 13);
6514 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006515
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006516 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6517 tmp &= ~(7 << 13);
6518 tmp |= (5 << 13);
6519 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006520
6521 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6522 tmp &= ~0xFF;
6523 tmp |= 0x1C;
6524 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6525
6526 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6527 tmp &= ~0xFF;
6528 tmp |= 0x1C;
6529 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6530
6531 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6532 tmp &= ~(0xFF << 16);
6533 tmp |= (0x1C << 16);
6534 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6535
6536 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6537 tmp &= ~(0xFF << 16);
6538 tmp |= (0x1C << 16);
6539 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6540
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006541 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6542 tmp |= (1 << 27);
6543 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006544
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006545 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6546 tmp |= (1 << 27);
6547 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006548
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006549 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6550 tmp &= ~(0xF << 28);
6551 tmp |= (4 << 28);
6552 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006553
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006554 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6555 tmp &= ~(0xF << 28);
6556 tmp |= (4 << 28);
6557 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006558}
6559
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006560/* Implements 3 different sequences from BSpec chapter "Display iCLK
6561 * Programming" based on the parameters passed:
6562 * - Sequence to enable CLKOUT_DP
6563 * - Sequence to enable CLKOUT_DP without spread
6564 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6565 */
6566static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6567 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006568{
6569 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006570 uint32_t reg, tmp;
6571
6572 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6573 with_spread = true;
6574 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6575 with_fdi, "LP PCH doesn't have FDI\n"))
6576 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006577
6578 mutex_lock(&dev_priv->dpio_lock);
6579
6580 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6581 tmp &= ~SBI_SSCCTL_DISABLE;
6582 tmp |= SBI_SSCCTL_PATHALT;
6583 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6584
6585 udelay(24);
6586
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006587 if (with_spread) {
6588 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6589 tmp &= ~SBI_SSCCTL_PATHALT;
6590 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006591
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006592 if (with_fdi) {
6593 lpt_reset_fdi_mphy(dev_priv);
6594 lpt_program_fdi_mphy(dev_priv);
6595 }
6596 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02006597
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006598 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6599 SBI_GEN0 : SBI_DBUFF0;
6600 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6601 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6602 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01006603
6604 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006605}
6606
Paulo Zanoni47701c32013-07-23 11:19:25 -03006607/* Sequence to disable CLKOUT_DP */
6608static void lpt_disable_clkout_dp(struct drm_device *dev)
6609{
6610 struct drm_i915_private *dev_priv = dev->dev_private;
6611 uint32_t reg, tmp;
6612
6613 mutex_lock(&dev_priv->dpio_lock);
6614
6615 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6616 SBI_GEN0 : SBI_DBUFF0;
6617 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6618 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6619 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6620
6621 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6622 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6623 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6624 tmp |= SBI_SSCCTL_PATHALT;
6625 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6626 udelay(32);
6627 }
6628 tmp |= SBI_SSCCTL_DISABLE;
6629 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6630 }
6631
6632 mutex_unlock(&dev_priv->dpio_lock);
6633}
6634
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006635static void lpt_init_pch_refclk(struct drm_device *dev)
6636{
6637 struct drm_mode_config *mode_config = &dev->mode_config;
6638 struct intel_encoder *encoder;
6639 bool has_vga = false;
6640
6641 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
6642 switch (encoder->type) {
6643 case INTEL_OUTPUT_ANALOG:
6644 has_vga = true;
6645 break;
6646 }
6647 }
6648
Paulo Zanoni47701c32013-07-23 11:19:25 -03006649 if (has_vga)
6650 lpt_enable_clkout_dp(dev, true, true);
6651 else
6652 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006653}
6654
Paulo Zanonidde86e22012-12-01 12:04:25 -02006655/*
6656 * Initialize reference clocks when the driver loads
6657 */
6658void intel_init_pch_refclk(struct drm_device *dev)
6659{
6660 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6661 ironlake_init_pch_refclk(dev);
6662 else if (HAS_PCH_LPT(dev))
6663 lpt_init_pch_refclk(dev);
6664}
6665
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006666static int ironlake_get_refclk(struct drm_crtc *crtc)
6667{
6668 struct drm_device *dev = crtc->dev;
6669 struct drm_i915_private *dev_priv = dev->dev_private;
6670 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006671 int num_connectors = 0;
6672 bool is_lvds = false;
6673
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02006674 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006675 switch (encoder->type) {
6676 case INTEL_OUTPUT_LVDS:
6677 is_lvds = true;
6678 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006679 }
6680 num_connectors++;
6681 }
6682
6683 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006684 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006685 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006686 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006687 }
6688
6689 return 120000;
6690}
6691
Daniel Vetter6ff93602013-04-19 11:24:36 +02006692static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03006693{
6694 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
6695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6696 int pipe = intel_crtc->pipe;
6697 uint32_t val;
6698
Daniel Vetter78114072013-06-13 00:54:57 +02006699 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03006700
Daniel Vetter965e0c42013-03-27 00:44:57 +01006701 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03006702 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006703 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006704 break;
6705 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006706 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006707 break;
6708 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006709 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006710 break;
6711 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006712 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006713 break;
6714 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03006715 /* Case prevented by intel_choose_pipe_bpp_dither. */
6716 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03006717 }
6718
Daniel Vetterd8b32242013-04-25 17:54:44 +02006719 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03006720 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6721
Daniel Vetter6ff93602013-04-19 11:24:36 +02006722 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03006723 val |= PIPECONF_INTERLACED_ILK;
6724 else
6725 val |= PIPECONF_PROGRESSIVE;
6726
Daniel Vetter50f3b012013-03-27 00:44:56 +01006727 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006728 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006729
Paulo Zanonic8203562012-09-12 10:06:29 -03006730 I915_WRITE(PIPECONF(pipe), val);
6731 POSTING_READ(PIPECONF(pipe));
6732}
6733
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006734/*
6735 * Set up the pipe CSC unit.
6736 *
6737 * Currently only full range RGB to limited range RGB conversion
6738 * is supported, but eventually this should handle various
6739 * RGB<->YCbCr scenarios as well.
6740 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01006741static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006742{
6743 struct drm_device *dev = crtc->dev;
6744 struct drm_i915_private *dev_priv = dev->dev_private;
6745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6746 int pipe = intel_crtc->pipe;
6747 uint16_t coeff = 0x7800; /* 1.0 */
6748
6749 /*
6750 * TODO: Check what kind of values actually come out of the pipe
6751 * with these coeff/postoff values and adjust to get the best
6752 * accuracy. Perhaps we even need to take the bpc value into
6753 * consideration.
6754 */
6755
Daniel Vetter50f3b012013-03-27 00:44:56 +01006756 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006757 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6758
6759 /*
6760 * GY/GU and RY/RU should be the other way around according
6761 * to BSpec, but reality doesn't agree. Just set them up in
6762 * a way that results in the correct picture.
6763 */
6764 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6765 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6766
6767 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6768 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6769
6770 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6771 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6772
6773 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6774 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6775 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6776
6777 if (INTEL_INFO(dev)->gen > 6) {
6778 uint16_t postoff = 0;
6779
Daniel Vetter50f3b012013-03-27 00:44:56 +01006780 if (intel_crtc->config.limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02006781 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006782
6783 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6784 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6785 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6786
6787 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6788 } else {
6789 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6790
Daniel Vetter50f3b012013-03-27 00:44:56 +01006791 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006792 mode |= CSC_BLACK_SCREEN_OFFSET;
6793
6794 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6795 }
6796}
6797
Daniel Vetter6ff93602013-04-19 11:24:36 +02006798static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006799{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006800 struct drm_device *dev = crtc->dev;
6801 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006802 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006803 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006804 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006805 uint32_t val;
6806
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006807 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006808
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006809 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006810 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6811
Daniel Vetter6ff93602013-04-19 11:24:36 +02006812 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006813 val |= PIPECONF_INTERLACED_ILK;
6814 else
6815 val |= PIPECONF_PROGRESSIVE;
6816
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006817 I915_WRITE(PIPECONF(cpu_transcoder), val);
6818 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006819
6820 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6821 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006822
6823 if (IS_BROADWELL(dev)) {
6824 val = 0;
6825
6826 switch (intel_crtc->config.pipe_bpp) {
6827 case 18:
6828 val |= PIPEMISC_DITHER_6_BPC;
6829 break;
6830 case 24:
6831 val |= PIPEMISC_DITHER_8_BPC;
6832 break;
6833 case 30:
6834 val |= PIPEMISC_DITHER_10_BPC;
6835 break;
6836 case 36:
6837 val |= PIPEMISC_DITHER_12_BPC;
6838 break;
6839 default:
6840 /* Case prevented by pipe_config_set_bpp. */
6841 BUG();
6842 }
6843
6844 if (intel_crtc->config.dither)
6845 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6846
6847 I915_WRITE(PIPEMISC(pipe), val);
6848 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006849}
6850
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006851static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006852 intel_clock_t *clock,
6853 bool *has_reduced_clock,
6854 intel_clock_t *reduced_clock)
6855{
6856 struct drm_device *dev = crtc->dev;
6857 struct drm_i915_private *dev_priv = dev->dev_private;
6858 struct intel_encoder *intel_encoder;
6859 int refclk;
6860 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02006861 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006862
6863 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6864 switch (intel_encoder->type) {
6865 case INTEL_OUTPUT_LVDS:
6866 is_lvds = true;
6867 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006868 }
6869 }
6870
6871 refclk = ironlake_get_refclk(crtc);
6872
6873 /*
6874 * Returns a set of divisors for the desired target clock with the given
6875 * refclk, or FALSE. The returned values represent the clock equation:
6876 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6877 */
6878 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006879 ret = dev_priv->display.find_dpll(limit, crtc,
6880 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006881 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006882 if (!ret)
6883 return false;
6884
6885 if (is_lvds && dev_priv->lvds_downclock_avail) {
6886 /*
6887 * Ensure we match the reduced clock's P to the target clock.
6888 * If the clocks don't match, we can't switch the display clock
6889 * by using the FP0/FP1. In such case we will disable the LVDS
6890 * downclock feature.
6891 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006892 *has_reduced_clock =
6893 dev_priv->display.find_dpll(limit, crtc,
6894 dev_priv->lvds_downclock,
6895 refclk, clock,
6896 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006897 }
6898
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006899 return true;
6900}
6901
Paulo Zanonid4b19312012-11-29 11:29:32 -02006902int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6903{
6904 /*
6905 * Account for spread spectrum to avoid
6906 * oversubscribing the link. Max center spread
6907 * is 2.5%; use 5% for safety's sake.
6908 */
6909 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02006910 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02006911}
6912
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006913static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006914{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006915 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006916}
6917
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006918static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006919 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006920 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006921{
6922 struct drm_crtc *crtc = &intel_crtc->base;
6923 struct drm_device *dev = crtc->dev;
6924 struct drm_i915_private *dev_priv = dev->dev_private;
6925 struct intel_encoder *intel_encoder;
6926 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006927 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006928 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006929
6930 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6931 switch (intel_encoder->type) {
6932 case INTEL_OUTPUT_LVDS:
6933 is_lvds = true;
6934 break;
6935 case INTEL_OUTPUT_SDVO:
6936 case INTEL_OUTPUT_HDMI:
6937 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006938 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006939 }
6940
6941 num_connectors++;
6942 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006943
Chris Wilsonc1858122010-12-03 21:35:48 +00006944 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006945 factor = 21;
6946 if (is_lvds) {
6947 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006948 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006949 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006950 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006951 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006952 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006953
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006954 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006955 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006956
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006957 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6958 *fp2 |= FP_CB_TUNE;
6959
Chris Wilson5eddb702010-09-11 13:48:45 +01006960 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006961
Eric Anholta07d6782011-03-30 13:01:08 -07006962 if (is_lvds)
6963 dpll |= DPLLB_MODE_LVDS;
6964 else
6965 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006966
Daniel Vetteref1b4602013-06-01 17:17:04 +02006967 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6968 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006969
6970 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006971 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006972 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006973 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006974
Eric Anholta07d6782011-03-30 13:01:08 -07006975 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006976 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006977 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006978 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006979
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006980 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006981 case 5:
6982 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6983 break;
6984 case 7:
6985 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6986 break;
6987 case 10:
6988 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6989 break;
6990 case 14:
6991 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6992 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006993 }
6994
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006995 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006996 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006997 else
6998 dpll |= PLL_REF_INPUT_DREFCLK;
6999
Daniel Vetter959e16d2013-06-05 13:34:21 +02007000 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007001}
7002
Jesse Barnes79e53942008-11-07 14:24:08 -08007003static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08007004 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007005 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08007006{
7007 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007009 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007010 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007011 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007012 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01007013 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007014 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02007015 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007016
7017 for_each_encoder_on_crtc(dev, crtc, encoder) {
7018 switch (encoder->type) {
7019 case INTEL_OUTPUT_LVDS:
7020 is_lvds = true;
7021 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007022 }
7023
7024 num_connectors++;
7025 }
7026
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007027 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7028 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7029
Daniel Vetterff9a6752013-06-01 17:16:21 +02007030 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007031 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02007032 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007033 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7034 return -EINVAL;
7035 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007036 /* Compat-code for transition, will disappear. */
7037 if (!intel_crtc->config.clock_set) {
7038 intel_crtc->config.dpll.n = clock.n;
7039 intel_crtc->config.dpll.m1 = clock.m1;
7040 intel_crtc->config.dpll.m2 = clock.m2;
7041 intel_crtc->config.dpll.p1 = clock.p1;
7042 intel_crtc->config.dpll.p2 = clock.p2;
7043 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007044
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007045 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01007046 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007047 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007048 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007049 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007050
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007051 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007052 &fp, &reduced_clock,
7053 has_reduced_clock ? &fp2 : NULL);
7054
Daniel Vetter959e16d2013-06-05 13:34:21 +02007055 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007056 intel_crtc->config.dpll_hw_state.fp0 = fp;
7057 if (has_reduced_clock)
7058 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7059 else
7060 intel_crtc->config.dpll_hw_state.fp1 = fp;
7061
Daniel Vetterb89a1d32013-06-05 13:34:24 +02007062 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007063 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007064 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Daniel Vetter29407aa2014-04-24 23:55:08 +02007065 pipe_name(intel_crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007066 return -EINVAL;
7067 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007068 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02007069 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007070
Jani Nikulad330a952014-01-21 11:24:25 +02007071 if (is_lvds && has_reduced_clock && i915.powersave)
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007072 intel_crtc->lowfreq_avail = true;
7073 else
7074 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007075
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007076 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007077}
7078
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007079static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7080 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007081{
7082 struct drm_device *dev = crtc->base.dev;
7083 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007084 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007085
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007086 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7087 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7088 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7089 & ~TU_SIZE_MASK;
7090 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7091 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7092 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7093}
7094
7095static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7096 enum transcoder transcoder,
7097 struct intel_link_m_n *m_n)
7098{
7099 struct drm_device *dev = crtc->base.dev;
7100 struct drm_i915_private *dev_priv = dev->dev_private;
7101 enum pipe pipe = crtc->pipe;
7102
7103 if (INTEL_INFO(dev)->gen >= 5) {
7104 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7105 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7106 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7107 & ~TU_SIZE_MASK;
7108 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7109 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7110 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7111 } else {
7112 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7113 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7114 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7115 & ~TU_SIZE_MASK;
7116 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7117 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7118 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7119 }
7120}
7121
7122void intel_dp_get_m_n(struct intel_crtc *crtc,
7123 struct intel_crtc_config *pipe_config)
7124{
7125 if (crtc->config.has_pch_encoder)
7126 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7127 else
7128 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7129 &pipe_config->dp_m_n);
7130}
7131
Daniel Vetter72419202013-04-04 13:28:53 +02007132static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7133 struct intel_crtc_config *pipe_config)
7134{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007135 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7136 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02007137}
7138
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007139static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7140 struct intel_crtc_config *pipe_config)
7141{
7142 struct drm_device *dev = crtc->base.dev;
7143 struct drm_i915_private *dev_priv = dev->dev_private;
7144 uint32_t tmp;
7145
7146 tmp = I915_READ(PF_CTL(crtc->pipe));
7147
7148 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01007149 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007150 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7151 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02007152
7153 /* We currently do not free assignements of panel fitters on
7154 * ivb/hsw (since we don't use the higher upscaling modes which
7155 * differentiates them) so just WARN about this case for now. */
7156 if (IS_GEN7(dev)) {
7157 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7158 PF_PIPE_SEL_IVB(crtc->pipe));
7159 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007160 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007161}
7162
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007163static void ironlake_get_plane_config(struct intel_crtc *crtc,
7164 struct intel_plane_config *plane_config)
7165{
7166 struct drm_device *dev = crtc->base.dev;
7167 struct drm_i915_private *dev_priv = dev->dev_private;
7168 u32 val, base, offset;
7169 int pipe = crtc->pipe, plane = crtc->plane;
7170 int fourcc, pixel_format;
7171 int aligned_height;
7172
Dave Airlie66e514c2014-04-03 07:51:54 +10007173 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7174 if (!crtc->base.primary->fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007175 DRM_DEBUG_KMS("failed to alloc fb\n");
7176 return;
7177 }
7178
7179 val = I915_READ(DSPCNTR(plane));
7180
7181 if (INTEL_INFO(dev)->gen >= 4)
7182 if (val & DISPPLANE_TILED)
7183 plane_config->tiled = true;
7184
7185 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7186 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10007187 crtc->base.primary->fb->pixel_format = fourcc;
7188 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007189 drm_format_plane_cpp(fourcc, 0) * 8;
7190
7191 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7192 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7193 offset = I915_READ(DSPOFFSET(plane));
7194 } else {
7195 if (plane_config->tiled)
7196 offset = I915_READ(DSPTILEOFF(plane));
7197 else
7198 offset = I915_READ(DSPLINOFF(plane));
7199 }
7200 plane_config->base = base;
7201
7202 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007203 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7204 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007205
7206 val = I915_READ(DSPSTRIDE(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007207 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007208
Dave Airlie66e514c2014-04-03 07:51:54 +10007209 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007210 plane_config->tiled);
7211
Dave Airlie66e514c2014-04-03 07:51:54 +10007212 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007213 aligned_height, PAGE_SIZE);
7214
7215 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 +10007216 pipe, plane, crtc->base.primary->fb->width,
7217 crtc->base.primary->fb->height,
7218 crtc->base.primary->fb->bits_per_pixel, base,
7219 crtc->base.primary->fb->pitches[0],
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007220 plane_config->size);
7221}
7222
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007223static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7224 struct intel_crtc_config *pipe_config)
7225{
7226 struct drm_device *dev = crtc->base.dev;
7227 struct drm_i915_private *dev_priv = dev->dev_private;
7228 uint32_t tmp;
7229
Daniel Vettere143a212013-07-04 12:01:15 +02007230 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007231 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007232
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007233 tmp = I915_READ(PIPECONF(crtc->pipe));
7234 if (!(tmp & PIPECONF_ENABLE))
7235 return false;
7236
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007237 switch (tmp & PIPECONF_BPC_MASK) {
7238 case PIPECONF_6BPC:
7239 pipe_config->pipe_bpp = 18;
7240 break;
7241 case PIPECONF_8BPC:
7242 pipe_config->pipe_bpp = 24;
7243 break;
7244 case PIPECONF_10BPC:
7245 pipe_config->pipe_bpp = 30;
7246 break;
7247 case PIPECONF_12BPC:
7248 pipe_config->pipe_bpp = 36;
7249 break;
7250 default:
7251 break;
7252 }
7253
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007254 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7255 pipe_config->limited_color_range = true;
7256
Daniel Vetterab9412b2013-05-03 11:49:46 +02007257 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02007258 struct intel_shared_dpll *pll;
7259
Daniel Vetter88adfff2013-03-28 10:42:01 +01007260 pipe_config->has_pch_encoder = true;
7261
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007262 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7263 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7264 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007265
7266 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007267
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007268 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02007269 pipe_config->shared_dpll =
7270 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007271 } else {
7272 tmp = I915_READ(PCH_DPLL_SEL);
7273 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7274 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7275 else
7276 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7277 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02007278
7279 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7280
7281 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7282 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02007283
7284 tmp = pipe_config->dpll_hw_state.dpll;
7285 pipe_config->pixel_multiplier =
7286 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7287 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007288
7289 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007290 } else {
7291 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007292 }
7293
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007294 intel_get_pipe_timings(crtc, pipe_config);
7295
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007296 ironlake_get_pfit_config(crtc, pipe_config);
7297
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007298 return true;
7299}
7300
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007301static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7302{
7303 struct drm_device *dev = dev_priv->dev;
7304 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
7305 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007306
Damien Lespiaud3fcc802014-05-13 23:32:22 +01007307 for_each_intel_crtc(dev, crtc)
Paulo Zanoni798183c2013-12-06 20:29:01 -02007308 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007309 pipe_name(crtc->pipe));
7310
7311 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
7312 WARN(plls->spll_refcount, "SPLL enabled\n");
7313 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
7314 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
7315 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7316 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7317 "CPU PWM1 enabled\n");
7318 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7319 "CPU PWM2 enabled\n");
7320 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7321 "PCH PWM1 enabled\n");
7322 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7323 "Utility pin enabled\n");
7324 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7325
Paulo Zanoni9926ada2014-04-01 19:39:47 -03007326 /*
7327 * In theory we can still leave IRQs enabled, as long as only the HPD
7328 * interrupts remain enabled. We used to check for that, but since it's
7329 * gen-specific and since we only disable LCPLL after we fully disable
7330 * the interrupts, the check below should be enough.
7331 */
7332 WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007333}
7334
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007335static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7336{
7337 struct drm_device *dev = dev_priv->dev;
7338
7339 if (IS_HASWELL(dev)) {
7340 mutex_lock(&dev_priv->rps.hw_lock);
7341 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7342 val))
7343 DRM_ERROR("Failed to disable D_COMP\n");
7344 mutex_unlock(&dev_priv->rps.hw_lock);
7345 } else {
7346 I915_WRITE(D_COMP, val);
7347 }
7348 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007349}
7350
7351/*
7352 * This function implements pieces of two sequences from BSpec:
7353 * - Sequence for display software to disable LCPLL
7354 * - Sequence for display software to allow package C8+
7355 * The steps implemented here are just the steps that actually touch the LCPLL
7356 * register. Callers should take care of disabling all the display engine
7357 * functions, doing the mode unset, fixing interrupts, etc.
7358 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007359static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7360 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007361{
7362 uint32_t val;
7363
7364 assert_can_disable_lcpll(dev_priv);
7365
7366 val = I915_READ(LCPLL_CTL);
7367
7368 if (switch_to_fclk) {
7369 val |= LCPLL_CD_SOURCE_FCLK;
7370 I915_WRITE(LCPLL_CTL, val);
7371
7372 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7373 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7374 DRM_ERROR("Switching to FCLK failed\n");
7375
7376 val = I915_READ(LCPLL_CTL);
7377 }
7378
7379 val |= LCPLL_PLL_DISABLE;
7380 I915_WRITE(LCPLL_CTL, val);
7381 POSTING_READ(LCPLL_CTL);
7382
7383 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7384 DRM_ERROR("LCPLL still locked\n");
7385
7386 val = I915_READ(D_COMP);
7387 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007388 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007389 ndelay(100);
7390
7391 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
7392 DRM_ERROR("D_COMP RCOMP still in progress\n");
7393
7394 if (allow_power_down) {
7395 val = I915_READ(LCPLL_CTL);
7396 val |= LCPLL_POWER_DOWN_ALLOW;
7397 I915_WRITE(LCPLL_CTL, val);
7398 POSTING_READ(LCPLL_CTL);
7399 }
7400}
7401
7402/*
7403 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7404 * source.
7405 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007406static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007407{
7408 uint32_t val;
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007409 unsigned long irqflags;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007410
7411 val = I915_READ(LCPLL_CTL);
7412
7413 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7414 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7415 return;
7416
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007417 /*
7418 * Make sure we're not on PC8 state before disabling PC8, otherwise
7419 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7420 *
7421 * The other problem is that hsw_restore_lcpll() is called as part of
7422 * the runtime PM resume sequence, so we can't just call
7423 * gen6_gt_force_wake_get() because that function calls
7424 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7425 * while we are on the resume sequence. So to solve this problem we have
7426 * to call special forcewake code that doesn't touch runtime PM and
7427 * doesn't enable the forcewake delayed work.
7428 */
7429 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7430 if (dev_priv->uncore.forcewake_count++ == 0)
7431 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7432 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanoni215733f2013-08-19 13:18:07 -03007433
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007434 if (val & LCPLL_POWER_DOWN_ALLOW) {
7435 val &= ~LCPLL_POWER_DOWN_ALLOW;
7436 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02007437 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007438 }
7439
7440 val = I915_READ(D_COMP);
7441 val |= D_COMP_COMP_FORCE;
7442 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007443 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007444
7445 val = I915_READ(LCPLL_CTL);
7446 val &= ~LCPLL_PLL_DISABLE;
7447 I915_WRITE(LCPLL_CTL, val);
7448
7449 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7450 DRM_ERROR("LCPLL not locked yet\n");
7451
7452 if (val & LCPLL_CD_SOURCE_FCLK) {
7453 val = I915_READ(LCPLL_CTL);
7454 val &= ~LCPLL_CD_SOURCE_FCLK;
7455 I915_WRITE(LCPLL_CTL, val);
7456
7457 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7458 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7459 DRM_ERROR("Switching back to LCPLL failed\n");
7460 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03007461
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007462 /* See the big comment above. */
7463 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7464 if (--dev_priv->uncore.forcewake_count == 0)
7465 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7466 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007467}
7468
Paulo Zanoni765dab672014-03-07 20:08:18 -03007469/*
7470 * Package states C8 and deeper are really deep PC states that can only be
7471 * reached when all the devices on the system allow it, so even if the graphics
7472 * device allows PC8+, it doesn't mean the system will actually get to these
7473 * states. Our driver only allows PC8+ when going into runtime PM.
7474 *
7475 * The requirements for PC8+ are that all the outputs are disabled, the power
7476 * well is disabled and most interrupts are disabled, and these are also
7477 * requirements for runtime PM. When these conditions are met, we manually do
7478 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7479 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7480 * hang the machine.
7481 *
7482 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7483 * the state of some registers, so when we come back from PC8+ we need to
7484 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7485 * need to take care of the registers kept by RC6. Notice that this happens even
7486 * if we don't put the device in PCI D3 state (which is what currently happens
7487 * because of the runtime PM support).
7488 *
7489 * For more, read "Display Sequences for Package C8" on the hardware
7490 * documentation.
7491 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007492void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007493{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007494 struct drm_device *dev = dev_priv->dev;
7495 uint32_t val;
7496
Paulo Zanonic67a4702013-08-19 13:18:09 -03007497 DRM_DEBUG_KMS("Enabling package C8+\n");
7498
Paulo Zanonic67a4702013-08-19 13:18:09 -03007499 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7500 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7501 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7502 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7503 }
7504
7505 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007506 hsw_disable_lcpll(dev_priv, true, true);
7507}
7508
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007509void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007510{
7511 struct drm_device *dev = dev_priv->dev;
7512 uint32_t val;
7513
Paulo Zanonic67a4702013-08-19 13:18:09 -03007514 DRM_DEBUG_KMS("Disabling package C8+\n");
7515
7516 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007517 lpt_init_pch_refclk(dev);
7518
7519 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7520 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7521 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7522 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7523 }
7524
7525 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007526}
7527
Paulo Zanoni9a952a02014-03-07 20:12:34 -03007528static void snb_modeset_global_resources(struct drm_device *dev)
7529{
7530 modeset_update_crtc_power_domains(dev);
7531}
7532
Imre Deak4f074122013-10-16 17:25:51 +03007533static void haswell_modeset_global_resources(struct drm_device *dev)
7534{
Paulo Zanonida723562013-12-19 11:54:51 -02007535 modeset_update_crtc_power_domains(dev);
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02007536}
7537
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007538static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007539 int x, int y,
7540 struct drm_framebuffer *fb)
7541{
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007542 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007543
Paulo Zanoni566b7342013-11-25 15:27:08 -02007544 if (!intel_ddi_pll_select(intel_crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007545 return -EINVAL;
Paulo Zanoni566b7342013-11-25 15:27:08 -02007546 intel_ddi_pll_enable(intel_crtc);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007547
Daniel Vetter644cef32014-04-24 23:55:07 +02007548 intel_crtc->lowfreq_avail = false;
7549
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007550 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007551}
7552
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007553static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7554 struct intel_crtc_config *pipe_config)
7555{
7556 struct drm_device *dev = crtc->base.dev;
7557 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007558 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007559 uint32_t tmp;
7560
Imre Deakb5482bd2014-03-05 16:20:55 +02007561 if (!intel_display_power_enabled(dev_priv,
7562 POWER_DOMAIN_PIPE(crtc->pipe)))
7563 return false;
7564
Daniel Vettere143a212013-07-04 12:01:15 +02007565 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007566 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7567
Daniel Vettereccb1402013-05-22 00:50:22 +02007568 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7569 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7570 enum pipe trans_edp_pipe;
7571 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7572 default:
7573 WARN(1, "unknown pipe linked to edp transcoder\n");
7574 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7575 case TRANS_DDI_EDP_INPUT_A_ON:
7576 trans_edp_pipe = PIPE_A;
7577 break;
7578 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7579 trans_edp_pipe = PIPE_B;
7580 break;
7581 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7582 trans_edp_pipe = PIPE_C;
7583 break;
7584 }
7585
7586 if (trans_edp_pipe == crtc->pipe)
7587 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7588 }
7589
Imre Deakda7e29b2014-02-18 00:02:02 +02007590 if (!intel_display_power_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02007591 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03007592 return false;
7593
Daniel Vettereccb1402013-05-22 00:50:22 +02007594 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007595 if (!(tmp & PIPECONF_ENABLE))
7596 return false;
7597
Daniel Vetter88adfff2013-03-28 10:42:01 +01007598 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03007599 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01007600 * DDI E. So just check whether this pipe is wired to DDI E and whether
7601 * the PCH transcoder is on.
7602 */
Daniel Vettereccb1402013-05-22 00:50:22 +02007603 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01007604 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02007605 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01007606 pipe_config->has_pch_encoder = true;
7607
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007608 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7609 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7610 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007611
7612 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007613 }
7614
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007615 intel_get_pipe_timings(crtc, pipe_config);
7616
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007617 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Imre Deakda7e29b2014-02-18 00:02:02 +02007618 if (intel_display_power_enabled(dev_priv, pfit_domain))
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007619 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01007620
Jesse Barnese59150d2014-01-07 13:30:45 -08007621 if (IS_HASWELL(dev))
7622 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7623 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007624
Daniel Vetter6c49f242013-06-06 12:45:25 +02007625 pipe_config->pixel_multiplier = 1;
7626
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007627 return true;
7628}
7629
Jani Nikula1a915102013-10-16 12:34:48 +03007630static struct {
7631 int clock;
7632 u32 config;
7633} hdmi_audio_clock[] = {
7634 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7635 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7636 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7637 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7638 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7639 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7640 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7641 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7642 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7643 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7644};
7645
7646/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7647static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7648{
7649 int i;
7650
7651 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7652 if (mode->clock == hdmi_audio_clock[i].clock)
7653 break;
7654 }
7655
7656 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7657 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7658 i = 1;
7659 }
7660
7661 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7662 hdmi_audio_clock[i].clock,
7663 hdmi_audio_clock[i].config);
7664
7665 return hdmi_audio_clock[i].config;
7666}
7667
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007668static bool intel_eld_uptodate(struct drm_connector *connector,
7669 int reg_eldv, uint32_t bits_eldv,
7670 int reg_elda, uint32_t bits_elda,
7671 int reg_edid)
7672{
7673 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7674 uint8_t *eld = connector->eld;
7675 uint32_t i;
7676
7677 i = I915_READ(reg_eldv);
7678 i &= bits_eldv;
7679
7680 if (!eld[0])
7681 return !i;
7682
7683 if (!i)
7684 return false;
7685
7686 i = I915_READ(reg_elda);
7687 i &= ~bits_elda;
7688 I915_WRITE(reg_elda, i);
7689
7690 for (i = 0; i < eld[2]; i++)
7691 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7692 return false;
7693
7694 return true;
7695}
7696
Wu Fengguange0dac652011-09-05 14:25:34 +08007697static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007698 struct drm_crtc *crtc,
7699 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007700{
7701 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7702 uint8_t *eld = connector->eld;
7703 uint32_t eldv;
7704 uint32_t len;
7705 uint32_t i;
7706
7707 i = I915_READ(G4X_AUD_VID_DID);
7708
7709 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7710 eldv = G4X_ELDV_DEVCL_DEVBLC;
7711 else
7712 eldv = G4X_ELDV_DEVCTG;
7713
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007714 if (intel_eld_uptodate(connector,
7715 G4X_AUD_CNTL_ST, eldv,
7716 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7717 G4X_HDMIW_HDMIEDID))
7718 return;
7719
Wu Fengguange0dac652011-09-05 14:25:34 +08007720 i = I915_READ(G4X_AUD_CNTL_ST);
7721 i &= ~(eldv | G4X_ELD_ADDR);
7722 len = (i >> 9) & 0x1f; /* ELD buffer size */
7723 I915_WRITE(G4X_AUD_CNTL_ST, i);
7724
7725 if (!eld[0])
7726 return;
7727
7728 len = min_t(uint8_t, eld[2], len);
7729 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7730 for (i = 0; i < len; i++)
7731 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7732
7733 i = I915_READ(G4X_AUD_CNTL_ST);
7734 i |= eldv;
7735 I915_WRITE(G4X_AUD_CNTL_ST, i);
7736}
7737
Wang Xingchao83358c852012-08-16 22:43:37 +08007738static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007739 struct drm_crtc *crtc,
7740 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007741{
7742 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7743 uint8_t *eld = connector->eld;
Wang Xingchao83358c852012-08-16 22:43:37 +08007744 uint32_t eldv;
7745 uint32_t i;
7746 int len;
7747 int pipe = to_intel_crtc(crtc)->pipe;
7748 int tmp;
7749
7750 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7751 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7752 int aud_config = HSW_AUD_CFG(pipe);
7753 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7754
Wang Xingchao83358c852012-08-16 22:43:37 +08007755 /* Audio output enable */
7756 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7757 tmp = I915_READ(aud_cntrl_st2);
7758 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7759 I915_WRITE(aud_cntrl_st2, tmp);
Daniel Vetterc7905792014-04-16 16:56:09 +02007760 POSTING_READ(aud_cntrl_st2);
Wang Xingchao83358c852012-08-16 22:43:37 +08007761
Daniel Vetterc7905792014-04-16 16:56:09 +02007762 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Wang Xingchao83358c852012-08-16 22:43:37 +08007763
7764 /* Set ELD valid state */
7765 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007766 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007767 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7768 I915_WRITE(aud_cntrl_st2, tmp);
7769 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007770 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007771
7772 /* Enable HDMI mode */
7773 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007774 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007775 /* clear N_programing_enable and N_value_index */
7776 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7777 I915_WRITE(aud_config, tmp);
7778
7779 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7780
7781 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7782
7783 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7784 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7785 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7786 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007787 } else {
7788 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7789 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007790
7791 if (intel_eld_uptodate(connector,
7792 aud_cntrl_st2, eldv,
7793 aud_cntl_st, IBX_ELD_ADDRESS,
7794 hdmiw_hdmiedid))
7795 return;
7796
7797 i = I915_READ(aud_cntrl_st2);
7798 i &= ~eldv;
7799 I915_WRITE(aud_cntrl_st2, i);
7800
7801 if (!eld[0])
7802 return;
7803
7804 i = I915_READ(aud_cntl_st);
7805 i &= ~IBX_ELD_ADDRESS;
7806 I915_WRITE(aud_cntl_st, i);
7807 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7808 DRM_DEBUG_DRIVER("port num:%d\n", i);
7809
7810 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7811 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7812 for (i = 0; i < len; i++)
7813 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7814
7815 i = I915_READ(aud_cntrl_st2);
7816 i |= eldv;
7817 I915_WRITE(aud_cntrl_st2, i);
7818
7819}
7820
Wu Fengguange0dac652011-09-05 14:25:34 +08007821static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007822 struct drm_crtc *crtc,
7823 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007824{
7825 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7826 uint8_t *eld = connector->eld;
7827 uint32_t eldv;
7828 uint32_t i;
7829 int len;
7830 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007831 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007832 int aud_cntl_st;
7833 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007834 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007835
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007836 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007837 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7838 aud_config = IBX_AUD_CFG(pipe);
7839 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007840 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007841 } else if (IS_VALLEYVIEW(connector->dev)) {
7842 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7843 aud_config = VLV_AUD_CFG(pipe);
7844 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7845 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007846 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007847 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7848 aud_config = CPT_AUD_CFG(pipe);
7849 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007850 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007851 }
7852
Wang Xingchao9b138a82012-08-09 16:52:18 +08007853 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007854
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007855 if (IS_VALLEYVIEW(connector->dev)) {
7856 struct intel_encoder *intel_encoder;
7857 struct intel_digital_port *intel_dig_port;
7858
7859 intel_encoder = intel_attached_encoder(connector);
7860 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7861 i = intel_dig_port->port;
7862 } else {
7863 i = I915_READ(aud_cntl_st);
7864 i = (i >> 29) & DIP_PORT_SEL_MASK;
7865 /* DIP_Port_Select, 0x1 = PortB */
7866 }
7867
Wu Fengguange0dac652011-09-05 14:25:34 +08007868 if (!i) {
7869 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7870 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007871 eldv = IBX_ELD_VALIDB;
7872 eldv |= IBX_ELD_VALIDB << 4;
7873 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007874 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007875 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007876 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007877 }
7878
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007879 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7880 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7881 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007882 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007883 } else {
7884 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7885 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007886
7887 if (intel_eld_uptodate(connector,
7888 aud_cntrl_st2, eldv,
7889 aud_cntl_st, IBX_ELD_ADDRESS,
7890 hdmiw_hdmiedid))
7891 return;
7892
Wu Fengguange0dac652011-09-05 14:25:34 +08007893 i = I915_READ(aud_cntrl_st2);
7894 i &= ~eldv;
7895 I915_WRITE(aud_cntrl_st2, i);
7896
7897 if (!eld[0])
7898 return;
7899
Wu Fengguange0dac652011-09-05 14:25:34 +08007900 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007901 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007902 I915_WRITE(aud_cntl_st, i);
7903
7904 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7905 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7906 for (i = 0; i < len; i++)
7907 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7908
7909 i = I915_READ(aud_cntrl_st2);
7910 i |= eldv;
7911 I915_WRITE(aud_cntrl_st2, i);
7912}
7913
7914void intel_write_eld(struct drm_encoder *encoder,
7915 struct drm_display_mode *mode)
7916{
7917 struct drm_crtc *crtc = encoder->crtc;
7918 struct drm_connector *connector;
7919 struct drm_device *dev = encoder->dev;
7920 struct drm_i915_private *dev_priv = dev->dev_private;
7921
7922 connector = drm_select_eld(encoder, mode);
7923 if (!connector)
7924 return;
7925
7926 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7927 connector->base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03007928 connector->name,
Wu Fengguange0dac652011-09-05 14:25:34 +08007929 connector->encoder->base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +03007930 connector->encoder->name);
Wu Fengguange0dac652011-09-05 14:25:34 +08007931
7932 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7933
7934 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007935 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007936}
7937
Chris Wilson560b85b2010-08-07 11:01:38 +01007938static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7939{
7940 struct drm_device *dev = crtc->dev;
7941 struct drm_i915_private *dev_priv = dev->dev_private;
7942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson4b0e3332014-05-30 16:35:26 +03007943 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01007944
Chris Wilson4b0e3332014-05-30 16:35:26 +03007945 if (base != intel_crtc->cursor_base) {
Chris Wilson560b85b2010-08-07 11:01:38 +01007946 /* On these chipsets we can only modify the base whilst
7947 * the cursor is disabled.
7948 */
Chris Wilson4b0e3332014-05-30 16:35:26 +03007949 if (intel_crtc->cursor_cntl) {
7950 I915_WRITE(_CURACNTR, 0);
7951 POSTING_READ(_CURACNTR);
7952 intel_crtc->cursor_cntl = 0;
7953 }
7954
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007955 I915_WRITE(_CURABASE, base);
Chris Wilson4b0e3332014-05-30 16:35:26 +03007956 POSTING_READ(_CURABASE);
7957 }
Chris Wilson560b85b2010-08-07 11:01:38 +01007958
Chris Wilson4b0e3332014-05-30 16:35:26 +03007959 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7960 cntl = 0;
7961 if (base)
7962 cntl = (CURSOR_ENABLE |
Chris Wilson560b85b2010-08-07 11:01:38 +01007963 CURSOR_GAMMA_ENABLE |
Chris Wilson4b0e3332014-05-30 16:35:26 +03007964 CURSOR_FORMAT_ARGB);
7965 if (intel_crtc->cursor_cntl != cntl) {
7966 I915_WRITE(_CURACNTR, cntl);
7967 POSTING_READ(_CURACNTR);
7968 intel_crtc->cursor_cntl = cntl;
7969 }
Chris Wilson560b85b2010-08-07 11:01:38 +01007970}
7971
7972static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7973{
7974 struct drm_device *dev = crtc->dev;
7975 struct drm_i915_private *dev_priv = dev->dev_private;
7976 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7977 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03007978 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01007979
Chris Wilson4b0e3332014-05-30 16:35:26 +03007980 cntl = 0;
7981 if (base) {
7982 cntl = MCURSOR_GAMMA_ENABLE;
7983 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307984 case 64:
7985 cntl |= CURSOR_MODE_64_ARGB_AX;
7986 break;
7987 case 128:
7988 cntl |= CURSOR_MODE_128_ARGB_AX;
7989 break;
7990 case 256:
7991 cntl |= CURSOR_MODE_256_ARGB_AX;
7992 break;
7993 default:
7994 WARN_ON(1);
7995 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01007996 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03007997 cntl |= pipe << 28; /* Connect to correct pipe */
Chris Wilson560b85b2010-08-07 11:01:38 +01007998 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03007999 if (intel_crtc->cursor_cntl != cntl) {
8000 I915_WRITE(CURCNTR(pipe), cntl);
8001 POSTING_READ(CURCNTR(pipe));
8002 intel_crtc->cursor_cntl = cntl;
8003 }
8004
Chris Wilson560b85b2010-08-07 11:01:38 +01008005 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08008006 I915_WRITE(CURBASE(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01008007 POSTING_READ(CURBASE(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01008008}
8009
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008010static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
8011{
8012 struct drm_device *dev = crtc->dev;
8013 struct drm_i915_private *dev_priv = dev->dev_private;
8014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8015 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008016 uint32_t cntl;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008017
Chris Wilson4b0e3332014-05-30 16:35:26 +03008018 cntl = 0;
8019 if (base) {
8020 cntl = MCURSOR_GAMMA_ENABLE;
8021 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308022 case 64:
8023 cntl |= CURSOR_MODE_64_ARGB_AX;
8024 break;
8025 case 128:
8026 cntl |= CURSOR_MODE_128_ARGB_AX;
8027 break;
8028 case 256:
8029 cntl |= CURSOR_MODE_256_ARGB_AX;
8030 break;
8031 default:
8032 WARN_ON(1);
8033 return;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008034 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008035 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008036 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8037 cntl |= CURSOR_PIPE_CSC_ENABLE;
8038
8039 if (intel_crtc->cursor_cntl != cntl) {
8040 I915_WRITE(CURCNTR(pipe), cntl);
8041 POSTING_READ(CURCNTR(pipe));
8042 intel_crtc->cursor_cntl = cntl;
8043 }
8044
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008045 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008046 I915_WRITE(CURBASE(pipe), base);
8047 POSTING_READ(CURBASE(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008048}
8049
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008050/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008051static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8052 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008053{
8054 struct drm_device *dev = crtc->dev;
8055 struct drm_i915_private *dev_priv = dev->dev_private;
8056 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8057 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008058 int x = crtc->cursor_x;
8059 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008060 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008061
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008062 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008063 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008064
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008065 if (x >= intel_crtc->config.pipe_src_w)
8066 base = 0;
8067
8068 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008069 base = 0;
8070
8071 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008072 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008073 base = 0;
8074
8075 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8076 x = -x;
8077 }
8078 pos |= x << CURSOR_X_SHIFT;
8079
8080 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008081 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008082 base = 0;
8083
8084 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8085 y = -y;
8086 }
8087 pos |= y << CURSOR_Y_SHIFT;
8088
Chris Wilson4b0e3332014-05-30 16:35:26 +03008089 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008090 return;
8091
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008092 I915_WRITE(CURPOS(pipe), pos);
8093
8094 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008095 ivb_update_cursor(crtc, base);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008096 else if (IS_845G(dev) || IS_I865G(dev))
8097 i845_update_cursor(crtc, base);
8098 else
8099 i9xx_update_cursor(crtc, base);
Chris Wilson4b0e3332014-05-30 16:35:26 +03008100 intel_crtc->cursor_base = base;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008101}
8102
Matt Ropere3287952014-06-10 08:28:12 -07008103/*
8104 * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
8105 *
8106 * Note that the object's reference will be consumed if the update fails. If
8107 * the update succeeds, the reference of the old object (if any) will be
8108 * consumed.
8109 */
8110static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8111 struct drm_i915_gem_object *obj,
8112 uint32_t width, uint32_t height)
Jesse Barnes79e53942008-11-07 14:24:08 -08008113{
8114 struct drm_device *dev = crtc->dev;
8115 struct drm_i915_private *dev_priv = dev->dev_private;
8116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02008117 enum pipe pipe = intel_crtc->pipe;
Chris Wilson64f962e2014-03-26 12:38:15 +00008118 unsigned old_width;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008119 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008120 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008121
Jesse Barnes79e53942008-11-07 14:24:08 -08008122 /* if we want to turn off the cursor ignore width and height */
Matt Ropere3287952014-06-10 08:28:12 -07008123 if (!obj) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008124 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008125 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00008126 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10008127 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008128 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08008129 }
8130
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308131 /* Check for which cursor types we support */
8132 if (!((width == 64 && height == 64) ||
8133 (width == 128 && height == 128 && !IS_GEN2(dev)) ||
8134 (width == 256 && height == 256 && !IS_GEN2(dev)))) {
8135 DRM_DEBUG("Cursor dimension not supported\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08008136 return -EINVAL;
8137 }
8138
Chris Wilson05394f32010-11-08 19:18:58 +00008139 if (obj->base.size < width * height * 4) {
Matt Ropere3287952014-06-10 08:28:12 -07008140 DRM_DEBUG_KMS("buffer is too small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10008141 ret = -ENOMEM;
8142 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008143 }
8144
Dave Airlie71acb5e2008-12-30 20:31:46 +10008145 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008146 mutex_lock(&dev->struct_mutex);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008147 if (!INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00008148 unsigned alignment;
8149
Chris Wilsond9e86c02010-11-10 16:40:20 +00008150 if (obj->tiling_mode) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008151 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Chris Wilsond9e86c02010-11-10 16:40:20 +00008152 ret = -EINVAL;
8153 goto fail_locked;
8154 }
8155
Chris Wilson693db182013-03-05 14:52:39 +00008156 /* Note that the w/a also requires 2 PTE of padding following
8157 * the bo. We currently fill all unused PTE with the shadow
8158 * page and so we should always have valid PTE following the
8159 * cursor preventing the VT-d warning.
8160 */
8161 alignment = 0;
8162 if (need_vtd_wa(dev))
8163 alignment = 64*1024;
8164
8165 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01008166 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008167 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01008168 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008169 }
8170
Chris Wilsond9e86c02010-11-10 16:40:20 +00008171 ret = i915_gem_object_put_fence(obj);
8172 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008173 DRM_DEBUG_KMS("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00008174 goto fail_unpin;
8175 }
8176
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008177 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008178 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01008179 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson00731152014-05-21 12:42:56 +01008180 ret = i915_gem_object_attach_phys(obj, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008181 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008182 DRM_DEBUG_KMS("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008183 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10008184 }
Chris Wilson00731152014-05-21 12:42:56 +01008185 addr = obj->phys_handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008186 }
8187
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008188 if (IS_GEN2(dev))
Jesse Barnes14b603912009-05-20 16:47:08 -04008189 I915_WRITE(CURSIZE, (height << 12) | width);
8190
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008191 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008192 if (intel_crtc->cursor_bo) {
Chris Wilson00731152014-05-21 12:42:56 +01008193 if (!INTEL_INFO(dev)->cursor_needs_physical)
Chris Wilsoncc98b412013-08-09 12:25:09 +01008194 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008195 }
Jesse Barnes80824002009-09-10 15:28:06 -07008196
Daniel Vettera071fa02014-06-18 23:28:09 +02008197 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8198 INTEL_FRONTBUFFER_CURSOR(pipe));
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008199 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008200
Chris Wilson64f962e2014-03-26 12:38:15 +00008201 old_width = intel_crtc->cursor_width;
8202
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008203 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00008204 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008205 intel_crtc->cursor_width = width;
8206 intel_crtc->cursor_height = height;
8207
Chris Wilson64f962e2014-03-26 12:38:15 +00008208 if (intel_crtc->active) {
8209 if (old_width != width)
8210 intel_update_watermarks(crtc);
Ville Syrjäläf2f5f7712013-09-17 18:33:44 +03008211 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Chris Wilson64f962e2014-03-26 12:38:15 +00008212 }
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008213
Jesse Barnes79e53942008-11-07 14:24:08 -08008214 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008215fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01008216 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008217fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10008218 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00008219fail:
Chris Wilson05394f32010-11-08 19:18:58 +00008220 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10008221 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008222}
8223
Jesse Barnes79e53942008-11-07 14:24:08 -08008224static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008225 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008226{
James Simmons72034252010-08-03 01:33:19 +01008227 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008228 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008229
James Simmons72034252010-08-03 01:33:19 +01008230 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008231 intel_crtc->lut_r[i] = red[i] >> 8;
8232 intel_crtc->lut_g[i] = green[i] >> 8;
8233 intel_crtc->lut_b[i] = blue[i] >> 8;
8234 }
8235
8236 intel_crtc_load_lut(crtc);
8237}
8238
Jesse Barnes79e53942008-11-07 14:24:08 -08008239/* VESA 640x480x72Hz mode to set on the pipe */
8240static struct drm_display_mode load_detect_mode = {
8241 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8242 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8243};
8244
Daniel Vettera8bb6812014-02-10 18:00:39 +01008245struct drm_framebuffer *
8246__intel_framebuffer_create(struct drm_device *dev,
8247 struct drm_mode_fb_cmd2 *mode_cmd,
8248 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008249{
8250 struct intel_framebuffer *intel_fb;
8251 int ret;
8252
8253 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8254 if (!intel_fb) {
8255 drm_gem_object_unreference_unlocked(&obj->base);
8256 return ERR_PTR(-ENOMEM);
8257 }
8258
8259 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008260 if (ret)
8261 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008262
8263 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008264err:
8265 drm_gem_object_unreference_unlocked(&obj->base);
8266 kfree(intel_fb);
8267
8268 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008269}
8270
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008271static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008272intel_framebuffer_create(struct drm_device *dev,
8273 struct drm_mode_fb_cmd2 *mode_cmd,
8274 struct drm_i915_gem_object *obj)
8275{
8276 struct drm_framebuffer *fb;
8277 int ret;
8278
8279 ret = i915_mutex_lock_interruptible(dev);
8280 if (ret)
8281 return ERR_PTR(ret);
8282 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8283 mutex_unlock(&dev->struct_mutex);
8284
8285 return fb;
8286}
8287
Chris Wilsond2dff872011-04-19 08:36:26 +01008288static u32
8289intel_framebuffer_pitch_for_width(int width, int bpp)
8290{
8291 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8292 return ALIGN(pitch, 64);
8293}
8294
8295static u32
8296intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8297{
8298 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
8299 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
8300}
8301
8302static struct drm_framebuffer *
8303intel_framebuffer_create_for_mode(struct drm_device *dev,
8304 struct drm_display_mode *mode,
8305 int depth, int bpp)
8306{
8307 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008308 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008309
8310 obj = i915_gem_alloc_object(dev,
8311 intel_framebuffer_size_for_mode(mode, bpp));
8312 if (obj == NULL)
8313 return ERR_PTR(-ENOMEM);
8314
8315 mode_cmd.width = mode->hdisplay;
8316 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008317 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8318 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008319 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008320
8321 return intel_framebuffer_create(dev, &mode_cmd, obj);
8322}
8323
8324static struct drm_framebuffer *
8325mode_fits_in_fbdev(struct drm_device *dev,
8326 struct drm_display_mode *mode)
8327{
Daniel Vetter4520f532013-10-09 09:18:51 +02008328#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008329 struct drm_i915_private *dev_priv = dev->dev_private;
8330 struct drm_i915_gem_object *obj;
8331 struct drm_framebuffer *fb;
8332
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008333 if (!dev_priv->fbdev)
8334 return NULL;
8335
8336 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008337 return NULL;
8338
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008339 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008340 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008341
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008342 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008343 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8344 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008345 return NULL;
8346
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008347 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008348 return NULL;
8349
8350 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008351#else
8352 return NULL;
8353#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008354}
8355
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008356bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008357 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008358 struct intel_load_detect_pipe *old,
8359 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008360{
8361 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008362 struct intel_encoder *intel_encoder =
8363 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008364 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008365 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008366 struct drm_crtc *crtc = NULL;
8367 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008368 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008369 struct drm_mode_config *config = &dev->mode_config;
8370 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008371
Chris Wilsond2dff872011-04-19 08:36:26 +01008372 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008373 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008374 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008375
Rob Clark51fd3712013-11-19 12:10:12 -05008376 drm_modeset_acquire_init(ctx, 0);
8377
8378retry:
8379 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8380 if (ret)
8381 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008382
Jesse Barnes79e53942008-11-07 14:24:08 -08008383 /*
8384 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008385 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008386 * - if the connector already has an assigned crtc, use it (but make
8387 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008388 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008389 * - try to find the first unused crtc that can drive this connector,
8390 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008391 */
8392
8393 /* See if we already have a CRTC for this connector */
8394 if (encoder->crtc) {
8395 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008396
Rob Clark51fd3712013-11-19 12:10:12 -05008397 ret = drm_modeset_lock(&crtc->mutex, ctx);
8398 if (ret)
8399 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01008400
Daniel Vetter24218aa2012-08-12 19:27:11 +02008401 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008402 old->load_detect_temp = false;
8403
8404 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008405 if (connector->dpms != DRM_MODE_DPMS_ON)
8406 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008407
Chris Wilson71731882011-04-19 23:10:58 +01008408 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008409 }
8410
8411 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008412 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008413 i++;
8414 if (!(encoder->possible_crtcs & (1 << i)))
8415 continue;
8416 if (!possible_crtc->enabled) {
8417 crtc = possible_crtc;
8418 break;
8419 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008420 }
8421
8422 /*
8423 * If we didn't find an unused CRTC, don't use any.
8424 */
8425 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008426 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05008427 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08008428 }
8429
Rob Clark51fd3712013-11-19 12:10:12 -05008430 ret = drm_modeset_lock(&crtc->mutex, ctx);
8431 if (ret)
8432 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02008433 intel_encoder->new_crtc = to_intel_crtc(crtc);
8434 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008435
8436 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008437 intel_crtc->new_enabled = true;
8438 intel_crtc->new_config = &intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008439 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008440 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008441 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008442
Chris Wilson64927112011-04-20 07:25:26 +01008443 if (!mode)
8444 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008445
Chris Wilsond2dff872011-04-19 08:36:26 +01008446 /* We need a framebuffer large enough to accommodate all accesses
8447 * that the plane may generate whilst we perform load detection.
8448 * We can not rely on the fbcon either being present (we get called
8449 * during its initialisation to detect all boot displays, or it may
8450 * not even exist) or that it is large enough to satisfy the
8451 * requested mode.
8452 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008453 fb = mode_fits_in_fbdev(dev, mode);
8454 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008455 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008456 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8457 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008458 } else
8459 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008460 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008461 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008462 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008463 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008464
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008465 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008466 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008467 if (old->release_fb)
8468 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008469 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008470 }
Chris Wilson71731882011-04-19 23:10:58 +01008471
Jesse Barnes79e53942008-11-07 14:24:08 -08008472 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008473 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008474 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008475
8476 fail:
8477 intel_crtc->new_enabled = crtc->enabled;
8478 if (intel_crtc->new_enabled)
8479 intel_crtc->new_config = &intel_crtc->config;
8480 else
8481 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05008482fail_unlock:
8483 if (ret == -EDEADLK) {
8484 drm_modeset_backoff(ctx);
8485 goto retry;
8486 }
8487
8488 drm_modeset_drop_locks(ctx);
8489 drm_modeset_acquire_fini(ctx);
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008490
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008491 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008492}
8493
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008494void intel_release_load_detect_pipe(struct drm_connector *connector,
Rob Clark51fd3712013-11-19 12:10:12 -05008495 struct intel_load_detect_pipe *old,
8496 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008497{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008498 struct intel_encoder *intel_encoder =
8499 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008500 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008501 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008502 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008503
Chris Wilsond2dff872011-04-19 08:36:26 +01008504 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008505 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008506 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008507
Chris Wilson8261b192011-04-19 23:18:09 +01008508 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008509 to_intel_connector(connector)->new_encoder = NULL;
8510 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008511 intel_crtc->new_enabled = false;
8512 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008513 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01008514
Daniel Vetter36206362012-12-10 20:42:17 +01008515 if (old->release_fb) {
8516 drm_framebuffer_unregister_private(old->release_fb);
8517 drm_framebuffer_unreference(old->release_fb);
8518 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008519
Rob Clark51fd3712013-11-19 12:10:12 -05008520 goto unlock;
Chris Wilson0622a532011-04-21 09:32:11 +01008521 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008522 }
8523
Eric Anholtc751ce42010-03-25 11:48:48 -07008524 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008525 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8526 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01008527
Rob Clark51fd3712013-11-19 12:10:12 -05008528unlock:
8529 drm_modeset_drop_locks(ctx);
8530 drm_modeset_acquire_fini(ctx);
Jesse Barnes79e53942008-11-07 14:24:08 -08008531}
8532
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008533static int i9xx_pll_refclk(struct drm_device *dev,
8534 const struct intel_crtc_config *pipe_config)
8535{
8536 struct drm_i915_private *dev_priv = dev->dev_private;
8537 u32 dpll = pipe_config->dpll_hw_state.dpll;
8538
8539 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008540 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008541 else if (HAS_PCH_SPLIT(dev))
8542 return 120000;
8543 else if (!IS_GEN2(dev))
8544 return 96000;
8545 else
8546 return 48000;
8547}
8548
Jesse Barnes79e53942008-11-07 14:24:08 -08008549/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008550static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8551 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08008552{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008553 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008554 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008555 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008556 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008557 u32 fp;
8558 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008559 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08008560
8561 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03008562 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008563 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008564 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008565
8566 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008567 if (IS_PINEVIEW(dev)) {
8568 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8569 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008570 } else {
8571 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8572 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8573 }
8574
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008575 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008576 if (IS_PINEVIEW(dev))
8577 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8578 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008579 else
8580 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008581 DPLL_FPA01_P1_POST_DIV_SHIFT);
8582
8583 switch (dpll & DPLL_MODE_MASK) {
8584 case DPLLB_MODE_DAC_SERIAL:
8585 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8586 5 : 10;
8587 break;
8588 case DPLLB_MODE_LVDS:
8589 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8590 7 : 14;
8591 break;
8592 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008593 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008594 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008595 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008596 }
8597
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008598 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008599 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008600 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008601 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008602 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008603 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008604 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008605
8606 if (is_lvds) {
8607 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8608 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008609
8610 if (lvds & LVDS_CLKB_POWER_UP)
8611 clock.p2 = 7;
8612 else
8613 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008614 } else {
8615 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8616 clock.p1 = 2;
8617 else {
8618 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8619 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8620 }
8621 if (dpll & PLL_P2_DIVIDE_BY_4)
8622 clock.p2 = 4;
8623 else
8624 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008625 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008626
8627 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008628 }
8629
Ville Syrjälä18442d02013-09-13 16:00:08 +03008630 /*
8631 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008632 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03008633 * encoder's get_config() function.
8634 */
8635 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008636}
8637
Ville Syrjälä6878da02013-09-13 15:59:11 +03008638int intel_dotclock_calculate(int link_freq,
8639 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008640{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008641 /*
8642 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008643 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008644 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008645 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008646 *
8647 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008648 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008649 */
8650
Ville Syrjälä6878da02013-09-13 15:59:11 +03008651 if (!m_n->link_n)
8652 return 0;
8653
8654 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8655}
8656
Ville Syrjälä18442d02013-09-13 16:00:08 +03008657static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8658 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008659{
8660 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008661
8662 /* read out port_clock from the DPLL */
8663 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008664
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008665 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008666 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008667 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008668 * agree once we know their relationship in the encoder's
8669 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008670 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008671 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008672 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8673 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008674}
8675
8676/** Returns the currently programmed mode of the given pipe. */
8677struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8678 struct drm_crtc *crtc)
8679{
Jesse Barnes548f2452011-02-17 10:40:53 -08008680 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008682 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008683 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008684 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008685 int htot = I915_READ(HTOTAL(cpu_transcoder));
8686 int hsync = I915_READ(HSYNC(cpu_transcoder));
8687 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8688 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008689 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008690
8691 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8692 if (!mode)
8693 return NULL;
8694
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008695 /*
8696 * Construct a pipe_config sufficient for getting the clock info
8697 * back out of crtc_clock_get.
8698 *
8699 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8700 * to use a real value here instead.
8701 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008702 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008703 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008704 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8705 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8706 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008707 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8708
Ville Syrjälä773ae032013-09-23 17:48:20 +03008709 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008710 mode->hdisplay = (htot & 0xffff) + 1;
8711 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8712 mode->hsync_start = (hsync & 0xffff) + 1;
8713 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8714 mode->vdisplay = (vtot & 0xffff) + 1;
8715 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8716 mode->vsync_start = (vsync & 0xffff) + 1;
8717 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8718
8719 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008720
8721 return mode;
8722}
8723
Daniel Vetter3dec0092010-08-20 21:40:52 +02008724static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07008725{
8726 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008727 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008728 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8729 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008730 int dpll_reg = DPLL(pipe);
8731 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008732
Eric Anholtbad720f2009-10-22 16:11:14 -07008733 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008734 return;
8735
8736 if (!dev_priv->lvds_downclock_avail)
8737 return;
8738
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008739 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008740 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008741 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008742
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008743 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008744
8745 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8746 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008747 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008748
Jesse Barnes652c3932009-08-17 13:31:43 -07008749 dpll = I915_READ(dpll_reg);
8750 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008751 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008752 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008753}
8754
8755static void intel_decrease_pllclock(struct drm_crtc *crtc)
8756{
8757 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008758 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008759 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008760
Eric Anholtbad720f2009-10-22 16:11:14 -07008761 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008762 return;
8763
8764 if (!dev_priv->lvds_downclock_avail)
8765 return;
8766
8767 /*
8768 * Since this is called by a timer, we should never get here in
8769 * the manual case.
8770 */
8771 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008772 int pipe = intel_crtc->pipe;
8773 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008774 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008775
Zhao Yakui44d98a62009-10-09 11:39:40 +08008776 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008777
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008778 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008779
Chris Wilson074b5e12012-05-02 12:07:06 +01008780 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008781 dpll |= DISPLAY_RATE_SELECT_FPA1;
8782 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008783 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008784 dpll = I915_READ(dpll_reg);
8785 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008786 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008787 }
8788
8789}
8790
Chris Wilsonf047e392012-07-21 12:31:41 +01008791void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008792{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008793 struct drm_i915_private *dev_priv = dev->dev_private;
8794
Chris Wilsonf62a0072014-02-21 17:55:39 +00008795 if (dev_priv->mm.busy)
8796 return;
8797
Paulo Zanoni43694d62014-03-07 20:08:08 -03008798 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008799 i915_update_gfx_val(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00008800 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01008801}
8802
8803void intel_mark_idle(struct drm_device *dev)
8804{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008805 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008806 struct drm_crtc *crtc;
8807
Chris Wilsonf62a0072014-02-21 17:55:39 +00008808 if (!dev_priv->mm.busy)
8809 return;
8810
8811 dev_priv->mm.busy = false;
8812
Jani Nikulad330a952014-01-21 11:24:25 +02008813 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008814 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00008815
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008816 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07008817 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00008818 continue;
8819
8820 intel_decrease_pllclock(crtc);
8821 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008822
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008823 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008824 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008825
8826out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03008827 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008828}
8829
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -07008830
Chris Wilsonc65355b2013-06-06 16:53:41 -03008831void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01008832 struct intel_engine_cs *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008833{
8834 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008835 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008836
Daniel Vetter3108e992014-06-18 13:59:05 +02008837 intel_edp_psr_exit(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -07008838
Jani Nikulad330a952014-01-21 11:24:25 +02008839 if (!i915.powersave)
Jesse Barnes652c3932009-08-17 13:31:43 -07008840 return;
8841
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008842 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07008843 if (!crtc->primary->fb)
Jesse Barnes652c3932009-08-17 13:31:43 -07008844 continue;
8845
Matt Roperf4510a22014-04-01 15:22:40 -07008846 if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
Chris Wilsonc65355b2013-06-06 16:53:41 -03008847 continue;
8848
8849 intel_increase_pllclock(crtc);
8850 if (ring && intel_fbc_enabled(dev))
8851 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008852 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008853}
8854
Jesse Barnes79e53942008-11-07 14:24:08 -08008855static void intel_crtc_destroy(struct drm_crtc *crtc)
8856{
8857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008858 struct drm_device *dev = crtc->dev;
8859 struct intel_unpin_work *work;
8860 unsigned long flags;
8861
8862 spin_lock_irqsave(&dev->event_lock, flags);
8863 work = intel_crtc->unpin_work;
8864 intel_crtc->unpin_work = NULL;
8865 spin_unlock_irqrestore(&dev->event_lock, flags);
8866
8867 if (work) {
8868 cancel_work_sync(&work->work);
8869 kfree(work);
8870 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008871
8872 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008873
Jesse Barnes79e53942008-11-07 14:24:08 -08008874 kfree(intel_crtc);
8875}
8876
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008877static void intel_unpin_work_fn(struct work_struct *__work)
8878{
8879 struct intel_unpin_work *work =
8880 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008881 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008882
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008883 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008884 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008885 drm_gem_object_unreference(&work->pending_flip_obj->base);
8886 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008887
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008888 intel_update_fbc(dev);
8889 mutex_unlock(&dev->struct_mutex);
8890
8891 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8892 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8893
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008894 kfree(work);
8895}
8896
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008897static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008898 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008899{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008900 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8902 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008903 unsigned long flags;
8904
8905 /* Ignore early vblank irqs */
8906 if (intel_crtc == NULL)
8907 return;
8908
8909 spin_lock_irqsave(&dev->event_lock, flags);
8910 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008911
8912 /* Ensure we don't miss a work->pending update ... */
8913 smp_rmb();
8914
8915 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008916 spin_unlock_irqrestore(&dev->event_lock, flags);
8917 return;
8918 }
8919
Chris Wilsone7d841c2012-12-03 11:36:30 +00008920 /* and that the unpin work is consistent wrt ->pending. */
8921 smp_rmb();
8922
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008923 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008924
Rob Clark45a066e2012-10-08 14:50:40 -05008925 if (work->event)
8926 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008927
Daniel Vetter87b6b102014-05-15 15:33:46 +02008928 drm_crtc_vblank_put(crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008929
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008930 spin_unlock_irqrestore(&dev->event_lock, flags);
8931
Daniel Vetter2c10d572012-12-20 21:24:07 +01008932 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008933
8934 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008935
8936 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008937}
8938
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008939void intel_finish_page_flip(struct drm_device *dev, int pipe)
8940{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008941 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008942 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8943
Mario Kleiner49b14a52010-12-09 07:00:07 +01008944 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008945}
8946
8947void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8948{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008949 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008950 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8951
Mario Kleiner49b14a52010-12-09 07:00:07 +01008952 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008953}
8954
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03008955/* Is 'a' after or equal to 'b'? */
8956static bool g4x_flip_count_after_eq(u32 a, u32 b)
8957{
8958 return !((a - b) & 0x80000000);
8959}
8960
8961static bool page_flip_finished(struct intel_crtc *crtc)
8962{
8963 struct drm_device *dev = crtc->base.dev;
8964 struct drm_i915_private *dev_priv = dev->dev_private;
8965
8966 /*
8967 * The relevant registers doen't exist on pre-ctg.
8968 * As the flip done interrupt doesn't trigger for mmio
8969 * flips on gmch platforms, a flip count check isn't
8970 * really needed there. But since ctg has the registers,
8971 * include it in the check anyway.
8972 */
8973 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
8974 return true;
8975
8976 /*
8977 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
8978 * used the same base address. In that case the mmio flip might
8979 * have completed, but the CS hasn't even executed the flip yet.
8980 *
8981 * A flip count check isn't enough as the CS might have updated
8982 * the base address just after start of vblank, but before we
8983 * managed to process the interrupt. This means we'd complete the
8984 * CS flip too soon.
8985 *
8986 * Combining both checks should get us a good enough result. It may
8987 * still happen that the CS flip has been executed, but has not
8988 * yet actually completed. But in case the base address is the same
8989 * anyway, we don't really care.
8990 */
8991 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
8992 crtc->unpin_work->gtt_offset &&
8993 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
8994 crtc->unpin_work->flip_count);
8995}
8996
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008997void intel_prepare_page_flip(struct drm_device *dev, int plane)
8998{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008999 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009000 struct intel_crtc *intel_crtc =
9001 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9002 unsigned long flags;
9003
Chris Wilsone7d841c2012-12-03 11:36:30 +00009004 /* NB: An MMIO update of the plane base pointer will also
9005 * generate a page-flip completion irq, i.e. every modeset
9006 * is also accompanied by a spurious intel_prepare_page_flip().
9007 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009008 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009009 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009010 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009011 spin_unlock_irqrestore(&dev->event_lock, flags);
9012}
9013
Robin Schroereba905b2014-05-18 02:24:50 +02009014static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009015{
9016 /* Ensure that the work item is consistent when activating it ... */
9017 smp_wmb();
9018 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9019 /* and that it is marked active as soon as the irq could fire. */
9020 smp_wmb();
9021}
9022
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009023static int intel_gen2_queue_flip(struct drm_device *dev,
9024 struct drm_crtc *crtc,
9025 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009026 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009027 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009028 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009029{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009031 u32 flip_mask;
9032 int ret;
9033
Daniel Vetter6d90c952012-04-26 23:28:05 +02009034 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009035 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009036 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009037
9038 /* Can't queue multiple flips, so wait for the previous
9039 * one to finish before executing the next.
9040 */
9041 if (intel_crtc->plane)
9042 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9043 else
9044 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009045 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9046 intel_ring_emit(ring, MI_NOOP);
9047 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9048 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9049 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009050 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009051 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009052
9053 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009054 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009055 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009056}
9057
9058static int intel_gen3_queue_flip(struct drm_device *dev,
9059 struct drm_crtc *crtc,
9060 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009061 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009062 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009063 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009064{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009065 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009066 u32 flip_mask;
9067 int ret;
9068
Daniel Vetter6d90c952012-04-26 23:28:05 +02009069 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009070 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009071 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009072
9073 if (intel_crtc->plane)
9074 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9075 else
9076 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009077 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9078 intel_ring_emit(ring, MI_NOOP);
9079 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9080 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9081 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009082 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009083 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009084
Chris Wilsone7d841c2012-12-03 11:36:30 +00009085 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009086 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009087 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009088}
9089
9090static int intel_gen4_queue_flip(struct drm_device *dev,
9091 struct drm_crtc *crtc,
9092 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009093 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009094 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009095 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009096{
9097 struct drm_i915_private *dev_priv = dev->dev_private;
9098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9099 uint32_t pf, pipesrc;
9100 int ret;
9101
Daniel Vetter6d90c952012-04-26 23:28:05 +02009102 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009103 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009104 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009105
9106 /* i965+ uses the linear or tiled offsets from the
9107 * Display Registers (which do not change across a page-flip)
9108 * so we need only reprogram the base address.
9109 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009110 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9111 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9112 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009113 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009114 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009115
9116 /* XXX Enabling the panel-fitter across page-flip is so far
9117 * untested on non-native modes, so ignore it for now.
9118 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9119 */
9120 pf = 0;
9121 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009122 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009123
9124 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009125 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009126 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009127}
9128
9129static int intel_gen6_queue_flip(struct drm_device *dev,
9130 struct drm_crtc *crtc,
9131 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009132 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009133 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009134 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009135{
9136 struct drm_i915_private *dev_priv = dev->dev_private;
9137 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9138 uint32_t pf, pipesrc;
9139 int ret;
9140
Daniel Vetter6d90c952012-04-26 23:28:05 +02009141 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009142 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009143 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009144
Daniel Vetter6d90c952012-04-26 23:28:05 +02009145 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9146 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9147 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009148 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009149
Chris Wilson99d9acd2012-04-17 20:37:00 +01009150 /* Contrary to the suggestions in the documentation,
9151 * "Enable Panel Fitter" does not seem to be required when page
9152 * flipping with a non-native mode, and worse causes a normal
9153 * modeset to fail.
9154 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9155 */
9156 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009157 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009158 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009159
9160 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009161 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009162 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009163}
9164
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009165static int intel_gen7_queue_flip(struct drm_device *dev,
9166 struct drm_crtc *crtc,
9167 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009168 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009169 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009170 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009171{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009173 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009174 int len, ret;
9175
Robin Schroereba905b2014-05-18 02:24:50 +02009176 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009177 case PLANE_A:
9178 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9179 break;
9180 case PLANE_B:
9181 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9182 break;
9183 case PLANE_C:
9184 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9185 break;
9186 default:
9187 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009188 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009189 }
9190
Chris Wilsonffe74d72013-08-26 20:58:12 +01009191 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009192 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009193 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009194 /*
9195 * On Gen 8, SRM is now taking an extra dword to accommodate
9196 * 48bits addresses, and we need a NOOP for the batch size to
9197 * stay even.
9198 */
9199 if (IS_GEN8(dev))
9200 len += 2;
9201 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009202
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009203 /*
9204 * BSpec MI_DISPLAY_FLIP for IVB:
9205 * "The full packet must be contained within the same cache line."
9206 *
9207 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9208 * cacheline, if we ever start emitting more commands before
9209 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9210 * then do the cacheline alignment, and finally emit the
9211 * MI_DISPLAY_FLIP.
9212 */
9213 ret = intel_ring_cacheline_align(ring);
9214 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009215 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009216
Chris Wilsonffe74d72013-08-26 20:58:12 +01009217 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009218 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009219 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009220
Chris Wilsonffe74d72013-08-26 20:58:12 +01009221 /* Unmask the flip-done completion message. Note that the bspec says that
9222 * we should do this for both the BCS and RCS, and that we must not unmask
9223 * more than one flip event at any time (or ensure that one flip message
9224 * can be sent by waiting for flip-done prior to queueing new flips).
9225 * Experimentation says that BCS works despite DERRMR masking all
9226 * flip-done completion events and that unmasking all planes at once
9227 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9228 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9229 */
9230 if (ring->id == RCS) {
9231 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9232 intel_ring_emit(ring, DERRMR);
9233 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9234 DERRMR_PIPEB_PRI_FLIP_DONE |
9235 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009236 if (IS_GEN8(dev))
9237 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9238 MI_SRM_LRM_GLOBAL_GTT);
9239 else
9240 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9241 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009242 intel_ring_emit(ring, DERRMR);
9243 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009244 if (IS_GEN8(dev)) {
9245 intel_ring_emit(ring, 0);
9246 intel_ring_emit(ring, MI_NOOP);
9247 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009248 }
9249
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009250 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009251 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009252 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009253 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009254
9255 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009256 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009257 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009258}
9259
Sourab Gupta84c33a62014-06-02 16:47:17 +05309260static bool use_mmio_flip(struct intel_engine_cs *ring,
9261 struct drm_i915_gem_object *obj)
9262{
9263 /*
9264 * This is not being used for older platforms, because
9265 * non-availability of flip done interrupt forces us to use
9266 * CS flips. Older platforms derive flip done using some clever
9267 * tricks involving the flip_pending status bits and vblank irqs.
9268 * So using MMIO flips there would disrupt this mechanism.
9269 */
9270
9271 if (INTEL_INFO(ring->dev)->gen < 5)
9272 return false;
9273
9274 if (i915.use_mmio_flip < 0)
9275 return false;
9276 else if (i915.use_mmio_flip > 0)
9277 return true;
9278 else
9279 return ring != obj->ring;
9280}
9281
9282static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9283{
9284 struct drm_device *dev = intel_crtc->base.dev;
9285 struct drm_i915_private *dev_priv = dev->dev_private;
9286 struct intel_framebuffer *intel_fb =
9287 to_intel_framebuffer(intel_crtc->base.primary->fb);
9288 struct drm_i915_gem_object *obj = intel_fb->obj;
9289 u32 dspcntr;
9290 u32 reg;
9291
9292 intel_mark_page_flip_active(intel_crtc);
9293
9294 reg = DSPCNTR(intel_crtc->plane);
9295 dspcntr = I915_READ(reg);
9296
9297 if (INTEL_INFO(dev)->gen >= 4) {
9298 if (obj->tiling_mode != I915_TILING_NONE)
9299 dspcntr |= DISPPLANE_TILED;
9300 else
9301 dspcntr &= ~DISPPLANE_TILED;
9302 }
9303 I915_WRITE(reg, dspcntr);
9304
9305 I915_WRITE(DSPSURF(intel_crtc->plane),
9306 intel_crtc->unpin_work->gtt_offset);
9307 POSTING_READ(DSPSURF(intel_crtc->plane));
9308}
9309
9310static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9311{
9312 struct intel_engine_cs *ring;
9313 int ret;
9314
9315 lockdep_assert_held(&obj->base.dev->struct_mutex);
9316
9317 if (!obj->last_write_seqno)
9318 return 0;
9319
9320 ring = obj->ring;
9321
9322 if (i915_seqno_passed(ring->get_seqno(ring, true),
9323 obj->last_write_seqno))
9324 return 0;
9325
9326 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9327 if (ret)
9328 return ret;
9329
9330 if (WARN_ON(!ring->irq_get(ring)))
9331 return 0;
9332
9333 return 1;
9334}
9335
9336void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9337{
9338 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9339 struct intel_crtc *intel_crtc;
9340 unsigned long irq_flags;
9341 u32 seqno;
9342
9343 seqno = ring->get_seqno(ring, false);
9344
9345 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9346 for_each_intel_crtc(ring->dev, intel_crtc) {
9347 struct intel_mmio_flip *mmio_flip;
9348
9349 mmio_flip = &intel_crtc->mmio_flip;
9350 if (mmio_flip->seqno == 0)
9351 continue;
9352
9353 if (ring->id != mmio_flip->ring_id)
9354 continue;
9355
9356 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9357 intel_do_mmio_flip(intel_crtc);
9358 mmio_flip->seqno = 0;
9359 ring->irq_put(ring);
9360 }
9361 }
9362 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9363}
9364
9365static int intel_queue_mmio_flip(struct drm_device *dev,
9366 struct drm_crtc *crtc,
9367 struct drm_framebuffer *fb,
9368 struct drm_i915_gem_object *obj,
9369 struct intel_engine_cs *ring,
9370 uint32_t flags)
9371{
9372 struct drm_i915_private *dev_priv = dev->dev_private;
9373 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9374 unsigned long irq_flags;
9375 int ret;
9376
9377 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9378 return -EBUSY;
9379
9380 ret = intel_postpone_flip(obj);
9381 if (ret < 0)
9382 return ret;
9383 if (ret == 0) {
9384 intel_do_mmio_flip(intel_crtc);
9385 return 0;
9386 }
9387
9388 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9389 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9390 intel_crtc->mmio_flip.ring_id = obj->ring->id;
9391 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9392
9393 /*
9394 * Double check to catch cases where irq fired before
9395 * mmio flip data was ready
9396 */
9397 intel_notify_mmio_flip(obj->ring);
9398 return 0;
9399}
9400
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009401static int intel_default_queue_flip(struct drm_device *dev,
9402 struct drm_crtc *crtc,
9403 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009404 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009405 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009406 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009407{
9408 return -ENODEV;
9409}
9410
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009411static int intel_crtc_page_flip(struct drm_crtc *crtc,
9412 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009413 struct drm_pending_vblank_event *event,
9414 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009415{
9416 struct drm_device *dev = crtc->dev;
9417 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009418 struct drm_framebuffer *old_fb = crtc->primary->fb;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02009419 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02009421 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009422 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009423 struct intel_engine_cs *ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009424 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01009425 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009426
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009427 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009428 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009429 return -EINVAL;
9430
9431 /*
9432 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9433 * Note that pitch changes could also affect these register.
9434 */
9435 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009436 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9437 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009438 return -EINVAL;
9439
Chris Wilsonf900db42014-02-20 09:26:13 +00009440 if (i915_terminally_wedged(&dev_priv->gpu_error))
9441 goto out_hang;
9442
Daniel Vetterb14c5672013-09-19 12:18:32 +02009443 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009444 if (work == NULL)
9445 return -ENOMEM;
9446
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -07009447 /* Exit PSR early in page flip */
Daniel Vetter3108e992014-06-18 13:59:05 +02009448 intel_edp_psr_exit(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -07009449
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009450 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009451 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02009452 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009453 INIT_WORK(&work->work, intel_unpin_work_fn);
9454
Daniel Vetter87b6b102014-05-15 15:33:46 +02009455 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009456 if (ret)
9457 goto free_work;
9458
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009459 /* We borrow the event spin lock for protecting unpin_work */
9460 spin_lock_irqsave(&dev->event_lock, flags);
9461 if (intel_crtc->unpin_work) {
9462 spin_unlock_irqrestore(&dev->event_lock, flags);
9463 kfree(work);
Daniel Vetter87b6b102014-05-15 15:33:46 +02009464 drm_crtc_vblank_put(crtc);
Chris Wilson468f0b42010-05-27 13:18:13 +01009465
9466 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009467 return -EBUSY;
9468 }
9469 intel_crtc->unpin_work = work;
9470 spin_unlock_irqrestore(&dev->event_lock, flags);
9471
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009472 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9473 flush_workqueue(dev_priv->wq);
9474
Chris Wilson79158102012-05-23 11:13:58 +01009475 ret = i915_mutex_lock_interruptible(dev);
9476 if (ret)
9477 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009478
Jesse Barnes75dfca82010-02-10 15:09:44 -08009479 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00009480 drm_gem_object_reference(&work->old_fb_obj->base);
9481 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009482
Matt Roperf4510a22014-04-01 15:22:40 -07009483 crtc->primary->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01009484
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009485 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009486
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01009487 work->enable_stall_check = true;
9488
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009489 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02009490 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009491
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009492 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +02009493 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009494
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009495 if (IS_VALLEYVIEW(dev)) {
9496 ring = &dev_priv->ring[BCS];
9497 } else if (INTEL_INFO(dev)->gen >= 7) {
9498 ring = obj->ring;
9499 if (ring == NULL || ring->id != RCS)
9500 ring = &dev_priv->ring[BCS];
9501 } else {
9502 ring = &dev_priv->ring[RCS];
9503 }
9504
9505 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009506 if (ret)
9507 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009508
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009509 work->gtt_offset =
9510 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9511
Sourab Gupta84c33a62014-06-02 16:47:17 +05309512 if (use_mmio_flip(ring, obj))
9513 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9514 page_flip_flags);
9515 else
9516 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
9517 page_flip_flags);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009518 if (ret)
9519 goto cleanup_unpin;
9520
Daniel Vettera071fa02014-06-18 23:28:09 +02009521 i915_gem_track_fb(work->old_fb_obj, obj,
9522 INTEL_FRONTBUFFER_PRIMARY(pipe));
9523
Chris Wilson7782de32011-07-08 12:22:41 +01009524 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03009525 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009526 mutex_unlock(&dev->struct_mutex);
9527
Jesse Barnese5510fa2010-07-01 16:48:37 -07009528 trace_i915_flip_request(intel_crtc->plane, obj);
9529
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009530 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01009531
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009532cleanup_unpin:
9533 intel_unpin_fb_obj(obj);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009534cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009535 atomic_dec(&intel_crtc->unpin_work_count);
Matt Roperf4510a22014-04-01 15:22:40 -07009536 crtc->primary->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00009537 drm_gem_object_unreference(&work->old_fb_obj->base);
9538 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01009539 mutex_unlock(&dev->struct_mutex);
9540
Chris Wilson79158102012-05-23 11:13:58 +01009541cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01009542 spin_lock_irqsave(&dev->event_lock, flags);
9543 intel_crtc->unpin_work = NULL;
9544 spin_unlock_irqrestore(&dev->event_lock, flags);
9545
Daniel Vetter87b6b102014-05-15 15:33:46 +02009546 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009547free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01009548 kfree(work);
9549
Chris Wilsonf900db42014-02-20 09:26:13 +00009550 if (ret == -EIO) {
9551out_hang:
9552 intel_crtc_wait_for_pending_flips(crtc);
9553 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9554 if (ret == 0 && event)
Daniel Vettera071fa02014-06-18 23:28:09 +02009555 drm_send_vblank_event(dev, pipe, event);
Chris Wilsonf900db42014-02-20 09:26:13 +00009556 }
Chris Wilson96b099f2010-06-07 14:03:04 +01009557 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009558}
9559
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009560static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009561 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9562 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009563};
9564
Daniel Vetter9a935852012-07-05 22:34:27 +02009565/**
9566 * intel_modeset_update_staged_output_state
9567 *
9568 * Updates the staged output configuration state, e.g. after we've read out the
9569 * current hw state.
9570 */
9571static void intel_modeset_update_staged_output_state(struct drm_device *dev)
9572{
Ville Syrjälä76688512014-01-10 11:28:06 +02009573 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009574 struct intel_encoder *encoder;
9575 struct intel_connector *connector;
9576
9577 list_for_each_entry(connector, &dev->mode_config.connector_list,
9578 base.head) {
9579 connector->new_encoder =
9580 to_intel_encoder(connector->base.encoder);
9581 }
9582
9583 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9584 base.head) {
9585 encoder->new_crtc =
9586 to_intel_crtc(encoder->base.crtc);
9587 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009588
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009589 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009590 crtc->new_enabled = crtc->base.enabled;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009591
9592 if (crtc->new_enabled)
9593 crtc->new_config = &crtc->config;
9594 else
9595 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +02009596 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009597}
9598
9599/**
9600 * intel_modeset_commit_output_state
9601 *
9602 * This function copies the stage display pipe configuration to the real one.
9603 */
9604static void intel_modeset_commit_output_state(struct drm_device *dev)
9605{
Ville Syrjälä76688512014-01-10 11:28:06 +02009606 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009607 struct intel_encoder *encoder;
9608 struct intel_connector *connector;
9609
9610 list_for_each_entry(connector, &dev->mode_config.connector_list,
9611 base.head) {
9612 connector->base.encoder = &connector->new_encoder->base;
9613 }
9614
9615 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9616 base.head) {
9617 encoder->base.crtc = &encoder->new_crtc->base;
9618 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009619
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009620 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009621 crtc->base.enabled = crtc->new_enabled;
9622 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009623}
9624
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009625static void
Robin Schroereba905b2014-05-18 02:24:50 +02009626connected_sink_compute_bpp(struct intel_connector *connector,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009627 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009628{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009629 int bpp = pipe_config->pipe_bpp;
9630
9631 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9632 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03009633 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009634
9635 /* Don't use an invalid EDID bpc value */
9636 if (connector->base.display_info.bpc &&
9637 connector->base.display_info.bpc * 3 < bpp) {
9638 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9639 bpp, connector->base.display_info.bpc*3);
9640 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9641 }
9642
9643 /* Clamp bpp to 8 on screens without EDID 1.4 */
9644 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9645 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9646 bpp);
9647 pipe_config->pipe_bpp = 24;
9648 }
9649}
9650
9651static int
9652compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9653 struct drm_framebuffer *fb,
9654 struct intel_crtc_config *pipe_config)
9655{
9656 struct drm_device *dev = crtc->base.dev;
9657 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009658 int bpp;
9659
Daniel Vetterd42264b2013-03-28 16:38:08 +01009660 switch (fb->pixel_format) {
9661 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009662 bpp = 8*3; /* since we go through a colormap */
9663 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009664 case DRM_FORMAT_XRGB1555:
9665 case DRM_FORMAT_ARGB1555:
9666 /* checked in intel_framebuffer_init already */
9667 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9668 return -EINVAL;
9669 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009670 bpp = 6*3; /* min is 18bpp */
9671 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009672 case DRM_FORMAT_XBGR8888:
9673 case DRM_FORMAT_ABGR8888:
9674 /* checked in intel_framebuffer_init already */
9675 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9676 return -EINVAL;
9677 case DRM_FORMAT_XRGB8888:
9678 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009679 bpp = 8*3;
9680 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009681 case DRM_FORMAT_XRGB2101010:
9682 case DRM_FORMAT_ARGB2101010:
9683 case DRM_FORMAT_XBGR2101010:
9684 case DRM_FORMAT_ABGR2101010:
9685 /* checked in intel_framebuffer_init already */
9686 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01009687 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009688 bpp = 10*3;
9689 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01009690 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009691 default:
9692 DRM_DEBUG_KMS("unsupported depth\n");
9693 return -EINVAL;
9694 }
9695
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009696 pipe_config->pipe_bpp = bpp;
9697
9698 /* Clamp display bpp to EDID value */
9699 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009700 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02009701 if (!connector->new_encoder ||
9702 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009703 continue;
9704
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009705 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009706 }
9707
9708 return bpp;
9709}
9710
Daniel Vetter644db712013-09-19 14:53:58 +02009711static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9712{
9713 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9714 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01009715 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02009716 mode->crtc_hdisplay, mode->crtc_hsync_start,
9717 mode->crtc_hsync_end, mode->crtc_htotal,
9718 mode->crtc_vdisplay, mode->crtc_vsync_start,
9719 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9720}
9721
Daniel Vetterc0b03412013-05-28 12:05:54 +02009722static void intel_dump_pipe_config(struct intel_crtc *crtc,
9723 struct intel_crtc_config *pipe_config,
9724 const char *context)
9725{
9726 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9727 context, pipe_name(crtc->pipe));
9728
9729 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9730 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9731 pipe_config->pipe_bpp, pipe_config->dither);
9732 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9733 pipe_config->has_pch_encoder,
9734 pipe_config->fdi_lanes,
9735 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9736 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9737 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009738 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9739 pipe_config->has_dp_encoder,
9740 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9741 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9742 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009743 DRM_DEBUG_KMS("requested mode:\n");
9744 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9745 DRM_DEBUG_KMS("adjusted mode:\n");
9746 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02009747 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009748 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009749 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9750 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009751 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9752 pipe_config->gmch_pfit.control,
9753 pipe_config->gmch_pfit.pgm_ratios,
9754 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009755 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02009756 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009757 pipe_config->pch_pfit.size,
9758 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009759 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03009760 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009761}
9762
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009763static bool encoders_cloneable(const struct intel_encoder *a,
9764 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009765{
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009766 /* masks could be asymmetric, so check both ways */
9767 return a == b || (a->cloneable & (1 << b->type) &&
9768 b->cloneable & (1 << a->type));
9769}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009770
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009771static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9772 struct intel_encoder *encoder)
9773{
9774 struct drm_device *dev = crtc->base.dev;
9775 struct intel_encoder *source_encoder;
9776
9777 list_for_each_entry(source_encoder,
9778 &dev->mode_config.encoder_list, base.head) {
9779 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009780 continue;
9781
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009782 if (!encoders_cloneable(encoder, source_encoder))
9783 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009784 }
9785
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009786 return true;
9787}
9788
9789static bool check_encoder_cloning(struct intel_crtc *crtc)
9790{
9791 struct drm_device *dev = crtc->base.dev;
9792 struct intel_encoder *encoder;
9793
9794 list_for_each_entry(encoder,
9795 &dev->mode_config.encoder_list, base.head) {
9796 if (encoder->new_crtc != crtc)
9797 continue;
9798
9799 if (!check_single_encoder_cloning(crtc, encoder))
9800 return false;
9801 }
9802
9803 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009804}
9805
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009806static struct intel_crtc_config *
9807intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009808 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009809 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02009810{
9811 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02009812 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009813 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01009814 int plane_bpp, ret = -EINVAL;
9815 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02009816
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009817 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009818 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9819 return ERR_PTR(-EINVAL);
9820 }
9821
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009822 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9823 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02009824 return ERR_PTR(-ENOMEM);
9825
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009826 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9827 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009828
Daniel Vettere143a212013-07-04 12:01:15 +02009829 pipe_config->cpu_transcoder =
9830 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009831 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009832
Imre Deak2960bc92013-07-30 13:36:32 +03009833 /*
9834 * Sanitize sync polarity flags based on requested ones. If neither
9835 * positive or negative polarity is requested, treat this as meaning
9836 * negative polarity.
9837 */
9838 if (!(pipe_config->adjusted_mode.flags &
9839 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9840 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9841
9842 if (!(pipe_config->adjusted_mode.flags &
9843 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9844 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9845
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009846 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9847 * plane pixel format and any sink constraints into account. Returns the
9848 * source plane bpp so that dithering can be selected on mismatches
9849 * after encoders and crtc also have had their say. */
9850 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9851 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009852 if (plane_bpp < 0)
9853 goto fail;
9854
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03009855 /*
9856 * Determine the real pipe dimensions. Note that stereo modes can
9857 * increase the actual pipe size due to the frame doubling and
9858 * insertion of additional space for blanks between the frame. This
9859 * is stored in the crtc timings. We use the requested mode to do this
9860 * computation to clearly distinguish it from the adjusted mode, which
9861 * can be changed by the connectors in the below retry loop.
9862 */
9863 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9864 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9865 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9866
Daniel Vettere29c22c2013-02-21 00:00:16 +01009867encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02009868 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02009869 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02009870 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009871
Daniel Vetter135c81b2013-07-21 21:37:09 +02009872 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01009873 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02009874
Daniel Vetter7758a112012-07-08 19:40:39 +02009875 /* Pass our mode to the connectors and the CRTC to give them a chance to
9876 * adjust it according to limitations or connector properties, and also
9877 * a chance to reject the mode entirely.
9878 */
9879 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9880 base.head) {
9881
9882 if (&encoder->new_crtc->base != crtc)
9883 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01009884
Daniel Vetterefea6e82013-07-21 21:36:59 +02009885 if (!(encoder->compute_config(encoder, pipe_config))) {
9886 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02009887 goto fail;
9888 }
9889 }
9890
Daniel Vetterff9a6752013-06-01 17:16:21 +02009891 /* Set default port clock if not overwritten by the encoder. Needs to be
9892 * done afterwards in case the encoder adjusts the mode. */
9893 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01009894 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9895 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009896
Daniel Vettera43f6e02013-06-07 23:10:32 +02009897 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009898 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02009899 DRM_DEBUG_KMS("CRTC fixup failed\n");
9900 goto fail;
9901 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01009902
9903 if (ret == RETRY) {
9904 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9905 ret = -EINVAL;
9906 goto fail;
9907 }
9908
9909 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9910 retry = false;
9911 goto encoder_retry;
9912 }
9913
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009914 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9915 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9916 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9917
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009918 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02009919fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009920 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009921 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02009922}
9923
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009924/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9925 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9926static void
9927intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9928 unsigned *prepare_pipes, unsigned *disable_pipes)
9929{
9930 struct intel_crtc *intel_crtc;
9931 struct drm_device *dev = crtc->dev;
9932 struct intel_encoder *encoder;
9933 struct intel_connector *connector;
9934 struct drm_crtc *tmp_crtc;
9935
9936 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9937
9938 /* Check which crtcs have changed outputs connected to them, these need
9939 * to be part of the prepare_pipes mask. We don't (yet) support global
9940 * modeset across multiple crtcs, so modeset_pipes will only have one
9941 * bit set at most. */
9942 list_for_each_entry(connector, &dev->mode_config.connector_list,
9943 base.head) {
9944 if (connector->base.encoder == &connector->new_encoder->base)
9945 continue;
9946
9947 if (connector->base.encoder) {
9948 tmp_crtc = connector->base.encoder->crtc;
9949
9950 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9951 }
9952
9953 if (connector->new_encoder)
9954 *prepare_pipes |=
9955 1 << connector->new_encoder->new_crtc->pipe;
9956 }
9957
9958 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9959 base.head) {
9960 if (encoder->base.crtc == &encoder->new_crtc->base)
9961 continue;
9962
9963 if (encoder->base.crtc) {
9964 tmp_crtc = encoder->base.crtc;
9965
9966 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9967 }
9968
9969 if (encoder->new_crtc)
9970 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9971 }
9972
Ville Syrjälä76688512014-01-10 11:28:06 +02009973 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009974 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009975 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009976 continue;
9977
Ville Syrjälä76688512014-01-10 11:28:06 +02009978 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009979 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +02009980 else
9981 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009982 }
9983
9984
9985 /* set_mode is also used to update properties on life display pipes. */
9986 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +02009987 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009988 *prepare_pipes |= 1 << intel_crtc->pipe;
9989
Daniel Vetterb6c51642013-04-12 18:48:43 +02009990 /*
9991 * For simplicity do a full modeset on any pipe where the output routing
9992 * changed. We could be more clever, but that would require us to be
9993 * more careful with calling the relevant encoder->mode_set functions.
9994 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009995 if (*prepare_pipes)
9996 *modeset_pipes = *prepare_pipes;
9997
9998 /* ... and mask these out. */
9999 *modeset_pipes &= ~(*disable_pipes);
10000 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020010001
10002 /*
10003 * HACK: We don't (yet) fully support global modesets. intel_set_config
10004 * obies this rule, but the modeset restore mode of
10005 * intel_modeset_setup_hw_state does not.
10006 */
10007 *modeset_pipes &= 1 << intel_crtc->pipe;
10008 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020010009
10010 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10011 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010012}
10013
Daniel Vetterea9d7582012-07-10 10:42:52 +020010014static bool intel_crtc_in_use(struct drm_crtc *crtc)
10015{
10016 struct drm_encoder *encoder;
10017 struct drm_device *dev = crtc->dev;
10018
10019 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10020 if (encoder->crtc == crtc)
10021 return true;
10022
10023 return false;
10024}
10025
10026static void
10027intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10028{
10029 struct intel_encoder *intel_encoder;
10030 struct intel_crtc *intel_crtc;
10031 struct drm_connector *connector;
10032
10033 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
10034 base.head) {
10035 if (!intel_encoder->base.crtc)
10036 continue;
10037
10038 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10039
10040 if (prepare_pipes & (1 << intel_crtc->pipe))
10041 intel_encoder->connectors_active = false;
10042 }
10043
10044 intel_modeset_commit_output_state(dev);
10045
Ville Syrjälä76688512014-01-10 11:28:06 +020010046 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010047 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010048 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010049 WARN_ON(intel_crtc->new_config &&
10050 intel_crtc->new_config != &intel_crtc->config);
10051 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010052 }
10053
10054 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10055 if (!connector->encoder || !connector->encoder->crtc)
10056 continue;
10057
10058 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10059
10060 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020010061 struct drm_property *dpms_property =
10062 dev->mode_config.dpms_property;
10063
Daniel Vetterea9d7582012-07-10 10:42:52 +020010064 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050010065 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020010066 dpms_property,
10067 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010068
10069 intel_encoder = to_intel_encoder(connector->encoder);
10070 intel_encoder->connectors_active = true;
10071 }
10072 }
10073
10074}
10075
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010076static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010077{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010078 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010079
10080 if (clock1 == clock2)
10081 return true;
10082
10083 if (!clock1 || !clock2)
10084 return false;
10085
10086 diff = abs(clock1 - clock2);
10087
10088 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10089 return true;
10090
10091 return false;
10092}
10093
Daniel Vetter25c5b262012-07-08 22:08:04 +020010094#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10095 list_for_each_entry((intel_crtc), \
10096 &(dev)->mode_config.crtc_list, \
10097 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020010098 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020010099
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010100static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010101intel_pipe_config_compare(struct drm_device *dev,
10102 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010103 struct intel_crtc_config *pipe_config)
10104{
Daniel Vetter66e985c2013-06-05 13:34:20 +020010105#define PIPE_CONF_CHECK_X(name) \
10106 if (current_config->name != pipe_config->name) { \
10107 DRM_ERROR("mismatch in " #name " " \
10108 "(expected 0x%08x, found 0x%08x)\n", \
10109 current_config->name, \
10110 pipe_config->name); \
10111 return false; \
10112 }
10113
Daniel Vetter08a24032013-04-19 11:25:34 +020010114#define PIPE_CONF_CHECK_I(name) \
10115 if (current_config->name != pipe_config->name) { \
10116 DRM_ERROR("mismatch in " #name " " \
10117 "(expected %i, found %i)\n", \
10118 current_config->name, \
10119 pipe_config->name); \
10120 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010010121 }
10122
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010123#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10124 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070010125 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010126 "(expected %i, found %i)\n", \
10127 current_config->name & (mask), \
10128 pipe_config->name & (mask)); \
10129 return false; \
10130 }
10131
Ville Syrjälä5e550652013-09-06 23:29:07 +030010132#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10133 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10134 DRM_ERROR("mismatch in " #name " " \
10135 "(expected %i, found %i)\n", \
10136 current_config->name, \
10137 pipe_config->name); \
10138 return false; \
10139 }
10140
Daniel Vetterbb760062013-06-06 14:55:52 +020010141#define PIPE_CONF_QUIRK(quirk) \
10142 ((current_config->quirks | pipe_config->quirks) & (quirk))
10143
Daniel Vettereccb1402013-05-22 00:50:22 +020010144 PIPE_CONF_CHECK_I(cpu_transcoder);
10145
Daniel Vetter08a24032013-04-19 11:25:34 +020010146 PIPE_CONF_CHECK_I(has_pch_encoder);
10147 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020010148 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10149 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10150 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10151 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10152 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020010153
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010154 PIPE_CONF_CHECK_I(has_dp_encoder);
10155 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10156 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10157 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10158 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10159 PIPE_CONF_CHECK_I(dp_m_n.tu);
10160
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010161 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10162 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10163 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10164 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10165 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10166 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10167
10168 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10169 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10170 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10171 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10172 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10173 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10174
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010175 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020010176 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020010177 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10178 IS_VALLEYVIEW(dev))
10179 PIPE_CONF_CHECK_I(limited_color_range);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010180
Daniel Vetter9ed109a2014-04-24 23:54:52 +020010181 PIPE_CONF_CHECK_I(has_audio);
10182
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010183 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10184 DRM_MODE_FLAG_INTERLACE);
10185
Daniel Vetterbb760062013-06-06 14:55:52 +020010186 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10187 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10188 DRM_MODE_FLAG_PHSYNC);
10189 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10190 DRM_MODE_FLAG_NHSYNC);
10191 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10192 DRM_MODE_FLAG_PVSYNC);
10193 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10194 DRM_MODE_FLAG_NVSYNC);
10195 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010196
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010197 PIPE_CONF_CHECK_I(pipe_src_w);
10198 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010199
Daniel Vetter99535992014-04-13 12:00:33 +020010200 /*
10201 * FIXME: BIOS likes to set up a cloned config with lvds+external
10202 * screen. Since we don't yet re-compute the pipe config when moving
10203 * just the lvds port away to another pipe the sw tracking won't match.
10204 *
10205 * Proper atomic modesets with recomputed global state will fix this.
10206 * Until then just don't check gmch state for inherited modes.
10207 */
10208 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10209 PIPE_CONF_CHECK_I(gmch_pfit.control);
10210 /* pfit ratios are autocomputed by the hw on gen4+ */
10211 if (INTEL_INFO(dev)->gen < 4)
10212 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10213 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10214 }
10215
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010216 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10217 if (current_config->pch_pfit.enabled) {
10218 PIPE_CONF_CHECK_I(pch_pfit.pos);
10219 PIPE_CONF_CHECK_I(pch_pfit.size);
10220 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010221
Jesse Barnese59150d2014-01-07 13:30:45 -080010222 /* BDW+ don't expose a synchronous way to read the state */
10223 if (IS_HASWELL(dev))
10224 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010225
Ville Syrjälä282740f2013-09-04 18:30:03 +030010226 PIPE_CONF_CHECK_I(double_wide);
10227
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010228 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010229 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020010230 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010231 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10232 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010233
Ville Syrjälä42571ae2013-09-06 23:29:00 +030010234 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10235 PIPE_CONF_CHECK_I(pipe_bpp);
10236
Jesse Barnesa9a7e982014-01-20 14:18:04 -080010237 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10238 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030010239
Daniel Vetter66e985c2013-06-05 13:34:20 +020010240#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020010241#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010242#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030010243#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020010244#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010245
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010246 return true;
10247}
10248
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010249static void
10250check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010251{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010252 struct intel_connector *connector;
10253
10254 list_for_each_entry(connector, &dev->mode_config.connector_list,
10255 base.head) {
10256 /* This also checks the encoder/connector hw state with the
10257 * ->get_hw_state callbacks. */
10258 intel_connector_check_state(connector);
10259
10260 WARN(&connector->new_encoder->base != connector->base.encoder,
10261 "connector's staged encoder doesn't match current encoder\n");
10262 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010263}
10264
10265static void
10266check_encoder_state(struct drm_device *dev)
10267{
10268 struct intel_encoder *encoder;
10269 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010270
10271 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10272 base.head) {
10273 bool enabled = false;
10274 bool active = false;
10275 enum pipe pipe, tracked_pipe;
10276
10277 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10278 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030010279 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010280
10281 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10282 "encoder's stage crtc doesn't match current crtc\n");
10283 WARN(encoder->connectors_active && !encoder->base.crtc,
10284 "encoder's active_connectors set, but no crtc\n");
10285
10286 list_for_each_entry(connector, &dev->mode_config.connector_list,
10287 base.head) {
10288 if (connector->base.encoder != &encoder->base)
10289 continue;
10290 enabled = true;
10291 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10292 active = true;
10293 }
10294 WARN(!!encoder->base.crtc != enabled,
10295 "encoder's enabled state mismatch "
10296 "(expected %i, found %i)\n",
10297 !!encoder->base.crtc, enabled);
10298 WARN(active && !encoder->base.crtc,
10299 "active encoder with no crtc\n");
10300
10301 WARN(encoder->connectors_active != active,
10302 "encoder's computed active state doesn't match tracked active state "
10303 "(expected %i, found %i)\n", active, encoder->connectors_active);
10304
10305 active = encoder->get_hw_state(encoder, &pipe);
10306 WARN(active != encoder->connectors_active,
10307 "encoder's hw state doesn't match sw tracking "
10308 "(expected %i, found %i)\n",
10309 encoder->connectors_active, active);
10310
10311 if (!encoder->base.crtc)
10312 continue;
10313
10314 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10315 WARN(active && pipe != tracked_pipe,
10316 "active encoder's pipe doesn't match"
10317 "(expected %i, found %i)\n",
10318 tracked_pipe, pipe);
10319
10320 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010321}
10322
10323static void
10324check_crtc_state(struct drm_device *dev)
10325{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010326 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010327 struct intel_crtc *crtc;
10328 struct intel_encoder *encoder;
10329 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010330
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010331 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010332 bool enabled = false;
10333 bool active = false;
10334
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010335 memset(&pipe_config, 0, sizeof(pipe_config));
10336
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010337 DRM_DEBUG_KMS("[CRTC:%d]\n",
10338 crtc->base.base.id);
10339
10340 WARN(crtc->active && !crtc->base.enabled,
10341 "active crtc, but not enabled in sw tracking\n");
10342
10343 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10344 base.head) {
10345 if (encoder->base.crtc != &crtc->base)
10346 continue;
10347 enabled = true;
10348 if (encoder->connectors_active)
10349 active = true;
10350 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010351
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010352 WARN(active != crtc->active,
10353 "crtc's computed active state doesn't match tracked active state "
10354 "(expected %i, found %i)\n", active, crtc->active);
10355 WARN(enabled != crtc->base.enabled,
10356 "crtc's computed enabled state doesn't match tracked enabled state "
10357 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10358
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010359 active = dev_priv->display.get_pipe_config(crtc,
10360 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020010361
10362 /* hw state is inconsistent with the pipe A quirk */
10363 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
10364 active = crtc->active;
10365
Daniel Vetter6c49f242013-06-06 12:45:25 +020010366 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10367 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030010368 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020010369 if (encoder->base.crtc != &crtc->base)
10370 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010010371 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020010372 encoder->get_config(encoder, &pipe_config);
10373 }
10374
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010375 WARN(crtc->active != active,
10376 "crtc active state doesn't match with hw state "
10377 "(expected %i, found %i)\n", crtc->active, active);
10378
Daniel Vetterc0b03412013-05-28 12:05:54 +020010379 if (active &&
10380 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10381 WARN(1, "pipe state doesn't match!\n");
10382 intel_dump_pipe_config(crtc, &pipe_config,
10383 "[hw state]");
10384 intel_dump_pipe_config(crtc, &crtc->config,
10385 "[sw state]");
10386 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010387 }
10388}
10389
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010390static void
10391check_shared_dpll_state(struct drm_device *dev)
10392{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010393 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010394 struct intel_crtc *crtc;
10395 struct intel_dpll_hw_state dpll_hw_state;
10396 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020010397
10398 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10399 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10400 int enabled_crtcs = 0, active_crtcs = 0;
10401 bool active;
10402
10403 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10404
10405 DRM_DEBUG_KMS("%s\n", pll->name);
10406
10407 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10408
10409 WARN(pll->active > pll->refcount,
10410 "more active pll users than references: %i vs %i\n",
10411 pll->active, pll->refcount);
10412 WARN(pll->active && !pll->on,
10413 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +020010414 WARN(pll->on && !pll->active,
10415 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010416 WARN(pll->on != active,
10417 "pll on state mismatch (expected %i, found %i)\n",
10418 pll->on, active);
10419
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010420 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020010421 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10422 enabled_crtcs++;
10423 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10424 active_crtcs++;
10425 }
10426 WARN(pll->active != active_crtcs,
10427 "pll active crtcs mismatch (expected %i, found %i)\n",
10428 pll->active, active_crtcs);
10429 WARN(pll->refcount != enabled_crtcs,
10430 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10431 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010432
10433 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10434 sizeof(dpll_hw_state)),
10435 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010436 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010437}
10438
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010439void
10440intel_modeset_check_state(struct drm_device *dev)
10441{
10442 check_connector_state(dev);
10443 check_encoder_state(dev);
10444 check_crtc_state(dev);
10445 check_shared_dpll_state(dev);
10446}
10447
Ville Syrjälä18442d02013-09-13 16:00:08 +030010448void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10449 int dotclock)
10450{
10451 /*
10452 * FDI already provided one idea for the dotclock.
10453 * Yell if the encoder disagrees.
10454 */
Damien Lespiau241bfc32013-09-25 16:45:37 +010010455 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010456 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +010010457 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010458}
10459
Ville Syrjälä80715b22014-05-15 20:23:23 +030010460static void update_scanline_offset(struct intel_crtc *crtc)
10461{
10462 struct drm_device *dev = crtc->base.dev;
10463
10464 /*
10465 * The scanline counter increments at the leading edge of hsync.
10466 *
10467 * On most platforms it starts counting from vtotal-1 on the
10468 * first active line. That means the scanline counter value is
10469 * always one less than what we would expect. Ie. just after
10470 * start of vblank, which also occurs at start of hsync (on the
10471 * last active line), the scanline counter will read vblank_start-1.
10472 *
10473 * On gen2 the scanline counter starts counting from 1 instead
10474 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10475 * to keep the value positive), instead of adding one.
10476 *
10477 * On HSW+ the behaviour of the scanline counter depends on the output
10478 * type. For DP ports it behaves like most other platforms, but on HDMI
10479 * there's an extra 1 line difference. So we need to add two instead of
10480 * one to the value.
10481 */
10482 if (IS_GEN2(dev)) {
10483 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10484 int vtotal;
10485
10486 vtotal = mode->crtc_vtotal;
10487 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10488 vtotal /= 2;
10489
10490 crtc->scanline_offset = vtotal - 1;
10491 } else if (HAS_DDI(dev) &&
10492 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
10493 crtc->scanline_offset = 2;
10494 } else
10495 crtc->scanline_offset = 1;
10496}
10497
Daniel Vetterf30da182013-04-11 20:22:50 +020010498static int __intel_set_mode(struct drm_crtc *crtc,
10499 struct drm_display_mode *mode,
10500 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +020010501{
10502 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010503 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010504 struct drm_display_mode *saved_mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010505 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010506 struct intel_crtc *intel_crtc;
10507 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010508 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020010509
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010510 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010511 if (!saved_mode)
10512 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020010513
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010514 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +020010515 &prepare_pipes, &disable_pipes);
10516
Tim Gardner3ac18232012-12-07 07:54:26 -070010517 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010518
Daniel Vetter25c5b262012-07-08 22:08:04 +020010519 /* Hack: Because we don't (yet) support global modeset on multiple
10520 * crtcs, we don't keep track of the new mode for more than one crtc.
10521 * Hence simply check whether any bit is set in modeset_pipes in all the
10522 * pieces of code that are not yet converted to deal with mutliple crtcs
10523 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010524 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010525 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010526 if (IS_ERR(pipe_config)) {
10527 ret = PTR_ERR(pipe_config);
10528 pipe_config = NULL;
10529
Tim Gardner3ac18232012-12-07 07:54:26 -070010530 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010531 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020010532 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10533 "[modeset]");
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010534 to_intel_crtc(crtc)->new_config = pipe_config;
Daniel Vettera6778b32012-07-02 09:56:42 +020010535 }
10536
Jesse Barnes30a970c2013-11-04 13:48:12 -080010537 /*
10538 * See if the config requires any additional preparation, e.g.
10539 * to adjust global state with pipes off. We need to do this
10540 * here so we can get the modeset_pipe updated config for the new
10541 * mode set on this crtc. For other crtcs we need to use the
10542 * adjusted_mode bits in the crtc directly.
10543 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020010544 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020010545 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080010546
Ville Syrjäläc164f832013-11-05 22:34:12 +020010547 /* may have added more to prepare_pipes than we should */
10548 prepare_pipes &= ~disable_pipes;
10549 }
10550
Daniel Vetter460da9162013-03-27 00:44:51 +010010551 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10552 intel_crtc_disable(&intel_crtc->base);
10553
Daniel Vetterea9d7582012-07-10 10:42:52 +020010554 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10555 if (intel_crtc->base.enabled)
10556 dev_priv->display.crtc_disable(&intel_crtc->base);
10557 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010558
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020010559 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10560 * to set it here already despite that we pass it down the callchain.
10561 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010562 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020010563 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010564 /* mode_set/enable/disable functions rely on a correct pipe
10565 * config. */
10566 to_intel_crtc(crtc)->config = *pipe_config;
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010567 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020010568
10569 /*
10570 * Calculate and store various constants which
10571 * are later needed by vblank and swap-completion
10572 * timestamping. They are derived from true hwmode.
10573 */
10574 drm_calc_timestamping_constants(crtc,
10575 &pipe_config->adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010576 }
Daniel Vetter7758a112012-07-08 19:40:39 +020010577
Daniel Vetterea9d7582012-07-10 10:42:52 +020010578 /* Only after disabling all output pipelines that will be changed can we
10579 * update the the output configuration. */
10580 intel_modeset_update_state(dev, prepare_pipes);
10581
Daniel Vetter47fab732012-10-26 10:58:18 +020010582 if (dev_priv->display.modeset_global_resources)
10583 dev_priv->display.modeset_global_resources(dev);
10584
Daniel Vettera6778b32012-07-02 09:56:42 +020010585 /* Set up the DPLL and any encoders state that needs to adjust or depend
10586 * on the DPLL.
10587 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010588 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Daniel Vetter4c107942014-04-24 23:55:05 +020010589 struct drm_framebuffer *old_fb;
Daniel Vettera071fa02014-06-18 23:28:09 +020010590 struct drm_i915_gem_object *old_obj = NULL;
10591 struct drm_i915_gem_object *obj =
10592 to_intel_framebuffer(fb)->obj;
Daniel Vetter4c107942014-04-24 23:55:05 +020010593
10594 mutex_lock(&dev->struct_mutex);
10595 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vettera071fa02014-06-18 23:28:09 +020010596 obj,
Daniel Vetter4c107942014-04-24 23:55:05 +020010597 NULL);
10598 if (ret != 0) {
10599 DRM_ERROR("pin & fence failed\n");
10600 mutex_unlock(&dev->struct_mutex);
10601 goto done;
10602 }
10603 old_fb = crtc->primary->fb;
Daniel Vettera071fa02014-06-18 23:28:09 +020010604 if (old_fb) {
10605 old_obj = to_intel_framebuffer(old_fb)->obj;
10606 intel_unpin_fb_obj(old_obj);
10607 }
10608 i915_gem_track_fb(old_obj, obj,
10609 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Daniel Vetter4c107942014-04-24 23:55:05 +020010610 mutex_unlock(&dev->struct_mutex);
10611
10612 crtc->primary->fb = fb;
10613 crtc->x = x;
10614 crtc->y = y;
10615
Daniel Vetter4271b752014-04-24 23:55:00 +020010616 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
10617 x, y, fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010618 if (ret)
10619 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +020010620 }
10621
10622 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030010623 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10624 update_scanline_offset(intel_crtc);
10625
Daniel Vetter25c5b262012-07-08 22:08:04 +020010626 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030010627 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010628
Daniel Vettera6778b32012-07-02 09:56:42 +020010629 /* FIXME: add subpixel order */
10630done:
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010631 if (ret && crtc->enabled)
Tim Gardner3ac18232012-12-07 07:54:26 -070010632 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010633
Tim Gardner3ac18232012-12-07 07:54:26 -070010634out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010635 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -070010636 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020010637 return ret;
10638}
10639
Damien Lespiaue7457a92013-08-08 22:28:59 +010010640static int intel_set_mode(struct drm_crtc *crtc,
10641 struct drm_display_mode *mode,
10642 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020010643{
10644 int ret;
10645
10646 ret = __intel_set_mode(crtc, mode, x, y, fb);
10647
10648 if (ret == 0)
10649 intel_modeset_check_state(crtc->dev);
10650
10651 return ret;
10652}
10653
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010654void intel_crtc_restore_mode(struct drm_crtc *crtc)
10655{
Matt Roperf4510a22014-04-01 15:22:40 -070010656 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010657}
10658
Daniel Vetter25c5b262012-07-08 22:08:04 +020010659#undef for_each_intel_crtc_masked
10660
Daniel Vetterd9e55602012-07-04 22:16:09 +020010661static void intel_set_config_free(struct intel_set_config *config)
10662{
10663 if (!config)
10664 return;
10665
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010666 kfree(config->save_connector_encoders);
10667 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020010668 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020010669 kfree(config);
10670}
10671
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010672static int intel_set_config_save_state(struct drm_device *dev,
10673 struct intel_set_config *config)
10674{
Ville Syrjälä76688512014-01-10 11:28:06 +020010675 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010676 struct drm_encoder *encoder;
10677 struct drm_connector *connector;
10678 int count;
10679
Ville Syrjälä76688512014-01-10 11:28:06 +020010680 config->save_crtc_enabled =
10681 kcalloc(dev->mode_config.num_crtc,
10682 sizeof(bool), GFP_KERNEL);
10683 if (!config->save_crtc_enabled)
10684 return -ENOMEM;
10685
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010686 config->save_encoder_crtcs =
10687 kcalloc(dev->mode_config.num_encoder,
10688 sizeof(struct drm_crtc *), GFP_KERNEL);
10689 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010690 return -ENOMEM;
10691
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010692 config->save_connector_encoders =
10693 kcalloc(dev->mode_config.num_connector,
10694 sizeof(struct drm_encoder *), GFP_KERNEL);
10695 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010696 return -ENOMEM;
10697
10698 /* Copy data. Note that driver private data is not affected.
10699 * Should anything bad happen only the expected state is
10700 * restored, not the drivers personal bookkeeping.
10701 */
10702 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010703 for_each_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010704 config->save_crtc_enabled[count++] = crtc->enabled;
10705 }
10706
10707 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010708 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010709 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010710 }
10711
10712 count = 0;
10713 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010714 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010715 }
10716
10717 return 0;
10718}
10719
10720static void intel_set_config_restore_state(struct drm_device *dev,
10721 struct intel_set_config *config)
10722{
Ville Syrjälä76688512014-01-10 11:28:06 +020010723 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010724 struct intel_encoder *encoder;
10725 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010726 int count;
10727
10728 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010729 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010730 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010731
10732 if (crtc->new_enabled)
10733 crtc->new_config = &crtc->config;
10734 else
10735 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010736 }
10737
10738 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010739 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10740 encoder->new_crtc =
10741 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010742 }
10743
10744 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010745 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10746 connector->new_encoder =
10747 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010748 }
10749}
10750
Imre Deake3de42b2013-05-03 19:44:07 +020010751static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010010752is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020010753{
10754 int i;
10755
Chris Wilson2e57f472013-07-17 12:14:40 +010010756 if (set->num_connectors == 0)
10757 return false;
10758
10759 if (WARN_ON(set->connectors == NULL))
10760 return false;
10761
10762 for (i = 0; i < set->num_connectors; i++)
10763 if (set->connectors[i]->encoder &&
10764 set->connectors[i]->encoder->crtc == set->crtc &&
10765 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020010766 return true;
10767
10768 return false;
10769}
10770
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010771static void
10772intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10773 struct intel_set_config *config)
10774{
10775
10776 /* We should be able to check here if the fb has the same properties
10777 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010010778 if (is_crtc_connector_off(set)) {
10779 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070010780 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070010781 /*
10782 * If we have no fb, we can only flip as long as the crtc is
10783 * active, otherwise we need a full mode set. The crtc may
10784 * be active if we've only disabled the primary plane, or
10785 * in fastboot situations.
10786 */
Matt Roperf4510a22014-04-01 15:22:40 -070010787 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010788 struct intel_crtc *intel_crtc =
10789 to_intel_crtc(set->crtc);
10790
Matt Roper3b150f02014-05-29 08:06:53 -070010791 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010792 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10793 config->fb_changed = true;
10794 } else {
10795 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10796 config->mode_changed = true;
10797 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010798 } else if (set->fb == NULL) {
10799 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010010800 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070010801 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010802 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010803 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010804 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010805 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010806 }
10807
Daniel Vetter835c5872012-07-10 18:11:08 +020010808 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010809 config->fb_changed = true;
10810
10811 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10812 DRM_DEBUG_KMS("modes are different, full mode set\n");
10813 drm_mode_debug_printmodeline(&set->crtc->mode);
10814 drm_mode_debug_printmodeline(set->mode);
10815 config->mode_changed = true;
10816 }
Chris Wilsona1d95702013-08-13 18:48:47 +010010817
10818 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10819 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010820}
10821
Daniel Vetter2e431052012-07-04 22:42:15 +020010822static int
Daniel Vetter9a935852012-07-05 22:34:27 +020010823intel_modeset_stage_output_state(struct drm_device *dev,
10824 struct drm_mode_set *set,
10825 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020010826{
Daniel Vetter9a935852012-07-05 22:34:27 +020010827 struct intel_connector *connector;
10828 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020010829 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030010830 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020010831
Damien Lespiau9abdda72013-02-13 13:29:23 +000010832 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020010833 * of connectors. For paranoia, double-check this. */
10834 WARN_ON(!set->fb && (set->num_connectors != 0));
10835 WARN_ON(set->fb && (set->num_connectors == 0));
10836
Daniel Vetter9a935852012-07-05 22:34:27 +020010837 list_for_each_entry(connector, &dev->mode_config.connector_list,
10838 base.head) {
10839 /* Otherwise traverse passed in connector list and get encoders
10840 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010841 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010842 if (set->connectors[ro] == &connector->base) {
10843 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +020010844 break;
10845 }
10846 }
10847
Daniel Vetter9a935852012-07-05 22:34:27 +020010848 /* If we disable the crtc, disable all its connectors. Also, if
10849 * the connector is on the changing crtc but not on the new
10850 * connector list, disable it. */
10851 if ((!set->fb || ro == set->num_connectors) &&
10852 connector->base.encoder &&
10853 connector->base.encoder->crtc == set->crtc) {
10854 connector->new_encoder = NULL;
10855
10856 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
10857 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010858 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020010859 }
10860
10861
10862 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +020010863 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010864 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020010865 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010866 }
10867 /* connector->new_encoder is now updated for all connectors. */
10868
10869 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +020010870 list_for_each_entry(connector, &dev->mode_config.connector_list,
10871 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010872 struct drm_crtc *new_crtc;
10873
Daniel Vetter9a935852012-07-05 22:34:27 +020010874 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020010875 continue;
10876
Daniel Vetter9a935852012-07-05 22:34:27 +020010877 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020010878
10879 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010880 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020010881 new_crtc = set->crtc;
10882 }
10883
10884 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010010885 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
10886 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010887 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020010888 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010889 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
10890
10891 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
10892 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010893 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020010894 new_crtc->base.id);
10895 }
10896
10897 /* Check for any encoders that needs to be disabled. */
10898 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10899 base.head) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010900 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010901 list_for_each_entry(connector,
10902 &dev->mode_config.connector_list,
10903 base.head) {
10904 if (connector->new_encoder == encoder) {
10905 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010906 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020010907 }
10908 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010909
10910 if (num_connectors == 0)
10911 encoder->new_crtc = NULL;
10912 else if (num_connectors > 1)
10913 return -EINVAL;
10914
Daniel Vetter9a935852012-07-05 22:34:27 +020010915 /* Only now check for crtc changes so we don't miss encoders
10916 * that will be disabled. */
10917 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020010918 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010919 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020010920 }
10921 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010922 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010923
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010924 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010925 crtc->new_enabled = false;
10926
10927 list_for_each_entry(encoder,
10928 &dev->mode_config.encoder_list,
10929 base.head) {
10930 if (encoder->new_crtc == crtc) {
10931 crtc->new_enabled = true;
10932 break;
10933 }
10934 }
10935
10936 if (crtc->new_enabled != crtc->base.enabled) {
10937 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
10938 crtc->new_enabled ? "en" : "dis");
10939 config->mode_changed = true;
10940 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010941
10942 if (crtc->new_enabled)
10943 crtc->new_config = &crtc->config;
10944 else
10945 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010946 }
10947
Daniel Vetter2e431052012-07-04 22:42:15 +020010948 return 0;
10949}
10950
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010951static void disable_crtc_nofb(struct intel_crtc *crtc)
10952{
10953 struct drm_device *dev = crtc->base.dev;
10954 struct intel_encoder *encoder;
10955 struct intel_connector *connector;
10956
10957 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
10958 pipe_name(crtc->pipe));
10959
10960 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10961 if (connector->new_encoder &&
10962 connector->new_encoder->new_crtc == crtc)
10963 connector->new_encoder = NULL;
10964 }
10965
10966 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10967 if (encoder->new_crtc == crtc)
10968 encoder->new_crtc = NULL;
10969 }
10970
10971 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010972 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010973}
10974
Daniel Vetter2e431052012-07-04 22:42:15 +020010975static int intel_crtc_set_config(struct drm_mode_set *set)
10976{
10977 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020010978 struct drm_mode_set save_set;
10979 struct intel_set_config *config;
10980 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020010981
Daniel Vetter8d3e3752012-07-05 16:09:09 +020010982 BUG_ON(!set);
10983 BUG_ON(!set->crtc);
10984 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020010985
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010010986 /* Enforce sane interface api - has been abused by the fb helper. */
10987 BUG_ON(!set->mode && set->fb);
10988 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020010989
Daniel Vetter2e431052012-07-04 22:42:15 +020010990 if (set->fb) {
10991 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
10992 set->crtc->base.id, set->fb->base.id,
10993 (int)set->num_connectors, set->x, set->y);
10994 } else {
10995 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020010996 }
10997
10998 dev = set->crtc->dev;
10999
11000 ret = -ENOMEM;
11001 config = kzalloc(sizeof(*config), GFP_KERNEL);
11002 if (!config)
11003 goto out_config;
11004
11005 ret = intel_set_config_save_state(dev, config);
11006 if (ret)
11007 goto out_config;
11008
11009 save_set.crtc = set->crtc;
11010 save_set.mode = &set->crtc->mode;
11011 save_set.x = set->crtc->x;
11012 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070011013 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020011014
11015 /* Compute whether we need a full modeset, only an fb base update or no
11016 * change at all. In the future we might also check whether only the
11017 * mode changed, e.g. for LVDS where we only change the panel fitter in
11018 * such cases. */
11019 intel_set_config_compute_mode_changes(set, config);
11020
Daniel Vetter9a935852012-07-05 22:34:27 +020011021 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020011022 if (ret)
11023 goto fail;
11024
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011025 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011026 ret = intel_set_mode(set->crtc, set->mode,
11027 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011028 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070011029 struct drm_i915_private *dev_priv = dev->dev_private;
11030 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11031
Ville Syrjälä4878cae2013-02-18 19:08:48 +020011032 intel_crtc_wait_for_pending_flips(set->crtc);
11033
Daniel Vetter4f660f42012-07-02 09:47:37 +020011034 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020011035 set->x, set->y, set->fb);
Matt Roper3b150f02014-05-29 08:06:53 -070011036
11037 /*
11038 * We need to make sure the primary plane is re-enabled if it
11039 * has previously been turned off.
11040 */
11041 if (!intel_crtc->primary_enabled && ret == 0) {
11042 WARN_ON(!intel_crtc->active);
11043 intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
11044 intel_crtc->pipe);
11045 }
11046
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011047 /*
11048 * In the fastboot case this may be our only check of the
11049 * state after boot. It would be better to only do it on
11050 * the first update, but we don't have a nice way of doing that
11051 * (and really, set_config isn't used much for high freq page
11052 * flipping, so increasing its cost here shouldn't be a big
11053 * deal).
11054 */
Jani Nikulad330a952014-01-21 11:24:25 +020011055 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011056 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020011057 }
11058
Chris Wilson2d05eae2013-05-03 17:36:25 +010011059 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020011060 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11061 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020011062fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010011063 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011064
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011065 /*
11066 * HACK: if the pipe was on, but we didn't have a framebuffer,
11067 * force the pipe off to avoid oopsing in the modeset code
11068 * due to fb==NULL. This should only happen during boot since
11069 * we don't yet reconstruct the FB from the hardware state.
11070 */
11071 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11072 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11073
Chris Wilson2d05eae2013-05-03 17:36:25 +010011074 /* Try to restore the config */
11075 if (config->mode_changed &&
11076 intel_set_mode(save_set.crtc, save_set.mode,
11077 save_set.x, save_set.y, save_set.fb))
11078 DRM_ERROR("failed to restore config after modeset failure\n");
11079 }
Daniel Vetter50f56112012-07-02 09:35:43 +020011080
Daniel Vetterd9e55602012-07-04 22:16:09 +020011081out_config:
11082 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011083 return ret;
11084}
11085
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011086static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011087 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020011088 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011089 .destroy = intel_crtc_destroy,
11090 .page_flip = intel_crtc_page_flip,
11091};
11092
Paulo Zanoni79f689a2012-10-05 12:05:52 -030011093static void intel_cpu_pll_init(struct drm_device *dev)
11094{
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011095 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -030011096 intel_ddi_pll_init(dev);
11097}
11098
Daniel Vetter53589012013-06-05 13:34:16 +020011099static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11100 struct intel_shared_dpll *pll,
11101 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011102{
Daniel Vetter53589012013-06-05 13:34:16 +020011103 uint32_t val;
11104
11105 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020011106 hw_state->dpll = val;
11107 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11108 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020011109
11110 return val & DPLL_VCO_ENABLE;
11111}
11112
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011113static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11114 struct intel_shared_dpll *pll)
11115{
11116 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11117 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11118}
11119
Daniel Vettere7b903d2013-06-05 13:34:14 +020011120static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11121 struct intel_shared_dpll *pll)
11122{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011123 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020011124 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020011125
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011126 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11127
11128 /* Wait for the clocks to stabilize. */
11129 POSTING_READ(PCH_DPLL(pll->id));
11130 udelay(150);
11131
11132 /* The pixel multiplier can only be updated once the
11133 * DPLL is enabled and the clocks are stable.
11134 *
11135 * So write it again.
11136 */
11137 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11138 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011139 udelay(200);
11140}
11141
11142static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11143 struct intel_shared_dpll *pll)
11144{
11145 struct drm_device *dev = dev_priv->dev;
11146 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011147
11148 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011149 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020011150 if (intel_crtc_to_shared_dpll(crtc) == pll)
11151 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
11152 }
11153
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011154 I915_WRITE(PCH_DPLL(pll->id), 0);
11155 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011156 udelay(200);
11157}
11158
Daniel Vetter46edb022013-06-05 13:34:12 +020011159static char *ibx_pch_dpll_names[] = {
11160 "PCH DPLL A",
11161 "PCH DPLL B",
11162};
11163
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011164static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011165{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011166 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011167 int i;
11168
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011169 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011170
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011171 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020011172 dev_priv->shared_dplls[i].id = i;
11173 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011174 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011175 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11176 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020011177 dev_priv->shared_dplls[i].get_hw_state =
11178 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011179 }
11180}
11181
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011182static void intel_shared_dpll_init(struct drm_device *dev)
11183{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011184 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011185
11186 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
11187 ibx_pch_dpll_init(dev);
11188 else
11189 dev_priv->num_shared_dpll = 0;
11190
11191 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011192}
11193
Matt Roper465c1202014-05-29 08:06:54 -070011194static int
11195intel_primary_plane_disable(struct drm_plane *plane)
11196{
11197 struct drm_device *dev = plane->dev;
11198 struct drm_i915_private *dev_priv = dev->dev_private;
11199 struct intel_plane *intel_plane = to_intel_plane(plane);
11200 struct intel_crtc *intel_crtc;
11201
11202 if (!plane->fb)
11203 return 0;
11204
11205 BUG_ON(!plane->crtc);
11206
11207 intel_crtc = to_intel_crtc(plane->crtc);
11208
11209 /*
11210 * Even though we checked plane->fb above, it's still possible that
11211 * the primary plane has been implicitly disabled because the crtc
11212 * coordinates given weren't visible, or because we detected
11213 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11214 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11215 * In either case, we need to unpin the FB and let the fb pointer get
11216 * updated, but otherwise we don't need to touch the hardware.
11217 */
11218 if (!intel_crtc->primary_enabled)
11219 goto disable_unpin;
11220
11221 intel_crtc_wait_for_pending_flips(plane->crtc);
11222 intel_disable_primary_hw_plane(dev_priv, intel_plane->plane,
11223 intel_plane->pipe);
Matt Roper465c1202014-05-29 08:06:54 -070011224disable_unpin:
Daniel Vettera071fa02014-06-18 23:28:09 +020011225 i915_gem_track_fb(to_intel_framebuffer(plane->fb)->obj, NULL,
11226 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Matt Roper465c1202014-05-29 08:06:54 -070011227 intel_unpin_fb_obj(to_intel_framebuffer(plane->fb)->obj);
11228 plane->fb = NULL;
11229
11230 return 0;
11231}
11232
11233static int
11234intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11235 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11236 unsigned int crtc_w, unsigned int crtc_h,
11237 uint32_t src_x, uint32_t src_y,
11238 uint32_t src_w, uint32_t src_h)
11239{
11240 struct drm_device *dev = crtc->dev;
11241 struct drm_i915_private *dev_priv = dev->dev_private;
11242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11243 struct intel_plane *intel_plane = to_intel_plane(plane);
Daniel Vettera071fa02014-06-18 23:28:09 +020011244 struct drm_i915_gem_object *obj, *old_obj = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070011245 struct drm_rect dest = {
11246 /* integer pixels */
11247 .x1 = crtc_x,
11248 .y1 = crtc_y,
11249 .x2 = crtc_x + crtc_w,
11250 .y2 = crtc_y + crtc_h,
11251 };
11252 struct drm_rect src = {
11253 /* 16.16 fixed point */
11254 .x1 = src_x,
11255 .y1 = src_y,
11256 .x2 = src_x + src_w,
11257 .y2 = src_y + src_h,
11258 };
11259 const struct drm_rect clip = {
11260 /* integer pixels */
11261 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
11262 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
11263 };
11264 bool visible;
11265 int ret;
11266
11267 ret = drm_plane_helper_check_update(plane, crtc, fb,
11268 &src, &dest, &clip,
11269 DRM_PLANE_HELPER_NO_SCALING,
11270 DRM_PLANE_HELPER_NO_SCALING,
11271 false, true, &visible);
11272
11273 if (ret)
11274 return ret;
11275
Daniel Vettera071fa02014-06-18 23:28:09 +020011276 if (plane->fb)
11277 old_obj = to_intel_framebuffer(plane->fb)->obj;
11278 obj = to_intel_framebuffer(fb)->obj;
11279
Matt Roper465c1202014-05-29 08:06:54 -070011280 /*
11281 * If the CRTC isn't enabled, we're just pinning the framebuffer,
11282 * updating the fb pointer, and returning without touching the
11283 * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
11284 * turn on the display with all planes setup as desired.
11285 */
11286 if (!crtc->enabled) {
11287 /*
11288 * If we already called setplane while the crtc was disabled,
11289 * we may have an fb pinned; unpin it.
11290 */
11291 if (plane->fb)
Daniel Vettera071fa02014-06-18 23:28:09 +020011292 intel_unpin_fb_obj(old_obj);
11293
11294 i915_gem_track_fb(old_obj, obj,
11295 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Matt Roper465c1202014-05-29 08:06:54 -070011296
11297 /* Pin and return without programming hardware */
Daniel Vettera071fa02014-06-18 23:28:09 +020011298 return intel_pin_and_fence_fb_obj(dev, obj, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070011299 }
11300
11301 intel_crtc_wait_for_pending_flips(crtc);
11302
11303 /*
11304 * If clipping results in a non-visible primary plane, we'll disable
11305 * the primary plane. Note that this is a bit different than what
11306 * happens if userspace explicitly disables the plane by passing fb=0
11307 * because plane->fb still gets set and pinned.
11308 */
11309 if (!visible) {
11310 /*
11311 * Try to pin the new fb first so that we can bail out if we
11312 * fail.
11313 */
11314 if (plane->fb != fb) {
Daniel Vettera071fa02014-06-18 23:28:09 +020011315 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
Matt Roper465c1202014-05-29 08:06:54 -070011316 if (ret)
11317 return ret;
11318 }
11319
Daniel Vettera071fa02014-06-18 23:28:09 +020011320 i915_gem_track_fb(old_obj, obj,
11321 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
11322
Matt Roper465c1202014-05-29 08:06:54 -070011323 if (intel_crtc->primary_enabled)
11324 intel_disable_primary_hw_plane(dev_priv,
11325 intel_plane->plane,
11326 intel_plane->pipe);
11327
11328
11329 if (plane->fb != fb)
11330 if (plane->fb)
Daniel Vettera071fa02014-06-18 23:28:09 +020011331 intel_unpin_fb_obj(old_obj);
Matt Roper465c1202014-05-29 08:06:54 -070011332
11333 return 0;
11334 }
11335
11336 ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
11337 if (ret)
11338 return ret;
11339
11340 if (!intel_crtc->primary_enabled)
11341 intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
11342 intel_crtc->pipe);
11343
11344 return 0;
11345}
11346
Matt Roper3d7d6512014-06-10 08:28:13 -070011347/* Common destruction function for both primary and cursor planes */
11348static void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070011349{
11350 struct intel_plane *intel_plane = to_intel_plane(plane);
11351 drm_plane_cleanup(plane);
11352 kfree(intel_plane);
11353}
11354
11355static const struct drm_plane_funcs intel_primary_plane_funcs = {
11356 .update_plane = intel_primary_plane_setplane,
11357 .disable_plane = intel_primary_plane_disable,
Matt Roper3d7d6512014-06-10 08:28:13 -070011358 .destroy = intel_plane_destroy,
Matt Roper465c1202014-05-29 08:06:54 -070011359};
11360
11361static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11362 int pipe)
11363{
11364 struct intel_plane *primary;
11365 const uint32_t *intel_primary_formats;
11366 int num_formats;
11367
11368 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11369 if (primary == NULL)
11370 return NULL;
11371
11372 primary->can_scale = false;
11373 primary->max_downscale = 1;
11374 primary->pipe = pipe;
11375 primary->plane = pipe;
11376 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11377 primary->plane = !pipe;
11378
11379 if (INTEL_INFO(dev)->gen <= 3) {
11380 intel_primary_formats = intel_primary_formats_gen2;
11381 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11382 } else {
11383 intel_primary_formats = intel_primary_formats_gen4;
11384 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11385 }
11386
11387 drm_universal_plane_init(dev, &primary->base, 0,
11388 &intel_primary_plane_funcs,
11389 intel_primary_formats, num_formats,
11390 DRM_PLANE_TYPE_PRIMARY);
11391 return &primary->base;
11392}
11393
Matt Roper3d7d6512014-06-10 08:28:13 -070011394static int
11395intel_cursor_plane_disable(struct drm_plane *plane)
11396{
11397 if (!plane->fb)
11398 return 0;
11399
11400 BUG_ON(!plane->crtc);
11401
11402 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11403}
11404
11405static int
11406intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
11407 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11408 unsigned int crtc_w, unsigned int crtc_h,
11409 uint32_t src_x, uint32_t src_y,
11410 uint32_t src_w, uint32_t src_h)
11411{
11412 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11413 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11414 struct drm_i915_gem_object *obj = intel_fb->obj;
11415 struct drm_rect dest = {
11416 /* integer pixels */
11417 .x1 = crtc_x,
11418 .y1 = crtc_y,
11419 .x2 = crtc_x + crtc_w,
11420 .y2 = crtc_y + crtc_h,
11421 };
11422 struct drm_rect src = {
11423 /* 16.16 fixed point */
11424 .x1 = src_x,
11425 .y1 = src_y,
11426 .x2 = src_x + src_w,
11427 .y2 = src_y + src_h,
11428 };
11429 const struct drm_rect clip = {
11430 /* integer pixels */
11431 .x2 = intel_crtc->config.pipe_src_w,
11432 .y2 = intel_crtc->config.pipe_src_h,
11433 };
11434 bool visible;
11435 int ret;
11436
11437 ret = drm_plane_helper_check_update(plane, crtc, fb,
11438 &src, &dest, &clip,
11439 DRM_PLANE_HELPER_NO_SCALING,
11440 DRM_PLANE_HELPER_NO_SCALING,
11441 true, true, &visible);
11442 if (ret)
11443 return ret;
11444
11445 crtc->cursor_x = crtc_x;
11446 crtc->cursor_y = crtc_y;
11447 if (fb != crtc->cursor->fb) {
11448 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
11449 } else {
11450 intel_crtc_update_cursor(crtc, visible);
11451 return 0;
11452 }
11453}
11454static const struct drm_plane_funcs intel_cursor_plane_funcs = {
11455 .update_plane = intel_cursor_plane_update,
11456 .disable_plane = intel_cursor_plane_disable,
11457 .destroy = intel_plane_destroy,
11458};
11459
11460static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
11461 int pipe)
11462{
11463 struct intel_plane *cursor;
11464
11465 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
11466 if (cursor == NULL)
11467 return NULL;
11468
11469 cursor->can_scale = false;
11470 cursor->max_downscale = 1;
11471 cursor->pipe = pipe;
11472 cursor->plane = pipe;
11473
11474 drm_universal_plane_init(dev, &cursor->base, 0,
11475 &intel_cursor_plane_funcs,
11476 intel_cursor_formats,
11477 ARRAY_SIZE(intel_cursor_formats),
11478 DRM_PLANE_TYPE_CURSOR);
11479 return &cursor->base;
11480}
11481
Hannes Ederb358d0a2008-12-18 21:18:47 +010011482static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080011483{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011484 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080011485 struct intel_crtc *intel_crtc;
Matt Roper3d7d6512014-06-10 08:28:13 -070011486 struct drm_plane *primary = NULL;
11487 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070011488 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080011489
Daniel Vetter955382f2013-09-19 14:05:45 +020011490 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080011491 if (intel_crtc == NULL)
11492 return;
11493
Matt Roper465c1202014-05-29 08:06:54 -070011494 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070011495 if (!primary)
11496 goto fail;
11497
11498 cursor = intel_cursor_plane_create(dev, pipe);
11499 if (!cursor)
11500 goto fail;
11501
Matt Roper465c1202014-05-29 08:06:54 -070011502 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070011503 cursor, &intel_crtc_funcs);
11504 if (ret)
11505 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011506
11507 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080011508 for (i = 0; i < 256; i++) {
11509 intel_crtc->lut_r[i] = i;
11510 intel_crtc->lut_g[i] = i;
11511 intel_crtc->lut_b[i] = i;
11512 }
11513
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020011514 /*
11515 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020011516 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020011517 */
Jesse Barnes80824002009-09-10 15:28:06 -070011518 intel_crtc->pipe = pipe;
11519 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010011520 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080011521 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010011522 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070011523 }
11524
Chris Wilson4b0e3332014-05-30 16:35:26 +030011525 intel_crtc->cursor_base = ~0;
11526 intel_crtc->cursor_cntl = ~0;
11527
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030011528 init_waitqueue_head(&intel_crtc->vbl_wait);
11529
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080011530 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
11531 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
11532 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
11533 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
11534
Jesse Barnes79e53942008-11-07 14:24:08 -080011535 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020011536
11537 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070011538 return;
11539
11540fail:
11541 if (primary)
11542 drm_plane_cleanup(primary);
11543 if (cursor)
11544 drm_plane_cleanup(cursor);
11545 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080011546}
11547
Jesse Barnes752aa882013-10-31 18:55:49 +020011548enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
11549{
11550 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011551 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020011552
Rob Clark51fd3712013-11-19 12:10:12 -050011553 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020011554
11555 if (!encoder)
11556 return INVALID_PIPE;
11557
11558 return to_intel_crtc(encoder->crtc)->pipe;
11559}
11560
Carl Worth08d7b3d2009-04-29 14:43:54 -070011561int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000011562 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070011563{
Carl Worth08d7b3d2009-04-29 14:43:54 -070011564 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020011565 struct drm_mode_object *drmmode_obj;
11566 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011567
Daniel Vetter1cff8f62012-04-24 09:55:08 +020011568 if (!drm_core_check_feature(dev, DRIVER_MODESET))
11569 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011570
Daniel Vetterc05422d2009-08-11 16:05:30 +020011571 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
11572 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070011573
Daniel Vetterc05422d2009-08-11 16:05:30 +020011574 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070011575 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030011576 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011577 }
11578
Daniel Vetterc05422d2009-08-11 16:05:30 +020011579 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
11580 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011581
Daniel Vetterc05422d2009-08-11 16:05:30 +020011582 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011583}
11584
Daniel Vetter66a92782012-07-12 20:08:18 +020011585static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080011586{
Daniel Vetter66a92782012-07-12 20:08:18 +020011587 struct drm_device *dev = encoder->base.dev;
11588 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080011589 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080011590 int entry = 0;
11591
Daniel Vetter66a92782012-07-12 20:08:18 +020011592 list_for_each_entry(source_encoder,
11593 &dev->mode_config.encoder_list, base.head) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011594 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020011595 index_mask |= (1 << entry);
11596
Jesse Barnes79e53942008-11-07 14:24:08 -080011597 entry++;
11598 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010011599
Jesse Barnes79e53942008-11-07 14:24:08 -080011600 return index_mask;
11601}
11602
Chris Wilson4d302442010-12-14 19:21:29 +000011603static bool has_edp_a(struct drm_device *dev)
11604{
11605 struct drm_i915_private *dev_priv = dev->dev_private;
11606
11607 if (!IS_MOBILE(dev))
11608 return false;
11609
11610 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
11611 return false;
11612
Damien Lespiaue3589902014-02-07 19:12:50 +000011613 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000011614 return false;
11615
11616 return true;
11617}
11618
Damien Lespiauba0fbca2014-01-08 14:18:23 +000011619const char *intel_output_name(int output)
11620{
11621 static const char *names[] = {
11622 [INTEL_OUTPUT_UNUSED] = "Unused",
11623 [INTEL_OUTPUT_ANALOG] = "Analog",
11624 [INTEL_OUTPUT_DVO] = "DVO",
11625 [INTEL_OUTPUT_SDVO] = "SDVO",
11626 [INTEL_OUTPUT_LVDS] = "LVDS",
11627 [INTEL_OUTPUT_TVOUT] = "TV",
11628 [INTEL_OUTPUT_HDMI] = "HDMI",
11629 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
11630 [INTEL_OUTPUT_EDP] = "eDP",
11631 [INTEL_OUTPUT_DSI] = "DSI",
11632 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
11633 };
11634
11635 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
11636 return "Invalid";
11637
11638 return names[output];
11639}
11640
Jesse Barnes79e53942008-11-07 14:24:08 -080011641static void intel_setup_outputs(struct drm_device *dev)
11642{
Eric Anholt725e30a2009-01-22 13:01:02 -080011643 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010011644 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011645 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080011646
Daniel Vetterc9093352013-06-06 22:22:47 +020011647 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011648
Jesse Barnes27da3bd2014-04-04 16:12:07 -070011649 if (!IS_ULT(dev) && !IS_CHERRYVIEW(dev) && dev_priv->vbt.int_crt_support)
Paulo Zanoni79935fc2012-11-20 13:27:40 -020011650 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011651
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011652 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030011653 int found;
11654
11655 /* Haswell uses DDI functions to detect digital outputs */
11656 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
11657 /* DDI A only supports eDP */
11658 if (found)
11659 intel_ddi_init(dev, PORT_A);
11660
11661 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
11662 * register */
11663 found = I915_READ(SFUSE_STRAP);
11664
11665 if (found & SFUSE_STRAP_DDIB_DETECTED)
11666 intel_ddi_init(dev, PORT_B);
11667 if (found & SFUSE_STRAP_DDIC_DETECTED)
11668 intel_ddi_init(dev, PORT_C);
11669 if (found & SFUSE_STRAP_DDID_DETECTED)
11670 intel_ddi_init(dev, PORT_D);
11671 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011672 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020011673 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020011674
11675 if (has_edp_a(dev))
11676 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011677
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011678 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080011679 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010011680 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011681 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011682 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011683 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011684 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011685 }
11686
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011687 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011688 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011689
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011690 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011691 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011692
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011693 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011694 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011695
Daniel Vetter270b3042012-10-27 15:52:05 +020011696 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011697 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070011698 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030011699 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
11700 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
11701 PORT_B);
11702 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
11703 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
11704 }
11705
Jesse Barnes6f6005a2013-08-09 09:34:35 -070011706 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
11707 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
11708 PORT_C);
11709 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020011710 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Jesse Barnes6f6005a2013-08-09 09:34:35 -070011711 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053011712
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030011713 if (IS_CHERRYVIEW(dev)) {
11714 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
11715 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
11716 PORT_D);
11717 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
11718 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
11719 }
11720 }
11721
Jani Nikula3cfca972013-08-27 15:12:26 +030011722 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080011723 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080011724 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080011725
Paulo Zanonie2debe92013-02-18 19:00:27 -030011726 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011727 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011728 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011729 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
11730 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011731 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011732 }
Ma Ling27185ae2009-08-24 13:50:23 +080011733
Imre Deake7281ea2013-05-08 13:14:08 +030011734 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011735 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080011736 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040011737
11738 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040011739
Paulo Zanonie2debe92013-02-18 19:00:27 -030011740 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011741 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011742 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011743 }
Ma Ling27185ae2009-08-24 13:50:23 +080011744
Paulo Zanonie2debe92013-02-18 19:00:27 -030011745 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080011746
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011747 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
11748 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011749 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011750 }
Imre Deake7281ea2013-05-08 13:14:08 +030011751 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011752 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080011753 }
Ma Ling27185ae2009-08-24 13:50:23 +080011754
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011755 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030011756 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011757 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070011758 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080011759 intel_dvo_init(dev);
11760
Zhenyu Wang103a1962009-11-27 11:44:36 +080011761 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080011762 intel_tv_init(dev);
11763
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070011764 intel_edp_psr_init(dev);
11765
Chris Wilson4ef69c72010-09-09 15:14:28 +010011766 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
11767 encoder->base.possible_crtcs = encoder->crtc_mask;
11768 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020011769 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080011770 }
Chris Wilson47356eb2011-01-11 17:06:04 +000011771
Paulo Zanonidde86e22012-12-01 12:04:25 -020011772 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020011773
11774 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011775}
11776
11777static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
11778{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030011779 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080011780 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080011781
Daniel Vetteref2d6332014-02-10 18:00:38 +010011782 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030011783 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010011784 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030011785 drm_gem_object_unreference(&intel_fb->obj->base);
11786 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080011787 kfree(intel_fb);
11788}
11789
11790static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000011791 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080011792 unsigned int *handle)
11793{
11794 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011795 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080011796
Chris Wilson05394f32010-11-08 19:18:58 +000011797 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080011798}
11799
11800static const struct drm_framebuffer_funcs intel_fb_funcs = {
11801 .destroy = intel_user_framebuffer_destroy,
11802 .create_handle = intel_user_framebuffer_create_handle,
11803};
11804
Daniel Vetterb5ea6422014-03-02 21:18:00 +010011805static int intel_framebuffer_init(struct drm_device *dev,
11806 struct intel_framebuffer *intel_fb,
11807 struct drm_mode_fb_cmd2 *mode_cmd,
11808 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080011809{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080011810 int aligned_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010011811 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080011812 int ret;
11813
Daniel Vetterdd4916c2013-10-09 21:23:51 +020011814 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
11815
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011816 if (obj->tiling_mode == I915_TILING_Y) {
11817 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010011818 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011819 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011820
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011821 if (mode_cmd->pitches[0] & 63) {
11822 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
11823 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010011824 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011825 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011826
Chris Wilsona35cdaa2013-06-25 17:26:45 +010011827 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
11828 pitch_limit = 32*1024;
11829 } else if (INTEL_INFO(dev)->gen >= 4) {
11830 if (obj->tiling_mode)
11831 pitch_limit = 16*1024;
11832 else
11833 pitch_limit = 32*1024;
11834 } else if (INTEL_INFO(dev)->gen >= 3) {
11835 if (obj->tiling_mode)
11836 pitch_limit = 8*1024;
11837 else
11838 pitch_limit = 16*1024;
11839 } else
11840 /* XXX DSPC is limited to 4k tiled */
11841 pitch_limit = 8*1024;
11842
11843 if (mode_cmd->pitches[0] > pitch_limit) {
11844 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
11845 obj->tiling_mode ? "tiled" : "linear",
11846 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011847 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011848 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011849
11850 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011851 mode_cmd->pitches[0] != obj->stride) {
11852 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
11853 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011854 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011855 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011856
Ville Syrjälä57779d02012-10-31 17:50:14 +020011857 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011858 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020011859 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020011860 case DRM_FORMAT_RGB565:
11861 case DRM_FORMAT_XRGB8888:
11862 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020011863 break;
11864 case DRM_FORMAT_XRGB1555:
11865 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011866 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011867 DRM_DEBUG("unsupported pixel format: %s\n",
11868 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011869 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011870 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020011871 break;
11872 case DRM_FORMAT_XBGR8888:
11873 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020011874 case DRM_FORMAT_XRGB2101010:
11875 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020011876 case DRM_FORMAT_XBGR2101010:
11877 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011878 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011879 DRM_DEBUG("unsupported pixel format: %s\n",
11880 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011881 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011882 }
Jesse Barnesb5626742011-06-24 12:19:27 -070011883 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020011884 case DRM_FORMAT_YUYV:
11885 case DRM_FORMAT_UYVY:
11886 case DRM_FORMAT_YVYU:
11887 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011888 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011889 DRM_DEBUG("unsupported pixel format: %s\n",
11890 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011891 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011892 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011893 break;
11894 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011895 DRM_DEBUG("unsupported pixel format: %s\n",
11896 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010011897 return -EINVAL;
11898 }
11899
Ville Syrjälä90f9a332012-10-31 17:50:19 +020011900 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
11901 if (mode_cmd->offsets[0] != 0)
11902 return -EINVAL;
11903
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080011904 aligned_height = intel_align_height(dev, mode_cmd->height,
11905 obj->tiling_mode);
Daniel Vetter53155c02013-10-09 21:55:33 +020011906 /* FIXME drm helper for size checks (especially planar formats)? */
11907 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
11908 return -EINVAL;
11909
Daniel Vetterc7d73f62012-12-13 23:38:38 +010011910 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
11911 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020011912 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010011913
Jesse Barnes79e53942008-11-07 14:24:08 -080011914 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
11915 if (ret) {
11916 DRM_ERROR("framebuffer init failed %d\n", ret);
11917 return ret;
11918 }
11919
Jesse Barnes79e53942008-11-07 14:24:08 -080011920 return 0;
11921}
11922
Jesse Barnes79e53942008-11-07 14:24:08 -080011923static struct drm_framebuffer *
11924intel_user_framebuffer_create(struct drm_device *dev,
11925 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011926 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080011927{
Chris Wilson05394f32010-11-08 19:18:58 +000011928 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080011929
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011930 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
11931 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000011932 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010011933 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080011934
Chris Wilsond2dff872011-04-19 08:36:26 +010011935 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080011936}
11937
Daniel Vetter4520f532013-10-09 09:18:51 +020011938#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020011939static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020011940{
11941}
11942#endif
11943
Jesse Barnes79e53942008-11-07 14:24:08 -080011944static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080011945 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020011946 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080011947};
11948
Jesse Barnese70236a2009-09-21 10:42:27 -070011949/* Set up chip specific display functions */
11950static void intel_init_display(struct drm_device *dev)
11951{
11952 struct drm_i915_private *dev_priv = dev->dev_private;
11953
Daniel Vetteree9300b2013-06-03 22:40:22 +020011954 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
11955 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030011956 else if (IS_CHERRYVIEW(dev))
11957 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020011958 else if (IS_VALLEYVIEW(dev))
11959 dev_priv->display.find_dpll = vlv_find_best_dpll;
11960 else if (IS_PINEVIEW(dev))
11961 dev_priv->display.find_dpll = pnv_find_best_dpll;
11962 else
11963 dev_priv->display.find_dpll = i9xx_find_best_dpll;
11964
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011965 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011966 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080011967 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030011968 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020011969 dev_priv->display.crtc_enable = haswell_crtc_enable;
11970 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011971 dev_priv->display.off = haswell_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011972 dev_priv->display.update_primary_plane =
11973 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030011974 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011975 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080011976 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070011977 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020011978 dev_priv->display.crtc_enable = ironlake_crtc_enable;
11979 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011980 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011981 dev_priv->display.update_primary_plane =
11982 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070011983 } else if (IS_VALLEYVIEW(dev)) {
11984 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080011985 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Jesse Barnes89b667f2013-04-18 14:51:36 -070011986 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
11987 dev_priv->display.crtc_enable = valleyview_crtc_enable;
11988 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11989 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011990 dev_priv->display.update_primary_plane =
11991 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070011992 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011993 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080011994 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070011995 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020011996 dev_priv->display.crtc_enable = i9xx_crtc_enable;
11997 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011998 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011999 dev_priv->display.update_primary_plane =
12000 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070012001 }
Jesse Barnese70236a2009-09-21 10:42:27 -070012002
Jesse Barnese70236a2009-09-21 10:42:27 -070012003 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070012004 if (IS_VALLEYVIEW(dev))
12005 dev_priv->display.get_display_clock_speed =
12006 valleyview_get_display_clock_speed;
12007 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070012008 dev_priv->display.get_display_clock_speed =
12009 i945_get_display_clock_speed;
12010 else if (IS_I915G(dev))
12011 dev_priv->display.get_display_clock_speed =
12012 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012013 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012014 dev_priv->display.get_display_clock_speed =
12015 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012016 else if (IS_PINEVIEW(dev))
12017 dev_priv->display.get_display_clock_speed =
12018 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070012019 else if (IS_I915GM(dev))
12020 dev_priv->display.get_display_clock_speed =
12021 i915gm_get_display_clock_speed;
12022 else if (IS_I865G(dev))
12023 dev_priv->display.get_display_clock_speed =
12024 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020012025 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012026 dev_priv->display.get_display_clock_speed =
12027 i855_get_display_clock_speed;
12028 else /* 852, 830 */
12029 dev_priv->display.get_display_clock_speed =
12030 i830_get_display_clock_speed;
12031
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080012032 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010012033 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070012034 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080012035 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080012036 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070012037 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080012038 dev_priv->display.write_eld = ironlake_write_eld;
Paulo Zanoni9a952a02014-03-07 20:12:34 -030012039 dev_priv->display.modeset_global_resources =
12040 snb_modeset_global_resources;
Jesse Barnes357555c2011-04-28 15:09:55 -070012041 } else if (IS_IVYBRIDGE(dev)) {
12042 /* FIXME: detect B0+ stepping and use auto training */
12043 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080012044 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020012045 dev_priv->display.modeset_global_resources =
12046 ivb_modeset_global_resources;
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070012047 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030012048 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080012049 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020012050 dev_priv->display.modeset_global_resources =
12051 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020012052 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070012053 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080012054 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012055 } else if (IS_VALLEYVIEW(dev)) {
12056 dev_priv->display.modeset_global_resources =
12057 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040012058 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070012059 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012060
12061 /* Default just returns -ENODEV to indicate unsupported */
12062 dev_priv->display.queue_flip = intel_default_queue_flip;
12063
12064 switch (INTEL_INFO(dev)->gen) {
12065 case 2:
12066 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12067 break;
12068
12069 case 3:
12070 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12071 break;
12072
12073 case 4:
12074 case 5:
12075 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12076 break;
12077
12078 case 6:
12079 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12080 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012081 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070012082 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012083 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12084 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012085 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020012086
12087 intel_panel_init_backlight_funcs(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070012088}
12089
Jesse Barnesb690e962010-07-19 13:53:12 -070012090/*
12091 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12092 * resume, or other times. This quirk makes sure that's the case for
12093 * affected systems.
12094 */
Akshay Joshi0206e352011-08-16 15:34:10 -040012095static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070012096{
12097 struct drm_i915_private *dev_priv = dev->dev_private;
12098
12099 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012100 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070012101}
12102
Keith Packard435793d2011-07-12 14:56:22 -070012103/*
12104 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12105 */
12106static void quirk_ssc_force_disable(struct drm_device *dev)
12107{
12108 struct drm_i915_private *dev_priv = dev->dev_private;
12109 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012110 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070012111}
12112
Carsten Emde4dca20e2012-03-15 15:56:26 +010012113/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010012114 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12115 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010012116 */
12117static void quirk_invert_brightness(struct drm_device *dev)
12118{
12119 struct drm_i915_private *dev_priv = dev->dev_private;
12120 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012121 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070012122}
12123
12124struct intel_quirk {
12125 int device;
12126 int subsystem_vendor;
12127 int subsystem_device;
12128 void (*hook)(struct drm_device *dev);
12129};
12130
Egbert Eich5f85f172012-10-14 15:46:38 +020012131/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12132struct intel_dmi_quirk {
12133 void (*hook)(struct drm_device *dev);
12134 const struct dmi_system_id (*dmi_id_list)[];
12135};
12136
12137static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12138{
12139 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12140 return 1;
12141}
12142
12143static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12144 {
12145 .dmi_id_list = &(const struct dmi_system_id[]) {
12146 {
12147 .callback = intel_dmi_reverse_brightness,
12148 .ident = "NCR Corporation",
12149 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12150 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12151 },
12152 },
12153 { } /* terminating entry */
12154 },
12155 .hook = quirk_invert_brightness,
12156 },
12157};
12158
Ben Widawskyc43b5632012-04-16 14:07:40 -070012159static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070012160 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040012161 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070012162
Jesse Barnesb690e962010-07-19 13:53:12 -070012163 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12164 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12165
Jesse Barnesb690e962010-07-19 13:53:12 -070012166 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12167 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12168
Keith Packard435793d2011-07-12 14:56:22 -070012169 /* Lenovo U160 cannot use SSC on LVDS */
12170 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020012171
12172 /* Sony Vaio Y cannot use SSC on LVDS */
12173 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010012174
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010012175 /* Acer Aspire 5734Z must invert backlight brightness */
12176 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12177
12178 /* Acer/eMachines G725 */
12179 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12180
12181 /* Acer/eMachines e725 */
12182 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12183
12184 /* Acer/Packard Bell NCL20 */
12185 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12186
12187 /* Acer Aspire 4736Z */
12188 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020012189
12190 /* Acer Aspire 5336 */
12191 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -070012192};
12193
12194static void intel_init_quirks(struct drm_device *dev)
12195{
12196 struct pci_dev *d = dev->pdev;
12197 int i;
12198
12199 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12200 struct intel_quirk *q = &intel_quirks[i];
12201
12202 if (d->device == q->device &&
12203 (d->subsystem_vendor == q->subsystem_vendor ||
12204 q->subsystem_vendor == PCI_ANY_ID) &&
12205 (d->subsystem_device == q->subsystem_device ||
12206 q->subsystem_device == PCI_ANY_ID))
12207 q->hook(dev);
12208 }
Egbert Eich5f85f172012-10-14 15:46:38 +020012209 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12210 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12211 intel_dmi_quirks[i].hook(dev);
12212 }
Jesse Barnesb690e962010-07-19 13:53:12 -070012213}
12214
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012215/* Disable the VGA plane that we never use */
12216static void i915_disable_vga(struct drm_device *dev)
12217{
12218 struct drm_i915_private *dev_priv = dev->dev_private;
12219 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012220 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012221
Ville Syrjälä2b37c612014-01-22 21:32:38 +020012222 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012223 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070012224 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012225 sr1 = inb(VGA_SR_DATA);
12226 outb(sr1 | 1<<5, VGA_SR_DATA);
12227 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12228 udelay(300);
12229
12230 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
12231 POSTING_READ(vga_reg);
12232}
12233
Daniel Vetterf8175862012-04-10 15:50:11 +020012234void intel_modeset_init_hw(struct drm_device *dev)
12235{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030012236 intel_prepare_ddi(dev);
12237
Daniel Vetterf8175862012-04-10 15:50:11 +020012238 intel_init_clock_gating(dev);
12239
Jesse Barnes5382f5f352013-12-16 16:34:24 -080012240 intel_reset_dpio(dev);
Jesse Barnes40e9cf62013-10-03 11:35:46 -070012241
Daniel Vetter8090c6b2012-06-24 16:42:32 +020012242 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020012243}
12244
Imre Deak7d708ee2013-04-17 14:04:50 +030012245void intel_modeset_suspend_hw(struct drm_device *dev)
12246{
12247 intel_suspend_hw(dev);
12248}
12249
Jesse Barnes79e53942008-11-07 14:24:08 -080012250void intel_modeset_init(struct drm_device *dev)
12251{
Jesse Barnes652c3932009-08-17 13:31:43 -070012252 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000012253 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012254 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080012255 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080012256
12257 drm_mode_config_init(dev);
12258
12259 dev->mode_config.min_width = 0;
12260 dev->mode_config.min_height = 0;
12261
Dave Airlie019d96c2011-09-29 16:20:42 +010012262 dev->mode_config.preferred_depth = 24;
12263 dev->mode_config.prefer_shadow = 1;
12264
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020012265 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080012266
Jesse Barnesb690e962010-07-19 13:53:12 -070012267 intel_init_quirks(dev);
12268
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030012269 intel_init_pm(dev);
12270
Ben Widawskye3c74752013-04-05 13:12:39 -070012271 if (INTEL_INFO(dev)->num_pipes == 0)
12272 return;
12273
Jesse Barnese70236a2009-09-21 10:42:27 -070012274 intel_init_display(dev);
12275
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012276 if (IS_GEN2(dev)) {
12277 dev->mode_config.max_width = 2048;
12278 dev->mode_config.max_height = 2048;
12279 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070012280 dev->mode_config.max_width = 4096;
12281 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080012282 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012283 dev->mode_config.max_width = 8192;
12284 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080012285 }
Damien Lespiau068be562014-03-28 14:17:49 +000012286
12287 if (IS_GEN2(dev)) {
12288 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12289 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12290 } else {
12291 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12292 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12293 }
12294
Ben Widawsky5d4545a2013-01-17 12:45:15 -080012295 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080012296
Zhao Yakui28c97732009-10-09 11:39:41 +080012297 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012298 INTEL_INFO(dev)->num_pipes,
12299 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080012300
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012301 for_each_pipe(pipe) {
12302 intel_crtc_init(dev, pipe);
Damien Lespiau1fe47782014-03-03 17:31:47 +000012303 for_each_sprite(pipe, sprite) {
12304 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012305 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030012306 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000012307 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012308 }
Jesse Barnes79e53942008-11-07 14:24:08 -080012309 }
12310
Jesse Barnesf42bb702013-12-16 16:34:23 -080012311 intel_init_dpio(dev);
Jesse Barnes5382f5f352013-12-16 16:34:24 -080012312 intel_reset_dpio(dev);
Jesse Barnesf42bb702013-12-16 16:34:23 -080012313
Paulo Zanoni79f689a2012-10-05 12:05:52 -030012314 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012315 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012316
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012317 /* Just disable it once at startup */
12318 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012319 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000012320
12321 /* Just in case the BIOS is doing something questionable. */
12322 intel_disable_fbc(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012323
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012324 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012325 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012326 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012327
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012328 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080012329 if (!crtc->active)
12330 continue;
12331
Jesse Barnes46f297f2014-03-07 08:57:48 -080012332 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080012333 * Note that reserving the BIOS fb up front prevents us
12334 * from stuffing other stolen allocations like the ring
12335 * on top. This prevents some ugliness at boot time, and
12336 * can even allow for smooth boot transitions if the BIOS
12337 * fb is large enough for the active pipe configuration.
12338 */
12339 if (dev_priv->display.get_plane_config) {
12340 dev_priv->display.get_plane_config(crtc,
12341 &crtc->plane_config);
12342 /*
12343 * If the fb is shared between multiple heads, we'll
12344 * just get the first one.
12345 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080012346 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012347 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080012348 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010012349}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080012350
Daniel Vetter7fad7982012-07-04 17:51:47 +020012351static void intel_enable_pipe_a(struct drm_device *dev)
12352{
12353 struct intel_connector *connector;
12354 struct drm_connector *crt = NULL;
12355 struct intel_load_detect_pipe load_detect_temp;
Rob Clark51fd3712013-11-19 12:10:12 -050012356 struct drm_modeset_acquire_ctx ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020012357
12358 /* We can't just switch on the pipe A, we need to set things up with a
12359 * proper mode and output configuration. As a gross hack, enable pipe A
12360 * by enabling the load detect pipe once. */
12361 list_for_each_entry(connector,
12362 &dev->mode_config.connector_list,
12363 base.head) {
12364 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
12365 crt = &connector->base;
12366 break;
12367 }
12368 }
12369
12370 if (!crt)
12371 return;
12372
Rob Clark51fd3712013-11-19 12:10:12 -050012373 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
12374 intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020012375
12376
12377}
12378
Daniel Vetterfa555832012-10-10 23:14:00 +020012379static bool
12380intel_check_plane_mapping(struct intel_crtc *crtc)
12381{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012382 struct drm_device *dev = crtc->base.dev;
12383 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020012384 u32 reg, val;
12385
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012386 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020012387 return true;
12388
12389 reg = DSPCNTR(!crtc->plane);
12390 val = I915_READ(reg);
12391
12392 if ((val & DISPLAY_PLANE_ENABLE) &&
12393 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
12394 return false;
12395
12396 return true;
12397}
12398
Daniel Vetter24929352012-07-02 20:28:59 +020012399static void intel_sanitize_crtc(struct intel_crtc *crtc)
12400{
12401 struct drm_device *dev = crtc->base.dev;
12402 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020012403 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020012404
Daniel Vetter24929352012-07-02 20:28:59 +020012405 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020012406 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020012407 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
12408
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030012409 /* restore vblank interrupts to correct state */
12410 if (crtc->active)
12411 drm_vblank_on(dev, crtc->pipe);
12412 else
12413 drm_vblank_off(dev, crtc->pipe);
12414
Daniel Vetter24929352012-07-02 20:28:59 +020012415 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020012416 * disable the crtc (and hence change the state) if it is wrong. Note
12417 * that gen4+ has a fixed plane -> pipe mapping. */
12418 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020012419 struct intel_connector *connector;
12420 bool plane;
12421
Daniel Vetter24929352012-07-02 20:28:59 +020012422 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
12423 crtc->base.base.id);
12424
12425 /* Pipe has the wrong plane attached and the plane is active.
12426 * Temporarily change the plane mapping and disable everything
12427 * ... */
12428 plane = crtc->plane;
12429 crtc->plane = !plane;
12430 dev_priv->display.crtc_disable(&crtc->base);
12431 crtc->plane = plane;
12432
12433 /* ... and break all links. */
12434 list_for_each_entry(connector, &dev->mode_config.connector_list,
12435 base.head) {
12436 if (connector->encoder->base.crtc != &crtc->base)
12437 continue;
12438
Egbert Eich7f1950f2014-04-25 10:56:22 +020012439 connector->base.dpms = DRM_MODE_DPMS_OFF;
12440 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020012441 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020012442 /* multiple connectors may have the same encoder:
12443 * handle them and break crtc link separately */
12444 list_for_each_entry(connector, &dev->mode_config.connector_list,
12445 base.head)
12446 if (connector->encoder->base.crtc == &crtc->base) {
12447 connector->encoder->base.crtc = NULL;
12448 connector->encoder->connectors_active = false;
12449 }
Daniel Vetter24929352012-07-02 20:28:59 +020012450
12451 WARN_ON(crtc->active);
12452 crtc->base.enabled = false;
12453 }
Daniel Vetter24929352012-07-02 20:28:59 +020012454
Daniel Vetter7fad7982012-07-04 17:51:47 +020012455 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
12456 crtc->pipe == PIPE_A && !crtc->active) {
12457 /* BIOS forgot to enable pipe A, this mostly happens after
12458 * resume. Force-enable the pipe to fix this, the update_dpms
12459 * call below we restore the pipe to the right state, but leave
12460 * the required bits on. */
12461 intel_enable_pipe_a(dev);
12462 }
12463
Daniel Vetter24929352012-07-02 20:28:59 +020012464 /* Adjust the state of the output pipe according to whether we
12465 * have active connectors/encoders. */
12466 intel_crtc_update_dpms(&crtc->base);
12467
12468 if (crtc->active != crtc->base.enabled) {
12469 struct intel_encoder *encoder;
12470
12471 /* This can happen either due to bugs in the get_hw_state
12472 * functions or because the pipe is force-enabled due to the
12473 * pipe A quirk. */
12474 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
12475 crtc->base.base.id,
12476 crtc->base.enabled ? "enabled" : "disabled",
12477 crtc->active ? "enabled" : "disabled");
12478
12479 crtc->base.enabled = crtc->active;
12480
12481 /* Because we only establish the connector -> encoder ->
12482 * crtc links if something is active, this means the
12483 * crtc is now deactivated. Break the links. connector
12484 * -> encoder links are only establish when things are
12485 * actually up, hence no need to break them. */
12486 WARN_ON(crtc->active);
12487
12488 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
12489 WARN_ON(encoder->connectors_active);
12490 encoder->base.crtc = NULL;
12491 }
12492 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020012493
12494 if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010012495 /*
12496 * We start out with underrun reporting disabled to avoid races.
12497 * For correct bookkeeping mark this on active crtcs.
12498 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020012499 * Also on gmch platforms we dont have any hardware bits to
12500 * disable the underrun reporting. Which means we need to start
12501 * out with underrun reporting disabled also on inactive pipes,
12502 * since otherwise we'll complain about the garbage we read when
12503 * e.g. coming up after runtime pm.
12504 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010012505 * No protection against concurrent access is required - at
12506 * worst a fifo underrun happens which also sets this to false.
12507 */
12508 crtc->cpu_fifo_underrun_disabled = true;
12509 crtc->pch_fifo_underrun_disabled = true;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012510
12511 update_scanline_offset(crtc);
Daniel Vetter4cc31482014-03-24 00:01:41 +010012512 }
Daniel Vetter24929352012-07-02 20:28:59 +020012513}
12514
12515static void intel_sanitize_encoder(struct intel_encoder *encoder)
12516{
12517 struct intel_connector *connector;
12518 struct drm_device *dev = encoder->base.dev;
12519
12520 /* We need to check both for a crtc link (meaning that the
12521 * encoder is active and trying to read from a pipe) and the
12522 * pipe itself being active. */
12523 bool has_active_crtc = encoder->base.crtc &&
12524 to_intel_crtc(encoder->base.crtc)->active;
12525
12526 if (encoder->connectors_active && !has_active_crtc) {
12527 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
12528 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012529 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012530
12531 /* Connector is active, but has no active pipe. This is
12532 * fallout from our resume register restoring. Disable
12533 * the encoder manually again. */
12534 if (encoder->base.crtc) {
12535 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
12536 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012537 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012538 encoder->disable(encoder);
12539 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020012540 encoder->base.crtc = NULL;
12541 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020012542
12543 /* Inconsistent output/port/pipe state happens presumably due to
12544 * a bug in one of the get_hw_state functions. Or someplace else
12545 * in our code, like the register restore mess on resume. Clamp
12546 * things to off as a safer default. */
12547 list_for_each_entry(connector,
12548 &dev->mode_config.connector_list,
12549 base.head) {
12550 if (connector->encoder != encoder)
12551 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020012552 connector->base.dpms = DRM_MODE_DPMS_OFF;
12553 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020012554 }
12555 }
12556 /* Enabled encoders without active connectors will be fixed in
12557 * the crtc fixup. */
12558}
12559
Imre Deak04098752014-02-18 00:02:16 +020012560void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012561{
12562 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012563 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012564
Imre Deak04098752014-02-18 00:02:16 +020012565 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
12566 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
12567 i915_disable_vga(dev);
12568 }
12569}
12570
12571void i915_redisable_vga(struct drm_device *dev)
12572{
12573 struct drm_i915_private *dev_priv = dev->dev_private;
12574
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030012575 /* This function can be called both from intel_modeset_setup_hw_state or
12576 * at a very early point in our resume sequence, where the power well
12577 * structures are not yet restored. Since this function is at a very
12578 * paranoid "someone might have enabled VGA while we were not looking"
12579 * level, just check if the power well is enabled instead of trying to
12580 * follow the "don't touch the power well if we don't need it" policy
12581 * the rest of the driver uses. */
Imre Deak04098752014-02-18 00:02:16 +020012582 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030012583 return;
12584
Imre Deak04098752014-02-18 00:02:16 +020012585 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012586}
12587
Ville Syrjälä98ec7732014-04-30 17:43:01 +030012588static bool primary_get_hw_state(struct intel_crtc *crtc)
12589{
12590 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
12591
12592 if (!crtc->active)
12593 return false;
12594
12595 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
12596}
12597
Daniel Vetter30e984d2013-06-05 13:34:17 +020012598static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020012599{
12600 struct drm_i915_private *dev_priv = dev->dev_private;
12601 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020012602 struct intel_crtc *crtc;
12603 struct intel_encoder *encoder;
12604 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020012605 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020012606
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012607 for_each_intel_crtc(dev, crtc) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010012608 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020012609
Daniel Vetter99535992014-04-13 12:00:33 +020012610 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
12611
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012612 crtc->active = dev_priv->display.get_pipe_config(crtc,
12613 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020012614
12615 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030012616 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020012617
12618 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
12619 crtc->base.base.id,
12620 crtc->active ? "enabled" : "disabled");
12621 }
12622
Daniel Vetter53589012013-06-05 13:34:16 +020012623 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012624 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030012625 intel_ddi_setup_hw_pll_state(dev);
12626
Daniel Vetter53589012013-06-05 13:34:16 +020012627 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12628 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12629
12630 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
12631 pll->active = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012632 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020012633 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12634 pll->active++;
12635 }
12636 pll->refcount = pll->active;
12637
Daniel Vetter35c95372013-07-17 06:55:04 +020012638 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
12639 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020012640 }
12641
Daniel Vetter24929352012-07-02 20:28:59 +020012642 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12643 base.head) {
12644 pipe = 0;
12645
12646 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012647 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12648 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012649 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020012650 } else {
12651 encoder->base.crtc = NULL;
12652 }
12653
12654 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010012655 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020012656 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012657 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020012658 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010012659 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020012660 }
12661
12662 list_for_each_entry(connector, &dev->mode_config.connector_list,
12663 base.head) {
12664 if (connector->get_hw_state(connector)) {
12665 connector->base.dpms = DRM_MODE_DPMS_ON;
12666 connector->encoder->connectors_active = true;
12667 connector->base.encoder = &connector->encoder->base;
12668 } else {
12669 connector->base.dpms = DRM_MODE_DPMS_OFF;
12670 connector->base.encoder = NULL;
12671 }
12672 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
12673 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012674 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020012675 connector->base.encoder ? "enabled" : "disabled");
12676 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020012677}
12678
12679/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
12680 * and i915 state tracking structures. */
12681void intel_modeset_setup_hw_state(struct drm_device *dev,
12682 bool force_restore)
12683{
12684 struct drm_i915_private *dev_priv = dev->dev_private;
12685 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020012686 struct intel_crtc *crtc;
12687 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020012688 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020012689
12690 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020012691
Jesse Barnesbabea612013-06-26 18:57:38 +030012692 /*
12693 * Now that we have the config, copy it to each CRTC struct
12694 * Note that this could go away if we move to using crtc_config
12695 * checking everywhere.
12696 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012697 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020012698 if (crtc->active && i915.fastboot) {
Daniel Vetterf6a83282014-02-11 15:28:57 -080012699 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030012700 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
12701 crtc->base.base.id);
12702 drm_mode_debug_printmodeline(&crtc->base.mode);
12703 }
12704 }
12705
Daniel Vetter24929352012-07-02 20:28:59 +020012706 /* HW state is read out, now we need to sanitize this mess. */
12707 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12708 base.head) {
12709 intel_sanitize_encoder(encoder);
12710 }
12711
12712 for_each_pipe(pipe) {
12713 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12714 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012715 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020012716 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012717
Daniel Vetter35c95372013-07-17 06:55:04 +020012718 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12719 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12720
12721 if (!pll->on || pll->active)
12722 continue;
12723
12724 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
12725
12726 pll->disable(dev_priv, pll);
12727 pll->on = false;
12728 }
12729
Ville Syrjälä96f90c52013-12-05 15:51:38 +020012730 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030012731 ilk_wm_get_hw_state(dev);
12732
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012733 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030012734 i915_redisable_vga(dev);
12735
Daniel Vetterf30da182013-04-11 20:22:50 +020012736 /*
12737 * We need to use raw interfaces for restoring state to avoid
12738 * checking (bogus) intermediate states.
12739 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012740 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070012741 struct drm_crtc *crtc =
12742 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020012743
12744 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
Matt Roperf4510a22014-04-01 15:22:40 -070012745 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012746 }
12747 } else {
12748 intel_modeset_update_staged_output_state(dev);
12749 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012750
12751 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010012752}
12753
12754void intel_modeset_gem_init(struct drm_device *dev)
12755{
Jesse Barnes484b41d2014-03-07 08:57:55 -080012756 struct drm_crtc *c;
12757 struct intel_framebuffer *fb;
12758
Imre Deakae484342014-03-31 15:10:44 +030012759 mutex_lock(&dev->struct_mutex);
12760 intel_init_gt_powersave(dev);
12761 mutex_unlock(&dev->struct_mutex);
12762
Chris Wilson1833b132012-05-09 11:56:28 +010012763 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020012764
12765 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080012766
12767 /*
12768 * Make sure any fbs we allocated at startup are properly
12769 * pinned & fenced. When we do the allocation it's too early
12770 * for this.
12771 */
12772 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012773 for_each_crtc(dev, c) {
Dave Airlie66e514c2014-04-03 07:51:54 +100012774 if (!c->primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -080012775 continue;
12776
Dave Airlie66e514c2014-04-03 07:51:54 +100012777 fb = to_intel_framebuffer(c->primary->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -080012778 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
12779 DRM_ERROR("failed to pin boot fb on pipe %d\n",
12780 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100012781 drm_framebuffer_unreference(c->primary->fb);
12782 c->primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -080012783 }
12784 }
12785 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012786}
12787
Imre Deak4932e2c2014-02-11 17:12:48 +020012788void intel_connector_unregister(struct intel_connector *intel_connector)
12789{
12790 struct drm_connector *connector = &intel_connector->base;
12791
12792 intel_panel_destroy_backlight(connector);
12793 drm_sysfs_connector_remove(connector);
12794}
12795
Jesse Barnes79e53942008-11-07 14:24:08 -080012796void intel_modeset_cleanup(struct drm_device *dev)
12797{
Jesse Barnes652c3932009-08-17 13:31:43 -070012798 struct drm_i915_private *dev_priv = dev->dev_private;
12799 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030012800 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070012801
Daniel Vetterfd0c0642013-04-24 11:13:35 +020012802 /*
12803 * Interrupts and polling as the first thing to avoid creating havoc.
12804 * Too much stuff here (turning of rps, connectors, ...) would
12805 * experience fancy races otherwise.
12806 */
12807 drm_irq_uninstall(dev);
12808 cancel_work_sync(&dev_priv->hotplug_work);
12809 /*
12810 * Due to the hpd irq storm handling the hotplug work can re-arm the
12811 * poll handlers. Hence disable polling after hpd handling is shut down.
12812 */
Keith Packardf87ea762010-10-03 19:36:26 -070012813 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020012814
Jesse Barnes652c3932009-08-17 13:31:43 -070012815 mutex_lock(&dev->struct_mutex);
12816
Jesse Barnes723bfd72010-10-07 16:01:13 -070012817 intel_unregister_dsm_handler();
12818
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012819 for_each_crtc(dev, crtc) {
Jesse Barnes652c3932009-08-17 13:31:43 -070012820 /* Skip inactive CRTCs */
Matt Roperf4510a22014-04-01 15:22:40 -070012821 if (!crtc->primary->fb)
Jesse Barnes652c3932009-08-17 13:31:43 -070012822 continue;
12823
Daniel Vetter3dec0092010-08-20 21:40:52 +020012824 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070012825 }
12826
Chris Wilson973d04f2011-07-08 12:22:37 +010012827 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070012828
Daniel Vetter8090c6b2012-06-24 16:42:32 +020012829 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000012830
Daniel Vetter930ebb42012-06-29 23:32:16 +020012831 ironlake_teardown_rc6(dev);
12832
Kristian Høgsberg69341a52009-11-11 12:19:17 -050012833 mutex_unlock(&dev->struct_mutex);
12834
Chris Wilson1630fe72011-07-08 12:22:42 +010012835 /* flush any delayed tasks or pending work */
12836 flush_scheduled_work();
12837
Jani Nikuladb31af1d2013-11-08 16:48:53 +020012838 /* destroy the backlight and sysfs files before encoders/connectors */
12839 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020012840 struct intel_connector *intel_connector;
12841
12842 intel_connector = to_intel_connector(connector);
12843 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020012844 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030012845
Jesse Barnes79e53942008-11-07 14:24:08 -080012846 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010012847
12848 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030012849
12850 mutex_lock(&dev->struct_mutex);
12851 intel_cleanup_gt_powersave(dev);
12852 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012853}
12854
Dave Airlie28d52042009-09-21 14:33:58 +100012855/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080012856 * Return which encoder is currently attached for connector.
12857 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010012858struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080012859{
Chris Wilsondf0e9242010-09-09 16:20:55 +010012860 return &intel_attached_encoder(connector)->base;
12861}
Jesse Barnes79e53942008-11-07 14:24:08 -080012862
Chris Wilsondf0e9242010-09-09 16:20:55 +010012863void intel_connector_attach_encoder(struct intel_connector *connector,
12864 struct intel_encoder *encoder)
12865{
12866 connector->encoder = encoder;
12867 drm_mode_connector_attach_encoder(&connector->base,
12868 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080012869}
Dave Airlie28d52042009-09-21 14:33:58 +100012870
12871/*
12872 * set vga decode state - true == enable VGA decode
12873 */
12874int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
12875{
12876 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000012877 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100012878 u16 gmch_ctrl;
12879
Chris Wilson75fa0412014-02-07 18:37:02 -020012880 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
12881 DRM_ERROR("failed to read control word\n");
12882 return -EIO;
12883 }
12884
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020012885 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
12886 return 0;
12887
Dave Airlie28d52042009-09-21 14:33:58 +100012888 if (state)
12889 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
12890 else
12891 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020012892
12893 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
12894 DRM_ERROR("failed to write control word\n");
12895 return -EIO;
12896 }
12897
Dave Airlie28d52042009-09-21 14:33:58 +100012898 return 0;
12899}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012900
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012901struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012902
12903 u32 power_well_driver;
12904
Chris Wilson63b66e52013-08-08 15:12:06 +020012905 int num_transcoders;
12906
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012907 struct intel_cursor_error_state {
12908 u32 control;
12909 u32 position;
12910 u32 base;
12911 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010012912 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012913
12914 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020012915 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012916 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030012917 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010012918 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012919
12920 struct intel_plane_error_state {
12921 u32 control;
12922 u32 stride;
12923 u32 size;
12924 u32 pos;
12925 u32 addr;
12926 u32 surface;
12927 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010012928 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020012929
12930 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020012931 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020012932 enum transcoder cpu_transcoder;
12933
12934 u32 conf;
12935
12936 u32 htotal;
12937 u32 hblank;
12938 u32 hsync;
12939 u32 vtotal;
12940 u32 vblank;
12941 u32 vsync;
12942 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012943};
12944
12945struct intel_display_error_state *
12946intel_display_capture_error_state(struct drm_device *dev)
12947{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012948 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012949 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020012950 int transcoders[] = {
12951 TRANSCODER_A,
12952 TRANSCODER_B,
12953 TRANSCODER_C,
12954 TRANSCODER_EDP,
12955 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012956 int i;
12957
Chris Wilson63b66e52013-08-08 15:12:06 +020012958 if (INTEL_INFO(dev)->num_pipes == 0)
12959 return NULL;
12960
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012961 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012962 if (error == NULL)
12963 return NULL;
12964
Imre Deak190be112013-11-25 17:15:31 +020012965 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012966 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
12967
Damien Lespiau52331302012-08-15 19:23:25 +010012968 for_each_pipe(i) {
Imre Deakddf9c532013-11-27 22:02:02 +020012969 error->pipe[i].power_domain_on =
Imre Deakda7e29b2014-02-18 00:02:02 +020012970 intel_display_power_enabled_sw(dev_priv,
12971 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020012972 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012973 continue;
12974
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030012975 error->cursor[i].control = I915_READ(CURCNTR(i));
12976 error->cursor[i].position = I915_READ(CURPOS(i));
12977 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012978
12979 error->plane[i].control = I915_READ(DSPCNTR(i));
12980 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012981 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030012982 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012983 error->plane[i].pos = I915_READ(DSPPOS(i));
12984 }
Paulo Zanonica291362013-03-06 20:03:14 -030012985 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
12986 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012987 if (INTEL_INFO(dev)->gen >= 4) {
12988 error->plane[i].surface = I915_READ(DSPSURF(i));
12989 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
12990 }
12991
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012992 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030012993
12994 if (!HAS_PCH_SPLIT(dev))
12995 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020012996 }
12997
12998 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
12999 if (HAS_DDI(dev_priv->dev))
13000 error->num_transcoders++; /* Account for eDP. */
13001
13002 for (i = 0; i < error->num_transcoders; i++) {
13003 enum transcoder cpu_transcoder = transcoders[i];
13004
Imre Deakddf9c532013-11-27 22:02:02 +020013005 error->transcoder[i].power_domain_on =
Imre Deakda7e29b2014-02-18 00:02:02 +020013006 intel_display_power_enabled_sw(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020013007 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020013008 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013009 continue;
13010
Chris Wilson63b66e52013-08-08 15:12:06 +020013011 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13012
13013 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13014 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13015 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13016 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13017 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13018 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13019 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013020 }
13021
13022 return error;
13023}
13024
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013025#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13026
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013027void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013028intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013029 struct drm_device *dev,
13030 struct intel_display_error_state *error)
13031{
13032 int i;
13033
Chris Wilson63b66e52013-08-08 15:12:06 +020013034 if (!error)
13035 return;
13036
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013037 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020013038 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013039 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013040 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010013041 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013042 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020013043 err_printf(m, " Power: %s\n",
13044 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013045 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030013046 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013047
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013048 err_printf(m, "Plane [%d]:\n", i);
13049 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13050 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013051 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013052 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13053 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013054 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030013055 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013056 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013057 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013058 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13059 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013060 }
13061
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013062 err_printf(m, "Cursor [%d]:\n", i);
13063 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13064 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13065 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013066 }
Chris Wilson63b66e52013-08-08 15:12:06 +020013067
13068 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010013069 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020013070 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020013071 err_printf(m, " Power: %s\n",
13072 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020013073 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13074 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13075 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13076 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13077 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13078 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13079 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13080 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013081}