blob: d138b302f275479f4c2445deae10a9ba33957ef2 [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>
Keith Packardc0f372b32011-11-16 22:24:52 -080042#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043
Akshay Joshi0206e352011-08-16 15:34:10 -040044bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
Daniel Vetter3dec0092010-08-20 21:40:52 +020045static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010046static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080047
48typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040049 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080050} intel_range_t;
51
52typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040053 int dot_limit;
54 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080055} intel_p2_t;
56
57#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080058typedef struct intel_limit intel_limit_t;
59struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040060 intel_range_t dot, vco, n, m, m1, m2, p, p1;
61 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080062};
Jesse Barnes79e53942008-11-07 14:24:08 -080063
Jesse Barnes2377b742010-07-07 14:06:43 -070064/* FDI */
65#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
66
Daniel Vetterd2acd212012-10-20 20:57:43 +020067int
68intel_pch_rawclk(struct drm_device *dev)
69{
70 struct drm_i915_private *dev_priv = dev->dev_private;
71
72 WARN_ON(!HAS_PCH_SPLIT(dev));
73
74 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
75}
76
Chris Wilson021357a2010-09-07 20:54:59 +010077static inline u32 /* units of 100MHz */
78intel_fdi_link_freq(struct drm_device *dev)
79{
Chris Wilson8b99e682010-10-13 09:59:17 +010080 if (IS_GEN5(dev)) {
81 struct drm_i915_private *dev_priv = dev->dev_private;
82 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
83 } else
84 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +010085}
86
Keith Packarde4b36692009-06-05 19:22:17 -070087static const intel_limit_t intel_limits_i8xx_dvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -040088 .dot = { .min = 25000, .max = 350000 },
89 .vco = { .min = 930000, .max = 1400000 },
90 .n = { .min = 3, .max = 16 },
91 .m = { .min = 96, .max = 140 },
92 .m1 = { .min = 18, .max = 26 },
93 .m2 = { .min = 6, .max = 16 },
94 .p = { .min = 4, .max = 128 },
95 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -070096 .p2 = { .dot_limit = 165000,
97 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -070098};
99
100static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400101 .dot = { .min = 25000, .max = 350000 },
102 .vco = { .min = 930000, .max = 1400000 },
103 .n = { .min = 3, .max = 16 },
104 .m = { .min = 96, .max = 140 },
105 .m1 = { .min = 18, .max = 26 },
106 .m2 = { .min = 6, .max = 16 },
107 .p = { .min = 4, .max = 128 },
108 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700109 .p2 = { .dot_limit = 165000,
110 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700111};
Eric Anholt273e27c2011-03-30 13:01:10 -0700112
Keith Packarde4b36692009-06-05 19:22:17 -0700113static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400114 .dot = { .min = 20000, .max = 400000 },
115 .vco = { .min = 1400000, .max = 2800000 },
116 .n = { .min = 1, .max = 6 },
117 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100118 .m1 = { .min = 8, .max = 18 },
119 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400120 .p = { .min = 5, .max = 80 },
121 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700122 .p2 = { .dot_limit = 200000,
123 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700124};
125
126static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400127 .dot = { .min = 20000, .max = 400000 },
128 .vco = { .min = 1400000, .max = 2800000 },
129 .n = { .min = 1, .max = 6 },
130 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100131 .m1 = { .min = 8, .max = 18 },
132 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400133 .p = { .min = 7, .max = 98 },
134 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700135 .p2 = { .dot_limit = 112000,
136 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700137};
138
Eric Anholt273e27c2011-03-30 13:01:10 -0700139
Keith Packarde4b36692009-06-05 19:22:17 -0700140static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700141 .dot = { .min = 25000, .max = 270000 },
142 .vco = { .min = 1750000, .max = 3500000},
143 .n = { .min = 1, .max = 4 },
144 .m = { .min = 104, .max = 138 },
145 .m1 = { .min = 17, .max = 23 },
146 .m2 = { .min = 5, .max = 11 },
147 .p = { .min = 10, .max = 30 },
148 .p1 = { .min = 1, .max = 3},
149 .p2 = { .dot_limit = 270000,
150 .p2_slow = 10,
151 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800152 },
Keith Packarde4b36692009-06-05 19:22:17 -0700153};
154
155static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700156 .dot = { .min = 22000, .max = 400000 },
157 .vco = { .min = 1750000, .max = 3500000},
158 .n = { .min = 1, .max = 4 },
159 .m = { .min = 104, .max = 138 },
160 .m1 = { .min = 16, .max = 23 },
161 .m2 = { .min = 5, .max = 11 },
162 .p = { .min = 5, .max = 80 },
163 .p1 = { .min = 1, .max = 8},
164 .p2 = { .dot_limit = 165000,
165 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700166};
167
168static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700169 .dot = { .min = 20000, .max = 115000 },
170 .vco = { .min = 1750000, .max = 3500000 },
171 .n = { .min = 1, .max = 3 },
172 .m = { .min = 104, .max = 138 },
173 .m1 = { .min = 17, .max = 23 },
174 .m2 = { .min = 5, .max = 11 },
175 .p = { .min = 28, .max = 112 },
176 .p1 = { .min = 2, .max = 8 },
177 .p2 = { .dot_limit = 0,
178 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800179 },
Keith Packarde4b36692009-06-05 19:22:17 -0700180};
181
182static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700183 .dot = { .min = 80000, .max = 224000 },
184 .vco = { .min = 1750000, .max = 3500000 },
185 .n = { .min = 1, .max = 3 },
186 .m = { .min = 104, .max = 138 },
187 .m1 = { .min = 17, .max = 23 },
188 .m2 = { .min = 5, .max = 11 },
189 .p = { .min = 14, .max = 42 },
190 .p1 = { .min = 2, .max = 6 },
191 .p2 = { .dot_limit = 0,
192 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800193 },
Keith Packarde4b36692009-06-05 19:22:17 -0700194};
195
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500196static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400197 .dot = { .min = 20000, .max = 400000},
198 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700199 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400200 .n = { .min = 3, .max = 6 },
201 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700202 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400203 .m1 = { .min = 0, .max = 0 },
204 .m2 = { .min = 0, .max = 254 },
205 .p = { .min = 5, .max = 80 },
206 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700207 .p2 = { .dot_limit = 200000,
208 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700209};
210
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500211static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400212 .dot = { .min = 20000, .max = 400000 },
213 .vco = { .min = 1700000, .max = 3500000 },
214 .n = { .min = 3, .max = 6 },
215 .m = { .min = 2, .max = 256 },
216 .m1 = { .min = 0, .max = 0 },
217 .m2 = { .min = 0, .max = 254 },
218 .p = { .min = 7, .max = 112 },
219 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700220 .p2 = { .dot_limit = 112000,
221 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700222};
223
Eric Anholt273e27c2011-03-30 13:01:10 -0700224/* Ironlake / Sandybridge
225 *
226 * We calculate clock using (register_value + 2) for N/M1/M2, so here
227 * the range value for them is (actual_value - 2).
228 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800229static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700230 .dot = { .min = 25000, .max = 350000 },
231 .vco = { .min = 1760000, .max = 3510000 },
232 .n = { .min = 1, .max = 5 },
233 .m = { .min = 79, .max = 127 },
234 .m1 = { .min = 12, .max = 22 },
235 .m2 = { .min = 5, .max = 9 },
236 .p = { .min = 5, .max = 80 },
237 .p1 = { .min = 1, .max = 8 },
238 .p2 = { .dot_limit = 225000,
239 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700240};
241
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800242static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700243 .dot = { .min = 25000, .max = 350000 },
244 .vco = { .min = 1760000, .max = 3510000 },
245 .n = { .min = 1, .max = 3 },
246 .m = { .min = 79, .max = 118 },
247 .m1 = { .min = 12, .max = 22 },
248 .m2 = { .min = 5, .max = 9 },
249 .p = { .min = 28, .max = 112 },
250 .p1 = { .min = 2, .max = 8 },
251 .p2 = { .dot_limit = 225000,
252 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800253};
254
255static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700256 .dot = { .min = 25000, .max = 350000 },
257 .vco = { .min = 1760000, .max = 3510000 },
258 .n = { .min = 1, .max = 3 },
259 .m = { .min = 79, .max = 127 },
260 .m1 = { .min = 12, .max = 22 },
261 .m2 = { .min = 5, .max = 9 },
262 .p = { .min = 14, .max = 56 },
263 .p1 = { .min = 2, .max = 8 },
264 .p2 = { .dot_limit = 225000,
265 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800266};
267
Eric Anholt273e27c2011-03-30 13:01:10 -0700268/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800269static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700270 .dot = { .min = 25000, .max = 350000 },
271 .vco = { .min = 1760000, .max = 3510000 },
272 .n = { .min = 1, .max = 2 },
273 .m = { .min = 79, .max = 126 },
274 .m1 = { .min = 12, .max = 22 },
275 .m2 = { .min = 5, .max = 9 },
276 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400277 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700278 .p2 = { .dot_limit = 225000,
279 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800280};
281
282static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700283 .dot = { .min = 25000, .max = 350000 },
284 .vco = { .min = 1760000, .max = 3510000 },
285 .n = { .min = 1, .max = 3 },
286 .m = { .min = 79, .max = 126 },
287 .m1 = { .min = 12, .max = 22 },
288 .m2 = { .min = 5, .max = 9 },
289 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400290 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700291 .p2 = { .dot_limit = 225000,
292 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800293};
294
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700295static const intel_limit_t intel_limits_vlv_dac = {
296 .dot = { .min = 25000, .max = 270000 },
297 .vco = { .min = 4000000, .max = 6000000 },
298 .n = { .min = 1, .max = 7 },
299 .m = { .min = 22, .max = 450 }, /* guess */
300 .m1 = { .min = 2, .max = 3 },
301 .m2 = { .min = 11, .max = 156 },
302 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200303 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700304 .p2 = { .dot_limit = 270000,
305 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700306};
307
308static const intel_limit_t intel_limits_vlv_hdmi = {
Daniel Vetter75e53982013-04-18 21:10:43 +0200309 .dot = { .min = 25000, .max = 270000 },
310 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700311 .n = { .min = 1, .max = 7 },
312 .m = { .min = 60, .max = 300 }, /* guess */
313 .m1 = { .min = 2, .max = 3 },
314 .m2 = { .min = 11, .max = 156 },
315 .p = { .min = 10, .max = 30 },
316 .p1 = { .min = 2, .max = 3 },
317 .p2 = { .dot_limit = 270000,
318 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700319};
320
321static const intel_limit_t intel_limits_vlv_dp = {
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530322 .dot = { .min = 25000, .max = 270000 },
323 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700324 .n = { .min = 1, .max = 7 },
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530325 .m = { .min = 22, .max = 450 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700326 .m1 = { .min = 2, .max = 3 },
327 .m2 = { .min = 11, .max = 156 },
328 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200329 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700330 .p2 = { .dot_limit = 270000,
331 .p2_slow = 2, .p2_fast = 20 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700332};
333
Chris Wilson1b894b52010-12-14 20:04:54 +0000334static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
335 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800336{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800337 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800338 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800339
340 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100341 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000342 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800343 limit = &intel_limits_ironlake_dual_lvds_100m;
344 else
345 limit = &intel_limits_ironlake_dual_lvds;
346 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000347 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800348 limit = &intel_limits_ironlake_single_lvds_100m;
349 else
350 limit = &intel_limits_ironlake_single_lvds;
351 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200352 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800353 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800354
355 return limit;
356}
357
Ma Ling044c7c42009-03-18 20:13:23 +0800358static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
359{
360 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800361 const intel_limit_t *limit;
362
363 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100364 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700365 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800366 else
Keith Packarde4b36692009-06-05 19:22:17 -0700367 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800368 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
369 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700370 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800371 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700372 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800373 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700374 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800375
376 return limit;
377}
378
Chris Wilson1b894b52010-12-14 20:04:54 +0000379static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800380{
381 struct drm_device *dev = crtc->dev;
382 const intel_limit_t *limit;
383
Eric Anholtbad720f2009-10-22 16:11:14 -0700384 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000385 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800386 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800387 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500388 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800389 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500390 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800391 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500392 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700393 } else if (IS_VALLEYVIEW(dev)) {
394 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
395 limit = &intel_limits_vlv_dac;
396 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
397 limit = &intel_limits_vlv_hdmi;
398 else
399 limit = &intel_limits_vlv_dp;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100400 } else if (!IS_GEN2(dev)) {
401 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
402 limit = &intel_limits_i9xx_lvds;
403 else
404 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800405 } else {
406 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700407 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800408 else
Keith Packarde4b36692009-06-05 19:22:17 -0700409 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800410 }
411 return limit;
412}
413
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500414/* m1 is reserved as 0 in Pineview, n is a ring counter */
415static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800416{
Shaohua Li21778322009-02-23 15:19:16 +0800417 clock->m = clock->m2 + 2;
418 clock->p = clock->p1 * clock->p2;
419 clock->vco = refclk * clock->m / clock->n;
420 clock->dot = clock->vco / clock->p;
421}
422
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200423static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
424{
425 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
426}
427
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200428static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800429{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200430 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800431 clock->p = clock->p1 * clock->p2;
432 clock->vco = refclk * clock->m / (clock->n + 2);
433 clock->dot = clock->vco / clock->p;
434}
435
Jesse Barnes79e53942008-11-07 14:24:08 -0800436/**
437 * Returns whether any output on the specified pipe is of the specified type
438 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100439bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800440{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100441 struct drm_device *dev = crtc->dev;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100442 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800443
Daniel Vetter6c2b7c12012-07-05 09:50:24 +0200444 for_each_encoder_on_crtc(dev, crtc, encoder)
445 if (encoder->type == type)
Chris Wilson4ef69c72010-09-09 15:14:28 +0100446 return true;
447
448 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800449}
450
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800451#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800452/**
453 * Returns whether the given set of divisors are valid for a given refclk with
454 * the given connectors.
455 */
456
Chris Wilson1b894b52010-12-14 20:04:54 +0000457static bool intel_PLL_is_valid(struct drm_device *dev,
458 const intel_limit_t *limit,
459 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800460{
Jesse Barnes79e53942008-11-07 14:24:08 -0800461 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400462 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800463 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400464 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800465 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400466 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800467 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400468 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500469 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400470 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800471 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400472 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800473 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400474 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800475 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400476 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800477 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
478 * connector, etc., rather than just a single range.
479 */
480 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400481 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800482
483 return true;
484}
485
Ma Lingd4906092009-03-18 20:13:27 +0800486static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200487i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800488 int target, int refclk, intel_clock_t *match_clock,
489 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800490{
491 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800492 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800493 int err = target;
494
Daniel Vettera210b022012-11-26 17:22:08 +0100495 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800496 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100497 * For LVDS just rely on its current settings for dual-channel.
498 * We haven't figured out how to reliably set up different
499 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800500 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100501 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800502 clock.p2 = limit->p2.p2_fast;
503 else
504 clock.p2 = limit->p2.p2_slow;
505 } else {
506 if (target < limit->p2.dot_limit)
507 clock.p2 = limit->p2.p2_slow;
508 else
509 clock.p2 = limit->p2.p2_fast;
510 }
511
Akshay Joshi0206e352011-08-16 15:34:10 -0400512 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800513
Zhao Yakui42158662009-11-20 11:24:18 +0800514 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
515 clock.m1++) {
516 for (clock.m2 = limit->m2.min;
517 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200518 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800519 break;
520 for (clock.n = limit->n.min;
521 clock.n <= limit->n.max; clock.n++) {
522 for (clock.p1 = limit->p1.min;
523 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800524 int this_err;
525
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200526 i9xx_clock(refclk, &clock);
527 if (!intel_PLL_is_valid(dev, limit,
528 &clock))
529 continue;
530 if (match_clock &&
531 clock.p != match_clock->p)
532 continue;
533
534 this_err = abs(clock.dot - target);
535 if (this_err < err) {
536 *best_clock = clock;
537 err = this_err;
538 }
539 }
540 }
541 }
542 }
543
544 return (err != target);
545}
546
547static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200548pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
549 int target, int refclk, intel_clock_t *match_clock,
550 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200551{
552 struct drm_device *dev = crtc->dev;
553 intel_clock_t clock;
554 int err = target;
555
556 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
557 /*
558 * For LVDS just rely on its current settings for dual-channel.
559 * We haven't figured out how to reliably set up different
560 * single/dual channel state, if we even can.
561 */
562 if (intel_is_dual_link_lvds(dev))
563 clock.p2 = limit->p2.p2_fast;
564 else
565 clock.p2 = limit->p2.p2_slow;
566 } else {
567 if (target < limit->p2.dot_limit)
568 clock.p2 = limit->p2.p2_slow;
569 else
570 clock.p2 = limit->p2.p2_fast;
571 }
572
573 memset(best_clock, 0, sizeof(*best_clock));
574
575 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
576 clock.m1++) {
577 for (clock.m2 = limit->m2.min;
578 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200579 for (clock.n = limit->n.min;
580 clock.n <= limit->n.max; clock.n++) {
581 for (clock.p1 = limit->p1.min;
582 clock.p1 <= limit->p1.max; clock.p1++) {
583 int this_err;
584
585 pineview_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000586 if (!intel_PLL_is_valid(dev, limit,
587 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800588 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800589 if (match_clock &&
590 clock.p != match_clock->p)
591 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800592
593 this_err = abs(clock.dot - target);
594 if (this_err < err) {
595 *best_clock = clock;
596 err = this_err;
597 }
598 }
599 }
600 }
601 }
602
603 return (err != target);
604}
605
Ma Lingd4906092009-03-18 20:13:27 +0800606static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200607g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
608 int target, int refclk, intel_clock_t *match_clock,
609 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800610{
611 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800612 intel_clock_t clock;
613 int max_n;
614 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400615 /* approximately equals target * 0.00585 */
616 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800617 found = false;
618
619 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100620 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800621 clock.p2 = limit->p2.p2_fast;
622 else
623 clock.p2 = limit->p2.p2_slow;
624 } else {
625 if (target < limit->p2.dot_limit)
626 clock.p2 = limit->p2.p2_slow;
627 else
628 clock.p2 = limit->p2.p2_fast;
629 }
630
631 memset(best_clock, 0, sizeof(*best_clock));
632 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200633 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800634 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200635 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800636 for (clock.m1 = limit->m1.max;
637 clock.m1 >= limit->m1.min; clock.m1--) {
638 for (clock.m2 = limit->m2.max;
639 clock.m2 >= limit->m2.min; clock.m2--) {
640 for (clock.p1 = limit->p1.max;
641 clock.p1 >= limit->p1.min; clock.p1--) {
642 int this_err;
643
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200644 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000645 if (!intel_PLL_is_valid(dev, limit,
646 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800647 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000648
649 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800650 if (this_err < err_most) {
651 *best_clock = clock;
652 err_most = this_err;
653 max_n = clock.n;
654 found = true;
655 }
656 }
657 }
658 }
659 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800660 return found;
661}
Ma Lingd4906092009-03-18 20:13:27 +0800662
Zhenyu Wang2c072452009-06-05 15:38:42 +0800663static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200664vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
665 int target, int refclk, intel_clock_t *match_clock,
666 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700667{
668 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
669 u32 m, n, fastclk;
670 u32 updrate, minupdate, fracbits, p;
671 unsigned long bestppm, ppm, absppm;
672 int dotclk, flag;
673
Alan Coxaf447bd2012-07-25 13:49:18 +0100674 flag = 0;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700675 dotclk = target * 1000;
676 bestppm = 1000000;
677 ppm = absppm = 0;
678 fastclk = dotclk / (2*100);
679 updrate = 0;
680 minupdate = 19200;
681 fracbits = 1;
682 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
683 bestm1 = bestm2 = bestp1 = bestp2 = 0;
684
685 /* based on hardware requirement, prefer smaller n to precision */
686 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
687 updrate = refclk / n;
688 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
689 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
690 if (p2 > 10)
691 p2 = p2 - 1;
692 p = p1 * p2;
693 /* based on hardware requirement, prefer bigger m1,m2 values */
694 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
695 m2 = (((2*(fastclk * p * n / m1 )) +
696 refclk) / (2*refclk));
697 m = m1 * m2;
698 vco = updrate * m;
699 if (vco >= limit->vco.min && vco < limit->vco.max) {
700 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
701 absppm = (ppm > 0) ? ppm : (-ppm);
702 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
703 bestppm = 0;
704 flag = 1;
705 }
706 if (absppm < bestppm - 10) {
707 bestppm = absppm;
708 flag = 1;
709 }
710 if (flag) {
711 bestn = n;
712 bestm1 = m1;
713 bestm2 = m2;
714 bestp1 = p1;
715 bestp2 = p2;
716 flag = 0;
717 }
718 }
719 }
720 }
721 }
722 }
723 best_clock->n = bestn;
724 best_clock->m1 = bestm1;
725 best_clock->m2 = bestm2;
726 best_clock->p1 = bestp1;
727 best_clock->p2 = bestp2;
728
729 return true;
730}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700731
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200732enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
733 enum pipe pipe)
734{
735 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
737
Daniel Vetter3b117c82013-04-17 20:15:07 +0200738 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200739}
740
Paulo Zanonia928d532012-05-04 17:18:15 -0300741static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
742{
743 struct drm_i915_private *dev_priv = dev->dev_private;
744 u32 frame, frame_reg = PIPEFRAME(pipe);
745
746 frame = I915_READ(frame_reg);
747
748 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
749 DRM_DEBUG_KMS("vblank wait timed out\n");
750}
751
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700752/**
753 * intel_wait_for_vblank - wait for vblank on a given pipe
754 * @dev: drm device
755 * @pipe: pipe to wait for
756 *
757 * Wait for vblank to occur on a given pipe. Needed for various bits of
758 * mode setting code.
759 */
760void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800761{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700762 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800763 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700764
Paulo Zanonia928d532012-05-04 17:18:15 -0300765 if (INTEL_INFO(dev)->gen >= 5) {
766 ironlake_wait_for_vblank(dev, pipe);
767 return;
768 }
769
Chris Wilson300387c2010-09-05 20:25:43 +0100770 /* Clear existing vblank status. Note this will clear any other
771 * sticky status fields as well.
772 *
773 * This races with i915_driver_irq_handler() with the result
774 * that either function could miss a vblank event. Here it is not
775 * fatal, as we will either wait upon the next vblank interrupt or
776 * timeout. Generally speaking intel_wait_for_vblank() is only
777 * called during modeset at which time the GPU should be idle and
778 * should *not* be performing page flips and thus not waiting on
779 * vblanks...
780 * Currently, the result of us stealing a vblank from the irq
781 * handler is that a single frame will be skipped during swapbuffers.
782 */
783 I915_WRITE(pipestat_reg,
784 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
785
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700786 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100787 if (wait_for(I915_READ(pipestat_reg) &
788 PIPE_VBLANK_INTERRUPT_STATUS,
789 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700790 DRM_DEBUG_KMS("vblank wait timed out\n");
791}
792
Keith Packardab7ad7f2010-10-03 00:33:06 -0700793/*
794 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700795 * @dev: drm device
796 * @pipe: pipe to wait for
797 *
798 * After disabling a pipe, we can't wait for vblank in the usual way,
799 * spinning on the vblank interrupt status bit, since we won't actually
800 * see an interrupt when the pipe is disabled.
801 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700802 * On Gen4 and above:
803 * wait for the pipe register state bit to turn off
804 *
805 * Otherwise:
806 * wait for the display line value to settle (it usually
807 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100808 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700809 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100810void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700811{
812 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200813 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
814 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700815
Keith Packardab7ad7f2010-10-03 00:33:06 -0700816 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200817 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700818
Keith Packardab7ad7f2010-10-03 00:33:06 -0700819 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100820 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
821 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200822 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700823 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300824 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100825 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700826 unsigned long timeout = jiffies + msecs_to_jiffies(100);
827
Paulo Zanoni837ba002012-05-04 17:18:14 -0300828 if (IS_GEN2(dev))
829 line_mask = DSL_LINEMASK_GEN2;
830 else
831 line_mask = DSL_LINEMASK_GEN3;
832
Keith Packardab7ad7f2010-10-03 00:33:06 -0700833 /* Wait for the display line to settle */
834 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300835 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700836 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300837 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700838 time_after(timeout, jiffies));
839 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200840 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700841 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800842}
843
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000844/*
845 * ibx_digital_port_connected - is the specified port connected?
846 * @dev_priv: i915 private structure
847 * @port: the port to test
848 *
849 * Returns true if @port is connected, false otherwise.
850 */
851bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
852 struct intel_digital_port *port)
853{
854 u32 bit;
855
Damien Lespiauc36346e2012-12-13 16:09:03 +0000856 if (HAS_PCH_IBX(dev_priv->dev)) {
857 switch(port->port) {
858 case PORT_B:
859 bit = SDE_PORTB_HOTPLUG;
860 break;
861 case PORT_C:
862 bit = SDE_PORTC_HOTPLUG;
863 break;
864 case PORT_D:
865 bit = SDE_PORTD_HOTPLUG;
866 break;
867 default:
868 return true;
869 }
870 } else {
871 switch(port->port) {
872 case PORT_B:
873 bit = SDE_PORTB_HOTPLUG_CPT;
874 break;
875 case PORT_C:
876 bit = SDE_PORTC_HOTPLUG_CPT;
877 break;
878 case PORT_D:
879 bit = SDE_PORTD_HOTPLUG_CPT;
880 break;
881 default:
882 return true;
883 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000884 }
885
886 return I915_READ(SDEISR) & bit;
887}
888
Jesse Barnesb24e7172011-01-04 15:09:30 -0800889static const char *state_string(bool enabled)
890{
891 return enabled ? "on" : "off";
892}
893
894/* Only for pre-ILK configs */
895static void assert_pll(struct drm_i915_private *dev_priv,
896 enum pipe pipe, bool state)
897{
898 int reg;
899 u32 val;
900 bool cur_state;
901
902 reg = DPLL(pipe);
903 val = I915_READ(reg);
904 cur_state = !!(val & DPLL_VCO_ENABLE);
905 WARN(cur_state != state,
906 "PLL state assertion failure (expected %s, current %s)\n",
907 state_string(state), state_string(cur_state));
908}
909#define assert_pll_enabled(d, p) assert_pll(d, p, true)
910#define assert_pll_disabled(d, p) assert_pll(d, p, false)
911
Jesse Barnes040484a2011-01-03 12:14:26 -0800912/* For ILK+ */
Daniel Vettere72f9fb2013-06-05 13:34:06 +0200913static void assert_shared_dpll(struct drm_i915_private *dev_priv,
914 struct intel_shared_dpll *pll,
915 struct intel_crtc *crtc,
916 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800917{
Jesse Barnes040484a2011-01-03 12:14:26 -0800918 u32 val;
919 bool cur_state;
920
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300921 if (HAS_PCH_LPT(dev_priv->dev)) {
922 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
923 return;
924 }
925
Chris Wilson92b27b02012-05-20 18:10:50 +0100926 if (WARN (!pll,
927 "asserting PCH PLL %s with no PLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100928 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100929
Chris Wilson92b27b02012-05-20 18:10:50 +0100930 val = I915_READ(pll->pll_reg);
931 cur_state = !!(val & DPLL_VCO_ENABLE);
932 WARN(cur_state != state,
933 "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
934 pll->pll_reg, state_string(state), state_string(cur_state), val);
935
936 /* Make sure the selected PLL is correctly attached to the transcoder */
937 if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
Jesse Barnesd3ccbe82011-10-12 09:27:42 -0700938 u32 pch_dpll;
939
940 pch_dpll = I915_READ(PCH_DPLL_SEL);
Chris Wilson92b27b02012-05-20 18:10:50 +0100941 cur_state = pll->pll_reg == _PCH_DPLL_B;
942 if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +0300943 "PLL[%d] not attached to this transcoder %c: %08x\n",
944 cur_state, pipe_name(crtc->pipe), pch_dpll)) {
Chris Wilson92b27b02012-05-20 18:10:50 +0100945 cur_state = !!(val >> (4*crtc->pipe + 3));
946 WARN(cur_state != state,
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +0300947 "PLL[%d] not %s on this transcoder %c: %08x\n",
Chris Wilson92b27b02012-05-20 18:10:50 +0100948 pll->pll_reg == _PCH_DPLL_B,
949 state_string(state),
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +0300950 pipe_name(crtc->pipe),
Chris Wilson92b27b02012-05-20 18:10:50 +0100951 val);
952 }
Jesse Barnesd3ccbe82011-10-12 09:27:42 -0700953 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800954}
Daniel Vettere72f9fb2013-06-05 13:34:06 +0200955#define assert_shared_dpll_enabled(d, p, c) assert_shared_dpll(d, p, c, true)
956#define assert_shared_dpll_disabled(d, p, c) assert_shared_dpll(d, p, c, false)
Jesse Barnes040484a2011-01-03 12:14:26 -0800957
958static void assert_fdi_tx(struct drm_i915_private *dev_priv,
959 enum pipe pipe, bool state)
960{
961 int reg;
962 u32 val;
963 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -0200964 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
965 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -0800966
Paulo Zanoniaffa9352012-11-23 15:30:39 -0200967 if (HAS_DDI(dev_priv->dev)) {
968 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -0200969 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300970 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -0200971 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300972 } else {
973 reg = FDI_TX_CTL(pipe);
974 val = I915_READ(reg);
975 cur_state = !!(val & FDI_TX_ENABLE);
976 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800977 WARN(cur_state != state,
978 "FDI TX state assertion failure (expected %s, current %s)\n",
979 state_string(state), state_string(cur_state));
980}
981#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
982#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
983
984static void assert_fdi_rx(struct drm_i915_private *dev_priv,
985 enum pipe pipe, bool state)
986{
987 int reg;
988 u32 val;
989 bool cur_state;
990
Paulo Zanonid63fa0d2012-11-20 13:27:35 -0200991 reg = FDI_RX_CTL(pipe);
992 val = I915_READ(reg);
993 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -0800994 WARN(cur_state != state,
995 "FDI RX state assertion failure (expected %s, current %s)\n",
996 state_string(state), state_string(cur_state));
997}
998#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
999#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1000
1001static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1002 enum pipe pipe)
1003{
1004 int reg;
1005 u32 val;
1006
1007 /* ILK FDI PLL is always enabled */
1008 if (dev_priv->info->gen == 5)
1009 return;
1010
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001011 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001012 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001013 return;
1014
Jesse Barnes040484a2011-01-03 12:14:26 -08001015 reg = FDI_TX_CTL(pipe);
1016 val = I915_READ(reg);
1017 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1018}
1019
1020static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1021 enum pipe pipe)
1022{
1023 int reg;
1024 u32 val;
1025
1026 reg = FDI_RX_CTL(pipe);
1027 val = I915_READ(reg);
1028 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1029}
1030
Jesse Barnesea0760c2011-01-04 15:09:32 -08001031static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1032 enum pipe pipe)
1033{
1034 int pp_reg, lvds_reg;
1035 u32 val;
1036 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001037 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001038
1039 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1040 pp_reg = PCH_PP_CONTROL;
1041 lvds_reg = PCH_LVDS;
1042 } else {
1043 pp_reg = PP_CONTROL;
1044 lvds_reg = LVDS;
1045 }
1046
1047 val = I915_READ(pp_reg);
1048 if (!(val & PANEL_POWER_ON) ||
1049 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1050 locked = false;
1051
1052 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1053 panel_pipe = PIPE_B;
1054
1055 WARN(panel_pipe == pipe && locked,
1056 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001057 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001058}
1059
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001060void assert_pipe(struct drm_i915_private *dev_priv,
1061 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001062{
1063 int reg;
1064 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001065 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001066 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1067 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001068
Daniel Vetter8e636782012-01-22 01:36:48 +01001069 /* if we need the pipe A quirk it must be always on */
1070 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1071 state = true;
1072
Paulo Zanonib97186f2013-05-03 12:15:36 -03001073 if (!intel_display_power_enabled(dev_priv->dev,
1074 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001075 cur_state = false;
1076 } else {
1077 reg = PIPECONF(cpu_transcoder);
1078 val = I915_READ(reg);
1079 cur_state = !!(val & PIPECONF_ENABLE);
1080 }
1081
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001082 WARN(cur_state != state,
1083 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001084 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001085}
1086
Chris Wilson931872f2012-01-16 23:01:13 +00001087static void assert_plane(struct drm_i915_private *dev_priv,
1088 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001089{
1090 int reg;
1091 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001092 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001093
1094 reg = DSPCNTR(plane);
1095 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001096 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1097 WARN(cur_state != state,
1098 "plane %c assertion failure (expected %s, current %s)\n",
1099 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001100}
1101
Chris Wilson931872f2012-01-16 23:01:13 +00001102#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1103#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1104
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1106 enum pipe pipe)
1107{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001108 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001109 int reg, i;
1110 u32 val;
1111 int cur_pipe;
1112
Ville Syrjälä653e1022013-06-04 13:49:05 +03001113 /* Primary planes are fixed to pipes on gen4+ */
1114 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001115 reg = DSPCNTR(pipe);
1116 val = I915_READ(reg);
1117 WARN((val & DISPLAY_PLANE_ENABLE),
1118 "plane %c assertion failure, should be disabled but not\n",
1119 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001120 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001121 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001122
Jesse Barnesb24e7172011-01-04 15:09:30 -08001123 /* Need to check both planes against the pipe */
Ville Syrjälä653e1022013-06-04 13:49:05 +03001124 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001125 reg = DSPCNTR(i);
1126 val = I915_READ(reg);
1127 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1128 DISPPLANE_SEL_PIPE_SHIFT;
1129 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001130 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1131 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001132 }
1133}
1134
Jesse Barnes19332d72013-03-28 09:55:38 -07001135static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1136 enum pipe pipe)
1137{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001138 struct drm_device *dev = dev_priv->dev;
Jesse Barnes19332d72013-03-28 09:55:38 -07001139 int reg, i;
1140 u32 val;
1141
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001142 if (IS_VALLEYVIEW(dev)) {
1143 for (i = 0; i < dev_priv->num_plane; i++) {
1144 reg = SPCNTR(pipe, i);
1145 val = I915_READ(reg);
1146 WARN((val & SP_ENABLE),
1147 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1148 sprite_name(pipe, i), pipe_name(pipe));
1149 }
1150 } else if (INTEL_INFO(dev)->gen >= 7) {
1151 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001152 val = I915_READ(reg);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001153 WARN((val & SPRITE_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001154 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001155 plane_name(pipe), pipe_name(pipe));
1156 } else if (INTEL_INFO(dev)->gen >= 5) {
1157 reg = DVSCNTR(pipe);
1158 val = I915_READ(reg);
1159 WARN((val & DVS_ENABLE),
1160 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1161 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001162 }
1163}
1164
Jesse Barnes92f25842011-01-04 15:09:34 -08001165static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1166{
1167 u32 val;
1168 bool enabled;
1169
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001170 if (HAS_PCH_LPT(dev_priv->dev)) {
1171 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1172 return;
1173 }
1174
Jesse Barnes92f25842011-01-04 15:09:34 -08001175 val = I915_READ(PCH_DREF_CONTROL);
1176 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1177 DREF_SUPERSPREAD_SOURCE_MASK));
1178 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1179}
1180
Daniel Vetterab9412b2013-05-03 11:49:46 +02001181static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1182 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001183{
1184 int reg;
1185 u32 val;
1186 bool enabled;
1187
Daniel Vetterab9412b2013-05-03 11:49:46 +02001188 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001189 val = I915_READ(reg);
1190 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001191 WARN(enabled,
1192 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1193 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001194}
1195
Keith Packard4e634382011-08-06 10:39:45 -07001196static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1197 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001198{
1199 if ((val & DP_PORT_EN) == 0)
1200 return false;
1201
1202 if (HAS_PCH_CPT(dev_priv->dev)) {
1203 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1204 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1205 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1206 return false;
1207 } else {
1208 if ((val & DP_PIPE_MASK) != (pipe << 30))
1209 return false;
1210 }
1211 return true;
1212}
1213
Keith Packard1519b992011-08-06 10:35:34 -07001214static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1215 enum pipe pipe, u32 val)
1216{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001217 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001218 return false;
1219
1220 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001221 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001222 return false;
1223 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001224 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001225 return false;
1226 }
1227 return true;
1228}
1229
1230static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1231 enum pipe pipe, u32 val)
1232{
1233 if ((val & LVDS_PORT_EN) == 0)
1234 return false;
1235
1236 if (HAS_PCH_CPT(dev_priv->dev)) {
1237 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1238 return false;
1239 } else {
1240 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1241 return false;
1242 }
1243 return true;
1244}
1245
1246static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1247 enum pipe pipe, u32 val)
1248{
1249 if ((val & ADPA_DAC_ENABLE) == 0)
1250 return false;
1251 if (HAS_PCH_CPT(dev_priv->dev)) {
1252 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1253 return false;
1254 } else {
1255 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1256 return false;
1257 }
1258 return true;
1259}
1260
Jesse Barnes291906f2011-02-02 12:28:03 -08001261static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001262 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001263{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001264 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001265 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001266 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001267 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001268
Daniel Vetter75c5da22012-09-10 21:58:29 +02001269 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1270 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001271 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001272}
1273
1274static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1275 enum pipe pipe, int reg)
1276{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001277 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001278 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001279 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001280 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001281
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001282 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001283 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001284 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001285}
1286
1287static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1288 enum pipe pipe)
1289{
1290 int reg;
1291 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001292
Keith Packardf0575e92011-07-25 22:12:43 -07001293 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1294 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1295 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001296
1297 reg = PCH_ADPA;
1298 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001299 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001300 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001301 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001302
1303 reg = PCH_LVDS;
1304 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001305 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001306 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001307 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001308
Paulo Zanonie2debe92013-02-18 19:00:27 -03001309 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1310 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1311 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001312}
1313
Jesse Barnesb24e7172011-01-04 15:09:30 -08001314/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001315 * intel_enable_pll - enable a PLL
1316 * @dev_priv: i915 private structure
1317 * @pipe: pipe PLL to enable
1318 *
1319 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1320 * make sure the PLL reg is writable first though, since the panel write
1321 * protect mechanism may be enabled.
1322 *
1323 * Note! This is for pre-ILK only.
Thomas Richter7434a252012-07-18 19:22:30 +02001324 *
1325 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001326 */
1327static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1328{
1329 int reg;
1330 u32 val;
1331
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001332 assert_pipe_disabled(dev_priv, pipe);
1333
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001334 /* No really, not for ILK+ */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001335 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001336
1337 /* PLL is protected by panel, make sure we can write it */
1338 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1339 assert_panel_unlocked(dev_priv, pipe);
1340
1341 reg = DPLL(pipe);
1342 val = I915_READ(reg);
1343 val |= DPLL_VCO_ENABLE;
1344
1345 /* We do this three times for luck */
1346 I915_WRITE(reg, val);
1347 POSTING_READ(reg);
1348 udelay(150); /* wait for warmup */
1349 I915_WRITE(reg, val);
1350 POSTING_READ(reg);
1351 udelay(150); /* wait for warmup */
1352 I915_WRITE(reg, val);
1353 POSTING_READ(reg);
1354 udelay(150); /* wait for warmup */
1355}
1356
1357/**
1358 * intel_disable_pll - disable a PLL
1359 * @dev_priv: i915 private structure
1360 * @pipe: pipe PLL to disable
1361 *
1362 * Disable the PLL for @pipe, making sure the pipe is off first.
1363 *
1364 * Note! This is for pre-ILK only.
1365 */
1366static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1367{
1368 int reg;
1369 u32 val;
1370
1371 /* Don't disable pipe A or pipe A PLLs if needed */
1372 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1373 return;
1374
1375 /* Make sure the pipe isn't still relying on us */
1376 assert_pipe_disabled(dev_priv, pipe);
1377
1378 reg = DPLL(pipe);
1379 val = I915_READ(reg);
1380 val &= ~DPLL_VCO_ENABLE;
1381 I915_WRITE(reg, val);
1382 POSTING_READ(reg);
1383}
1384
Jesse Barnes89b667f2013-04-18 14:51:36 -07001385void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1386{
1387 u32 port_mask;
1388
1389 if (!port)
1390 port_mask = DPLL_PORTB_READY_MASK;
1391 else
1392 port_mask = DPLL_PORTC_READY_MASK;
1393
1394 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1395 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1396 'B' + port, I915_READ(DPLL(0)));
1397}
1398
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001399/**
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001400 * ironlake_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001401 * @dev_priv: i915 private structure
1402 * @pipe: pipe PLL to enable
1403 *
1404 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1405 * drives the transcoder clock.
1406 */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001407static void ironlake_enable_shared_dpll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001408{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001409 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001410 struct intel_shared_dpll *pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001411 int reg;
1412 u32 val;
1413
Chris Wilson48da64a2012-05-13 20:16:12 +01001414 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001415 BUG_ON(dev_priv->info->gen < 5);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001416 pll = intel_crtc->shared_dpll;
Chris Wilson48da64a2012-05-13 20:16:12 +01001417 if (pll == NULL)
1418 return;
1419
1420 if (WARN_ON(pll->refcount == 0))
1421 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001422
1423 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1424 pll->pll_reg, pll->active, pll->on,
1425 intel_crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001426
1427 /* PCH refclock must be enabled first */
1428 assert_pch_refclk_enabled(dev_priv);
1429
Daniel Vettercdbd2312013-06-05 13:34:03 +02001430 if (pll->active++) {
1431 WARN_ON(!pll->on);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001432 assert_shared_dpll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001433 return;
1434 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001435 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001436
1437 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1438
1439 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001440 val = I915_READ(reg);
1441 val |= DPLL_VCO_ENABLE;
1442 I915_WRITE(reg, val);
1443 POSTING_READ(reg);
1444 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001445
1446 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001447}
1448
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001449static void intel_disable_shared_dpll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001450{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001451 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001452 struct intel_shared_dpll *pll = intel_crtc->shared_dpll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001453 int reg;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001454 u32 val;
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001455
Jesse Barnes92f25842011-01-04 15:09:34 -08001456 /* PCH only available on ILK+ */
1457 BUG_ON(dev_priv->info->gen < 5);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001458 if (pll == NULL)
1459 return;
1460
Chris Wilson48da64a2012-05-13 20:16:12 +01001461 if (WARN_ON(pll->refcount == 0))
1462 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001463
1464 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1465 pll->pll_reg, pll->active, pll->on,
1466 intel_crtc->base.base.id);
1467
Chris Wilson48da64a2012-05-13 20:16:12 +01001468 if (WARN_ON(pll->active == 0)) {
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001469 assert_shared_dpll_disabled(dev_priv, pll, NULL);
Chris Wilson48da64a2012-05-13 20:16:12 +01001470 return;
1471 }
1472
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001473 assert_shared_dpll_enabled(dev_priv, pll, NULL);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001474 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001475 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001476 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001477
1478 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
Jesse Barnes92f25842011-01-04 15:09:34 -08001479
1480 /* Make sure transcoder isn't still depending on us */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001481 assert_pch_transcoder_disabled(dev_priv, intel_crtc->pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001482
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001483 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001484 val = I915_READ(reg);
1485 val &= ~DPLL_VCO_ENABLE;
1486 I915_WRITE(reg, val);
1487 POSTING_READ(reg);
1488 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001489
1490 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001491}
1492
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001493static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1494 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001495{
Daniel Vetter23670b322012-11-01 09:15:30 +01001496 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001497 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetter23670b322012-11-01 09:15:30 +01001498 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001499
1500 /* PCH only available on ILK+ */
1501 BUG_ON(dev_priv->info->gen < 5);
1502
1503 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001504 assert_shared_dpll_enabled(dev_priv,
1505 to_intel_crtc(crtc)->shared_dpll,
1506 to_intel_crtc(crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001507
1508 /* FDI must be feeding us bits for PCH ports */
1509 assert_fdi_tx_enabled(dev_priv, pipe);
1510 assert_fdi_rx_enabled(dev_priv, pipe);
1511
Daniel Vetter23670b322012-11-01 09:15:30 +01001512 if (HAS_PCH_CPT(dev)) {
1513 /* Workaround: Set the timing override bit before enabling the
1514 * pch transcoder. */
1515 reg = TRANS_CHICKEN2(pipe);
1516 val = I915_READ(reg);
1517 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1518 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001519 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001520
Daniel Vetterab9412b2013-05-03 11:49:46 +02001521 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001522 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001523 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001524
1525 if (HAS_PCH_IBX(dev_priv->dev)) {
1526 /*
1527 * make the BPC in transcoder be consistent with
1528 * that in pipeconf reg.
1529 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001530 val &= ~PIPECONF_BPC_MASK;
1531 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001532 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001533
1534 val &= ~TRANS_INTERLACE_MASK;
1535 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001536 if (HAS_PCH_IBX(dev_priv->dev) &&
1537 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1538 val |= TRANS_LEGACY_INTERLACED_ILK;
1539 else
1540 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001541 else
1542 val |= TRANS_PROGRESSIVE;
1543
Jesse Barnes040484a2011-01-03 12:14:26 -08001544 I915_WRITE(reg, val | TRANS_ENABLE);
1545 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001546 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001547}
1548
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001549static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001550 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001551{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001552 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001553
1554 /* PCH only available on ILK+ */
1555 BUG_ON(dev_priv->info->gen < 5);
1556
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001557 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001558 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001559 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001560
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001561 /* Workaround: set timing override bit. */
1562 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001563 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001564 I915_WRITE(_TRANSA_CHICKEN2, val);
1565
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001566 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001567 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001568
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001569 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1570 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001571 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001572 else
1573 val |= TRANS_PROGRESSIVE;
1574
Daniel Vetterab9412b2013-05-03 11:49:46 +02001575 I915_WRITE(LPT_TRANSCONF, val);
1576 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001577 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001578}
1579
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001580static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1581 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001582{
Daniel Vetter23670b322012-11-01 09:15:30 +01001583 struct drm_device *dev = dev_priv->dev;
1584 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001585
1586 /* FDI relies on the transcoder */
1587 assert_fdi_tx_disabled(dev_priv, pipe);
1588 assert_fdi_rx_disabled(dev_priv, pipe);
1589
Jesse Barnes291906f2011-02-02 12:28:03 -08001590 /* Ports must be off as well */
1591 assert_pch_ports_disabled(dev_priv, pipe);
1592
Daniel Vetterab9412b2013-05-03 11:49:46 +02001593 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001594 val = I915_READ(reg);
1595 val &= ~TRANS_ENABLE;
1596 I915_WRITE(reg, val);
1597 /* wait for PCH transcoder off, transcoder state */
1598 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001599 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001600
1601 if (!HAS_PCH_IBX(dev)) {
1602 /* Workaround: Clear the timing override chicken bit again. */
1603 reg = TRANS_CHICKEN2(pipe);
1604 val = I915_READ(reg);
1605 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1606 I915_WRITE(reg, val);
1607 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001608}
1609
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001610static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001611{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001612 u32 val;
1613
Daniel Vetterab9412b2013-05-03 11:49:46 +02001614 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001615 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001616 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001617 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001618 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001619 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001620
1621 /* Workaround: clear timing override bit. */
1622 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001623 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001624 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001625}
1626
1627/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001628 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001629 * @dev_priv: i915 private structure
1630 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001631 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001632 *
1633 * Enable @pipe, making sure that various hardware specific requirements
1634 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1635 *
1636 * @pipe should be %PIPE_A or %PIPE_B.
1637 *
1638 * Will wait until the pipe is actually running (i.e. first vblank) before
1639 * returning.
1640 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001641static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1642 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001643{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001644 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1645 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001646 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001647 int reg;
1648 u32 val;
1649
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001650 assert_planes_disabled(dev_priv, pipe);
1651 assert_sprites_disabled(dev_priv, pipe);
1652
Paulo Zanoni681e5812012-12-06 11:12:38 -02001653 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001654 pch_transcoder = TRANSCODER_A;
1655 else
1656 pch_transcoder = pipe;
1657
Jesse Barnesb24e7172011-01-04 15:09:30 -08001658 /*
1659 * A pipe without a PLL won't actually be able to drive bits from
1660 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1661 * need the check.
1662 */
1663 if (!HAS_PCH_SPLIT(dev_priv->dev))
1664 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001665 else {
1666 if (pch_port) {
1667 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001668 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001669 assert_fdi_tx_pll_enabled(dev_priv,
1670 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001671 }
1672 /* FIXME: assert CPU port conditions for SNB+ */
1673 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001674
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001675 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001676 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001677 if (val & PIPECONF_ENABLE)
1678 return;
1679
1680 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001681 intel_wait_for_vblank(dev_priv->dev, pipe);
1682}
1683
1684/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001685 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001686 * @dev_priv: i915 private structure
1687 * @pipe: pipe to disable
1688 *
1689 * Disable @pipe, making sure that various hardware specific requirements
1690 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1691 *
1692 * @pipe should be %PIPE_A or %PIPE_B.
1693 *
1694 * Will wait until the pipe has shut down before returning.
1695 */
1696static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1697 enum pipe pipe)
1698{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001699 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1700 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001701 int reg;
1702 u32 val;
1703
1704 /*
1705 * Make sure planes won't keep trying to pump pixels to us,
1706 * or we might hang the display.
1707 */
1708 assert_planes_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001709 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001710
1711 /* Don't disable pipe A or pipe A PLLs if needed */
1712 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1713 return;
1714
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001715 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001716 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001717 if ((val & PIPECONF_ENABLE) == 0)
1718 return;
1719
1720 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001721 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1722}
1723
Keith Packardd74362c2011-07-28 14:47:14 -07001724/*
1725 * Plane regs are double buffered, going from enabled->disabled needs a
1726 * trigger in order to latch. The display address reg provides this.
1727 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001728void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001729 enum plane plane)
1730{
Damien Lespiau14f86142012-10-29 15:24:49 +00001731 if (dev_priv->info->gen >= 4)
1732 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1733 else
1734 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001735}
1736
Jesse Barnesb24e7172011-01-04 15:09:30 -08001737/**
1738 * intel_enable_plane - enable a display plane on a given pipe
1739 * @dev_priv: i915 private structure
1740 * @plane: plane to enable
1741 * @pipe: pipe being fed
1742 *
1743 * Enable @plane on @pipe, making sure that @pipe is running first.
1744 */
1745static void intel_enable_plane(struct drm_i915_private *dev_priv,
1746 enum plane plane, enum pipe pipe)
1747{
1748 int reg;
1749 u32 val;
1750
1751 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1752 assert_pipe_enabled(dev_priv, pipe);
1753
1754 reg = DSPCNTR(plane);
1755 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001756 if (val & DISPLAY_PLANE_ENABLE)
1757 return;
1758
1759 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001760 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001761 intel_wait_for_vblank(dev_priv->dev, pipe);
1762}
1763
Jesse Barnesb24e7172011-01-04 15:09:30 -08001764/**
1765 * intel_disable_plane - disable a display plane
1766 * @dev_priv: i915 private structure
1767 * @plane: plane to disable
1768 * @pipe: pipe consuming the data
1769 *
1770 * Disable @plane; should be an independent operation.
1771 */
1772static void intel_disable_plane(struct drm_i915_private *dev_priv,
1773 enum plane plane, enum pipe pipe)
1774{
1775 int reg;
1776 u32 val;
1777
1778 reg = DSPCNTR(plane);
1779 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001780 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1781 return;
1782
1783 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001784 intel_flush_display_plane(dev_priv, plane);
1785 intel_wait_for_vblank(dev_priv->dev, pipe);
1786}
1787
Chris Wilson693db182013-03-05 14:52:39 +00001788static bool need_vtd_wa(struct drm_device *dev)
1789{
1790#ifdef CONFIG_INTEL_IOMMU
1791 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1792 return true;
1793#endif
1794 return false;
1795}
1796
Chris Wilson127bd2a2010-07-23 23:32:05 +01001797int
Chris Wilson48b956c2010-09-14 12:50:34 +01001798intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001799 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001800 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001801{
Chris Wilsonce453d82011-02-21 14:43:56 +00001802 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001803 u32 alignment;
1804 int ret;
1805
Chris Wilson05394f32010-11-08 19:18:58 +00001806 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001807 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001808 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1809 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001810 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001811 alignment = 4 * 1024;
1812 else
1813 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001814 break;
1815 case I915_TILING_X:
1816 /* pin() will align the object as required by fence */
1817 alignment = 0;
1818 break;
1819 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02001820 /* Despite that we check this in framebuffer_init userspace can
1821 * screw us over and change the tiling after the fact. Only
1822 * pinned buffers can't change their tiling. */
1823 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001824 return -EINVAL;
1825 default:
1826 BUG();
1827 }
1828
Chris Wilson693db182013-03-05 14:52:39 +00001829 /* Note that the w/a also requires 64 PTE of padding following the
1830 * bo. We currently fill all unused PTE with the shadow page and so
1831 * we should always have valid PTE following the scanout preventing
1832 * the VT-d warning.
1833 */
1834 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1835 alignment = 256 * 1024;
1836
Chris Wilsonce453d82011-02-21 14:43:56 +00001837 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001838 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001839 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001840 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001841
1842 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1843 * fence, whereas 965+ only requires a fence if using
1844 * framebuffer compression. For simplicity, we always install
1845 * a fence as the cost is not that onerous.
1846 */
Chris Wilson06d98132012-04-17 15:31:24 +01001847 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001848 if (ret)
1849 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001850
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001851 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001852
Chris Wilsonce453d82011-02-21 14:43:56 +00001853 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001854 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001855
1856err_unpin:
1857 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001858err_interruptible:
1859 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001860 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001861}
1862
Chris Wilson1690e1e2011-12-14 13:57:08 +01001863void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1864{
1865 i915_gem_object_unpin_fence(obj);
1866 i915_gem_object_unpin(obj);
1867}
1868
Daniel Vetterc2c75132012-07-05 12:17:30 +02001869/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1870 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001871unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1872 unsigned int tiling_mode,
1873 unsigned int cpp,
1874 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001875{
Chris Wilsonbc752862013-02-21 20:04:31 +00001876 if (tiling_mode != I915_TILING_NONE) {
1877 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001878
Chris Wilsonbc752862013-02-21 20:04:31 +00001879 tile_rows = *y / 8;
1880 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001881
Chris Wilsonbc752862013-02-21 20:04:31 +00001882 tiles = *x / (512/cpp);
1883 *x %= 512/cpp;
1884
1885 return tile_rows * pitch * 8 + tiles * 4096;
1886 } else {
1887 unsigned int offset;
1888
1889 offset = *y * pitch + *x * cpp;
1890 *y = 0;
1891 *x = (offset & 4095) / cpp;
1892 return offset & -4096;
1893 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02001894}
1895
Jesse Barnes17638cd2011-06-24 12:19:23 -07001896static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1897 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001898{
1899 struct drm_device *dev = crtc->dev;
1900 struct drm_i915_private *dev_priv = dev->dev_private;
1901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1902 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001903 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001904 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001905 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001906 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001907 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001908
1909 switch (plane) {
1910 case 0:
1911 case 1:
1912 break;
1913 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001914 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07001915 return -EINVAL;
1916 }
1917
1918 intel_fb = to_intel_framebuffer(fb);
1919 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001920
Chris Wilson5eddb702010-09-11 13:48:45 +01001921 reg = DSPCNTR(plane);
1922 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001923 /* Mask out pixel format bits in case we change it */
1924 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001925 switch (fb->pixel_format) {
1926 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07001927 dspcntr |= DISPPLANE_8BPP;
1928 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001929 case DRM_FORMAT_XRGB1555:
1930 case DRM_FORMAT_ARGB1555:
1931 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07001932 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001933 case DRM_FORMAT_RGB565:
1934 dspcntr |= DISPPLANE_BGRX565;
1935 break;
1936 case DRM_FORMAT_XRGB8888:
1937 case DRM_FORMAT_ARGB8888:
1938 dspcntr |= DISPPLANE_BGRX888;
1939 break;
1940 case DRM_FORMAT_XBGR8888:
1941 case DRM_FORMAT_ABGR8888:
1942 dspcntr |= DISPPLANE_RGBX888;
1943 break;
1944 case DRM_FORMAT_XRGB2101010:
1945 case DRM_FORMAT_ARGB2101010:
1946 dspcntr |= DISPPLANE_BGRX101010;
1947 break;
1948 case DRM_FORMAT_XBGR2101010:
1949 case DRM_FORMAT_ABGR2101010:
1950 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07001951 break;
1952 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01001953 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07001954 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02001955
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001956 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00001957 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07001958 dspcntr |= DISPPLANE_TILED;
1959 else
1960 dspcntr &= ~DISPPLANE_TILED;
1961 }
1962
Ville Syrjäläde1aa622013-06-07 10:47:01 +03001963 if (IS_G4X(dev))
1964 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1965
Chris Wilson5eddb702010-09-11 13:48:45 +01001966 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07001967
Daniel Vettere506a0c2012-07-05 12:17:29 +02001968 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07001969
Daniel Vetterc2c75132012-07-05 12:17:30 +02001970 if (INTEL_INFO(dev)->gen >= 4) {
1971 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00001972 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
1973 fb->bits_per_pixel / 8,
1974 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02001975 linear_offset -= intel_crtc->dspaddr_offset;
1976 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02001977 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001978 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02001979
1980 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
1981 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001982 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001983 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02001984 I915_MODIFY_DISPBASE(DSPSURF(plane),
1985 obj->gtt_offset + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01001986 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02001987 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01001988 } else
Daniel Vettere506a0c2012-07-05 12:17:29 +02001989 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01001990 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001991
Jesse Barnes17638cd2011-06-24 12:19:23 -07001992 return 0;
1993}
1994
1995static int ironlake_update_plane(struct drm_crtc *crtc,
1996 struct drm_framebuffer *fb, int x, int y)
1997{
1998 struct drm_device *dev = crtc->dev;
1999 struct drm_i915_private *dev_priv = dev->dev_private;
2000 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2001 struct intel_framebuffer *intel_fb;
2002 struct drm_i915_gem_object *obj;
2003 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002004 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002005 u32 dspcntr;
2006 u32 reg;
2007
2008 switch (plane) {
2009 case 0:
2010 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002011 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002012 break;
2013 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002014 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002015 return -EINVAL;
2016 }
2017
2018 intel_fb = to_intel_framebuffer(fb);
2019 obj = intel_fb->obj;
2020
2021 reg = DSPCNTR(plane);
2022 dspcntr = I915_READ(reg);
2023 /* Mask out pixel format bits in case we change it */
2024 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002025 switch (fb->pixel_format) {
2026 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002027 dspcntr |= DISPPLANE_8BPP;
2028 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002029 case DRM_FORMAT_RGB565:
2030 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002031 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002032 case DRM_FORMAT_XRGB8888:
2033 case DRM_FORMAT_ARGB8888:
2034 dspcntr |= DISPPLANE_BGRX888;
2035 break;
2036 case DRM_FORMAT_XBGR8888:
2037 case DRM_FORMAT_ABGR8888:
2038 dspcntr |= DISPPLANE_RGBX888;
2039 break;
2040 case DRM_FORMAT_XRGB2101010:
2041 case DRM_FORMAT_ARGB2101010:
2042 dspcntr |= DISPPLANE_BGRX101010;
2043 break;
2044 case DRM_FORMAT_XBGR2101010:
2045 case DRM_FORMAT_ABGR2101010:
2046 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002047 break;
2048 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002049 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002050 }
2051
2052 if (obj->tiling_mode != I915_TILING_NONE)
2053 dspcntr |= DISPPLANE_TILED;
2054 else
2055 dspcntr &= ~DISPPLANE_TILED;
2056
2057 /* must disable */
2058 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2059
2060 I915_WRITE(reg, dspcntr);
2061
Daniel Vettere506a0c2012-07-05 12:17:29 +02002062 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002063 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002064 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2065 fb->bits_per_pixel / 8,
2066 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002067 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002068
Daniel Vettere506a0c2012-07-05 12:17:29 +02002069 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2070 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002071 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002072 I915_MODIFY_DISPBASE(DSPSURF(plane),
2073 obj->gtt_offset + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002074 if (IS_HASWELL(dev)) {
2075 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2076 } else {
2077 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2078 I915_WRITE(DSPLINOFF(plane), linear_offset);
2079 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002080 POSTING_READ(reg);
2081
2082 return 0;
2083}
2084
2085/* Assume fb object is pinned & idle & fenced and just update base pointers */
2086static int
2087intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2088 int x, int y, enum mode_set_atomic state)
2089{
2090 struct drm_device *dev = crtc->dev;
2091 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002092
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002093 if (dev_priv->display.disable_fbc)
2094 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002095 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002096
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002097 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002098}
2099
Ville Syrjälä96a02912013-02-18 19:08:49 +02002100void intel_display_handle_reset(struct drm_device *dev)
2101{
2102 struct drm_i915_private *dev_priv = dev->dev_private;
2103 struct drm_crtc *crtc;
2104
2105 /*
2106 * Flips in the rings have been nuked by the reset,
2107 * so complete all pending flips so that user space
2108 * will get its events and not get stuck.
2109 *
2110 * Also update the base address of all primary
2111 * planes to the the last fb to make sure we're
2112 * showing the correct fb after a reset.
2113 *
2114 * Need to make two loops over the crtcs so that we
2115 * don't try to grab a crtc mutex before the
2116 * pending_flip_queue really got woken up.
2117 */
2118
2119 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2121 enum plane plane = intel_crtc->plane;
2122
2123 intel_prepare_page_flip(dev, plane);
2124 intel_finish_page_flip_plane(dev, plane);
2125 }
2126
2127 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2129
2130 mutex_lock(&crtc->mutex);
2131 if (intel_crtc->active)
2132 dev_priv->display.update_plane(crtc, crtc->fb,
2133 crtc->x, crtc->y);
2134 mutex_unlock(&crtc->mutex);
2135 }
2136}
2137
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002138static int
Chris Wilson14667a42012-04-03 17:58:35 +01002139intel_finish_fb(struct drm_framebuffer *old_fb)
2140{
2141 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2142 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2143 bool was_interruptible = dev_priv->mm.interruptible;
2144 int ret;
2145
Chris Wilson14667a42012-04-03 17:58:35 +01002146 /* Big Hammer, we also need to ensure that any pending
2147 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2148 * current scanout is retired before unpinning the old
2149 * framebuffer.
2150 *
2151 * This should only fail upon a hung GPU, in which case we
2152 * can safely continue.
2153 */
2154 dev_priv->mm.interruptible = false;
2155 ret = i915_gem_object_finish_gpu(obj);
2156 dev_priv->mm.interruptible = was_interruptible;
2157
2158 return ret;
2159}
2160
Ville Syrjälä198598d2012-10-31 17:50:24 +02002161static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2162{
2163 struct drm_device *dev = crtc->dev;
2164 struct drm_i915_master_private *master_priv;
2165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2166
2167 if (!dev->primary->master)
2168 return;
2169
2170 master_priv = dev->primary->master->driver_priv;
2171 if (!master_priv->sarea_priv)
2172 return;
2173
2174 switch (intel_crtc->pipe) {
2175 case 0:
2176 master_priv->sarea_priv->pipeA_x = x;
2177 master_priv->sarea_priv->pipeA_y = y;
2178 break;
2179 case 1:
2180 master_priv->sarea_priv->pipeB_x = x;
2181 master_priv->sarea_priv->pipeB_y = y;
2182 break;
2183 default:
2184 break;
2185 }
2186}
2187
Chris Wilson14667a42012-04-03 17:58:35 +01002188static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002189intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002190 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002191{
2192 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002193 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002195 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002196 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002197
2198 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002199 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002200 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002201 return 0;
2202 }
2203
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002204 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002205 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2206 plane_name(intel_crtc->plane),
2207 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002208 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002209 }
2210
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002211 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002212 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002213 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002214 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002215 if (ret != 0) {
2216 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002217 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002218 return ret;
2219 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002220
Daniel Vetter94352cf2012-07-05 22:51:56 +02002221 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002222 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002223 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002224 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002225 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002226 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002227 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002228
Daniel Vetter94352cf2012-07-05 22:51:56 +02002229 old_fb = crtc->fb;
2230 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002231 crtc->x = x;
2232 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002233
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002234 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002235 if (intel_crtc->active && old_fb != fb)
2236 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002237 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002238 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002239
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002240 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002241 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002242
Ville Syrjälä198598d2012-10-31 17:50:24 +02002243 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002244
2245 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002246}
2247
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002248static void intel_fdi_normal_train(struct drm_crtc *crtc)
2249{
2250 struct drm_device *dev = crtc->dev;
2251 struct drm_i915_private *dev_priv = dev->dev_private;
2252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2253 int pipe = intel_crtc->pipe;
2254 u32 reg, temp;
2255
2256 /* enable normal train */
2257 reg = FDI_TX_CTL(pipe);
2258 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002259 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002260 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2261 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002262 } else {
2263 temp &= ~FDI_LINK_TRAIN_NONE;
2264 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002265 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002266 I915_WRITE(reg, temp);
2267
2268 reg = FDI_RX_CTL(pipe);
2269 temp = I915_READ(reg);
2270 if (HAS_PCH_CPT(dev)) {
2271 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2272 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2273 } else {
2274 temp &= ~FDI_LINK_TRAIN_NONE;
2275 temp |= FDI_LINK_TRAIN_NONE;
2276 }
2277 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2278
2279 /* wait one idle pattern time */
2280 POSTING_READ(reg);
2281 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002282
2283 /* IVB wants error correction enabled */
2284 if (IS_IVYBRIDGE(dev))
2285 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2286 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002287}
2288
Daniel Vetter1e833f42013-02-19 22:31:57 +01002289static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
2290{
2291 return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
2292}
2293
Daniel Vetter01a415f2012-10-27 15:58:40 +02002294static void ivb_modeset_global_resources(struct drm_device *dev)
2295{
2296 struct drm_i915_private *dev_priv = dev->dev_private;
2297 struct intel_crtc *pipe_B_crtc =
2298 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2299 struct intel_crtc *pipe_C_crtc =
2300 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2301 uint32_t temp;
2302
Daniel Vetter1e833f42013-02-19 22:31:57 +01002303 /*
2304 * When everything is off disable fdi C so that we could enable fdi B
2305 * with all lanes. Note that we don't care about enabled pipes without
2306 * an enabled pch encoder.
2307 */
2308 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2309 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002310 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2311 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2312
2313 temp = I915_READ(SOUTH_CHICKEN1);
2314 temp &= ~FDI_BC_BIFURCATION_SELECT;
2315 DRM_DEBUG_KMS("disabling fdi C rx\n");
2316 I915_WRITE(SOUTH_CHICKEN1, temp);
2317 }
2318}
2319
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002320/* The FDI link training functions for ILK/Ibexpeak. */
2321static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2322{
2323 struct drm_device *dev = crtc->dev;
2324 struct drm_i915_private *dev_priv = dev->dev_private;
2325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2326 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002327 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002328 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002329
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002330 /* FDI needs bits from pipe & plane first */
2331 assert_pipe_enabled(dev_priv, pipe);
2332 assert_plane_enabled(dev_priv, plane);
2333
Adam Jacksone1a44742010-06-25 15:32:14 -04002334 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2335 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002336 reg = FDI_RX_IMR(pipe);
2337 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002338 temp &= ~FDI_RX_SYMBOL_LOCK;
2339 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002340 I915_WRITE(reg, temp);
2341 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002342 udelay(150);
2343
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002344 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002345 reg = FDI_TX_CTL(pipe);
2346 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002347 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2348 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002349 temp &= ~FDI_LINK_TRAIN_NONE;
2350 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002351 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002352
Chris Wilson5eddb702010-09-11 13:48:45 +01002353 reg = FDI_RX_CTL(pipe);
2354 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002355 temp &= ~FDI_LINK_TRAIN_NONE;
2356 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002357 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2358
2359 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002360 udelay(150);
2361
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002362 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002363 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2364 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2365 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002366
Chris Wilson5eddb702010-09-11 13:48:45 +01002367 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002368 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002369 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002370 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2371
2372 if ((temp & FDI_RX_BIT_LOCK)) {
2373 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002374 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002375 break;
2376 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002377 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002378 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002379 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002380
2381 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002382 reg = FDI_TX_CTL(pipe);
2383 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002384 temp &= ~FDI_LINK_TRAIN_NONE;
2385 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002386 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002387
Chris Wilson5eddb702010-09-11 13:48:45 +01002388 reg = FDI_RX_CTL(pipe);
2389 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002390 temp &= ~FDI_LINK_TRAIN_NONE;
2391 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002392 I915_WRITE(reg, temp);
2393
2394 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002395 udelay(150);
2396
Chris Wilson5eddb702010-09-11 13:48:45 +01002397 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002398 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002399 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002400 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2401
2402 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002403 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002404 DRM_DEBUG_KMS("FDI train 2 done.\n");
2405 break;
2406 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002407 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002408 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002409 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002410
2411 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002412
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002413}
2414
Akshay Joshi0206e352011-08-16 15:34:10 -04002415static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002416 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2417 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2418 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2419 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2420};
2421
2422/* The FDI link training functions for SNB/Cougarpoint. */
2423static void gen6_fdi_link_train(struct drm_crtc *crtc)
2424{
2425 struct drm_device *dev = crtc->dev;
2426 struct drm_i915_private *dev_priv = dev->dev_private;
2427 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2428 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002429 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002430
Adam Jacksone1a44742010-06-25 15:32:14 -04002431 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2432 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002433 reg = FDI_RX_IMR(pipe);
2434 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002435 temp &= ~FDI_RX_SYMBOL_LOCK;
2436 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002437 I915_WRITE(reg, temp);
2438
2439 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002440 udelay(150);
2441
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002442 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002443 reg = FDI_TX_CTL(pipe);
2444 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002445 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2446 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002447 temp &= ~FDI_LINK_TRAIN_NONE;
2448 temp |= FDI_LINK_TRAIN_PATTERN_1;
2449 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2450 /* SNB-B */
2451 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002452 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002453
Daniel Vetterd74cf322012-10-26 10:58:13 +02002454 I915_WRITE(FDI_RX_MISC(pipe),
2455 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2456
Chris Wilson5eddb702010-09-11 13:48:45 +01002457 reg = FDI_RX_CTL(pipe);
2458 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002459 if (HAS_PCH_CPT(dev)) {
2460 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2461 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2462 } else {
2463 temp &= ~FDI_LINK_TRAIN_NONE;
2464 temp |= FDI_LINK_TRAIN_PATTERN_1;
2465 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002466 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2467
2468 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002469 udelay(150);
2470
Akshay Joshi0206e352011-08-16 15:34:10 -04002471 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002472 reg = FDI_TX_CTL(pipe);
2473 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002474 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2475 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002476 I915_WRITE(reg, temp);
2477
2478 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002479 udelay(500);
2480
Sean Paulfa37d392012-03-02 12:53:39 -05002481 for (retry = 0; retry < 5; retry++) {
2482 reg = FDI_RX_IIR(pipe);
2483 temp = I915_READ(reg);
2484 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2485 if (temp & FDI_RX_BIT_LOCK) {
2486 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2487 DRM_DEBUG_KMS("FDI train 1 done.\n");
2488 break;
2489 }
2490 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002491 }
Sean Paulfa37d392012-03-02 12:53:39 -05002492 if (retry < 5)
2493 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002494 }
2495 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002496 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002497
2498 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002499 reg = FDI_TX_CTL(pipe);
2500 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002501 temp &= ~FDI_LINK_TRAIN_NONE;
2502 temp |= FDI_LINK_TRAIN_PATTERN_2;
2503 if (IS_GEN6(dev)) {
2504 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2505 /* SNB-B */
2506 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2507 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002508 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002509
Chris Wilson5eddb702010-09-11 13:48:45 +01002510 reg = FDI_RX_CTL(pipe);
2511 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002512 if (HAS_PCH_CPT(dev)) {
2513 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2514 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2515 } else {
2516 temp &= ~FDI_LINK_TRAIN_NONE;
2517 temp |= FDI_LINK_TRAIN_PATTERN_2;
2518 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002519 I915_WRITE(reg, temp);
2520
2521 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002522 udelay(150);
2523
Akshay Joshi0206e352011-08-16 15:34:10 -04002524 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002525 reg = FDI_TX_CTL(pipe);
2526 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002527 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2528 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002529 I915_WRITE(reg, temp);
2530
2531 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002532 udelay(500);
2533
Sean Paulfa37d392012-03-02 12:53:39 -05002534 for (retry = 0; retry < 5; retry++) {
2535 reg = FDI_RX_IIR(pipe);
2536 temp = I915_READ(reg);
2537 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2538 if (temp & FDI_RX_SYMBOL_LOCK) {
2539 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2540 DRM_DEBUG_KMS("FDI train 2 done.\n");
2541 break;
2542 }
2543 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002544 }
Sean Paulfa37d392012-03-02 12:53:39 -05002545 if (retry < 5)
2546 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002547 }
2548 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002549 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002550
2551 DRM_DEBUG_KMS("FDI train done.\n");
2552}
2553
Jesse Barnes357555c2011-04-28 15:09:55 -07002554/* Manual link training for Ivy Bridge A0 parts */
2555static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2556{
2557 struct drm_device *dev = crtc->dev;
2558 struct drm_i915_private *dev_priv = dev->dev_private;
2559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2560 int pipe = intel_crtc->pipe;
2561 u32 reg, temp, i;
2562
2563 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2564 for train result */
2565 reg = FDI_RX_IMR(pipe);
2566 temp = I915_READ(reg);
2567 temp &= ~FDI_RX_SYMBOL_LOCK;
2568 temp &= ~FDI_RX_BIT_LOCK;
2569 I915_WRITE(reg, temp);
2570
2571 POSTING_READ(reg);
2572 udelay(150);
2573
Daniel Vetter01a415f2012-10-27 15:58:40 +02002574 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2575 I915_READ(FDI_RX_IIR(pipe)));
2576
Jesse Barnes357555c2011-04-28 15:09:55 -07002577 /* enable CPU FDI TX and PCH FDI RX */
2578 reg = FDI_TX_CTL(pipe);
2579 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002580 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2581 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Jesse Barnes357555c2011-04-28 15:09:55 -07002582 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2583 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2584 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2585 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002586 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002587 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2588
Daniel Vetterd74cf322012-10-26 10:58:13 +02002589 I915_WRITE(FDI_RX_MISC(pipe),
2590 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2591
Jesse Barnes357555c2011-04-28 15:09:55 -07002592 reg = FDI_RX_CTL(pipe);
2593 temp = I915_READ(reg);
2594 temp &= ~FDI_LINK_TRAIN_AUTO;
2595 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2596 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002597 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002598 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2599
2600 POSTING_READ(reg);
2601 udelay(150);
2602
Akshay Joshi0206e352011-08-16 15:34:10 -04002603 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002604 reg = FDI_TX_CTL(pipe);
2605 temp = I915_READ(reg);
2606 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2607 temp |= snb_b_fdi_train_param[i];
2608 I915_WRITE(reg, temp);
2609
2610 POSTING_READ(reg);
2611 udelay(500);
2612
2613 reg = FDI_RX_IIR(pipe);
2614 temp = I915_READ(reg);
2615 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2616
2617 if (temp & FDI_RX_BIT_LOCK ||
2618 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2619 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002620 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002621 break;
2622 }
2623 }
2624 if (i == 4)
2625 DRM_ERROR("FDI train 1 fail!\n");
2626
2627 /* Train 2 */
2628 reg = FDI_TX_CTL(pipe);
2629 temp = I915_READ(reg);
2630 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2631 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2632 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2633 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2634 I915_WRITE(reg, temp);
2635
2636 reg = FDI_RX_CTL(pipe);
2637 temp = I915_READ(reg);
2638 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2639 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2640 I915_WRITE(reg, temp);
2641
2642 POSTING_READ(reg);
2643 udelay(150);
2644
Akshay Joshi0206e352011-08-16 15:34:10 -04002645 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002646 reg = FDI_TX_CTL(pipe);
2647 temp = I915_READ(reg);
2648 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2649 temp |= snb_b_fdi_train_param[i];
2650 I915_WRITE(reg, temp);
2651
2652 POSTING_READ(reg);
2653 udelay(500);
2654
2655 reg = FDI_RX_IIR(pipe);
2656 temp = I915_READ(reg);
2657 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2658
2659 if (temp & FDI_RX_SYMBOL_LOCK) {
2660 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002661 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002662 break;
2663 }
2664 }
2665 if (i == 4)
2666 DRM_ERROR("FDI train 2 fail!\n");
2667
2668 DRM_DEBUG_KMS("FDI train done.\n");
2669}
2670
Daniel Vetter88cefb62012-08-12 19:27:14 +02002671static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002672{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002673 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002674 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002675 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002676 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002677
Jesse Barnesc64e3112010-09-10 11:27:03 -07002678
Jesse Barnes0e23b992010-09-10 11:10:00 -07002679 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002680 reg = FDI_RX_CTL(pipe);
2681 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002682 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2683 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002684 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002685 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2686
2687 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002688 udelay(200);
2689
2690 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002691 temp = I915_READ(reg);
2692 I915_WRITE(reg, temp | FDI_PCDCLK);
2693
2694 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002695 udelay(200);
2696
Paulo Zanoni20749732012-11-23 15:30:38 -02002697 /* Enable CPU FDI TX PLL, always on for Ironlake */
2698 reg = FDI_TX_CTL(pipe);
2699 temp = I915_READ(reg);
2700 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2701 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002702
Paulo Zanoni20749732012-11-23 15:30:38 -02002703 POSTING_READ(reg);
2704 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002705 }
2706}
2707
Daniel Vetter88cefb62012-08-12 19:27:14 +02002708static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2709{
2710 struct drm_device *dev = intel_crtc->base.dev;
2711 struct drm_i915_private *dev_priv = dev->dev_private;
2712 int pipe = intel_crtc->pipe;
2713 u32 reg, temp;
2714
2715 /* Switch from PCDclk to Rawclk */
2716 reg = FDI_RX_CTL(pipe);
2717 temp = I915_READ(reg);
2718 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2719
2720 /* Disable CPU FDI TX PLL */
2721 reg = FDI_TX_CTL(pipe);
2722 temp = I915_READ(reg);
2723 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2724
2725 POSTING_READ(reg);
2726 udelay(100);
2727
2728 reg = FDI_RX_CTL(pipe);
2729 temp = I915_READ(reg);
2730 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2731
2732 /* Wait for the clocks to turn off. */
2733 POSTING_READ(reg);
2734 udelay(100);
2735}
2736
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002737static void ironlake_fdi_disable(struct drm_crtc *crtc)
2738{
2739 struct drm_device *dev = crtc->dev;
2740 struct drm_i915_private *dev_priv = dev->dev_private;
2741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2742 int pipe = intel_crtc->pipe;
2743 u32 reg, temp;
2744
2745 /* disable CPU FDI tx and PCH FDI rx */
2746 reg = FDI_TX_CTL(pipe);
2747 temp = I915_READ(reg);
2748 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2749 POSTING_READ(reg);
2750
2751 reg = FDI_RX_CTL(pipe);
2752 temp = I915_READ(reg);
2753 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002754 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002755 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2756
2757 POSTING_READ(reg);
2758 udelay(100);
2759
2760 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002761 if (HAS_PCH_IBX(dev)) {
2762 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002763 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002764
2765 /* still set train pattern 1 */
2766 reg = FDI_TX_CTL(pipe);
2767 temp = I915_READ(reg);
2768 temp &= ~FDI_LINK_TRAIN_NONE;
2769 temp |= FDI_LINK_TRAIN_PATTERN_1;
2770 I915_WRITE(reg, temp);
2771
2772 reg = FDI_RX_CTL(pipe);
2773 temp = I915_READ(reg);
2774 if (HAS_PCH_CPT(dev)) {
2775 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2776 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2777 } else {
2778 temp &= ~FDI_LINK_TRAIN_NONE;
2779 temp |= FDI_LINK_TRAIN_PATTERN_1;
2780 }
2781 /* BPC in FDI rx is consistent with that in PIPECONF */
2782 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002783 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002784 I915_WRITE(reg, temp);
2785
2786 POSTING_READ(reg);
2787 udelay(100);
2788}
2789
Chris Wilson5bb61642012-09-27 21:25:58 +01002790static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2791{
2792 struct drm_device *dev = crtc->dev;
2793 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002795 unsigned long flags;
2796 bool pending;
2797
Ville Syrjälä10d83732013-01-29 18:13:34 +02002798 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2799 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002800 return false;
2801
2802 spin_lock_irqsave(&dev->event_lock, flags);
2803 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2804 spin_unlock_irqrestore(&dev->event_lock, flags);
2805
2806 return pending;
2807}
2808
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002809static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2810{
Chris Wilson0f911282012-04-17 10:05:38 +01002811 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002812 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002813
2814 if (crtc->fb == NULL)
2815 return;
2816
Daniel Vetter2c10d572012-12-20 21:24:07 +01002817 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2818
Chris Wilson5bb61642012-09-27 21:25:58 +01002819 wait_event(dev_priv->pending_flip_queue,
2820 !intel_crtc_has_pending_flip(crtc));
2821
Chris Wilson0f911282012-04-17 10:05:38 +01002822 mutex_lock(&dev->struct_mutex);
2823 intel_finish_fb(crtc->fb);
2824 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002825}
2826
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002827/* Program iCLKIP clock to the desired frequency */
2828static void lpt_program_iclkip(struct drm_crtc *crtc)
2829{
2830 struct drm_device *dev = crtc->dev;
2831 struct drm_i915_private *dev_priv = dev->dev_private;
2832 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2833 u32 temp;
2834
Daniel Vetter09153002012-12-12 14:06:44 +01002835 mutex_lock(&dev_priv->dpio_lock);
2836
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002837 /* It is necessary to ungate the pixclk gate prior to programming
2838 * the divisors, and gate it back when it is done.
2839 */
2840 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2841
2842 /* Disable SSCCTL */
2843 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002844 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2845 SBI_SSCCTL_DISABLE,
2846 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002847
2848 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2849 if (crtc->mode.clock == 20000) {
2850 auxdiv = 1;
2851 divsel = 0x41;
2852 phaseinc = 0x20;
2853 } else {
2854 /* The iCLK virtual clock root frequency is in MHz,
2855 * but the crtc->mode.clock in in KHz. To get the divisors,
2856 * it is necessary to divide one by another, so we
2857 * convert the virtual clock precision to KHz here for higher
2858 * precision.
2859 */
2860 u32 iclk_virtual_root_freq = 172800 * 1000;
2861 u32 iclk_pi_range = 64;
2862 u32 desired_divisor, msb_divisor_value, pi_value;
2863
2864 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2865 msb_divisor_value = desired_divisor / iclk_pi_range;
2866 pi_value = desired_divisor % iclk_pi_range;
2867
2868 auxdiv = 0;
2869 divsel = msb_divisor_value - 2;
2870 phaseinc = pi_value;
2871 }
2872
2873 /* This should not happen with any sane values */
2874 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2875 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2876 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2877 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2878
2879 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2880 crtc->mode.clock,
2881 auxdiv,
2882 divsel,
2883 phasedir,
2884 phaseinc);
2885
2886 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002887 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002888 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2889 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2890 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2891 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2892 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2893 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002894 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002895
2896 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002897 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002898 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2899 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002900 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002901
2902 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002903 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002904 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002905 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002906
2907 /* Wait for initialization time */
2908 udelay(24);
2909
2910 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01002911
2912 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002913}
2914
Daniel Vetter275f01b22013-05-03 11:49:47 +02002915static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
2916 enum pipe pch_transcoder)
2917{
2918 struct drm_device *dev = crtc->base.dev;
2919 struct drm_i915_private *dev_priv = dev->dev_private;
2920 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2921
2922 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
2923 I915_READ(HTOTAL(cpu_transcoder)));
2924 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
2925 I915_READ(HBLANK(cpu_transcoder)));
2926 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
2927 I915_READ(HSYNC(cpu_transcoder)));
2928
2929 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
2930 I915_READ(VTOTAL(cpu_transcoder)));
2931 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
2932 I915_READ(VBLANK(cpu_transcoder)));
2933 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
2934 I915_READ(VSYNC(cpu_transcoder)));
2935 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2936 I915_READ(VSYNCSHIFT(cpu_transcoder)));
2937}
2938
Jesse Barnesf67a5592011-01-05 10:31:48 -08002939/*
2940 * Enable PCH resources required for PCH ports:
2941 * - PCH PLLs
2942 * - FDI training & RX/TX
2943 * - update transcoder timings
2944 * - DP transcoding bits
2945 * - transcoder
2946 */
2947static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002948{
2949 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002950 struct drm_i915_private *dev_priv = dev->dev_private;
2951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2952 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002953 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002954
Daniel Vetterab9412b2013-05-03 11:49:46 +02002955 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01002956
Daniel Vettercd986ab2012-10-26 10:58:12 +02002957 /* Write the TU size bits before fdi link training, so that error
2958 * detection works. */
2959 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2960 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2961
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002962 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07002963 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002964
Daniel Vetter572deb32012-10-27 18:46:14 +02002965 /* XXX: pch pll's can be enabled any time before we enable the PCH
2966 * transcoder, and we actually should do this to not upset any PCH
2967 * transcoder that already use the clock when we share it.
2968 *
Daniel Vettere72f9fb2013-06-05 13:34:06 +02002969 * Note that enable_shared_dpll tries to do the right thing, but
2970 * get_shared_dpll unconditionally resets the pll - we need that to have
2971 * the right LVDS enable sequence. */
2972 ironlake_enable_shared_dpll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01002973
Paulo Zanoni303b81e2012-10-31 18:12:23 -02002974 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002975 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07002976
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002977 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002978 switch (pipe) {
2979 default:
2980 case 0:
2981 temp |= TRANSA_DPLL_ENABLE;
2982 sel = TRANSA_DPLLB_SEL;
2983 break;
2984 case 1:
2985 temp |= TRANSB_DPLL_ENABLE;
2986 sel = TRANSB_DPLLB_SEL;
2987 break;
2988 case 2:
2989 temp |= TRANSC_DPLL_ENABLE;
2990 sel = TRANSC_DPLLB_SEL;
2991 break;
Jesse Barnesd64311a2011-10-12 15:01:33 -07002992 }
Daniel Vettere72f9fb2013-06-05 13:34:06 +02002993 if (intel_crtc->shared_dpll->pll_reg == _PCH_DPLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002994 temp |= sel;
2995 else
2996 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002997 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002998 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002999
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003000 /* set transcoder timing, panel must allow it */
3001 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003002 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003003
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003004 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003005
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003006 /* For PCH DP, enable TRANS_DP_CTL */
3007 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003008 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3009 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003010 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003011 reg = TRANS_DP_CTL(pipe);
3012 temp = I915_READ(reg);
3013 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003014 TRANS_DP_SYNC_MASK |
3015 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003016 temp |= (TRANS_DP_OUTPUT_ENABLE |
3017 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003018 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003019
3020 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003021 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003022 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003023 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003024
3025 switch (intel_trans_dp_port_sel(crtc)) {
3026 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003027 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003028 break;
3029 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003030 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003031 break;
3032 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003033 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003034 break;
3035 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003036 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003037 }
3038
Chris Wilson5eddb702010-09-11 13:48:45 +01003039 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003040 }
3041
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003042 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003043}
3044
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003045static void lpt_pch_enable(struct drm_crtc *crtc)
3046{
3047 struct drm_device *dev = crtc->dev;
3048 struct drm_i915_private *dev_priv = dev->dev_private;
3049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003050 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003051
Daniel Vetterab9412b2013-05-03 11:49:46 +02003052 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003053
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003054 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003055
Paulo Zanoni0540e482012-10-31 18:12:40 -02003056 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003057 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003058
Paulo Zanoni937bb612012-10-31 18:12:47 -02003059 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003060}
3061
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003062static void intel_put_shared_dpll(struct intel_crtc *intel_crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003063{
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003064 struct intel_shared_dpll *pll = intel_crtc->shared_dpll;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003065
3066 if (pll == NULL)
3067 return;
3068
3069 if (pll->refcount == 0) {
3070 WARN(1, "bad PCH PLL refcount\n");
3071 return;
3072 }
3073
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003074 if (--pll->refcount == 0) {
3075 WARN_ON(pll->on);
3076 WARN_ON(pll->active);
3077 }
3078
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003079 intel_crtc->shared_dpll = NULL;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003080}
3081
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003082static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003083{
3084 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003085 struct intel_shared_dpll *pll;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003086 int i;
3087
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003088 pll = intel_crtc->shared_dpll;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003089 if (pll) {
Daniel Vettercdbd2312013-06-05 13:34:03 +02003090 DRM_DEBUG_KMS("CRTC:%d dropping existing PCH PLL %x\n",
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003091 intel_crtc->base.base.id, pll->pll_reg);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003092 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003093 }
3094
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003095 if (HAS_PCH_IBX(dev_priv->dev)) {
3096 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3097 i = intel_crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003098 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003099
3100 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3101 intel_crtc->base.base.id, pll->pll_reg);
3102
3103 goto found;
3104 }
3105
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003106 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3107 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003108
3109 /* Only want to check enabled timings first */
3110 if (pll->refcount == 0)
3111 continue;
3112
3113 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3114 fp == I915_READ(pll->fp0_reg)) {
3115 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3116 intel_crtc->base.base.id,
3117 pll->pll_reg, pll->refcount, pll->active);
3118
3119 goto found;
3120 }
3121 }
3122
3123 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003124 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3125 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003126 if (pll->refcount == 0) {
3127 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3128 intel_crtc->base.base.id, pll->pll_reg);
3129 goto found;
3130 }
3131 }
3132
3133 return NULL;
3134
3135found:
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003136 intel_crtc->shared_dpll = pll;
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003137 DRM_DEBUG_DRIVER("using pll %d for pipe %c\n", i, pipe_name(intel_crtc->pipe));
Daniel Vettercdbd2312013-06-05 13:34:03 +02003138 if (pll->active == 0) {
3139 DRM_DEBUG_DRIVER("setting up pll %d\n", i);
3140 WARN_ON(pll->on);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003141 assert_shared_dpll_disabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003142
Daniel Vettercdbd2312013-06-05 13:34:03 +02003143 /* Wait for the clocks to stabilize before rewriting the regs */
3144 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3145 POSTING_READ(pll->pll_reg);
3146 udelay(150);
Chris Wilsone04c7352012-05-02 20:43:56 +01003147
Daniel Vettercdbd2312013-06-05 13:34:03 +02003148 I915_WRITE(pll->fp0_reg, fp);
3149 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
3150 }
3151 pll->refcount++;
3152
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003153 return pll;
3154}
3155
Daniel Vettera1520312013-05-03 11:49:50 +02003156static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003157{
3158 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003159 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003160 u32 temp;
3161
3162 temp = I915_READ(dslreg);
3163 udelay(500);
3164 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003165 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003166 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003167 }
3168}
3169
Jesse Barnesb074cec2013-04-25 12:55:02 -07003170static void ironlake_pfit_enable(struct intel_crtc *crtc)
3171{
3172 struct drm_device *dev = crtc->base.dev;
3173 struct drm_i915_private *dev_priv = dev->dev_private;
3174 int pipe = crtc->pipe;
3175
Jesse Barnes0ef37f32013-05-03 13:26:37 -07003176 if (crtc->config.pch_pfit.size) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003177 /* Force use of hard-coded filter coefficients
3178 * as some pre-programmed values are broken,
3179 * e.g. x201.
3180 */
3181 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3182 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3183 PF_PIPE_SEL_IVB(pipe));
3184 else
3185 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3186 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3187 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
3188 }
3189}
3190
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003191static void intel_enable_planes(struct drm_crtc *crtc)
3192{
3193 struct drm_device *dev = crtc->dev;
3194 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3195 struct intel_plane *intel_plane;
3196
3197 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3198 if (intel_plane->pipe == pipe)
3199 intel_plane_restore(&intel_plane->base);
3200}
3201
3202static void intel_disable_planes(struct drm_crtc *crtc)
3203{
3204 struct drm_device *dev = crtc->dev;
3205 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3206 struct intel_plane *intel_plane;
3207
3208 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3209 if (intel_plane->pipe == pipe)
3210 intel_plane_disable(&intel_plane->base);
3211}
3212
Jesse Barnesf67a5592011-01-05 10:31:48 -08003213static void ironlake_crtc_enable(struct drm_crtc *crtc)
3214{
3215 struct drm_device *dev = crtc->dev;
3216 struct drm_i915_private *dev_priv = dev->dev_private;
3217 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003218 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003219 int pipe = intel_crtc->pipe;
3220 int plane = intel_crtc->plane;
3221 u32 temp;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003222
Daniel Vetter08a48462012-07-02 11:43:47 +02003223 WARN_ON(!crtc->enabled);
3224
Jesse Barnesf67a5592011-01-05 10:31:48 -08003225 if (intel_crtc->active)
3226 return;
3227
3228 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003229
3230 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3231 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3232
Jesse Barnesf67a5592011-01-05 10:31:48 -08003233 intel_update_watermarks(dev);
3234
3235 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3236 temp = I915_READ(PCH_LVDS);
3237 if ((temp & LVDS_PORT_EN) == 0)
3238 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3239 }
3240
Jesse Barnesf67a5592011-01-05 10:31:48 -08003241
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003242 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003243 /* Note: FDI PLL enabling _must_ be done before we enable the
3244 * cpu pipes, hence this is separate from all the other fdi/pch
3245 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003246 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003247 } else {
3248 assert_fdi_tx_disabled(dev_priv, pipe);
3249 assert_fdi_rx_disabled(dev_priv, pipe);
3250 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003251
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003252 for_each_encoder_on_crtc(dev, crtc, encoder)
3253 if (encoder->pre_enable)
3254 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003255
3256 /* Enable panel fitting for LVDS */
Jesse Barnesb074cec2013-04-25 12:55:02 -07003257 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003258
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003259 /*
3260 * On ILK+ LUT must be loaded before the pipe is running but with
3261 * clocks enabled
3262 */
3263 intel_crtc_load_lut(crtc);
3264
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003265 intel_enable_pipe(dev_priv, pipe,
3266 intel_crtc->config.has_pch_encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003267 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003268 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003269 intel_crtc_update_cursor(crtc, true);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003270
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003271 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003272 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003273
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003274 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003275 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003276 mutex_unlock(&dev->struct_mutex);
3277
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003278 for_each_encoder_on_crtc(dev, crtc, encoder)
3279 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003280
3281 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02003282 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003283
3284 /*
3285 * There seems to be a race in PCH platform hw (at least on some
3286 * outputs) where an enabled pipe still completes any pageflip right
3287 * away (as if the pipe is off) instead of waiting for vblank. As soon
3288 * as the first vblank happend, everything works as expected. Hence just
3289 * wait for one vblank before returning to avoid strange things
3290 * happening.
3291 */
3292 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003293}
3294
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003295/* IPS only exists on ULT machines and is tied to pipe A. */
3296static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3297{
3298 return IS_ULT(crtc->base.dev) && crtc->pipe == PIPE_A;
3299}
3300
3301static void hsw_enable_ips(struct intel_crtc *crtc)
3302{
3303 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3304
3305 if (!crtc->config.ips_enabled)
3306 return;
3307
3308 /* We can only enable IPS after we enable a plane and wait for a vblank.
3309 * We guarantee that the plane is enabled by calling intel_enable_ips
3310 * only after intel_enable_plane. And intel_enable_plane already waits
3311 * for a vblank, so all we need to do here is to enable the IPS bit. */
3312 assert_plane_enabled(dev_priv, crtc->plane);
3313 I915_WRITE(IPS_CTL, IPS_ENABLE);
3314}
3315
3316static void hsw_disable_ips(struct intel_crtc *crtc)
3317{
3318 struct drm_device *dev = crtc->base.dev;
3319 struct drm_i915_private *dev_priv = dev->dev_private;
3320
3321 if (!crtc->config.ips_enabled)
3322 return;
3323
3324 assert_plane_enabled(dev_priv, crtc->plane);
3325 I915_WRITE(IPS_CTL, 0);
3326
3327 /* We need to wait for a vblank before we can disable the plane. */
3328 intel_wait_for_vblank(dev, crtc->pipe);
3329}
3330
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003331static void haswell_crtc_enable(struct drm_crtc *crtc)
3332{
3333 struct drm_device *dev = crtc->dev;
3334 struct drm_i915_private *dev_priv = dev->dev_private;
3335 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3336 struct intel_encoder *encoder;
3337 int pipe = intel_crtc->pipe;
3338 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003339
3340 WARN_ON(!crtc->enabled);
3341
3342 if (intel_crtc->active)
3343 return;
3344
3345 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003346
3347 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3348 if (intel_crtc->config.has_pch_encoder)
3349 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3350
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003351 intel_update_watermarks(dev);
3352
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003353 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003354 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003355
3356 for_each_encoder_on_crtc(dev, crtc, encoder)
3357 if (encoder->pre_enable)
3358 encoder->pre_enable(encoder);
3359
Paulo Zanoni1f544382012-10-24 11:32:00 -02003360 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003361
Paulo Zanoni1f544382012-10-24 11:32:00 -02003362 /* Enable panel fitting for eDP */
Jesse Barnesb074cec2013-04-25 12:55:02 -07003363 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003364
3365 /*
3366 * On ILK+ LUT must be loaded before the pipe is running but with
3367 * clocks enabled
3368 */
3369 intel_crtc_load_lut(crtc);
3370
Paulo Zanoni1f544382012-10-24 11:32:00 -02003371 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003372 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003373
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003374 intel_enable_pipe(dev_priv, pipe,
3375 intel_crtc->config.has_pch_encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003376 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003377 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003378 intel_crtc_update_cursor(crtc, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003379
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003380 hsw_enable_ips(intel_crtc);
3381
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003382 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003383 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003384
3385 mutex_lock(&dev->struct_mutex);
3386 intel_update_fbc(dev);
3387 mutex_unlock(&dev->struct_mutex);
3388
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003389 for_each_encoder_on_crtc(dev, crtc, encoder)
3390 encoder->enable(encoder);
3391
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003392 /*
3393 * There seems to be a race in PCH platform hw (at least on some
3394 * outputs) where an enabled pipe still completes any pageflip right
3395 * away (as if the pipe is off) instead of waiting for vblank. As soon
3396 * as the first vblank happend, everything works as expected. Hence just
3397 * wait for one vblank before returning to avoid strange things
3398 * happening.
3399 */
3400 intel_wait_for_vblank(dev, intel_crtc->pipe);
3401}
3402
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003403static void ironlake_pfit_disable(struct intel_crtc *crtc)
3404{
3405 struct drm_device *dev = crtc->base.dev;
3406 struct drm_i915_private *dev_priv = dev->dev_private;
3407 int pipe = crtc->pipe;
3408
3409 /* To avoid upsetting the power well on haswell only disable the pfit if
3410 * it's in use. The hw state code will make sure we get this right. */
3411 if (crtc->config.pch_pfit.size) {
3412 I915_WRITE(PF_CTL(pipe), 0);
3413 I915_WRITE(PF_WIN_POS(pipe), 0);
3414 I915_WRITE(PF_WIN_SZ(pipe), 0);
3415 }
3416}
3417
Jesse Barnes6be4a602010-09-10 10:26:01 -07003418static void ironlake_crtc_disable(struct drm_crtc *crtc)
3419{
3420 struct drm_device *dev = crtc->dev;
3421 struct drm_i915_private *dev_priv = dev->dev_private;
3422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003423 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003424 int pipe = intel_crtc->pipe;
3425 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003426 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003427
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003428
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003429 if (!intel_crtc->active)
3430 return;
3431
Daniel Vetterea9d7582012-07-10 10:42:52 +02003432 for_each_encoder_on_crtc(dev, crtc, encoder)
3433 encoder->disable(encoder);
3434
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003435 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003436 drm_vblank_off(dev, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003437
Chris Wilson973d04f2011-07-08 12:22:37 +01003438 if (dev_priv->cfb_plane == plane)
3439 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003440
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003441 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003442 intel_disable_planes(crtc);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003443 intel_disable_plane(dev_priv, plane, pipe);
3444
Daniel Vetterd925c592013-06-05 13:34:04 +02003445 if (intel_crtc->config.has_pch_encoder)
3446 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3447
Jesse Barnesb24e7172011-01-04 15:09:30 -08003448 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003449
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003450 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003451
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003452 for_each_encoder_on_crtc(dev, crtc, encoder)
3453 if (encoder->post_disable)
3454 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003455
Daniel Vetterd925c592013-06-05 13:34:04 +02003456 if (intel_crtc->config.has_pch_encoder) {
3457 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003458
Daniel Vetterd925c592013-06-05 13:34:04 +02003459 ironlake_disable_pch_transcoder(dev_priv, pipe);
3460 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003461
Daniel Vetterd925c592013-06-05 13:34:04 +02003462 if (HAS_PCH_CPT(dev)) {
3463 /* disable TRANS_DP_CTL */
3464 reg = TRANS_DP_CTL(pipe);
3465 temp = I915_READ(reg);
3466 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3467 TRANS_DP_PORT_SEL_MASK);
3468 temp |= TRANS_DP_PORT_SEL_NONE;
3469 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003470
Daniel Vetterd925c592013-06-05 13:34:04 +02003471 /* disable DPLL_SEL */
3472 temp = I915_READ(PCH_DPLL_SEL);
3473 switch (pipe) {
3474 case 0:
3475 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
3476 break;
3477 case 1:
3478 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
3479 break;
3480 case 2:
3481 /* C shares PLL A or B */
3482 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
3483 break;
3484 default:
3485 BUG(); /* wtf */
3486 }
3487 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003488 }
Daniel Vetterd925c592013-06-05 13:34:04 +02003489
3490 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003491 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02003492
3493 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003494 }
3495
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003496 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003497 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003498
3499 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003500 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003501 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003502}
3503
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003504static void haswell_crtc_disable(struct drm_crtc *crtc)
3505{
3506 struct drm_device *dev = crtc->dev;
3507 struct drm_i915_private *dev_priv = dev->dev_private;
3508 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3509 struct intel_encoder *encoder;
3510 int pipe = intel_crtc->pipe;
3511 int plane = intel_crtc->plane;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003512 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003513
3514 if (!intel_crtc->active)
3515 return;
3516
3517 for_each_encoder_on_crtc(dev, crtc, encoder)
3518 encoder->disable(encoder);
3519
3520 intel_crtc_wait_for_pending_flips(crtc);
3521 drm_vblank_off(dev, pipe);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003522
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003523 /* FBC must be disabled before disabling the plane on HSW. */
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003524 if (dev_priv->cfb_plane == plane)
3525 intel_disable_fbc(dev);
3526
Paulo Zanoni42db64e2013-05-31 16:33:22 -03003527 hsw_disable_ips(intel_crtc);
3528
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003529 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003530 intel_disable_planes(crtc);
Rodrigo Vivi891348b2013-05-06 19:37:36 -03003531 intel_disable_plane(dev_priv, plane, pipe);
3532
Paulo Zanoni86642812013-04-12 17:57:57 -03003533 if (intel_crtc->config.has_pch_encoder)
3534 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003535 intel_disable_pipe(dev_priv, pipe);
3536
Paulo Zanoniad80a812012-10-24 16:06:19 -02003537 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003538
Daniel Vetter3f8dce32013-05-08 10:36:30 +02003539 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003540
Paulo Zanoni1f544382012-10-24 11:32:00 -02003541 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003542
3543 for_each_encoder_on_crtc(dev, crtc, encoder)
3544 if (encoder->post_disable)
3545 encoder->post_disable(encoder);
3546
Daniel Vetter88adfff2013-03-28 10:42:01 +01003547 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003548 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003549 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003550 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003551 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003552
3553 intel_crtc->active = false;
3554 intel_update_watermarks(dev);
3555
3556 mutex_lock(&dev->struct_mutex);
3557 intel_update_fbc(dev);
3558 mutex_unlock(&dev->struct_mutex);
3559}
3560
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003561static void ironlake_crtc_off(struct drm_crtc *crtc)
3562{
3563 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003564 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003565}
3566
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003567static void haswell_crtc_off(struct drm_crtc *crtc)
3568{
3569 intel_ddi_put_crtc_pll(crtc);
3570}
3571
Daniel Vetter02e792f2009-09-15 22:57:34 +02003572static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3573{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003574 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003575 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003576 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003577
Chris Wilson23f09ce2010-08-12 13:53:37 +01003578 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003579 dev_priv->mm.interruptible = false;
3580 (void) intel_overlay_switch_off(intel_crtc->overlay);
3581 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003582 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003583 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003584
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003585 /* Let userspace switch the overlay on again. In most cases userspace
3586 * has to recompute where to put it anyway.
3587 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003588}
3589
Egbert Eich61bc95c2013-03-04 09:24:38 -05003590/**
3591 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3592 * cursor plane briefly if not already running after enabling the display
3593 * plane.
3594 * This workaround avoids occasional blank screens when self refresh is
3595 * enabled.
3596 */
3597static void
3598g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3599{
3600 u32 cntl = I915_READ(CURCNTR(pipe));
3601
3602 if ((cntl & CURSOR_MODE) == 0) {
3603 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3604
3605 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3606 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3607 intel_wait_for_vblank(dev_priv->dev, pipe);
3608 I915_WRITE(CURCNTR(pipe), cntl);
3609 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3610 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3611 }
3612}
3613
Jesse Barnes2dd24552013-04-25 12:55:01 -07003614static void i9xx_pfit_enable(struct intel_crtc *crtc)
3615{
3616 struct drm_device *dev = crtc->base.dev;
3617 struct drm_i915_private *dev_priv = dev->dev_private;
3618 struct intel_crtc_config *pipe_config = &crtc->config;
3619
Daniel Vetter328d8e82013-05-08 10:36:31 +02003620 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07003621 return;
3622
Daniel Vetterc0b03412013-05-28 12:05:54 +02003623 /*
3624 * The panel fitter should only be adjusted whilst the pipe is disabled,
3625 * according to register description and PRM.
3626 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07003627 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
3628 assert_pipe_disabled(dev_priv, crtc->pipe);
3629
Jesse Barnesb074cec2013-04-25 12:55:02 -07003630 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
3631 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02003632
3633 /* Border color in case we don't scale up to the full screen. Black by
3634 * default, change to something else for debugging. */
3635 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07003636}
3637
Jesse Barnes89b667f2013-04-18 14:51:36 -07003638static void valleyview_crtc_enable(struct drm_crtc *crtc)
3639{
3640 struct drm_device *dev = crtc->dev;
3641 struct drm_i915_private *dev_priv = dev->dev_private;
3642 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3643 struct intel_encoder *encoder;
3644 int pipe = intel_crtc->pipe;
3645 int plane = intel_crtc->plane;
3646
3647 WARN_ON(!crtc->enabled);
3648
3649 if (intel_crtc->active)
3650 return;
3651
3652 intel_crtc->active = true;
3653 intel_update_watermarks(dev);
3654
3655 mutex_lock(&dev_priv->dpio_lock);
3656
3657 for_each_encoder_on_crtc(dev, crtc, encoder)
3658 if (encoder->pre_pll_enable)
3659 encoder->pre_pll_enable(encoder);
3660
3661 intel_enable_pll(dev_priv, pipe);
3662
3663 for_each_encoder_on_crtc(dev, crtc, encoder)
3664 if (encoder->pre_enable)
3665 encoder->pre_enable(encoder);
3666
3667 /* VLV wants encoder enabling _before_ the pipe is up. */
3668 for_each_encoder_on_crtc(dev, crtc, encoder)
3669 encoder->enable(encoder);
3670
Jesse Barnes2dd24552013-04-25 12:55:01 -07003671 /* Enable panel fitting for eDP */
3672 i9xx_pfit_enable(intel_crtc);
3673
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003674 intel_crtc_load_lut(crtc);
3675
Jesse Barnes89b667f2013-04-18 14:51:36 -07003676 intel_enable_pipe(dev_priv, pipe, false);
3677 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003678 intel_enable_planes(crtc);
Ville Syrjälä5c38d482013-06-04 13:49:00 +03003679 intel_crtc_update_cursor(crtc, true);
Jesse Barnes89b667f2013-04-18 14:51:36 -07003680
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003681 intel_update_fbc(dev);
3682
Jesse Barnes89b667f2013-04-18 14:51:36 -07003683 mutex_unlock(&dev_priv->dpio_lock);
3684}
3685
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003686static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003687{
3688 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003689 struct drm_i915_private *dev_priv = dev->dev_private;
3690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003691 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003692 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003693 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003694
Daniel Vetter08a48462012-07-02 11:43:47 +02003695 WARN_ON(!crtc->enabled);
3696
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003697 if (intel_crtc->active)
3698 return;
3699
3700 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003701 intel_update_watermarks(dev);
3702
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003703 intel_enable_pll(dev_priv, pipe);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003704
3705 for_each_encoder_on_crtc(dev, crtc, encoder)
3706 if (encoder->pre_enable)
3707 encoder->pre_enable(encoder);
3708
Jesse Barnes2dd24552013-04-25 12:55:01 -07003709 /* Enable panel fitting for LVDS */
3710 i9xx_pfit_enable(intel_crtc);
3711
Ville Syrjälä63cbb072013-06-04 13:48:59 +03003712 intel_crtc_load_lut(crtc);
3713
Jesse Barnes040484a2011-01-03 12:14:26 -08003714 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003715 intel_enable_plane(dev_priv, plane, pipe);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003716 intel_enable_planes(crtc);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003717 /* The fixup needs to happen before cursor is enabled */
Egbert Eich61bc95c2013-03-04 09:24:38 -05003718 if (IS_G4X(dev))
3719 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjälä22e407d2013-06-07 18:52:24 +03003720 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003721
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003722 /* Give the overlay scaler a chance to enable if it's on this pipe */
3723 intel_crtc_dpms_overlay(intel_crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003724
Ville Syrjäläf440eb12013-06-04 13:49:01 +03003725 intel_update_fbc(dev);
3726
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003727 for_each_encoder_on_crtc(dev, crtc, encoder)
3728 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003729}
3730
Daniel Vetter87476d62013-04-11 16:29:06 +02003731static void i9xx_pfit_disable(struct intel_crtc *crtc)
3732{
3733 struct drm_device *dev = crtc->base.dev;
3734 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02003735
3736 if (!crtc->config.gmch_pfit.control)
3737 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02003738
3739 assert_pipe_disabled(dev_priv, crtc->pipe);
3740
Daniel Vetter328d8e82013-05-08 10:36:31 +02003741 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
3742 I915_READ(PFIT_CONTROL));
3743 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02003744}
3745
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003746static void i9xx_crtc_disable(struct drm_crtc *crtc)
3747{
3748 struct drm_device *dev = crtc->dev;
3749 struct drm_i915_private *dev_priv = dev->dev_private;
3750 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003751 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003752 int pipe = intel_crtc->pipe;
3753 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003754
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003755 if (!intel_crtc->active)
3756 return;
3757
Daniel Vetterea9d7582012-07-10 10:42:52 +02003758 for_each_encoder_on_crtc(dev, crtc, encoder)
3759 encoder->disable(encoder);
3760
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003761 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003762 intel_crtc_wait_for_pending_flips(crtc);
3763 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003764
Chris Wilson973d04f2011-07-08 12:22:37 +01003765 if (dev_priv->cfb_plane == plane)
3766 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003767
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003768 intel_crtc_dpms_overlay(intel_crtc, false);
3769 intel_crtc_update_cursor(crtc, false);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003770 intel_disable_planes(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003771 intel_disable_plane(dev_priv, plane, pipe);
Ville Syrjälä0d5b8c62013-06-04 13:49:02 +03003772
Jesse Barnesb24e7172011-01-04 15:09:30 -08003773 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003774
Daniel Vetter87476d62013-04-11 16:29:06 +02003775 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003776
Jesse Barnes89b667f2013-04-18 14:51:36 -07003777 for_each_encoder_on_crtc(dev, crtc, encoder)
3778 if (encoder->post_disable)
3779 encoder->post_disable(encoder);
3780
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003781 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003782
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003783 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003784 intel_update_fbc(dev);
3785 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003786}
3787
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003788static void i9xx_crtc_off(struct drm_crtc *crtc)
3789{
3790}
3791
Daniel Vetter976f8a22012-07-08 22:34:21 +02003792static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3793 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003794{
3795 struct drm_device *dev = crtc->dev;
3796 struct drm_i915_master_private *master_priv;
3797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3798 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003799
3800 if (!dev->primary->master)
3801 return;
3802
3803 master_priv = dev->primary->master->driver_priv;
3804 if (!master_priv->sarea_priv)
3805 return;
3806
Jesse Barnes79e53942008-11-07 14:24:08 -08003807 switch (pipe) {
3808 case 0:
3809 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3810 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3811 break;
3812 case 1:
3813 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3814 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3815 break;
3816 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003817 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003818 break;
3819 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003820}
3821
Daniel Vetter976f8a22012-07-08 22:34:21 +02003822/**
3823 * Sets the power management mode of the pipe and plane.
3824 */
3825void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003826{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003827 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003828 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003829 struct intel_encoder *intel_encoder;
3830 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003831
Daniel Vetter976f8a22012-07-08 22:34:21 +02003832 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3833 enable |= intel_encoder->connectors_active;
3834
3835 if (enable)
3836 dev_priv->display.crtc_enable(crtc);
3837 else
3838 dev_priv->display.crtc_disable(crtc);
3839
3840 intel_crtc_update_sarea(crtc, enable);
3841}
3842
Daniel Vetter976f8a22012-07-08 22:34:21 +02003843static void intel_crtc_disable(struct drm_crtc *crtc)
3844{
3845 struct drm_device *dev = crtc->dev;
3846 struct drm_connector *connector;
3847 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003849
3850 /* crtc should still be enabled when we disable it. */
3851 WARN_ON(!crtc->enabled);
3852
3853 dev_priv->display.crtc_disable(crtc);
Paulo Zanonic77bf562013-05-03 12:15:40 -03003854 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003855 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003856 dev_priv->display.off(crtc);
3857
Chris Wilson931872f2012-01-16 23:01:13 +00003858 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3859 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003860
3861 if (crtc->fb) {
3862 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003863 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003864 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003865 crtc->fb = NULL;
3866 }
3867
3868 /* Update computed state. */
3869 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3870 if (!connector->encoder || !connector->encoder->crtc)
3871 continue;
3872
3873 if (connector->encoder->crtc != crtc)
3874 continue;
3875
3876 connector->dpms = DRM_MODE_DPMS_OFF;
3877 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003878 }
3879}
3880
Daniel Vettera261b242012-07-26 19:21:47 +02003881void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003882{
Daniel Vettera261b242012-07-26 19:21:47 +02003883 struct drm_crtc *crtc;
3884
3885 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3886 if (crtc->enabled)
3887 intel_crtc_disable(crtc);
3888 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003889}
3890
Chris Wilsonea5b2132010-08-04 13:50:23 +01003891void intel_encoder_destroy(struct drm_encoder *encoder)
3892{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003893 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003894
Chris Wilsonea5b2132010-08-04 13:50:23 +01003895 drm_encoder_cleanup(encoder);
3896 kfree(intel_encoder);
3897}
3898
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003899/* Simple dpms helper for encodres with just one connector, no cloning and only
3900 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3901 * state of the entire output pipe. */
3902void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3903{
3904 if (mode == DRM_MODE_DPMS_ON) {
3905 encoder->connectors_active = true;
3906
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003907 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003908 } else {
3909 encoder->connectors_active = false;
3910
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003911 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003912 }
3913}
3914
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003915/* Cross check the actual hw state with our own modeset state tracking (and it's
3916 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003917static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003918{
3919 if (connector->get_hw_state(connector)) {
3920 struct intel_encoder *encoder = connector->encoder;
3921 struct drm_crtc *crtc;
3922 bool encoder_enabled;
3923 enum pipe pipe;
3924
3925 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3926 connector->base.base.id,
3927 drm_get_connector_name(&connector->base));
3928
3929 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3930 "wrong connector dpms state\n");
3931 WARN(connector->base.encoder != &encoder->base,
3932 "active connector not linked to encoder\n");
3933 WARN(!encoder->connectors_active,
3934 "encoder->connectors_active not set\n");
3935
3936 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3937 WARN(!encoder_enabled, "encoder not enabled\n");
3938 if (WARN_ON(!encoder->base.crtc))
3939 return;
3940
3941 crtc = encoder->base.crtc;
3942
3943 WARN(!crtc->enabled, "crtc not enabled\n");
3944 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3945 WARN(pipe != to_intel_crtc(crtc)->pipe,
3946 "encoder active on the wrong pipe\n");
3947 }
3948}
3949
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003950/* Even simpler default implementation, if there's really no special case to
3951 * consider. */
3952void intel_connector_dpms(struct drm_connector *connector, int mode)
3953{
3954 struct intel_encoder *encoder = intel_attached_encoder(connector);
3955
3956 /* All the simple cases only support two dpms states. */
3957 if (mode != DRM_MODE_DPMS_ON)
3958 mode = DRM_MODE_DPMS_OFF;
3959
3960 if (mode == connector->dpms)
3961 return;
3962
3963 connector->dpms = mode;
3964
3965 /* Only need to change hw state when actually enabled */
3966 if (encoder->base.crtc)
3967 intel_encoder_dpms(encoder, mode);
3968 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003969 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003970
Daniel Vetterb9805142012-08-31 17:37:33 +02003971 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003972}
3973
Daniel Vetterf0947c32012-07-02 13:10:34 +02003974/* Simple connector->get_hw_state implementation for encoders that support only
3975 * one connector and no cloning and hence the encoder state determines the state
3976 * of the connector. */
3977bool intel_connector_get_hw_state(struct intel_connector *connector)
3978{
Daniel Vetter24929352012-07-02 20:28:59 +02003979 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003980 struct intel_encoder *encoder = connector->encoder;
3981
3982 return encoder->get_hw_state(encoder, &pipe);
3983}
3984
Daniel Vetter1857e1d2013-04-29 19:34:16 +02003985static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
3986 struct intel_crtc_config *pipe_config)
3987{
3988 struct drm_i915_private *dev_priv = dev->dev_private;
3989 struct intel_crtc *pipe_B_crtc =
3990 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
3991
3992 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
3993 pipe_name(pipe), pipe_config->fdi_lanes);
3994 if (pipe_config->fdi_lanes > 4) {
3995 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
3996 pipe_name(pipe), pipe_config->fdi_lanes);
3997 return false;
3998 }
3999
4000 if (IS_HASWELL(dev)) {
4001 if (pipe_config->fdi_lanes > 2) {
4002 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4003 pipe_config->fdi_lanes);
4004 return false;
4005 } else {
4006 return true;
4007 }
4008 }
4009
4010 if (INTEL_INFO(dev)->num_pipes == 2)
4011 return true;
4012
4013 /* Ivybridge 3 pipe is really complicated */
4014 switch (pipe) {
4015 case PIPE_A:
4016 return true;
4017 case PIPE_B:
4018 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4019 pipe_config->fdi_lanes > 2) {
4020 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4021 pipe_name(pipe), pipe_config->fdi_lanes);
4022 return false;
4023 }
4024 return true;
4025 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01004026 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004027 pipe_B_crtc->config.fdi_lanes <= 2) {
4028 if (pipe_config->fdi_lanes > 2) {
4029 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4030 pipe_name(pipe), pipe_config->fdi_lanes);
4031 return false;
4032 }
4033 } else {
4034 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4035 return false;
4036 }
4037 return true;
4038 default:
4039 BUG();
4040 }
4041}
4042
Daniel Vettere29c22c2013-02-21 00:00:16 +01004043#define RETRY 1
4044static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4045 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02004046{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004047 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004048 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02004049 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01004050 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004051
Daniel Vettere29c22c2013-02-21 00:00:16 +01004052retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02004053 /* FDI is a binary signal running at ~2.7GHz, encoding
4054 * each output octet as 10 bits. The actual frequency
4055 * is stored as a divider into a 100MHz clock, and the
4056 * mode pixel clock is stored in units of 1KHz.
4057 * Hence the bw of each lane in terms of the mode signal
4058 * is:
4059 */
4060 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4061
Daniel Vetterff9a6752013-06-01 17:16:21 +02004062 fdi_dotclock = adjusted_mode->clock;
Daniel Vetteref1b4602013-06-01 17:17:04 +02004063 fdi_dotclock /= pipe_config->pixel_multiplier;
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004064
4065 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004066 pipe_config->pipe_bpp);
4067
4068 pipe_config->fdi_lanes = lane;
4069
Daniel Vetter2bd89a02013-06-01 17:16:19 +02004070 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02004071 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02004072
Daniel Vettere29c22c2013-02-21 00:00:16 +01004073 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4074 intel_crtc->pipe, pipe_config);
4075 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4076 pipe_config->pipe_bpp -= 2*3;
4077 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4078 pipe_config->pipe_bpp);
4079 needs_recompute = true;
4080 pipe_config->bw_constrained = true;
4081
4082 goto retry;
4083 }
4084
4085 if (needs_recompute)
4086 return RETRY;
4087
4088 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02004089}
4090
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004091static void hsw_compute_ips_config(struct intel_crtc *crtc,
4092 struct intel_crtc_config *pipe_config)
4093{
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03004094 pipe_config->ips_enabled = i915_enable_ips &&
4095 hsw_crtc_supports_ips(crtc) &&
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004096 pipe_config->pipe_bpp == 24;
4097}
4098
Daniel Vettere29c22c2013-02-21 00:00:16 +01004099static int intel_crtc_compute_config(struct drm_crtc *crtc,
4100 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004101{
Zhenyu Wang2c072452009-06-05 15:38:42 +08004102 struct drm_device *dev = crtc->dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004103 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004104 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson89749352010-09-12 18:25:19 +01004105
Eric Anholtbad720f2009-10-22 16:11:14 -07004106 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004107 /* FDI link clock is fixed at 2.7G */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004108 if (pipe_config->requested_mode.clock * 3
4109 > IRONLAKE_FDI_FREQ * 4)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004110 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004111 }
Chris Wilson89749352010-09-12 18:25:19 +01004112
Daniel Vetterf9bef082012-04-15 19:53:19 +02004113 /* All interlaced capable intel hw wants timings in frames. Note though
4114 * that intel_lvds_mode_fixup does some funny tricks with the crtc
4115 * timings, so we need to be careful not to clobber these.*/
Daniel Vetter7ae89232013-03-27 00:44:52 +01004116 if (!pipe_config->timings_set)
Daniel Vetterf9bef082012-04-15 19:53:19 +02004117 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01004118
Damien Lespiau8693a822013-05-03 18:48:11 +01004119 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4120 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03004121 */
4122 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4123 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01004124 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03004125
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004126 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004127 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004128 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004129 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4130 * for lvds. */
4131 pipe_config->pipe_bpp = 8*3;
4132 }
4133
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004134 if (IS_HASWELL(dev))
4135 hsw_compute_ips_config(intel_crtc, pipe_config);
4136
Daniel Vetter877d48d2013-04-19 11:24:43 +02004137 if (pipe_config->has_pch_encoder)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004138 return ironlake_fdi_compute_config(intel_crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02004139
Daniel Vettere29c22c2013-02-21 00:00:16 +01004140 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004141}
4142
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004143static int valleyview_get_display_clock_speed(struct drm_device *dev)
4144{
4145 return 400000; /* FIXME */
4146}
4147
Jesse Barnese70236a2009-09-21 10:42:27 -07004148static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004149{
Jesse Barnese70236a2009-09-21 10:42:27 -07004150 return 400000;
4151}
Jesse Barnes79e53942008-11-07 14:24:08 -08004152
Jesse Barnese70236a2009-09-21 10:42:27 -07004153static int i915_get_display_clock_speed(struct drm_device *dev)
4154{
4155 return 333000;
4156}
Jesse Barnes79e53942008-11-07 14:24:08 -08004157
Jesse Barnese70236a2009-09-21 10:42:27 -07004158static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4159{
4160 return 200000;
4161}
Jesse Barnes79e53942008-11-07 14:24:08 -08004162
Jesse Barnese70236a2009-09-21 10:42:27 -07004163static int i915gm_get_display_clock_speed(struct drm_device *dev)
4164{
4165 u16 gcfgc = 0;
4166
4167 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4168
4169 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004170 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004171 else {
4172 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4173 case GC_DISPLAY_CLOCK_333_MHZ:
4174 return 333000;
4175 default:
4176 case GC_DISPLAY_CLOCK_190_200_MHZ:
4177 return 190000;
4178 }
4179 }
4180}
Jesse Barnes79e53942008-11-07 14:24:08 -08004181
Jesse Barnese70236a2009-09-21 10:42:27 -07004182static int i865_get_display_clock_speed(struct drm_device *dev)
4183{
4184 return 266000;
4185}
4186
4187static int i855_get_display_clock_speed(struct drm_device *dev)
4188{
4189 u16 hpllcc = 0;
4190 /* Assume that the hardware is in the high speed state. This
4191 * should be the default.
4192 */
4193 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4194 case GC_CLOCK_133_200:
4195 case GC_CLOCK_100_200:
4196 return 200000;
4197 case GC_CLOCK_166_250:
4198 return 250000;
4199 case GC_CLOCK_100_133:
4200 return 133000;
4201 }
4202
4203 /* Shouldn't happen */
4204 return 0;
4205}
4206
4207static int i830_get_display_clock_speed(struct drm_device *dev)
4208{
4209 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004210}
4211
Zhenyu Wang2c072452009-06-05 15:38:42 +08004212static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004213intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004214{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004215 while (*num > DATA_LINK_M_N_MASK ||
4216 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004217 *num >>= 1;
4218 *den >>= 1;
4219 }
4220}
4221
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004222static void compute_m_n(unsigned int m, unsigned int n,
4223 uint32_t *ret_m, uint32_t *ret_n)
4224{
4225 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4226 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4227 intel_reduce_m_n_ratio(ret_m, ret_n);
4228}
4229
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004230void
4231intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4232 int pixel_clock, int link_clock,
4233 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004234{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004235 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03004236
4237 compute_m_n(bits_per_pixel * pixel_clock,
4238 link_clock * nlanes * 8,
4239 &m_n->gmch_m, &m_n->gmch_n);
4240
4241 compute_m_n(pixel_clock, link_clock,
4242 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004243}
4244
Chris Wilsona7615032011-01-12 17:04:08 +00004245static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4246{
Keith Packard72bbe58c2011-09-26 16:09:45 -07004247 if (i915_panel_use_ssc >= 0)
4248 return i915_panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004249 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004250 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004251}
4252
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004253static int vlv_get_refclk(struct drm_crtc *crtc)
4254{
4255 struct drm_device *dev = crtc->dev;
4256 struct drm_i915_private *dev_priv = dev->dev_private;
4257 int refclk = 27000; /* for DP & HDMI */
4258
4259 return 100000; /* only one validated so far */
4260
4261 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4262 refclk = 96000;
4263 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4264 if (intel_panel_use_ssc(dev_priv))
4265 refclk = 100000;
4266 else
4267 refclk = 96000;
4268 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4269 refclk = 100000;
4270 }
4271
4272 return refclk;
4273}
4274
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004275static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4276{
4277 struct drm_device *dev = crtc->dev;
4278 struct drm_i915_private *dev_priv = dev->dev_private;
4279 int refclk;
4280
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004281 if (IS_VALLEYVIEW(dev)) {
4282 refclk = vlv_get_refclk(crtc);
4283 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004284 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004285 refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004286 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4287 refclk / 1000);
4288 } else if (!IS_GEN2(dev)) {
4289 refclk = 96000;
4290 } else {
4291 refclk = 48000;
4292 }
4293
4294 return refclk;
4295}
4296
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004297static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
4298{
4299 return (1 << dpll->n) << 16 | dpll->m1 << 8 | dpll->m2;
4300}
4301
4302static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4303{
4304 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
4305}
4306
Daniel Vetterf47709a2013-03-28 10:42:02 +01004307static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004308 intel_clock_t *reduced_clock)
4309{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004310 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004311 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004312 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004313 u32 fp, fp2 = 0;
4314
4315 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004316 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004317 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004318 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004319 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004320 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004321 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004322 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004323 }
4324
4325 I915_WRITE(FP0(pipe), fp);
4326
Daniel Vetterf47709a2013-03-28 10:42:02 +01004327 crtc->lowfreq_avail = false;
4328 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004329 reduced_clock && i915_powersave) {
4330 I915_WRITE(FP1(pipe), fp2);
Daniel Vetterf47709a2013-03-28 10:42:02 +01004331 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004332 } else {
4333 I915_WRITE(FP1(pipe), fp);
4334 }
4335}
4336
Jesse Barnes89b667f2013-04-18 14:51:36 -07004337static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4338{
4339 u32 reg_val;
4340
4341 /*
4342 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4343 * and set it to a reasonable value instead.
4344 */
Jani Nikulaae992582013-05-22 15:36:19 +03004345 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004346 reg_val &= 0xffffff00;
4347 reg_val |= 0x00000030;
Jani Nikulaae992582013-05-22 15:36:19 +03004348 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004349
Jani Nikulaae992582013-05-22 15:36:19 +03004350 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004351 reg_val &= 0x8cffffff;
4352 reg_val = 0x8c000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004353 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004354
Jani Nikulaae992582013-05-22 15:36:19 +03004355 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004356 reg_val &= 0xffffff00;
Jani Nikulaae992582013-05-22 15:36:19 +03004357 vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004358
Jani Nikulaae992582013-05-22 15:36:19 +03004359 reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004360 reg_val &= 0x00ffffff;
4361 reg_val |= 0xb0000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004362 vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004363}
4364
Daniel Vetterb5518422013-05-03 11:49:48 +02004365static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
4366 struct intel_link_m_n *m_n)
4367{
4368 struct drm_device *dev = crtc->base.dev;
4369 struct drm_i915_private *dev_priv = dev->dev_private;
4370 int pipe = crtc->pipe;
4371
Daniel Vettere3b95f12013-05-03 11:49:49 +02004372 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4373 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4374 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4375 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004376}
4377
4378static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
4379 struct intel_link_m_n *m_n)
4380{
4381 struct drm_device *dev = crtc->base.dev;
4382 struct drm_i915_private *dev_priv = dev->dev_private;
4383 int pipe = crtc->pipe;
4384 enum transcoder transcoder = crtc->config.cpu_transcoder;
4385
4386 if (INTEL_INFO(dev)->gen >= 5) {
4387 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
4388 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
4389 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
4390 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
4391 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02004392 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
4393 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4394 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
4395 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02004396 }
4397}
4398
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004399static void intel_dp_set_m_n(struct intel_crtc *crtc)
4400{
4401 if (crtc->config.has_pch_encoder)
4402 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4403 else
4404 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4405}
4406
Daniel Vetterf47709a2013-03-28 10:42:02 +01004407static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004408{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004409 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004410 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004411 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004412 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004413 u32 dpll, mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004414 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004415 bool is_hdmi;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004416 u32 coreclk, reg_val, dpll_md;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004417
Daniel Vetter09153002012-12-12 14:06:44 +01004418 mutex_lock(&dev_priv->dpio_lock);
4419
Jesse Barnes89b667f2013-04-18 14:51:36 -07004420 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004421
Daniel Vetterf47709a2013-03-28 10:42:02 +01004422 bestn = crtc->config.dpll.n;
4423 bestm1 = crtc->config.dpll.m1;
4424 bestm2 = crtc->config.dpll.m2;
4425 bestp1 = crtc->config.dpll.p1;
4426 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004427
Jesse Barnes89b667f2013-04-18 14:51:36 -07004428 /* See eDP HDMI DPIO driver vbios notes doc */
4429
4430 /* PLL B needs special handling */
4431 if (pipe)
4432 vlv_pllb_recal_opamp(dev_priv);
4433
4434 /* Set up Tx target for periodic Rcomp update */
Jani Nikulaae992582013-05-22 15:36:19 +03004435 vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004436
4437 /* Disable target IRef on PLL */
Jani Nikulaae992582013-05-22 15:36:19 +03004438 reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004439 reg_val &= 0x00ffffff;
Jani Nikulaae992582013-05-22 15:36:19 +03004440 vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004441
4442 /* Disable fast lock */
Jani Nikulaae992582013-05-22 15:36:19 +03004443 vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004444
4445 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004446 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4447 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4448 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004449 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07004450
4451 /*
4452 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
4453 * but we don't support that).
4454 * Note: don't use the DAC post divider as it seems unstable.
4455 */
4456 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Jani Nikulaae992582013-05-22 15:36:19 +03004457 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004458
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004459 mdiv |= DPIO_ENABLE_CALIBRATION;
Jani Nikulaae992582013-05-22 15:36:19 +03004460 vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004461
Jesse Barnes89b667f2013-04-18 14:51:36 -07004462 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02004463 if (crtc->config.port_clock == 162000 ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07004464 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Jani Nikulaae992582013-05-22 15:36:19 +03004465 vlv_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004466 0x005f0021);
4467 else
Jani Nikulaae992582013-05-22 15:36:19 +03004468 vlv_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004469 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004470
Jesse Barnes89b667f2013-04-18 14:51:36 -07004471 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4472 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4473 /* Use SSC source */
4474 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004475 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004476 0x0df40000);
4477 else
Jani Nikulaae992582013-05-22 15:36:19 +03004478 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004479 0x0df70000);
4480 } else { /* HDMI or VGA */
4481 /* Use bend source */
4482 if (!pipe)
Jani Nikulaae992582013-05-22 15:36:19 +03004483 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004484 0x0df70000);
4485 else
Jani Nikulaae992582013-05-22 15:36:19 +03004486 vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07004487 0x0df40000);
4488 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004489
Jani Nikulaae992582013-05-22 15:36:19 +03004490 coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004491 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4492 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4493 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4494 coreclk |= 0x01000000;
Jani Nikulaae992582013-05-22 15:36:19 +03004495 vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004496
Jani Nikulaae992582013-05-22 15:36:19 +03004497 vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004498
4499 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4500 if (encoder->pre_pll_enable)
4501 encoder->pre_pll_enable(encoder);
4502
4503 /* Enable DPIO clock input */
4504 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4505 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4506 if (pipe)
4507 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004508
4509 dpll |= DPLL_VCO_ENABLE;
4510 I915_WRITE(DPLL(pipe), dpll);
4511 POSTING_READ(DPLL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004512 udelay(150);
4513
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004514 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4515 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4516
Daniel Vetteref1b4602013-06-01 17:17:04 +02004517 dpll_md = (crtc->config.pixel_multiplier - 1)
4518 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004519 I915_WRITE(DPLL_MD(pipe), dpll_md);
4520 POSTING_READ(DPLL_MD(pipe));
Daniel Vetterf47709a2013-03-28 10:42:02 +01004521
Jesse Barnes89b667f2013-04-18 14:51:36 -07004522 if (crtc->config.has_dp_encoder)
4523 intel_dp_set_m_n(crtc);
Daniel Vetter09153002012-12-12 14:06:44 +01004524
4525 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004526}
4527
Daniel Vetterf47709a2013-03-28 10:42:02 +01004528static void i9xx_update_pll(struct intel_crtc *crtc,
4529 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004530 int num_connectors)
4531{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004532 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004533 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterdafd2262012-11-26 17:22:07 +01004534 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004535 int pipe = crtc->pipe;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004536 u32 dpll;
4537 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004538 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004539
Daniel Vetterf47709a2013-03-28 10:42:02 +01004540 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304541
Daniel Vetterf47709a2013-03-28 10:42:02 +01004542 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4543 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004544
4545 dpll = DPLL_VGA_MODE_DIS;
4546
Daniel Vetterf47709a2013-03-28 10:42:02 +01004547 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004548 dpll |= DPLLB_MODE_LVDS;
4549 else
4550 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004551
Daniel Vetteref1b4602013-06-01 17:17:04 +02004552 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02004553 dpll |= (crtc->config.pixel_multiplier - 1)
4554 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004555 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02004556
4557 if (is_sdvo)
4558 dpll |= DPLL_DVO_HIGH_SPEED;
4559
Daniel Vetterf47709a2013-03-28 10:42:02 +01004560 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004561 dpll |= DPLL_DVO_HIGH_SPEED;
4562
4563 /* compute bitmask from p1 value */
4564 if (IS_PINEVIEW(dev))
4565 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4566 else {
4567 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4568 if (IS_G4X(dev) && reduced_clock)
4569 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4570 }
4571 switch (clock->p2) {
4572 case 5:
4573 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4574 break;
4575 case 7:
4576 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4577 break;
4578 case 10:
4579 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4580 break;
4581 case 14:
4582 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4583 break;
4584 }
4585 if (INTEL_INFO(dev)->gen >= 4)
4586 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4587
Daniel Vetter09ede542013-04-30 14:01:45 +02004588 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004589 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004590 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004591 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4592 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4593 else
4594 dpll |= PLL_REF_INPUT_DREFCLK;
4595
4596 dpll |= DPLL_VCO_ENABLE;
4597 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4598 POSTING_READ(DPLL(pipe));
4599 udelay(150);
4600
Daniel Vetterf47709a2013-03-28 10:42:02 +01004601 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetterdafd2262012-11-26 17:22:07 +01004602 if (encoder->pre_pll_enable)
4603 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004604
Daniel Vetterf47709a2013-03-28 10:42:02 +01004605 if (crtc->config.has_dp_encoder)
4606 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004607
4608 I915_WRITE(DPLL(pipe), dpll);
4609
4610 /* Wait for the clocks to stabilize. */
4611 POSTING_READ(DPLL(pipe));
4612 udelay(150);
4613
4614 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02004615 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
4616 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02004617 I915_WRITE(DPLL_MD(pipe), dpll_md);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004618 } else {
4619 /* The pixel multiplier can only be updated once the
4620 * DPLL is enabled and the clocks are stable.
4621 *
4622 * So write it again.
4623 */
4624 I915_WRITE(DPLL(pipe), dpll);
4625 }
4626}
4627
Daniel Vetterf47709a2013-03-28 10:42:02 +01004628static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004629 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004630 int num_connectors)
4631{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004632 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004633 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterdafd2262012-11-26 17:22:07 +01004634 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004635 int pipe = crtc->pipe;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004636 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004637 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004638
Daniel Vetterf47709a2013-03-28 10:42:02 +01004639 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304640
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004641 dpll = DPLL_VGA_MODE_DIS;
4642
Daniel Vetterf47709a2013-03-28 10:42:02 +01004643 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004644 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4645 } else {
4646 if (clock->p1 == 2)
4647 dpll |= PLL_P1_DIVIDE_BY_TWO;
4648 else
4649 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4650 if (clock->p2 == 4)
4651 dpll |= PLL_P2_DIVIDE_BY_4;
4652 }
4653
Daniel Vetterf47709a2013-03-28 10:42:02 +01004654 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004655 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4656 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4657 else
4658 dpll |= PLL_REF_INPUT_DREFCLK;
4659
4660 dpll |= DPLL_VCO_ENABLE;
4661 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4662 POSTING_READ(DPLL(pipe));
4663 udelay(150);
4664
Daniel Vetterf47709a2013-03-28 10:42:02 +01004665 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetterdafd2262012-11-26 17:22:07 +01004666 if (encoder->pre_pll_enable)
4667 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004668
Daniel Vetter5b5896e2012-09-11 12:37:55 +02004669 I915_WRITE(DPLL(pipe), dpll);
4670
4671 /* Wait for the clocks to stabilize. */
4672 POSTING_READ(DPLL(pipe));
4673 udelay(150);
4674
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004675 /* The pixel multiplier can only be updated once the
4676 * DPLL is enabled and the clocks are stable.
4677 *
4678 * So write it again.
4679 */
4680 I915_WRITE(DPLL(pipe), dpll);
4681}
4682
Daniel Vetter8a654f32013-06-01 17:16:22 +02004683static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004684{
4685 struct drm_device *dev = intel_crtc->base.dev;
4686 struct drm_i915_private *dev_priv = dev->dev_private;
4687 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004688 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02004689 struct drm_display_mode *adjusted_mode =
4690 &intel_crtc->config.adjusted_mode;
4691 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004692 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
4693
4694 /* We need to be careful not to changed the adjusted mode, for otherwise
4695 * the hw state checker will get angry at the mismatch. */
4696 crtc_vtotal = adjusted_mode->crtc_vtotal;
4697 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004698
4699 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4700 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004701 crtc_vtotal -= 1;
4702 crtc_vblank_end -= 1;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004703 vsyncshift = adjusted_mode->crtc_hsync_start
4704 - adjusted_mode->crtc_htotal / 2;
4705 } else {
4706 vsyncshift = 0;
4707 }
4708
4709 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004710 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004711
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004712 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004713 (adjusted_mode->crtc_hdisplay - 1) |
4714 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004715 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004716 (adjusted_mode->crtc_hblank_start - 1) |
4717 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004718 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004719 (adjusted_mode->crtc_hsync_start - 1) |
4720 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4721
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004722 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004723 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004724 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004725 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004726 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02004727 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004728 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004729 (adjusted_mode->crtc_vsync_start - 1) |
4730 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4731
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004732 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4733 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4734 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4735 * bits. */
4736 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4737 (pipe == PIPE_B || pipe == PIPE_C))
4738 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4739
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004740 /* pipesrc controls the size that is scaled from, which should
4741 * always be the user's requested size.
4742 */
4743 I915_WRITE(PIPESRC(pipe),
4744 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4745}
4746
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02004747static void intel_get_pipe_timings(struct intel_crtc *crtc,
4748 struct intel_crtc_config *pipe_config)
4749{
4750 struct drm_device *dev = crtc->base.dev;
4751 struct drm_i915_private *dev_priv = dev->dev_private;
4752 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4753 uint32_t tmp;
4754
4755 tmp = I915_READ(HTOTAL(cpu_transcoder));
4756 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4757 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4758 tmp = I915_READ(HBLANK(cpu_transcoder));
4759 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
4760 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
4761 tmp = I915_READ(HSYNC(cpu_transcoder));
4762 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4763 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4764
4765 tmp = I915_READ(VTOTAL(cpu_transcoder));
4766 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4767 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4768 tmp = I915_READ(VBLANK(cpu_transcoder));
4769 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
4770 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
4771 tmp = I915_READ(VSYNC(cpu_transcoder));
4772 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4773 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4774
4775 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
4776 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4777 pipe_config->adjusted_mode.crtc_vtotal += 1;
4778 pipe_config->adjusted_mode.crtc_vblank_end += 1;
4779 }
4780
4781 tmp = I915_READ(PIPESRC(crtc->pipe));
4782 pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
4783 pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
4784}
4785
Daniel Vetter84b046f2013-02-19 18:48:54 +01004786static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4787{
4788 struct drm_device *dev = intel_crtc->base.dev;
4789 struct drm_i915_private *dev_priv = dev->dev_private;
4790 uint32_t pipeconf;
4791
4792 pipeconf = I915_READ(PIPECONF(intel_crtc->pipe));
4793
4794 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4795 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4796 * core speed.
4797 *
4798 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4799 * pipe == 0 check?
4800 */
4801 if (intel_crtc->config.requested_mode.clock >
4802 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4803 pipeconf |= PIPECONF_DOUBLE_WIDE;
4804 else
4805 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4806 }
4807
Daniel Vetterff9ce462013-04-24 14:57:17 +02004808 /* only g4x and later have fancy bpc/dither controls */
4809 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
4810 pipeconf &= ~(PIPECONF_BPC_MASK |
4811 PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
Daniel Vetter84b046f2013-02-19 18:48:54 +01004812
Daniel Vetterff9ce462013-04-24 14:57:17 +02004813 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4814 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
4815 pipeconf |= PIPECONF_DITHER_EN |
4816 PIPECONF_DITHER_TYPE_SP;
4817
4818 switch (intel_crtc->config.pipe_bpp) {
4819 case 18:
4820 pipeconf |= PIPECONF_6BPC;
4821 break;
4822 case 24:
4823 pipeconf |= PIPECONF_8BPC;
4824 break;
4825 case 30:
4826 pipeconf |= PIPECONF_10BPC;
4827 break;
4828 default:
4829 /* Case prevented by intel_choose_pipe_bpp_dither. */
4830 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01004831 }
4832 }
4833
4834 if (HAS_PIPE_CXSR(dev)) {
4835 if (intel_crtc->lowfreq_avail) {
4836 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4837 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4838 } else {
4839 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4840 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4841 }
4842 }
4843
4844 pipeconf &= ~PIPECONF_INTERLACE_MASK;
4845 if (!IS_GEN2(dev) &&
4846 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4847 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4848 else
4849 pipeconf |= PIPECONF_PROGRESSIVE;
4850
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03004851 if (IS_VALLEYVIEW(dev)) {
4852 if (intel_crtc->config.limited_color_range)
4853 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
4854 else
4855 pipeconf &= ~PIPECONF_COLOR_RANGE_SELECT;
4856 }
4857
Daniel Vetter84b046f2013-02-19 18:48:54 +01004858 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4859 POSTING_READ(PIPECONF(intel_crtc->pipe));
4860}
4861
Eric Anholtf564048e2011-03-30 13:01:02 -07004862static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004863 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004864 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004865{
4866 struct drm_device *dev = crtc->dev;
4867 struct drm_i915_private *dev_priv = dev->dev_private;
4868 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004869 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08004870 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004871 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004872 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004873 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004874 u32 dspcntr;
Daniel Vettera16af722013-04-30 14:01:44 +02004875 bool ok, has_reduced_clock = false;
4876 bool is_lvds = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004877 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004878 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004879 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004880
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004881 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004882 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004883 case INTEL_OUTPUT_LVDS:
4884 is_lvds = true;
4885 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004886 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004887
Eric Anholtc751ce42010-03-25 11:48:48 -07004888 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004889 }
4890
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004891 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004892
Ma Lingd4906092009-03-18 20:13:27 +08004893 /*
4894 * Returns a set of divisors for the desired target clock with the given
4895 * refclk, or FALSE. The returned values represent the clock equation:
4896 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4897 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004898 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02004899 ok = dev_priv->display.find_dpll(limit, crtc,
4900 intel_crtc->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004901 refclk, NULL, &clock);
4902 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004903 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004904 return -EINVAL;
4905 }
4906
4907 /* Ensure that the cursor is valid for the new mode before changing... */
4908 intel_crtc_update_cursor(crtc, true);
4909
4910 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004911 /*
4912 * Ensure we match the reduced clock's P to the target clock.
4913 * If the clocks don't match, we can't switch the display clock
4914 * by using the FP0/FP1. In such case we will disable the LVDS
4915 * downclock feature.
4916 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02004917 has_reduced_clock =
4918 dev_priv->display.find_dpll(limit, crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004919 dev_priv->lvds_downclock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02004920 refclk, &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004921 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004922 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004923 /* Compat-code for transition, will disappear. */
4924 if (!intel_crtc->config.clock_set) {
4925 intel_crtc->config.dpll.n = clock.n;
4926 intel_crtc->config.dpll.m1 = clock.m1;
4927 intel_crtc->config.dpll.m2 = clock.m2;
4928 intel_crtc->config.dpll.p1 = clock.p1;
4929 intel_crtc->config.dpll.p2 = clock.p2;
4930 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004931
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004932 if (IS_GEN2(dev))
Daniel Vetter8a654f32013-06-01 17:16:22 +02004933 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304934 has_reduced_clock ? &reduced_clock : NULL,
4935 num_connectors);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004936 else if (IS_VALLEYVIEW(dev))
Daniel Vetterf47709a2013-03-28 10:42:02 +01004937 vlv_update_pll(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004938 else
Daniel Vetterf47709a2013-03-28 10:42:02 +01004939 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004940 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07004941 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004942
Eric Anholtf564048e2011-03-30 13:01:02 -07004943 /* Set up the display plane register */
4944 dspcntr = DISPPLANE_GAMMA_ENABLE;
4945
Jesse Barnesda6ecc52013-03-08 10:46:00 -08004946 if (!IS_VALLEYVIEW(dev)) {
4947 if (pipe == 0)
4948 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4949 else
4950 dspcntr |= DISPPLANE_SEL_PIPE_B;
4951 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004952
Daniel Vetter8a654f32013-06-01 17:16:22 +02004953 intel_set_pipe_timings(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004954
4955 /* pipesrc and dspsize control the size that is scaled from,
4956 * which should always be the user's requested size.
4957 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004958 I915_WRITE(DSPSIZE(plane),
4959 ((mode->vdisplay - 1) << 16) |
4960 (mode->hdisplay - 1));
4961 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004962
Daniel Vetter84b046f2013-02-19 18:48:54 +01004963 i9xx_set_pipeconf(intel_crtc);
4964
Eric Anholtf564048e2011-03-30 13:01:02 -07004965 I915_WRITE(DSPCNTR(plane), dspcntr);
4966 POSTING_READ(DSPCNTR(plane));
4967
Daniel Vetter94352cf2012-07-05 22:51:56 +02004968 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004969
4970 intel_update_watermarks(dev);
4971
Eric Anholtf564048e2011-03-30 13:01:02 -07004972 return ret;
4973}
4974
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02004975static void i9xx_get_pfit_config(struct intel_crtc *crtc,
4976 struct intel_crtc_config *pipe_config)
4977{
4978 struct drm_device *dev = crtc->base.dev;
4979 struct drm_i915_private *dev_priv = dev->dev_private;
4980 uint32_t tmp;
4981
4982 tmp = I915_READ(PFIT_CONTROL);
4983
4984 if (INTEL_INFO(dev)->gen < 4) {
4985 if (crtc->pipe != PIPE_B)
4986 return;
4987
4988 /* gen2/3 store dither state in pfit control, needs to match */
4989 pipe_config->gmch_pfit.control = tmp & PANEL_8TO6_DITHER_ENABLE;
4990 } else {
4991 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4992 return;
4993 }
4994
4995 if (!(tmp & PFIT_ENABLE))
4996 return;
4997
4998 pipe_config->gmch_pfit.control = I915_READ(PFIT_CONTROL);
4999 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5000 if (INTEL_INFO(dev)->gen < 5)
5001 pipe_config->gmch_pfit.lvds_border_bits =
5002 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5003}
5004
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005005static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5006 struct intel_crtc_config *pipe_config)
5007{
5008 struct drm_device *dev = crtc->base.dev;
5009 struct drm_i915_private *dev_priv = dev->dev_private;
5010 uint32_t tmp;
5011
Daniel Vettereccb1402013-05-22 00:50:22 +02005012 pipe_config->cpu_transcoder = crtc->pipe;
5013
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005014 tmp = I915_READ(PIPECONF(crtc->pipe));
5015 if (!(tmp & PIPECONF_ENABLE))
5016 return false;
5017
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005018 intel_get_pipe_timings(crtc, pipe_config);
5019
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005020 i9xx_get_pfit_config(crtc, pipe_config);
5021
Daniel Vetter6c49f242013-06-06 12:45:25 +02005022 if (INTEL_INFO(dev)->gen >= 4) {
5023 tmp = I915_READ(DPLL_MD(crtc->pipe));
5024 pipe_config->pixel_multiplier =
5025 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5026 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
5027 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5028 tmp = I915_READ(DPLL(crtc->pipe));
5029 pipe_config->pixel_multiplier =
5030 ((tmp & SDVO_MULTIPLIER_MASK)
5031 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5032 } else {
5033 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5034 * port and will be fixed up in the encoder->get_config
5035 * function. */
5036 pipe_config->pixel_multiplier = 1;
5037 }
5038
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005039 return true;
5040}
5041
Paulo Zanonidde86e22012-12-01 12:04:25 -02005042static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005043{
5044 struct drm_i915_private *dev_priv = dev->dev_private;
5045 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005046 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005047 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005048 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005049 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005050 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005051 bool has_ck505 = false;
5052 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005053
5054 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005055 list_for_each_entry(encoder, &mode_config->encoder_list,
5056 base.head) {
5057 switch (encoder->type) {
5058 case INTEL_OUTPUT_LVDS:
5059 has_panel = true;
5060 has_lvds = true;
5061 break;
5062 case INTEL_OUTPUT_EDP:
5063 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03005064 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07005065 has_cpu_edp = true;
5066 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005067 }
5068 }
5069
Keith Packard99eb6a02011-09-26 14:29:12 -07005070 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005071 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07005072 can_ssc = has_ck505;
5073 } else {
5074 has_ck505 = false;
5075 can_ssc = true;
5076 }
5077
Imre Deak2de69052013-05-08 13:14:04 +03005078 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5079 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005080
5081 /* Ironlake: try to setup display ref clock before DPLL
5082 * enabling. This is only under driver's control after
5083 * PCH B stepping, previous chipset stepping should be
5084 * ignoring this setting.
5085 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005086 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005087
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005088 /* As we must carefully and slowly disable/enable each source in turn,
5089 * compute the final state we want first and check if we need to
5090 * make any changes at all.
5091 */
5092 final = val;
5093 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07005094 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005095 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07005096 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005097 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5098
5099 final &= ~DREF_SSC_SOURCE_MASK;
5100 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5101 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005102
Keith Packard199e5d72011-09-22 12:01:57 -07005103 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005104 final |= DREF_SSC_SOURCE_ENABLE;
5105
5106 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5107 final |= DREF_SSC1_ENABLE;
5108
5109 if (has_cpu_edp) {
5110 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5111 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5112 else
5113 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5114 } else
5115 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5116 } else {
5117 final |= DREF_SSC_SOURCE_DISABLE;
5118 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5119 }
5120
5121 if (final == val)
5122 return;
5123
5124 /* Always enable nonspread source */
5125 val &= ~DREF_NONSPREAD_SOURCE_MASK;
5126
5127 if (has_ck505)
5128 val |= DREF_NONSPREAD_CK505_ENABLE;
5129 else
5130 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5131
5132 if (has_panel) {
5133 val &= ~DREF_SSC_SOURCE_MASK;
5134 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005135
Keith Packard199e5d72011-09-22 12:01:57 -07005136 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005137 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005138 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005139 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02005140 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005141 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005142
5143 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005144 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005145 POSTING_READ(PCH_DREF_CONTROL);
5146 udelay(200);
5147
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005148 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005149
5150 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005151 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005152 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005153 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005154 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005155 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005156 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005157 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005158 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005159 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005160
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005161 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005162 POSTING_READ(PCH_DREF_CONTROL);
5163 udelay(200);
5164 } else {
5165 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5166
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005167 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005168
5169 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005170 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005171
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005172 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005173 POSTING_READ(PCH_DREF_CONTROL);
5174 udelay(200);
5175
5176 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005177 val &= ~DREF_SSC_SOURCE_MASK;
5178 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005179
5180 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005181 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005182
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005183 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005184 POSTING_READ(PCH_DREF_CONTROL);
5185 udelay(200);
5186 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005187
5188 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005189}
5190
Paulo Zanonidde86e22012-12-01 12:04:25 -02005191/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
5192static void lpt_init_pch_refclk(struct drm_device *dev)
5193{
5194 struct drm_i915_private *dev_priv = dev->dev_private;
5195 struct drm_mode_config *mode_config = &dev->mode_config;
5196 struct intel_encoder *encoder;
5197 bool has_vga = false;
5198 bool is_sdv = false;
5199 u32 tmp;
5200
5201 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5202 switch (encoder->type) {
5203 case INTEL_OUTPUT_ANALOG:
5204 has_vga = true;
5205 break;
5206 }
5207 }
5208
5209 if (!has_vga)
5210 return;
5211
Daniel Vetterc00db242013-01-22 15:33:27 +01005212 mutex_lock(&dev_priv->dpio_lock);
5213
Paulo Zanonidde86e22012-12-01 12:04:25 -02005214 /* XXX: Rip out SDV support once Haswell ships for real. */
5215 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
5216 is_sdv = true;
5217
5218 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5219 tmp &= ~SBI_SSCCTL_DISABLE;
5220 tmp |= SBI_SSCCTL_PATHALT;
5221 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5222
5223 udelay(24);
5224
5225 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5226 tmp &= ~SBI_SSCCTL_PATHALT;
5227 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5228
5229 if (!is_sdv) {
5230 tmp = I915_READ(SOUTH_CHICKEN2);
5231 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5232 I915_WRITE(SOUTH_CHICKEN2, tmp);
5233
5234 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5235 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5236 DRM_ERROR("FDI mPHY reset assert timeout\n");
5237
5238 tmp = I915_READ(SOUTH_CHICKEN2);
5239 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5240 I915_WRITE(SOUTH_CHICKEN2, tmp);
5241
5242 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5243 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
5244 100))
5245 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5246 }
5247
5248 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5249 tmp &= ~(0xFF << 24);
5250 tmp |= (0x12 << 24);
5251 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5252
Paulo Zanonidde86e22012-12-01 12:04:25 -02005253 if (is_sdv) {
5254 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5255 tmp |= 0x7FFF;
5256 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5257 }
5258
5259 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5260 tmp |= (1 << 11);
5261 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5262
5263 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5264 tmp |= (1 << 11);
5265 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5266
5267 if (is_sdv) {
5268 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5269 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5270 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5271
5272 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5273 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5274 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5275
5276 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5277 tmp |= (0x3F << 8);
5278 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5279
5280 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5281 tmp |= (0x3F << 8);
5282 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5283 }
5284
5285 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5286 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5287 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5288
5289 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5290 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5291 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5292
5293 if (!is_sdv) {
5294 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5295 tmp &= ~(7 << 13);
5296 tmp |= (5 << 13);
5297 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5298
5299 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5300 tmp &= ~(7 << 13);
5301 tmp |= (5 << 13);
5302 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5303 }
5304
5305 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5306 tmp &= ~0xFF;
5307 tmp |= 0x1C;
5308 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5309
5310 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5311 tmp &= ~0xFF;
5312 tmp |= 0x1C;
5313 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5314
5315 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5316 tmp &= ~(0xFF << 16);
5317 tmp |= (0x1C << 16);
5318 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5319
5320 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5321 tmp &= ~(0xFF << 16);
5322 tmp |= (0x1C << 16);
5323 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5324
5325 if (!is_sdv) {
5326 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5327 tmp |= (1 << 27);
5328 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5329
5330 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5331 tmp |= (1 << 27);
5332 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5333
5334 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5335 tmp &= ~(0xF << 28);
5336 tmp |= (4 << 28);
5337 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5338
5339 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5340 tmp &= ~(0xF << 28);
5341 tmp |= (4 << 28);
5342 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5343 }
5344
5345 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5346 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5347 tmp |= SBI_DBUFF0_ENABLE;
5348 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005349
5350 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005351}
5352
5353/*
5354 * Initialize reference clocks when the driver loads
5355 */
5356void intel_init_pch_refclk(struct drm_device *dev)
5357{
5358 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5359 ironlake_init_pch_refclk(dev);
5360 else if (HAS_PCH_LPT(dev))
5361 lpt_init_pch_refclk(dev);
5362}
5363
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005364static int ironlake_get_refclk(struct drm_crtc *crtc)
5365{
5366 struct drm_device *dev = crtc->dev;
5367 struct drm_i915_private *dev_priv = dev->dev_private;
5368 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005369 int num_connectors = 0;
5370 bool is_lvds = false;
5371
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005372 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005373 switch (encoder->type) {
5374 case INTEL_OUTPUT_LVDS:
5375 is_lvds = true;
5376 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005377 }
5378 num_connectors++;
5379 }
5380
5381 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5382 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005383 dev_priv->vbt.lvds_ssc_freq);
5384 return dev_priv->vbt.lvds_ssc_freq * 1000;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005385 }
5386
5387 return 120000;
5388}
5389
Daniel Vetter6ff93602013-04-19 11:24:36 +02005390static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03005391{
5392 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5394 int pipe = intel_crtc->pipe;
5395 uint32_t val;
5396
5397 val = I915_READ(PIPECONF(pipe));
5398
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005399 val &= ~PIPECONF_BPC_MASK;
Daniel Vetter965e0c42013-03-27 00:44:57 +01005400 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005401 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005402 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005403 break;
5404 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005405 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005406 break;
5407 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005408 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005409 break;
5410 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005411 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005412 break;
5413 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005414 /* Case prevented by intel_choose_pipe_bpp_dither. */
5415 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005416 }
5417
5418 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
Daniel Vetterd8b32242013-04-25 17:54:44 +02005419 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03005420 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5421
5422 val &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetter6ff93602013-04-19 11:24:36 +02005423 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03005424 val |= PIPECONF_INTERLACED_ILK;
5425 else
5426 val |= PIPECONF_PROGRESSIVE;
5427
Daniel Vetter50f3b012013-03-27 00:44:56 +01005428 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005429 val |= PIPECONF_COLOR_RANGE_SELECT;
5430 else
5431 val &= ~PIPECONF_COLOR_RANGE_SELECT;
5432
Paulo Zanonic8203562012-09-12 10:06:29 -03005433 I915_WRITE(PIPECONF(pipe), val);
5434 POSTING_READ(PIPECONF(pipe));
5435}
5436
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005437/*
5438 * Set up the pipe CSC unit.
5439 *
5440 * Currently only full range RGB to limited range RGB conversion
5441 * is supported, but eventually this should handle various
5442 * RGB<->YCbCr scenarios as well.
5443 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005444static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005445{
5446 struct drm_device *dev = crtc->dev;
5447 struct drm_i915_private *dev_priv = dev->dev_private;
5448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5449 int pipe = intel_crtc->pipe;
5450 uint16_t coeff = 0x7800; /* 1.0 */
5451
5452 /*
5453 * TODO: Check what kind of values actually come out of the pipe
5454 * with these coeff/postoff values and adjust to get the best
5455 * accuracy. Perhaps we even need to take the bpc value into
5456 * consideration.
5457 */
5458
Daniel Vetter50f3b012013-03-27 00:44:56 +01005459 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005460 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5461
5462 /*
5463 * GY/GU and RY/RU should be the other way around according
5464 * to BSpec, but reality doesn't agree. Just set them up in
5465 * a way that results in the correct picture.
5466 */
5467 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5468 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5469
5470 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5471 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5472
5473 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5474 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5475
5476 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5477 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5478 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5479
5480 if (INTEL_INFO(dev)->gen > 6) {
5481 uint16_t postoff = 0;
5482
Daniel Vetter50f3b012013-03-27 00:44:56 +01005483 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005484 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5485
5486 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5487 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5488 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5489
5490 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5491 } else {
5492 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5493
Daniel Vetter50f3b012013-03-27 00:44:56 +01005494 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005495 mode |= CSC_BLACK_SCREEN_OFFSET;
5496
5497 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5498 }
5499}
5500
Daniel Vetter6ff93602013-04-19 11:24:36 +02005501static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005502{
5503 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005505 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005506 uint32_t val;
5507
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005508 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005509
5510 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
Daniel Vetterd8b32242013-04-25 17:54:44 +02005511 if (intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005512 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5513
5514 val &= ~PIPECONF_INTERLACE_MASK_HSW;
Daniel Vetter6ff93602013-04-19 11:24:36 +02005515 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005516 val |= PIPECONF_INTERLACED_ILK;
5517 else
5518 val |= PIPECONF_PROGRESSIVE;
5519
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005520 I915_WRITE(PIPECONF(cpu_transcoder), val);
5521 POSTING_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005522}
5523
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005524static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005525 intel_clock_t *clock,
5526 bool *has_reduced_clock,
5527 intel_clock_t *reduced_clock)
5528{
5529 struct drm_device *dev = crtc->dev;
5530 struct drm_i915_private *dev_priv = dev->dev_private;
5531 struct intel_encoder *intel_encoder;
5532 int refclk;
5533 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02005534 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005535
5536 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5537 switch (intel_encoder->type) {
5538 case INTEL_OUTPUT_LVDS:
5539 is_lvds = true;
5540 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005541 }
5542 }
5543
5544 refclk = ironlake_get_refclk(crtc);
5545
5546 /*
5547 * Returns a set of divisors for the desired target clock with the given
5548 * refclk, or FALSE. The returned values represent the clock equation:
5549 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5550 */
5551 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02005552 ret = dev_priv->display.find_dpll(limit, crtc,
5553 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02005554 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005555 if (!ret)
5556 return false;
5557
5558 if (is_lvds && dev_priv->lvds_downclock_avail) {
5559 /*
5560 * Ensure we match the reduced clock's P to the target clock.
5561 * If the clocks don't match, we can't switch the display clock
5562 * by using the FP0/FP1. In such case we will disable the LVDS
5563 * downclock feature.
5564 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02005565 *has_reduced_clock =
5566 dev_priv->display.find_dpll(limit, crtc,
5567 dev_priv->lvds_downclock,
5568 refclk, clock,
5569 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005570 }
5571
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005572 return true;
5573}
5574
Daniel Vetter01a415f2012-10-27 15:58:40 +02005575static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5576{
5577 struct drm_i915_private *dev_priv = dev->dev_private;
5578 uint32_t temp;
5579
5580 temp = I915_READ(SOUTH_CHICKEN1);
5581 if (temp & FDI_BC_BIFURCATION_SELECT)
5582 return;
5583
5584 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5585 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5586
5587 temp |= FDI_BC_BIFURCATION_SELECT;
5588 DRM_DEBUG_KMS("enabling fdi C rx\n");
5589 I915_WRITE(SOUTH_CHICKEN1, temp);
5590 POSTING_READ(SOUTH_CHICKEN1);
5591}
5592
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005593static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
5594{
5595 struct drm_device *dev = intel_crtc->base.dev;
5596 struct drm_i915_private *dev_priv = dev->dev_private;
5597
5598 switch (intel_crtc->pipe) {
5599 case PIPE_A:
5600 break;
5601 case PIPE_B:
5602 if (intel_crtc->config.fdi_lanes > 2)
5603 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5604 else
5605 cpt_enable_fdi_bc_bifurcation(dev);
5606
5607 break;
5608 case PIPE_C:
Daniel Vetter01a415f2012-10-27 15:58:40 +02005609 cpt_enable_fdi_bc_bifurcation(dev);
5610
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005611 break;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005612 default:
5613 BUG();
5614 }
5615}
5616
Paulo Zanonid4b19312012-11-29 11:29:32 -02005617int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5618{
5619 /*
5620 * Account for spread spectrum to avoid
5621 * oversubscribing the link. Max center spread
5622 * is 2.5%; use 5% for safety's sake.
5623 */
5624 u32 bps = target_clock * bpp * 21 / 20;
5625 return bps / (link_bw * 8) + 1;
5626}
5627
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005628static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
5629{
5630 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
5631}
5632
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005633static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005634 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005635 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005636{
5637 struct drm_crtc *crtc = &intel_crtc->base;
5638 struct drm_device *dev = crtc->dev;
5639 struct drm_i915_private *dev_priv = dev->dev_private;
5640 struct intel_encoder *intel_encoder;
5641 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005642 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02005643 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005644
5645 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5646 switch (intel_encoder->type) {
5647 case INTEL_OUTPUT_LVDS:
5648 is_lvds = true;
5649 break;
5650 case INTEL_OUTPUT_SDVO:
5651 case INTEL_OUTPUT_HDMI:
5652 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005653 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005654 }
5655
5656 num_connectors++;
5657 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005658
Chris Wilsonc1858122010-12-03 21:35:48 +00005659 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005660 factor = 21;
5661 if (is_lvds) {
5662 if ((intel_panel_use_ssc(dev_priv) &&
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005663 dev_priv->vbt.lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005664 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005665 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02005666 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07005667 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005668
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005669 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005670 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005671
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005672 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5673 *fp2 |= FP_CB_TUNE;
5674
Chris Wilson5eddb702010-09-11 13:48:45 +01005675 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005676
Eric Anholta07d6782011-03-30 13:01:08 -07005677 if (is_lvds)
5678 dpll |= DPLLB_MODE_LVDS;
5679 else
5680 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005681
Daniel Vetteref1b4602013-06-01 17:17:04 +02005682 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5683 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005684
5685 if (is_sdvo)
5686 dpll |= DPLL_DVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02005687 if (intel_crtc->config.has_dp_encoder)
Eric Anholta07d6782011-03-30 13:01:08 -07005688 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005689
Eric Anholta07d6782011-03-30 13:01:08 -07005690 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005691 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005692 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005693 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005694
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005695 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005696 case 5:
5697 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5698 break;
5699 case 7:
5700 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5701 break;
5702 case 10:
5703 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5704 break;
5705 case 14:
5706 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5707 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005708 }
5709
Daniel Vetterb4c09f32013-04-30 14:01:42 +02005710 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005711 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005712 else
5713 dpll |= PLL_REF_INPUT_DREFCLK;
5714
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005715 return dpll;
5716}
5717
Jesse Barnes79e53942008-11-07 14:24:08 -08005718static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005719 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005720 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005721{
5722 struct drm_device *dev = crtc->dev;
5723 struct drm_i915_private *dev_priv = dev->dev_private;
5724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5725 int pipe = intel_crtc->pipe;
5726 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005727 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005728 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005729 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005730 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005731 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005732 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005733 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005734
5735 for_each_encoder_on_crtc(dev, crtc, encoder) {
5736 switch (encoder->type) {
5737 case INTEL_OUTPUT_LVDS:
5738 is_lvds = true;
5739 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005740 }
5741
5742 num_connectors++;
5743 }
5744
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005745 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5746 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5747
Daniel Vetterff9a6752013-06-01 17:16:21 +02005748 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005749 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02005750 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005751 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5752 return -EINVAL;
5753 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01005754 /* Compat-code for transition, will disappear. */
5755 if (!intel_crtc->config.clock_set) {
5756 intel_crtc->config.dpll.n = clock.n;
5757 intel_crtc->config.dpll.m1 = clock.m1;
5758 intel_crtc->config.dpll.m2 = clock.m2;
5759 intel_crtc->config.dpll.p1 = clock.p1;
5760 intel_crtc->config.dpll.p2 = clock.p2;
5761 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005762
5763 /* Ensure that the cursor is valid for the new mode before changing... */
5764 intel_crtc_update_cursor(crtc, true);
5765
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005766 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01005767 if (intel_crtc->config.has_pch_encoder) {
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005768 struct intel_shared_dpll *pll;
Chris Wilson5eddb702010-09-11 13:48:45 +01005769
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005770 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005771 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005772 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005773
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005774 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005775 &fp, &reduced_clock,
5776 has_reduced_clock ? &fp2 : NULL);
5777
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005778 pll = intel_get_shared_dpll(intel_crtc, dpll, fp);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005779 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005780 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5781 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07005782 return -EINVAL;
5783 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005784 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005785 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005786
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005787 if (intel_crtc->config.has_dp_encoder)
5788 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005789
Daniel Vetterdafd2262012-11-26 17:22:07 +01005790 for_each_encoder_on_crtc(dev, crtc, encoder)
5791 if (encoder->pre_pll_enable)
5792 encoder->pre_pll_enable(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08005793
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005794 if (intel_crtc->shared_dpll) {
5795 I915_WRITE(intel_crtc->shared_dpll->pll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005796
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005797 /* Wait for the clocks to stabilize. */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005798 POSTING_READ(intel_crtc->shared_dpll->pll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005799 udelay(150);
5800
Eric Anholt8febb292011-03-30 13:01:07 -07005801 /* The pixel multiplier can only be updated once the
5802 * DPLL is enabled and the clocks are stable.
5803 *
5804 * So write it again.
5805 */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005806 I915_WRITE(intel_crtc->shared_dpll->pll_reg, dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08005807 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005808
Chris Wilson5eddb702010-09-11 13:48:45 +01005809 intel_crtc->lowfreq_avail = false;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005810 if (intel_crtc->shared_dpll) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07005811 if (is_lvds && has_reduced_clock && i915_powersave) {
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005812 I915_WRITE(intel_crtc->shared_dpll->fp1_reg, fp2);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005813 intel_crtc->lowfreq_avail = true;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005814 } else {
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005815 I915_WRITE(intel_crtc->shared_dpll->fp1_reg, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07005816 }
5817 }
5818
Daniel Vetter8a654f32013-06-01 17:16:22 +02005819 intel_set_pipe_timings(intel_crtc);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005820
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005821 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005822 intel_cpu_transcoder_set_m_n(intel_crtc,
5823 &intel_crtc->config.fdi_m_n);
5824 }
Chris Wilson5eddb702010-09-11 13:48:45 +01005825
Daniel Vetterebfd86f2013-04-19 11:24:44 +02005826 if (IS_IVYBRIDGE(dev))
5827 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005828
Daniel Vetter6ff93602013-04-19 11:24:36 +02005829 ironlake_set_pipeconf(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005830
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005831 /* Set up the display plane register */
5832 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005833 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005834
Daniel Vetter94352cf2012-07-05 22:51:56 +02005835 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005836
5837 intel_update_watermarks(dev);
5838
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005839 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005840}
5841
Daniel Vetter72419202013-04-04 13:28:53 +02005842static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5843 struct intel_crtc_config *pipe_config)
5844{
5845 struct drm_device *dev = crtc->base.dev;
5846 struct drm_i915_private *dev_priv = dev->dev_private;
5847 enum transcoder transcoder = pipe_config->cpu_transcoder;
5848
5849 pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
5850 pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
5851 pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
5852 & ~TU_SIZE_MASK;
5853 pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
5854 pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
5855 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5856}
5857
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005858static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5859 struct intel_crtc_config *pipe_config)
5860{
5861 struct drm_device *dev = crtc->base.dev;
5862 struct drm_i915_private *dev_priv = dev->dev_private;
5863 uint32_t tmp;
5864
5865 tmp = I915_READ(PF_CTL(crtc->pipe));
5866
5867 if (tmp & PF_ENABLE) {
5868 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
5869 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02005870
5871 /* We currently do not free assignements of panel fitters on
5872 * ivb/hsw (since we don't use the higher upscaling modes which
5873 * differentiates them) so just WARN about this case for now. */
5874 if (IS_GEN7(dev)) {
5875 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
5876 PF_PIPE_SEL_IVB(crtc->pipe));
5877 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005878 }
5879}
5880
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005881static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5882 struct intel_crtc_config *pipe_config)
5883{
5884 struct drm_device *dev = crtc->base.dev;
5885 struct drm_i915_private *dev_priv = dev->dev_private;
5886 uint32_t tmp;
5887
Daniel Vettereccb1402013-05-22 00:50:22 +02005888 pipe_config->cpu_transcoder = crtc->pipe;
5889
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005890 tmp = I915_READ(PIPECONF(crtc->pipe));
5891 if (!(tmp & PIPECONF_ENABLE))
5892 return false;
5893
Daniel Vetterab9412b2013-05-03 11:49:46 +02005894 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01005895 pipe_config->has_pch_encoder = true;
5896
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005897 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
5898 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5899 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02005900
5901 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02005902
5903 /* XXX: Can't properly read out the pch dpll pixel multiplier
5904 * since we don't have state tracking for pch clocks yet. */
5905 pipe_config->pixel_multiplier = 1;
5906 } else {
5907 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02005908 }
5909
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005910 intel_get_pipe_timings(crtc, pipe_config);
5911
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005912 ironlake_get_pfit_config(crtc, pipe_config);
5913
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005914 return true;
5915}
5916
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005917static void haswell_modeset_global_resources(struct drm_device *dev)
5918{
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005919 bool enable = false;
5920 struct intel_crtc *crtc;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005921
5922 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
Daniel Vettere7a639c2013-05-31 17:49:17 +02005923 if (!crtc->base.enabled)
5924 continue;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005925
Daniel Vettere7a639c2013-05-31 17:49:17 +02005926 if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.size ||
5927 crtc->config.cpu_transcoder != TRANSCODER_EDP)
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005928 enable = true;
5929 }
5930
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005931 intel_set_power_well(dev, enable);
5932}
5933
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005934static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005935 int x, int y,
5936 struct drm_framebuffer *fb)
5937{
5938 struct drm_device *dev = crtc->dev;
5939 struct drm_i915_private *dev_priv = dev->dev_private;
5940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005941 int plane = intel_crtc->plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005942 int ret;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005943
Daniel Vetterff9a6752013-06-01 17:16:21 +02005944 if (!intel_ddi_pll_mode_set(crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005945 return -EINVAL;
5946
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005947 /* Ensure that the cursor is valid for the new mode before changing... */
5948 intel_crtc_update_cursor(crtc, true);
5949
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005950 if (intel_crtc->config.has_dp_encoder)
5951 intel_dp_set_m_n(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005952
5953 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005954
Daniel Vetter8a654f32013-06-01 17:16:22 +02005955 intel_set_pipe_timings(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005956
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005957 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterca3a0ff2013-02-14 16:54:22 +01005958 intel_cpu_transcoder_set_m_n(intel_crtc,
5959 &intel_crtc->config.fdi_m_n);
5960 }
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005961
Daniel Vetter6ff93602013-04-19 11:24:36 +02005962 haswell_set_pipeconf(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005963
Daniel Vetter50f3b012013-03-27 00:44:56 +01005964 intel_set_pipe_csc(crtc);
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005965
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005966 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005967 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005968 POSTING_READ(DSPCNTR(plane));
5969
5970 ret = intel_pipe_set_base(crtc, x, y, fb);
5971
5972 intel_update_watermarks(dev);
5973
Jesse Barnes79e53942008-11-07 14:24:08 -08005974 return ret;
5975}
5976
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005977static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5978 struct intel_crtc_config *pipe_config)
5979{
5980 struct drm_device *dev = crtc->base.dev;
5981 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02005982 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005983 uint32_t tmp;
5984
Daniel Vettereccb1402013-05-22 00:50:22 +02005985 pipe_config->cpu_transcoder = crtc->pipe;
5986 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
5987 if (tmp & TRANS_DDI_FUNC_ENABLE) {
5988 enum pipe trans_edp_pipe;
5989 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
5990 default:
5991 WARN(1, "unknown pipe linked to edp transcoder\n");
5992 case TRANS_DDI_EDP_INPUT_A_ONOFF:
5993 case TRANS_DDI_EDP_INPUT_A_ON:
5994 trans_edp_pipe = PIPE_A;
5995 break;
5996 case TRANS_DDI_EDP_INPUT_B_ONOFF:
5997 trans_edp_pipe = PIPE_B;
5998 break;
5999 case TRANS_DDI_EDP_INPUT_C_ONOFF:
6000 trans_edp_pipe = PIPE_C;
6001 break;
6002 }
6003
6004 if (trans_edp_pipe == crtc->pipe)
6005 pipe_config->cpu_transcoder = TRANSCODER_EDP;
6006 }
6007
Paulo Zanonib97186f2013-05-03 12:15:36 -03006008 if (!intel_display_power_enabled(dev,
Daniel Vettereccb1402013-05-22 00:50:22 +02006009 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03006010 return false;
6011
Daniel Vettereccb1402013-05-22 00:50:22 +02006012 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006013 if (!(tmp & PIPECONF_ENABLE))
6014 return false;
6015
Daniel Vetter88adfff2013-03-28 10:42:01 +01006016 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03006017 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01006018 * DDI E. So just check whether this pipe is wired to DDI E and whether
6019 * the PCH transcoder is on.
6020 */
Daniel Vettereccb1402013-05-22 00:50:22 +02006021 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01006022 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02006023 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01006024 pipe_config->has_pch_encoder = true;
6025
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006026 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
6027 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6028 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02006029
6030 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02006031 }
6032
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006033 intel_get_pipe_timings(crtc, pipe_config);
6034
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006035 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
6036 if (intel_display_power_enabled(dev, pfit_domain))
6037 ironlake_get_pfit_config(crtc, pipe_config);
6038
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006039 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
6040 (I915_READ(IPS_CTL) & IPS_ENABLE);
6041
Daniel Vetter6c49f242013-06-06 12:45:25 +02006042 pipe_config->pixel_multiplier = 1;
6043
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006044 return true;
6045}
6046
Eric Anholtf564048e2011-03-30 13:01:02 -07006047static int intel_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07006048 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006049 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07006050{
6051 struct drm_device *dev = crtc->dev;
6052 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01006053 struct drm_encoder_helper_funcs *encoder_funcs;
6054 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07006055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006056 struct drm_display_mode *adjusted_mode =
6057 &intel_crtc->config.adjusted_mode;
6058 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07006059 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006060 int ret;
6061
Eric Anholt0b701d22011-03-30 13:01:03 -07006062 drm_vblank_pre_modeset(dev, pipe);
6063
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006064 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6065
Jesse Barnes79e53942008-11-07 14:24:08 -08006066 drm_vblank_post_modeset(dev, pipe);
6067
Daniel Vetter9256aa12012-10-31 19:26:13 +01006068 if (ret != 0)
6069 return ret;
6070
6071 for_each_encoder_on_crtc(dev, crtc, encoder) {
6072 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6073 encoder->base.base.id,
6074 drm_get_encoder_name(&encoder->base),
6075 mode->base.id, mode->name);
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006076 if (encoder->mode_set) {
6077 encoder->mode_set(encoder);
6078 } else {
6079 encoder_funcs = encoder->base.helper_private;
6080 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6081 }
Daniel Vetter9256aa12012-10-31 19:26:13 +01006082 }
6083
6084 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006085}
6086
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006087static bool intel_eld_uptodate(struct drm_connector *connector,
6088 int reg_eldv, uint32_t bits_eldv,
6089 int reg_elda, uint32_t bits_elda,
6090 int reg_edid)
6091{
6092 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6093 uint8_t *eld = connector->eld;
6094 uint32_t i;
6095
6096 i = I915_READ(reg_eldv);
6097 i &= bits_eldv;
6098
6099 if (!eld[0])
6100 return !i;
6101
6102 if (!i)
6103 return false;
6104
6105 i = I915_READ(reg_elda);
6106 i &= ~bits_elda;
6107 I915_WRITE(reg_elda, i);
6108
6109 for (i = 0; i < eld[2]; i++)
6110 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6111 return false;
6112
6113 return true;
6114}
6115
Wu Fengguange0dac652011-09-05 14:25:34 +08006116static void g4x_write_eld(struct drm_connector *connector,
6117 struct drm_crtc *crtc)
6118{
6119 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6120 uint8_t *eld = connector->eld;
6121 uint32_t eldv;
6122 uint32_t len;
6123 uint32_t i;
6124
6125 i = I915_READ(G4X_AUD_VID_DID);
6126
6127 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6128 eldv = G4X_ELDV_DEVCL_DEVBLC;
6129 else
6130 eldv = G4X_ELDV_DEVCTG;
6131
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006132 if (intel_eld_uptodate(connector,
6133 G4X_AUD_CNTL_ST, eldv,
6134 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6135 G4X_HDMIW_HDMIEDID))
6136 return;
6137
Wu Fengguange0dac652011-09-05 14:25:34 +08006138 i = I915_READ(G4X_AUD_CNTL_ST);
6139 i &= ~(eldv | G4X_ELD_ADDR);
6140 len = (i >> 9) & 0x1f; /* ELD buffer size */
6141 I915_WRITE(G4X_AUD_CNTL_ST, i);
6142
6143 if (!eld[0])
6144 return;
6145
6146 len = min_t(uint8_t, eld[2], len);
6147 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6148 for (i = 0; i < len; i++)
6149 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6150
6151 i = I915_READ(G4X_AUD_CNTL_ST);
6152 i |= eldv;
6153 I915_WRITE(G4X_AUD_CNTL_ST, i);
6154}
6155
Wang Xingchao83358c852012-08-16 22:43:37 +08006156static void haswell_write_eld(struct drm_connector *connector,
6157 struct drm_crtc *crtc)
6158{
6159 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6160 uint8_t *eld = connector->eld;
6161 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006162 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006163 uint32_t eldv;
6164 uint32_t i;
6165 int len;
6166 int pipe = to_intel_crtc(crtc)->pipe;
6167 int tmp;
6168
6169 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6170 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6171 int aud_config = HSW_AUD_CFG(pipe);
6172 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6173
6174
6175 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6176
6177 /* Audio output enable */
6178 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6179 tmp = I915_READ(aud_cntrl_st2);
6180 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6181 I915_WRITE(aud_cntrl_st2, tmp);
6182
6183 /* Wait for 1 vertical blank */
6184 intel_wait_for_vblank(dev, pipe);
6185
6186 /* Set ELD valid state */
6187 tmp = I915_READ(aud_cntrl_st2);
6188 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6189 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6190 I915_WRITE(aud_cntrl_st2, tmp);
6191 tmp = I915_READ(aud_cntrl_st2);
6192 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6193
6194 /* Enable HDMI mode */
6195 tmp = I915_READ(aud_config);
6196 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6197 /* clear N_programing_enable and N_value_index */
6198 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6199 I915_WRITE(aud_config, tmp);
6200
6201 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6202
6203 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006204 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006205
6206 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6207 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6208 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6209 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6210 } else
6211 I915_WRITE(aud_config, 0);
6212
6213 if (intel_eld_uptodate(connector,
6214 aud_cntrl_st2, eldv,
6215 aud_cntl_st, IBX_ELD_ADDRESS,
6216 hdmiw_hdmiedid))
6217 return;
6218
6219 i = I915_READ(aud_cntrl_st2);
6220 i &= ~eldv;
6221 I915_WRITE(aud_cntrl_st2, i);
6222
6223 if (!eld[0])
6224 return;
6225
6226 i = I915_READ(aud_cntl_st);
6227 i &= ~IBX_ELD_ADDRESS;
6228 I915_WRITE(aud_cntl_st, i);
6229 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6230 DRM_DEBUG_DRIVER("port num:%d\n", i);
6231
6232 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6233 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6234 for (i = 0; i < len; i++)
6235 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6236
6237 i = I915_READ(aud_cntrl_st2);
6238 i |= eldv;
6239 I915_WRITE(aud_cntrl_st2, i);
6240
6241}
6242
Wu Fengguange0dac652011-09-05 14:25:34 +08006243static void ironlake_write_eld(struct drm_connector *connector,
6244 struct drm_crtc *crtc)
6245{
6246 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6247 uint8_t *eld = connector->eld;
6248 uint32_t eldv;
6249 uint32_t i;
6250 int len;
6251 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006252 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006253 int aud_cntl_st;
6254 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006255 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006256
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006257 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006258 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6259 aud_config = IBX_AUD_CFG(pipe);
6260 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006261 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006262 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006263 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6264 aud_config = CPT_AUD_CFG(pipe);
6265 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006266 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006267 }
6268
Wang Xingchao9b138a82012-08-09 16:52:18 +08006269 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006270
6271 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006272 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006273 if (!i) {
6274 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6275 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006276 eldv = IBX_ELD_VALIDB;
6277 eldv |= IBX_ELD_VALIDB << 4;
6278 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006279 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006280 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006281 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006282 }
6283
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006284 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6285 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6286 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006287 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6288 } else
6289 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006290
6291 if (intel_eld_uptodate(connector,
6292 aud_cntrl_st2, eldv,
6293 aud_cntl_st, IBX_ELD_ADDRESS,
6294 hdmiw_hdmiedid))
6295 return;
6296
Wu Fengguange0dac652011-09-05 14:25:34 +08006297 i = I915_READ(aud_cntrl_st2);
6298 i &= ~eldv;
6299 I915_WRITE(aud_cntrl_st2, i);
6300
6301 if (!eld[0])
6302 return;
6303
Wu Fengguange0dac652011-09-05 14:25:34 +08006304 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006305 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006306 I915_WRITE(aud_cntl_st, i);
6307
6308 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6309 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6310 for (i = 0; i < len; i++)
6311 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6312
6313 i = I915_READ(aud_cntrl_st2);
6314 i |= eldv;
6315 I915_WRITE(aud_cntrl_st2, i);
6316}
6317
6318void intel_write_eld(struct drm_encoder *encoder,
6319 struct drm_display_mode *mode)
6320{
6321 struct drm_crtc *crtc = encoder->crtc;
6322 struct drm_connector *connector;
6323 struct drm_device *dev = encoder->dev;
6324 struct drm_i915_private *dev_priv = dev->dev_private;
6325
6326 connector = drm_select_eld(encoder, mode);
6327 if (!connector)
6328 return;
6329
6330 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6331 connector->base.id,
6332 drm_get_connector_name(connector),
6333 connector->encoder->base.id,
6334 drm_get_encoder_name(connector->encoder));
6335
6336 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6337
6338 if (dev_priv->display.write_eld)
6339 dev_priv->display.write_eld(connector, crtc);
6340}
6341
Jesse Barnes79e53942008-11-07 14:24:08 -08006342/** Loads the palette/gamma unit for the CRTC with the prepared values */
6343void intel_crtc_load_lut(struct drm_crtc *crtc)
6344{
6345 struct drm_device *dev = crtc->dev;
6346 struct drm_i915_private *dev_priv = dev->dev_private;
6347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006348 enum pipe pipe = intel_crtc->pipe;
6349 int palreg = PALETTE(pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006350 int i;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006351 bool reenable_ips = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006352
6353 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006354 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006355 return;
6356
Ville Syrjälä14420bd2013-06-04 13:49:07 +03006357 if (!HAS_PCH_SPLIT(dev_priv->dev))
6358 assert_pll_enabled(dev_priv, pipe);
6359
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006360 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006361 if (HAS_PCH_SPLIT(dev))
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006362 palreg = LGC_PALETTE(pipe);
6363
6364 /* Workaround : Do not read or write the pipe palette/gamma data while
6365 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
6366 */
6367 if (intel_crtc->config.ips_enabled &&
6368 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
6369 GAMMA_MODE_MODE_SPLIT)) {
6370 hsw_disable_ips(intel_crtc);
6371 reenable_ips = true;
6372 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08006373
Jesse Barnes79e53942008-11-07 14:24:08 -08006374 for (i = 0; i < 256; i++) {
6375 I915_WRITE(palreg + 4 * i,
6376 (intel_crtc->lut_r[i] << 16) |
6377 (intel_crtc->lut_g[i] << 8) |
6378 intel_crtc->lut_b[i]);
6379 }
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006380
6381 if (reenable_ips)
6382 hsw_enable_ips(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006383}
6384
Chris Wilson560b85b2010-08-07 11:01:38 +01006385static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6386{
6387 struct drm_device *dev = crtc->dev;
6388 struct drm_i915_private *dev_priv = dev->dev_private;
6389 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6390 bool visible = base != 0;
6391 u32 cntl;
6392
6393 if (intel_crtc->cursor_visible == visible)
6394 return;
6395
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006396 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006397 if (visible) {
6398 /* On these chipsets we can only modify the base whilst
6399 * the cursor is disabled.
6400 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006401 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006402
6403 cntl &= ~(CURSOR_FORMAT_MASK);
6404 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6405 cntl |= CURSOR_ENABLE |
6406 CURSOR_GAMMA_ENABLE |
6407 CURSOR_FORMAT_ARGB;
6408 } else
6409 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006410 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006411
6412 intel_crtc->cursor_visible = visible;
6413}
6414
6415static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6416{
6417 struct drm_device *dev = crtc->dev;
6418 struct drm_i915_private *dev_priv = dev->dev_private;
6419 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6420 int pipe = intel_crtc->pipe;
6421 bool visible = base != 0;
6422
6423 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006424 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006425 if (base) {
6426 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6427 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6428 cntl |= pipe << 28; /* Connect to correct pipe */
6429 } else {
6430 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6431 cntl |= CURSOR_MODE_DISABLE;
6432 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006433 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006434
6435 intel_crtc->cursor_visible = visible;
6436 }
6437 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006438 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006439}
6440
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006441static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6442{
6443 struct drm_device *dev = crtc->dev;
6444 struct drm_i915_private *dev_priv = dev->dev_private;
6445 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6446 int pipe = intel_crtc->pipe;
6447 bool visible = base != 0;
6448
6449 if (intel_crtc->cursor_visible != visible) {
6450 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6451 if (base) {
6452 cntl &= ~CURSOR_MODE;
6453 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6454 } else {
6455 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6456 cntl |= CURSOR_MODE_DISABLE;
6457 }
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006458 if (IS_HASWELL(dev))
6459 cntl |= CURSOR_PIPE_CSC_ENABLE;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006460 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6461
6462 intel_crtc->cursor_visible = visible;
6463 }
6464 /* and commit changes on next vblank */
6465 I915_WRITE(CURBASE_IVB(pipe), base);
6466}
6467
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006468/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006469static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6470 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006471{
6472 struct drm_device *dev = crtc->dev;
6473 struct drm_i915_private *dev_priv = dev->dev_private;
6474 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6475 int pipe = intel_crtc->pipe;
6476 int x = intel_crtc->cursor_x;
6477 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006478 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006479 bool visible;
6480
6481 pos = 0;
6482
Chris Wilson6b383a72010-09-13 13:54:26 +01006483 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006484 base = intel_crtc->cursor_addr;
6485 if (x > (int) crtc->fb->width)
6486 base = 0;
6487
6488 if (y > (int) crtc->fb->height)
6489 base = 0;
6490 } else
6491 base = 0;
6492
6493 if (x < 0) {
6494 if (x + intel_crtc->cursor_width < 0)
6495 base = 0;
6496
6497 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6498 x = -x;
6499 }
6500 pos |= x << CURSOR_X_SHIFT;
6501
6502 if (y < 0) {
6503 if (y + intel_crtc->cursor_height < 0)
6504 base = 0;
6505
6506 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6507 y = -y;
6508 }
6509 pos |= y << CURSOR_Y_SHIFT;
6510
6511 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006512 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006513 return;
6514
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006515 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006516 I915_WRITE(CURPOS_IVB(pipe), pos);
6517 ivb_update_cursor(crtc, base);
6518 } else {
6519 I915_WRITE(CURPOS(pipe), pos);
6520 if (IS_845G(dev) || IS_I865G(dev))
6521 i845_update_cursor(crtc, base);
6522 else
6523 i9xx_update_cursor(crtc, base);
6524 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006525}
6526
Jesse Barnes79e53942008-11-07 14:24:08 -08006527static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006528 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006529 uint32_t handle,
6530 uint32_t width, uint32_t height)
6531{
6532 struct drm_device *dev = crtc->dev;
6533 struct drm_i915_private *dev_priv = dev->dev_private;
6534 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006535 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006536 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006537 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006538
Jesse Barnes79e53942008-11-07 14:24:08 -08006539 /* if we want to turn off the cursor ignore width and height */
6540 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006541 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006542 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006543 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006544 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006545 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006546 }
6547
6548 /* Currently we only support 64x64 cursors */
6549 if (width != 64 || height != 64) {
6550 DRM_ERROR("we currently only support 64x64 cursors\n");
6551 return -EINVAL;
6552 }
6553
Chris Wilson05394f32010-11-08 19:18:58 +00006554 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006555 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006556 return -ENOENT;
6557
Chris Wilson05394f32010-11-08 19:18:58 +00006558 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006559 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006560 ret = -ENOMEM;
6561 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006562 }
6563
Dave Airlie71acb5e2008-12-30 20:31:46 +10006564 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006565 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006566 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00006567 unsigned alignment;
6568
Chris Wilsond9e86c02010-11-10 16:40:20 +00006569 if (obj->tiling_mode) {
6570 DRM_ERROR("cursor cannot be tiled\n");
6571 ret = -EINVAL;
6572 goto fail_locked;
6573 }
6574
Chris Wilson693db182013-03-05 14:52:39 +00006575 /* Note that the w/a also requires 2 PTE of padding following
6576 * the bo. We currently fill all unused PTE with the shadow
6577 * page and so we should always have valid PTE following the
6578 * cursor preventing the VT-d warning.
6579 */
6580 alignment = 0;
6581 if (need_vtd_wa(dev))
6582 alignment = 64*1024;
6583
6584 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006585 if (ret) {
6586 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006587 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006588 }
6589
Chris Wilsond9e86c02010-11-10 16:40:20 +00006590 ret = i915_gem_object_put_fence(obj);
6591 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006592 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006593 goto fail_unpin;
6594 }
6595
Chris Wilson05394f32010-11-08 19:18:58 +00006596 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006597 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006598 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006599 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006600 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6601 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006602 if (ret) {
6603 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006604 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006605 }
Chris Wilson05394f32010-11-08 19:18:58 +00006606 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006607 }
6608
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006609 if (IS_GEN2(dev))
Jesse Barnes14b603912009-05-20 16:47:08 -04006610 I915_WRITE(CURSIZE, (height << 12) | width);
6611
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006612 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006613 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006614 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006615 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006616 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6617 } else
6618 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006619 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006620 }
Jesse Barnes80824002009-09-10 15:28:06 -07006621
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006622 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006623
6624 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006625 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006626 intel_crtc->cursor_width = width;
6627 intel_crtc->cursor_height = height;
6628
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006629 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006630
Jesse Barnes79e53942008-11-07 14:24:08 -08006631 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006632fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006633 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006634fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006635 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006636fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006637 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006638 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006639}
6640
6641static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6642{
Jesse Barnes79e53942008-11-07 14:24:08 -08006643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006644
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006645 intel_crtc->cursor_x = x;
6646 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006647
Mika Kuoppala40ccc722013-04-23 17:27:08 +03006648 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08006649
6650 return 0;
6651}
6652
6653/** Sets the color ramps on behalf of RandR */
6654void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6655 u16 blue, int regno)
6656{
6657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6658
6659 intel_crtc->lut_r[regno] = red >> 8;
6660 intel_crtc->lut_g[regno] = green >> 8;
6661 intel_crtc->lut_b[regno] = blue >> 8;
6662}
6663
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006664void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6665 u16 *blue, int regno)
6666{
6667 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6668
6669 *red = intel_crtc->lut_r[regno] << 8;
6670 *green = intel_crtc->lut_g[regno] << 8;
6671 *blue = intel_crtc->lut_b[regno] << 8;
6672}
6673
Jesse Barnes79e53942008-11-07 14:24:08 -08006674static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006675 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006676{
James Simmons72034252010-08-03 01:33:19 +01006677 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006678 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006679
James Simmons72034252010-08-03 01:33:19 +01006680 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006681 intel_crtc->lut_r[i] = red[i] >> 8;
6682 intel_crtc->lut_g[i] = green[i] >> 8;
6683 intel_crtc->lut_b[i] = blue[i] >> 8;
6684 }
6685
6686 intel_crtc_load_lut(crtc);
6687}
6688
Jesse Barnes79e53942008-11-07 14:24:08 -08006689/* VESA 640x480x72Hz mode to set on the pipe */
6690static struct drm_display_mode load_detect_mode = {
6691 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6692 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6693};
6694
Chris Wilsond2dff872011-04-19 08:36:26 +01006695static struct drm_framebuffer *
6696intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006697 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006698 struct drm_i915_gem_object *obj)
6699{
6700 struct intel_framebuffer *intel_fb;
6701 int ret;
6702
6703 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6704 if (!intel_fb) {
6705 drm_gem_object_unreference_unlocked(&obj->base);
6706 return ERR_PTR(-ENOMEM);
6707 }
6708
6709 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6710 if (ret) {
6711 drm_gem_object_unreference_unlocked(&obj->base);
6712 kfree(intel_fb);
6713 return ERR_PTR(ret);
6714 }
6715
6716 return &intel_fb->base;
6717}
6718
6719static u32
6720intel_framebuffer_pitch_for_width(int width, int bpp)
6721{
6722 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6723 return ALIGN(pitch, 64);
6724}
6725
6726static u32
6727intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6728{
6729 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6730 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6731}
6732
6733static struct drm_framebuffer *
6734intel_framebuffer_create_for_mode(struct drm_device *dev,
6735 struct drm_display_mode *mode,
6736 int depth, int bpp)
6737{
6738 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006739 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006740
6741 obj = i915_gem_alloc_object(dev,
6742 intel_framebuffer_size_for_mode(mode, bpp));
6743 if (obj == NULL)
6744 return ERR_PTR(-ENOMEM);
6745
6746 mode_cmd.width = mode->hdisplay;
6747 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006748 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6749 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006750 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006751
6752 return intel_framebuffer_create(dev, &mode_cmd, obj);
6753}
6754
6755static struct drm_framebuffer *
6756mode_fits_in_fbdev(struct drm_device *dev,
6757 struct drm_display_mode *mode)
6758{
6759 struct drm_i915_private *dev_priv = dev->dev_private;
6760 struct drm_i915_gem_object *obj;
6761 struct drm_framebuffer *fb;
6762
6763 if (dev_priv->fbdev == NULL)
6764 return NULL;
6765
6766 obj = dev_priv->fbdev->ifb.obj;
6767 if (obj == NULL)
6768 return NULL;
6769
6770 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006771 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6772 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006773 return NULL;
6774
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006775 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006776 return NULL;
6777
6778 return fb;
6779}
6780
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006781bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006782 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006783 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006784{
6785 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006786 struct intel_encoder *intel_encoder =
6787 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006788 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006789 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006790 struct drm_crtc *crtc = NULL;
6791 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006792 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006793 int i = -1;
6794
Chris Wilsond2dff872011-04-19 08:36:26 +01006795 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6796 connector->base.id, drm_get_connector_name(connector),
6797 encoder->base.id, drm_get_encoder_name(encoder));
6798
Jesse Barnes79e53942008-11-07 14:24:08 -08006799 /*
6800 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006801 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006802 * - if the connector already has an assigned crtc, use it (but make
6803 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006804 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006805 * - try to find the first unused crtc that can drive this connector,
6806 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006807 */
6808
6809 /* See if we already have a CRTC for this connector */
6810 if (encoder->crtc) {
6811 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006812
Daniel Vetter7b240562012-12-12 00:35:33 +01006813 mutex_lock(&crtc->mutex);
6814
Daniel Vetter24218aa2012-08-12 19:27:11 +02006815 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006816 old->load_detect_temp = false;
6817
6818 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006819 if (connector->dpms != DRM_MODE_DPMS_ON)
6820 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006821
Chris Wilson71731882011-04-19 23:10:58 +01006822 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006823 }
6824
6825 /* Find an unused one (if possible) */
6826 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6827 i++;
6828 if (!(encoder->possible_crtcs & (1 << i)))
6829 continue;
6830 if (!possible_crtc->enabled) {
6831 crtc = possible_crtc;
6832 break;
6833 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006834 }
6835
6836 /*
6837 * If we didn't find an unused CRTC, don't use any.
6838 */
6839 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006840 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6841 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006842 }
6843
Daniel Vetter7b240562012-12-12 00:35:33 +01006844 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02006845 intel_encoder->new_crtc = to_intel_crtc(crtc);
6846 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006847
6848 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006849 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006850 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006851 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006852
Chris Wilson64927112011-04-20 07:25:26 +01006853 if (!mode)
6854 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006855
Chris Wilsond2dff872011-04-19 08:36:26 +01006856 /* We need a framebuffer large enough to accommodate all accesses
6857 * that the plane may generate whilst we perform load detection.
6858 * We can not rely on the fbcon either being present (we get called
6859 * during its initialisation to detect all boot displays, or it may
6860 * not even exist) or that it is large enough to satisfy the
6861 * requested mode.
6862 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006863 fb = mode_fits_in_fbdev(dev, mode);
6864 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006865 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006866 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6867 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006868 } else
6869 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006870 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006871 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01006872 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006873 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006874 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006875
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006876 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006877 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006878 if (old->release_fb)
6879 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01006880 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006881 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006882 }
Chris Wilson71731882011-04-19 23:10:58 +01006883
Jesse Barnes79e53942008-11-07 14:24:08 -08006884 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006885 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006886 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006887}
6888
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006889void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006890 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006891{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006892 struct intel_encoder *intel_encoder =
6893 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006894 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01006895 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08006896
Chris Wilsond2dff872011-04-19 08:36:26 +01006897 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6898 connector->base.id, drm_get_connector_name(connector),
6899 encoder->base.id, drm_get_encoder_name(encoder));
6900
Chris Wilson8261b192011-04-19 23:18:09 +01006901 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006902 to_intel_connector(connector)->new_encoder = NULL;
6903 intel_encoder->new_crtc = NULL;
6904 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006905
Daniel Vetter36206362012-12-10 20:42:17 +01006906 if (old->release_fb) {
6907 drm_framebuffer_unregister_private(old->release_fb);
6908 drm_framebuffer_unreference(old->release_fb);
6909 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006910
Daniel Vetter67c96402013-01-23 16:25:09 +00006911 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01006912 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006913 }
6914
Eric Anholtc751ce42010-03-25 11:48:48 -07006915 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006916 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6917 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01006918
6919 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08006920}
6921
6922/* Returns the clock of the currently programmed mode of the given pipe. */
6923static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6924{
6925 struct drm_i915_private *dev_priv = dev->dev_private;
6926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6927 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006928 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006929 u32 fp;
6930 intel_clock_t clock;
6931
6932 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006933 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006934 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006935 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006936
6937 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006938 if (IS_PINEVIEW(dev)) {
6939 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6940 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006941 } else {
6942 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6943 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6944 }
6945
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006946 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006947 if (IS_PINEVIEW(dev))
6948 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6949 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006950 else
6951 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006952 DPLL_FPA01_P1_POST_DIV_SHIFT);
6953
6954 switch (dpll & DPLL_MODE_MASK) {
6955 case DPLLB_MODE_DAC_SERIAL:
6956 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6957 5 : 10;
6958 break;
6959 case DPLLB_MODE_LVDS:
6960 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6961 7 : 14;
6962 break;
6963 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006964 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006965 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6966 return 0;
6967 }
6968
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006969 if (IS_PINEVIEW(dev))
6970 pineview_clock(96000, &clock);
6971 else
6972 i9xx_clock(96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006973 } else {
6974 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6975
6976 if (is_lvds) {
6977 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6978 DPLL_FPA01_P1_POST_DIV_SHIFT);
6979 clock.p2 = 14;
6980
6981 if ((dpll & PLL_REF_INPUT_MASK) ==
6982 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6983 /* XXX: might not be 66MHz */
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006984 i9xx_clock(66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006985 } else
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006986 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006987 } else {
6988 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6989 clock.p1 = 2;
6990 else {
6991 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6992 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6993 }
6994 if (dpll & PLL_P2_DIVIDE_BY_4)
6995 clock.p2 = 4;
6996 else
6997 clock.p2 = 2;
6998
Daniel Vetterac58c3f2013-06-01 17:16:17 +02006999 i9xx_clock(48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08007000 }
7001 }
7002
7003 /* XXX: It would be nice to validate the clocks, but we can't reuse
7004 * i830PllIsValid() because it relies on the xf86_config connector
7005 * configuration being accurate, which it isn't necessarily.
7006 */
7007
7008 return clock.dot;
7009}
7010
7011/** Returns the currently programmed mode of the given pipe. */
7012struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
7013 struct drm_crtc *crtc)
7014{
Jesse Barnes548f2452011-02-17 10:40:53 -08007015 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02007017 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007018 struct drm_display_mode *mode;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007019 int htot = I915_READ(HTOTAL(cpu_transcoder));
7020 int hsync = I915_READ(HSYNC(cpu_transcoder));
7021 int vtot = I915_READ(VTOTAL(cpu_transcoder));
7022 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08007023
7024 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7025 if (!mode)
7026 return NULL;
7027
7028 mode->clock = intel_crtc_clock_get(dev, crtc);
7029 mode->hdisplay = (htot & 0xffff) + 1;
7030 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
7031 mode->hsync_start = (hsync & 0xffff) + 1;
7032 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
7033 mode->vdisplay = (vtot & 0xffff) + 1;
7034 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
7035 mode->vsync_start = (vsync & 0xffff) + 1;
7036 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
7037
7038 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08007039
7040 return mode;
7041}
7042
Daniel Vetter3dec0092010-08-20 21:40:52 +02007043static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007044{
7045 struct drm_device *dev = crtc->dev;
7046 drm_i915_private_t *dev_priv = dev->dev_private;
7047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7048 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007049 int dpll_reg = DPLL(pipe);
7050 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007051
Eric Anholtbad720f2009-10-22 16:11:14 -07007052 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007053 return;
7054
7055 if (!dev_priv->lvds_downclock_avail)
7056 return;
7057
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007058 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007059 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007060 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007061
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007062 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007063
7064 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7065 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007066 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007067
Jesse Barnes652c3932009-08-17 13:31:43 -07007068 dpll = I915_READ(dpll_reg);
7069 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007070 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007071 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007072}
7073
7074static void intel_decrease_pllclock(struct drm_crtc *crtc)
7075{
7076 struct drm_device *dev = crtc->dev;
7077 drm_i915_private_t *dev_priv = dev->dev_private;
7078 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007079
Eric Anholtbad720f2009-10-22 16:11:14 -07007080 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007081 return;
7082
7083 if (!dev_priv->lvds_downclock_avail)
7084 return;
7085
7086 /*
7087 * Since this is called by a timer, we should never get here in
7088 * the manual case.
7089 */
7090 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007091 int pipe = intel_crtc->pipe;
7092 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007093 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007094
Zhao Yakui44d98a62009-10-09 11:39:40 +08007095 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007096
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007097 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007098
Chris Wilson074b5e12012-05-02 12:07:06 +01007099 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007100 dpll |= DISPLAY_RATE_SELECT_FPA1;
7101 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007102 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007103 dpll = I915_READ(dpll_reg);
7104 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007105 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007106 }
7107
7108}
7109
Chris Wilsonf047e392012-07-21 12:31:41 +01007110void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007111{
Chris Wilsonf047e392012-07-21 12:31:41 +01007112 i915_update_gfx_val(dev->dev_private);
7113}
7114
7115void intel_mark_idle(struct drm_device *dev)
7116{
Chris Wilson725a5b52013-01-08 11:02:57 +00007117 struct drm_crtc *crtc;
7118
7119 if (!i915_powersave)
7120 return;
7121
7122 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7123 if (!crtc->fb)
7124 continue;
7125
7126 intel_decrease_pllclock(crtc);
7127 }
Chris Wilsonf047e392012-07-21 12:31:41 +01007128}
7129
Chris Wilsonc65355b2013-06-06 16:53:41 -03007130void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
7131 struct intel_ring_buffer *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01007132{
7133 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007134 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007135
7136 if (!i915_powersave)
7137 return;
7138
Jesse Barnes652c3932009-08-17 13:31:43 -07007139 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007140 if (!crtc->fb)
7141 continue;
7142
Chris Wilsonc65355b2013-06-06 16:53:41 -03007143 if (to_intel_framebuffer(crtc->fb)->obj != obj)
7144 continue;
7145
7146 intel_increase_pllclock(crtc);
7147 if (ring && intel_fbc_enabled(dev))
7148 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07007149 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007150}
7151
Jesse Barnes79e53942008-11-07 14:24:08 -08007152static void intel_crtc_destroy(struct drm_crtc *crtc)
7153{
7154 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007155 struct drm_device *dev = crtc->dev;
7156 struct intel_unpin_work *work;
7157 unsigned long flags;
7158
7159 spin_lock_irqsave(&dev->event_lock, flags);
7160 work = intel_crtc->unpin_work;
7161 intel_crtc->unpin_work = NULL;
7162 spin_unlock_irqrestore(&dev->event_lock, flags);
7163
7164 if (work) {
7165 cancel_work_sync(&work->work);
7166 kfree(work);
7167 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007168
Mika Kuoppala40ccc722013-04-23 17:27:08 +03007169 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
7170
Jesse Barnes79e53942008-11-07 14:24:08 -08007171 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007172
Jesse Barnes79e53942008-11-07 14:24:08 -08007173 kfree(intel_crtc);
7174}
7175
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007176static void intel_unpin_work_fn(struct work_struct *__work)
7177{
7178 struct intel_unpin_work *work =
7179 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007180 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007181
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007182 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007183 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007184 drm_gem_object_unreference(&work->pending_flip_obj->base);
7185 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007186
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007187 intel_update_fbc(dev);
7188 mutex_unlock(&dev->struct_mutex);
7189
7190 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7191 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7192
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007193 kfree(work);
7194}
7195
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007196static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007197 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007198{
7199 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7201 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007202 unsigned long flags;
7203
7204 /* Ignore early vblank irqs */
7205 if (intel_crtc == NULL)
7206 return;
7207
7208 spin_lock_irqsave(&dev->event_lock, flags);
7209 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007210
7211 /* Ensure we don't miss a work->pending update ... */
7212 smp_rmb();
7213
7214 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007215 spin_unlock_irqrestore(&dev->event_lock, flags);
7216 return;
7217 }
7218
Chris Wilsone7d841c2012-12-03 11:36:30 +00007219 /* and that the unpin work is consistent wrt ->pending. */
7220 smp_rmb();
7221
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007222 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007223
Rob Clark45a066e2012-10-08 14:50:40 -05007224 if (work->event)
7225 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007226
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007227 drm_vblank_put(dev, intel_crtc->pipe);
7228
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007229 spin_unlock_irqrestore(&dev->event_lock, flags);
7230
Daniel Vetter2c10d572012-12-20 21:24:07 +01007231 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007232
7233 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007234
7235 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007236}
7237
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007238void intel_finish_page_flip(struct drm_device *dev, int pipe)
7239{
7240 drm_i915_private_t *dev_priv = dev->dev_private;
7241 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7242
Mario Kleiner49b14a52010-12-09 07:00:07 +01007243 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007244}
7245
7246void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7247{
7248 drm_i915_private_t *dev_priv = dev->dev_private;
7249 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7250
Mario Kleiner49b14a52010-12-09 07:00:07 +01007251 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007252}
7253
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007254void intel_prepare_page_flip(struct drm_device *dev, int plane)
7255{
7256 drm_i915_private_t *dev_priv = dev->dev_private;
7257 struct intel_crtc *intel_crtc =
7258 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7259 unsigned long flags;
7260
Chris Wilsone7d841c2012-12-03 11:36:30 +00007261 /* NB: An MMIO update of the plane base pointer will also
7262 * generate a page-flip completion irq, i.e. every modeset
7263 * is also accompanied by a spurious intel_prepare_page_flip().
7264 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007265 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007266 if (intel_crtc->unpin_work)
7267 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007268 spin_unlock_irqrestore(&dev->event_lock, flags);
7269}
7270
Chris Wilsone7d841c2012-12-03 11:36:30 +00007271inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7272{
7273 /* Ensure that the work item is consistent when activating it ... */
7274 smp_wmb();
7275 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7276 /* and that it is marked active as soon as the irq could fire. */
7277 smp_wmb();
7278}
7279
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007280static int intel_gen2_queue_flip(struct drm_device *dev,
7281 struct drm_crtc *crtc,
7282 struct drm_framebuffer *fb,
7283 struct drm_i915_gem_object *obj)
7284{
7285 struct drm_i915_private *dev_priv = dev->dev_private;
7286 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007287 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007288 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007289 int ret;
7290
Daniel Vetter6d90c952012-04-26 23:28:05 +02007291 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007292 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007293 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007294
Daniel Vetter6d90c952012-04-26 23:28:05 +02007295 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007296 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007297 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007298
7299 /* Can't queue multiple flips, so wait for the previous
7300 * one to finish before executing the next.
7301 */
7302 if (intel_crtc->plane)
7303 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7304 else
7305 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007306 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7307 intel_ring_emit(ring, MI_NOOP);
7308 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7309 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7310 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007311 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007312 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007313
7314 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007315 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007316 return 0;
7317
7318err_unpin:
7319 intel_unpin_fb_obj(obj);
7320err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007321 return ret;
7322}
7323
7324static int intel_gen3_queue_flip(struct drm_device *dev,
7325 struct drm_crtc *crtc,
7326 struct drm_framebuffer *fb,
7327 struct drm_i915_gem_object *obj)
7328{
7329 struct drm_i915_private *dev_priv = dev->dev_private;
7330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007331 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007332 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007333 int ret;
7334
Daniel Vetter6d90c952012-04-26 23:28:05 +02007335 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007336 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007337 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007338
Daniel Vetter6d90c952012-04-26 23:28:05 +02007339 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007340 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007341 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007342
7343 if (intel_crtc->plane)
7344 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7345 else
7346 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007347 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7348 intel_ring_emit(ring, MI_NOOP);
7349 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7350 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7351 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007352 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007353 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007354
Chris Wilsone7d841c2012-12-03 11:36:30 +00007355 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007356 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007357 return 0;
7358
7359err_unpin:
7360 intel_unpin_fb_obj(obj);
7361err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007362 return ret;
7363}
7364
7365static int intel_gen4_queue_flip(struct drm_device *dev,
7366 struct drm_crtc *crtc,
7367 struct drm_framebuffer *fb,
7368 struct drm_i915_gem_object *obj)
7369{
7370 struct drm_i915_private *dev_priv = dev->dev_private;
7371 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7372 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007373 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007374 int ret;
7375
Daniel Vetter6d90c952012-04-26 23:28:05 +02007376 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007377 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007378 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007379
Daniel Vetter6d90c952012-04-26 23:28:05 +02007380 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007381 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007382 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007383
7384 /* i965+ uses the linear or tiled offsets from the
7385 * Display Registers (which do not change across a page-flip)
7386 * so we need only reprogram the base address.
7387 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007388 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7389 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7390 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007391 intel_ring_emit(ring,
7392 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
7393 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007394
7395 /* XXX Enabling the panel-fitter across page-flip is so far
7396 * untested on non-native modes, so ignore it for now.
7397 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7398 */
7399 pf = 0;
7400 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007401 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007402
7403 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007404 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007405 return 0;
7406
7407err_unpin:
7408 intel_unpin_fb_obj(obj);
7409err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007410 return ret;
7411}
7412
7413static int intel_gen6_queue_flip(struct drm_device *dev,
7414 struct drm_crtc *crtc,
7415 struct drm_framebuffer *fb,
7416 struct drm_i915_gem_object *obj)
7417{
7418 struct drm_i915_private *dev_priv = dev->dev_private;
7419 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007420 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007421 uint32_t pf, pipesrc;
7422 int ret;
7423
Daniel Vetter6d90c952012-04-26 23:28:05 +02007424 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007425 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007426 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007427
Daniel Vetter6d90c952012-04-26 23:28:05 +02007428 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007429 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007430 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007431
Daniel Vetter6d90c952012-04-26 23:28:05 +02007432 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7433 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7434 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007435 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007436
Chris Wilson99d9acd2012-04-17 20:37:00 +01007437 /* Contrary to the suggestions in the documentation,
7438 * "Enable Panel Fitter" does not seem to be required when page
7439 * flipping with a non-native mode, and worse causes a normal
7440 * modeset to fail.
7441 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7442 */
7443 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007444 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007445 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007446
7447 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007448 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007449 return 0;
7450
7451err_unpin:
7452 intel_unpin_fb_obj(obj);
7453err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007454 return ret;
7455}
7456
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007457/*
7458 * On gen7 we currently use the blit ring because (in early silicon at least)
7459 * the render ring doesn't give us interrpts for page flip completion, which
7460 * means clients will hang after the first flip is queued. Fortunately the
7461 * blit ring generates interrupts properly, so use it instead.
7462 */
7463static int intel_gen7_queue_flip(struct drm_device *dev,
7464 struct drm_crtc *crtc,
7465 struct drm_framebuffer *fb,
7466 struct drm_i915_gem_object *obj)
7467{
7468 struct drm_i915_private *dev_priv = dev->dev_private;
7469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7470 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007471 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007472 int ret;
7473
7474 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7475 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007476 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007477
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007478 switch(intel_crtc->plane) {
7479 case PLANE_A:
7480 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7481 break;
7482 case PLANE_B:
7483 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7484 break;
7485 case PLANE_C:
7486 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7487 break;
7488 default:
7489 WARN_ONCE(1, "unknown plane in flip command\n");
7490 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007491 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007492 }
7493
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007494 ret = intel_ring_begin(ring, 4);
7495 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007496 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007497
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007498 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007499 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Daniel Vetterc2c75132012-07-05 12:17:30 +02007500 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007501 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007502
7503 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007504 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007505 return 0;
7506
7507err_unpin:
7508 intel_unpin_fb_obj(obj);
7509err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007510 return ret;
7511}
7512
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007513static int intel_default_queue_flip(struct drm_device *dev,
7514 struct drm_crtc *crtc,
7515 struct drm_framebuffer *fb,
7516 struct drm_i915_gem_object *obj)
7517{
7518 return -ENODEV;
7519}
7520
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007521static int intel_crtc_page_flip(struct drm_crtc *crtc,
7522 struct drm_framebuffer *fb,
7523 struct drm_pending_vblank_event *event)
7524{
7525 struct drm_device *dev = crtc->dev;
7526 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007527 struct drm_framebuffer *old_fb = crtc->fb;
7528 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007529 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7530 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007531 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007532 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007533
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007534 /* Can't change pixel format via MI display flips. */
7535 if (fb->pixel_format != crtc->fb->pixel_format)
7536 return -EINVAL;
7537
7538 /*
7539 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7540 * Note that pitch changes could also affect these register.
7541 */
7542 if (INTEL_INFO(dev)->gen > 3 &&
7543 (fb->offsets[0] != crtc->fb->offsets[0] ||
7544 fb->pitches[0] != crtc->fb->pitches[0]))
7545 return -EINVAL;
7546
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007547 work = kzalloc(sizeof *work, GFP_KERNEL);
7548 if (work == NULL)
7549 return -ENOMEM;
7550
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007551 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007552 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007553 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007554 INIT_WORK(&work->work, intel_unpin_work_fn);
7555
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007556 ret = drm_vblank_get(dev, intel_crtc->pipe);
7557 if (ret)
7558 goto free_work;
7559
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007560 /* We borrow the event spin lock for protecting unpin_work */
7561 spin_lock_irqsave(&dev->event_lock, flags);
7562 if (intel_crtc->unpin_work) {
7563 spin_unlock_irqrestore(&dev->event_lock, flags);
7564 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007565 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007566
7567 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007568 return -EBUSY;
7569 }
7570 intel_crtc->unpin_work = work;
7571 spin_unlock_irqrestore(&dev->event_lock, flags);
7572
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007573 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7574 flush_workqueue(dev_priv->wq);
7575
Chris Wilson79158102012-05-23 11:13:58 +01007576 ret = i915_mutex_lock_interruptible(dev);
7577 if (ret)
7578 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007579
Jesse Barnes75dfca82010-02-10 15:09:44 -08007580 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007581 drm_gem_object_reference(&work->old_fb_obj->base);
7582 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007583
7584 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007585
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007586 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007587
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007588 work->enable_stall_check = true;
7589
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007590 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02007591 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007592
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007593 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7594 if (ret)
7595 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007596
Chris Wilson7782de32011-07-08 12:22:41 +01007597 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03007598 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007599 mutex_unlock(&dev->struct_mutex);
7600
Jesse Barnese5510fa2010-07-01 16:48:37 -07007601 trace_i915_flip_request(intel_crtc->plane, obj);
7602
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007603 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007604
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007605cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007606 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007607 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007608 drm_gem_object_unreference(&work->old_fb_obj->base);
7609 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007610 mutex_unlock(&dev->struct_mutex);
7611
Chris Wilson79158102012-05-23 11:13:58 +01007612cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007613 spin_lock_irqsave(&dev->event_lock, flags);
7614 intel_crtc->unpin_work = NULL;
7615 spin_unlock_irqrestore(&dev->event_lock, flags);
7616
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007617 drm_vblank_put(dev, intel_crtc->pipe);
7618free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007619 kfree(work);
7620
7621 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007622}
7623
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007624static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007625 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7626 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007627};
7628
Daniel Vetter50f56112012-07-02 09:35:43 +02007629static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7630 struct drm_crtc *crtc)
7631{
7632 struct drm_device *dev;
7633 struct drm_crtc *tmp;
7634 int crtc_mask = 1;
7635
7636 WARN(!crtc, "checking null crtc?\n");
7637
7638 dev = crtc->dev;
7639
7640 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7641 if (tmp == crtc)
7642 break;
7643 crtc_mask <<= 1;
7644 }
7645
7646 if (encoder->possible_crtcs & crtc_mask)
7647 return true;
7648 return false;
7649}
7650
Daniel Vetter9a935852012-07-05 22:34:27 +02007651/**
7652 * intel_modeset_update_staged_output_state
7653 *
7654 * Updates the staged output configuration state, e.g. after we've read out the
7655 * current hw state.
7656 */
7657static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7658{
7659 struct intel_encoder *encoder;
7660 struct intel_connector *connector;
7661
7662 list_for_each_entry(connector, &dev->mode_config.connector_list,
7663 base.head) {
7664 connector->new_encoder =
7665 to_intel_encoder(connector->base.encoder);
7666 }
7667
7668 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7669 base.head) {
7670 encoder->new_crtc =
7671 to_intel_crtc(encoder->base.crtc);
7672 }
7673}
7674
7675/**
7676 * intel_modeset_commit_output_state
7677 *
7678 * This function copies the stage display pipe configuration to the real one.
7679 */
7680static void intel_modeset_commit_output_state(struct drm_device *dev)
7681{
7682 struct intel_encoder *encoder;
7683 struct intel_connector *connector;
7684
7685 list_for_each_entry(connector, &dev->mode_config.connector_list,
7686 base.head) {
7687 connector->base.encoder = &connector->new_encoder->base;
7688 }
7689
7690 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7691 base.head) {
7692 encoder->base.crtc = &encoder->new_crtc->base;
7693 }
7694}
7695
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007696static void
7697connected_sink_compute_bpp(struct intel_connector * connector,
7698 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007699{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007700 int bpp = pipe_config->pipe_bpp;
7701
7702 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
7703 connector->base.base.id,
7704 drm_get_connector_name(&connector->base));
7705
7706 /* Don't use an invalid EDID bpc value */
7707 if (connector->base.display_info.bpc &&
7708 connector->base.display_info.bpc * 3 < bpp) {
7709 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7710 bpp, connector->base.display_info.bpc*3);
7711 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
7712 }
7713
7714 /* Clamp bpp to 8 on screens without EDID 1.4 */
7715 if (connector->base.display_info.bpc == 0 && bpp > 24) {
7716 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7717 bpp);
7718 pipe_config->pipe_bpp = 24;
7719 }
7720}
7721
7722static int
7723compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7724 struct drm_framebuffer *fb,
7725 struct intel_crtc_config *pipe_config)
7726{
7727 struct drm_device *dev = crtc->base.dev;
7728 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007729 int bpp;
7730
Daniel Vetterd42264b2013-03-28 16:38:08 +01007731 switch (fb->pixel_format) {
7732 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007733 bpp = 8*3; /* since we go through a colormap */
7734 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007735 case DRM_FORMAT_XRGB1555:
7736 case DRM_FORMAT_ARGB1555:
7737 /* checked in intel_framebuffer_init already */
7738 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7739 return -EINVAL;
7740 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007741 bpp = 6*3; /* min is 18bpp */
7742 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007743 case DRM_FORMAT_XBGR8888:
7744 case DRM_FORMAT_ABGR8888:
7745 /* checked in intel_framebuffer_init already */
7746 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7747 return -EINVAL;
7748 case DRM_FORMAT_XRGB8888:
7749 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007750 bpp = 8*3;
7751 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007752 case DRM_FORMAT_XRGB2101010:
7753 case DRM_FORMAT_ARGB2101010:
7754 case DRM_FORMAT_XBGR2101010:
7755 case DRM_FORMAT_ABGR2101010:
7756 /* checked in intel_framebuffer_init already */
7757 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01007758 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007759 bpp = 10*3;
7760 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01007761 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007762 default:
7763 DRM_DEBUG_KMS("unsupported depth\n");
7764 return -EINVAL;
7765 }
7766
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007767 pipe_config->pipe_bpp = bpp;
7768
7769 /* Clamp display bpp to EDID value */
7770 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007771 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02007772 if (!connector->new_encoder ||
7773 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007774 continue;
7775
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007776 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007777 }
7778
7779 return bpp;
7780}
7781
Daniel Vetterc0b03412013-05-28 12:05:54 +02007782static void intel_dump_pipe_config(struct intel_crtc *crtc,
7783 struct intel_crtc_config *pipe_config,
7784 const char *context)
7785{
7786 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
7787 context, pipe_name(crtc->pipe));
7788
7789 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
7790 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
7791 pipe_config->pipe_bpp, pipe_config->dither);
7792 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7793 pipe_config->has_pch_encoder,
7794 pipe_config->fdi_lanes,
7795 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
7796 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
7797 pipe_config->fdi_m_n.tu);
7798 DRM_DEBUG_KMS("requested mode:\n");
7799 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
7800 DRM_DEBUG_KMS("adjusted mode:\n");
7801 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
7802 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
7803 pipe_config->gmch_pfit.control,
7804 pipe_config->gmch_pfit.pgm_ratios,
7805 pipe_config->gmch_pfit.lvds_border_bits);
7806 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x\n",
7807 pipe_config->pch_pfit.pos,
7808 pipe_config->pch_pfit.size);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007809 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Daniel Vetterc0b03412013-05-28 12:05:54 +02007810}
7811
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007812static bool check_encoder_cloning(struct drm_crtc *crtc)
7813{
7814 int num_encoders = 0;
7815 bool uncloneable_encoders = false;
7816 struct intel_encoder *encoder;
7817
7818 list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
7819 base.head) {
7820 if (&encoder->new_crtc->base != crtc)
7821 continue;
7822
7823 num_encoders++;
7824 if (!encoder->cloneable)
7825 uncloneable_encoders = true;
7826 }
7827
7828 return !(num_encoders > 1 && uncloneable_encoders);
7829}
7830
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007831static struct intel_crtc_config *
7832intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007833 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007834 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02007835{
7836 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02007837 struct drm_encoder_helper_funcs *encoder_funcs;
7838 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007839 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01007840 int plane_bpp, ret = -EINVAL;
7841 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02007842
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02007843 if (!check_encoder_cloning(crtc)) {
7844 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
7845 return ERR_PTR(-EINVAL);
7846 }
7847
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007848 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7849 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02007850 return ERR_PTR(-ENOMEM);
7851
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007852 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7853 drm_mode_copy(&pipe_config->requested_mode, mode);
Daniel Vettereccb1402013-05-22 00:50:22 +02007854 pipe_config->cpu_transcoder = to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007855
Daniel Vetter050f7ae2013-06-02 13:26:23 +02007856 /* Compute a starting value for pipe_config->pipe_bpp taking the source
7857 * plane pixel format and any sink constraints into account. Returns the
7858 * source plane bpp so that dithering can be selected on mismatches
7859 * after encoders and crtc also have had their say. */
7860 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
7861 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007862 if (plane_bpp < 0)
7863 goto fail;
7864
Daniel Vettere29c22c2013-02-21 00:00:16 +01007865encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02007866 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02007867 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02007868 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02007869
Daniel Vetter7758a112012-07-08 19:40:39 +02007870 /* Pass our mode to the connectors and the CRTC to give them a chance to
7871 * adjust it according to limitations or connector properties, and also
7872 * a chance to reject the mode entirely.
7873 */
7874 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7875 base.head) {
7876
7877 if (&encoder->new_crtc->base != crtc)
7878 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01007879
7880 if (encoder->compute_config) {
7881 if (!(encoder->compute_config(encoder, pipe_config))) {
7882 DRM_DEBUG_KMS("Encoder config failure\n");
7883 goto fail;
7884 }
7885
7886 continue;
7887 }
7888
Daniel Vetter7758a112012-07-08 19:40:39 +02007889 encoder_funcs = encoder->base.helper_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007890 if (!(encoder_funcs->mode_fixup(&encoder->base,
7891 &pipe_config->requested_mode,
7892 &pipe_config->adjusted_mode))) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007893 DRM_DEBUG_KMS("Encoder fixup failed\n");
7894 goto fail;
7895 }
7896 }
7897
Daniel Vetterff9a6752013-06-01 17:16:21 +02007898 /* Set default port clock if not overwritten by the encoder. Needs to be
7899 * done afterwards in case the encoder adjusts the mode. */
7900 if (!pipe_config->port_clock)
7901 pipe_config->port_clock = pipe_config->adjusted_mode.clock;
7902
Daniel Vettere29c22c2013-02-21 00:00:16 +01007903 ret = intel_crtc_compute_config(crtc, pipe_config);
7904 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007905 DRM_DEBUG_KMS("CRTC fixup failed\n");
7906 goto fail;
7907 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01007908
7909 if (ret == RETRY) {
7910 if (WARN(!retry, "loop in pipe configuration computation\n")) {
7911 ret = -EINVAL;
7912 goto fail;
7913 }
7914
7915 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
7916 retry = false;
7917 goto encoder_retry;
7918 }
7919
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007920 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7921 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7922 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7923
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007924 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02007925fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007926 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01007927 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02007928}
7929
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007930/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7931 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7932static void
7933intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7934 unsigned *prepare_pipes, unsigned *disable_pipes)
7935{
7936 struct intel_crtc *intel_crtc;
7937 struct drm_device *dev = crtc->dev;
7938 struct intel_encoder *encoder;
7939 struct intel_connector *connector;
7940 struct drm_crtc *tmp_crtc;
7941
7942 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7943
7944 /* Check which crtcs have changed outputs connected to them, these need
7945 * to be part of the prepare_pipes mask. We don't (yet) support global
7946 * modeset across multiple crtcs, so modeset_pipes will only have one
7947 * bit set at most. */
7948 list_for_each_entry(connector, &dev->mode_config.connector_list,
7949 base.head) {
7950 if (connector->base.encoder == &connector->new_encoder->base)
7951 continue;
7952
7953 if (connector->base.encoder) {
7954 tmp_crtc = connector->base.encoder->crtc;
7955
7956 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7957 }
7958
7959 if (connector->new_encoder)
7960 *prepare_pipes |=
7961 1 << connector->new_encoder->new_crtc->pipe;
7962 }
7963
7964 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7965 base.head) {
7966 if (encoder->base.crtc == &encoder->new_crtc->base)
7967 continue;
7968
7969 if (encoder->base.crtc) {
7970 tmp_crtc = encoder->base.crtc;
7971
7972 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7973 }
7974
7975 if (encoder->new_crtc)
7976 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7977 }
7978
7979 /* Check for any pipes that will be fully disabled ... */
7980 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7981 base.head) {
7982 bool used = false;
7983
7984 /* Don't try to disable disabled crtcs. */
7985 if (!intel_crtc->base.enabled)
7986 continue;
7987
7988 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7989 base.head) {
7990 if (encoder->new_crtc == intel_crtc)
7991 used = true;
7992 }
7993
7994 if (!used)
7995 *disable_pipes |= 1 << intel_crtc->pipe;
7996 }
7997
7998
7999 /* set_mode is also used to update properties on life display pipes. */
8000 intel_crtc = to_intel_crtc(crtc);
8001 if (crtc->enabled)
8002 *prepare_pipes |= 1 << intel_crtc->pipe;
8003
Daniel Vetterb6c51642013-04-12 18:48:43 +02008004 /*
8005 * For simplicity do a full modeset on any pipe where the output routing
8006 * changed. We could be more clever, but that would require us to be
8007 * more careful with calling the relevant encoder->mode_set functions.
8008 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008009 if (*prepare_pipes)
8010 *modeset_pipes = *prepare_pipes;
8011
8012 /* ... and mask these out. */
8013 *modeset_pipes &= ~(*disable_pipes);
8014 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02008015
8016 /*
8017 * HACK: We don't (yet) fully support global modesets. intel_set_config
8018 * obies this rule, but the modeset restore mode of
8019 * intel_modeset_setup_hw_state does not.
8020 */
8021 *modeset_pipes &= 1 << intel_crtc->pipe;
8022 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02008023
8024 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
8025 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008026}
8027
Daniel Vetterea9d7582012-07-10 10:42:52 +02008028static bool intel_crtc_in_use(struct drm_crtc *crtc)
8029{
8030 struct drm_encoder *encoder;
8031 struct drm_device *dev = crtc->dev;
8032
8033 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
8034 if (encoder->crtc == crtc)
8035 return true;
8036
8037 return false;
8038}
8039
8040static void
8041intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
8042{
8043 struct intel_encoder *intel_encoder;
8044 struct intel_crtc *intel_crtc;
8045 struct drm_connector *connector;
8046
8047 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
8048 base.head) {
8049 if (!intel_encoder->base.crtc)
8050 continue;
8051
8052 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
8053
8054 if (prepare_pipes & (1 << intel_crtc->pipe))
8055 intel_encoder->connectors_active = false;
8056 }
8057
8058 intel_modeset_commit_output_state(dev);
8059
8060 /* Update computed state. */
8061 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
8062 base.head) {
8063 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
8064 }
8065
8066 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
8067 if (!connector->encoder || !connector->encoder->crtc)
8068 continue;
8069
8070 intel_crtc = to_intel_crtc(connector->encoder->crtc);
8071
8072 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02008073 struct drm_property *dpms_property =
8074 dev->mode_config.dpms_property;
8075
Daniel Vetterea9d7582012-07-10 10:42:52 +02008076 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05008077 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02008078 dpms_property,
8079 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02008080
8081 intel_encoder = to_intel_encoder(connector->encoder);
8082 intel_encoder->connectors_active = true;
8083 }
8084 }
8085
8086}
8087
Daniel Vetter25c5b262012-07-08 22:08:04 +02008088#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
8089 list_for_each_entry((intel_crtc), \
8090 &(dev)->mode_config.crtc_list, \
8091 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02008092 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02008093
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008094static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008095intel_pipe_config_compare(struct drm_device *dev,
8096 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008097 struct intel_crtc_config *pipe_config)
8098{
Daniel Vetter08a24032013-04-19 11:25:34 +02008099#define PIPE_CONF_CHECK_I(name) \
8100 if (current_config->name != pipe_config->name) { \
8101 DRM_ERROR("mismatch in " #name " " \
8102 "(expected %i, found %i)\n", \
8103 current_config->name, \
8104 pipe_config->name); \
8105 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01008106 }
8107
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008108#define PIPE_CONF_CHECK_FLAGS(name, mask) \
8109 if ((current_config->name ^ pipe_config->name) & (mask)) { \
8110 DRM_ERROR("mismatch in " #name " " \
8111 "(expected %i, found %i)\n", \
8112 current_config->name & (mask), \
8113 pipe_config->name & (mask)); \
8114 return false; \
8115 }
8116
Daniel Vetterbb760062013-06-06 14:55:52 +02008117#define PIPE_CONF_QUIRK(quirk) \
8118 ((current_config->quirks | pipe_config->quirks) & (quirk))
8119
Daniel Vettereccb1402013-05-22 00:50:22 +02008120 PIPE_CONF_CHECK_I(cpu_transcoder);
8121
Daniel Vetter08a24032013-04-19 11:25:34 +02008122 PIPE_CONF_CHECK_I(has_pch_encoder);
8123 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02008124 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
8125 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
8126 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
8127 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
8128 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02008129
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008130 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
8131 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
8132 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
8133 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
8134 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
8135 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
8136
8137 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
8138 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
8139 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
8140 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
8141 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
8142 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
8143
Daniel Vetter6c49f242013-06-06 12:45:25 +02008144 if (!HAS_PCH_SPLIT(dev))
8145 PIPE_CONF_CHECK_I(pixel_multiplier);
8146
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008147 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8148 DRM_MODE_FLAG_INTERLACE);
8149
Daniel Vetterbb760062013-06-06 14:55:52 +02008150 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8151 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8152 DRM_MODE_FLAG_PHSYNC);
8153 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8154 DRM_MODE_FLAG_NHSYNC);
8155 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8156 DRM_MODE_FLAG_PVSYNC);
8157 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
8158 DRM_MODE_FLAG_NVSYNC);
8159 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008160
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008161 PIPE_CONF_CHECK_I(requested_mode.hdisplay);
8162 PIPE_CONF_CHECK_I(requested_mode.vdisplay);
8163
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008164 PIPE_CONF_CHECK_I(gmch_pfit.control);
8165 /* pfit ratios are autocomputed by the hw on gen4+ */
8166 if (INTEL_INFO(dev)->gen < 4)
8167 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
8168 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
8169 PIPE_CONF_CHECK_I(pch_pfit.pos);
8170 PIPE_CONF_CHECK_I(pch_pfit.size);
8171
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008172 PIPE_CONF_CHECK_I(ips_enabled);
8173
Daniel Vetter08a24032013-04-19 11:25:34 +02008174#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008175#undef PIPE_CONF_CHECK_FLAGS
Daniel Vetterbb760062013-06-06 14:55:52 +02008176#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008177
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008178 return true;
8179}
8180
Daniel Vetterb9805142012-08-31 17:37:33 +02008181void
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008182intel_modeset_check_state(struct drm_device *dev)
8183{
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008184 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008185 struct intel_crtc *crtc;
8186 struct intel_encoder *encoder;
8187 struct intel_connector *connector;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008188 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008189
8190 list_for_each_entry(connector, &dev->mode_config.connector_list,
8191 base.head) {
8192 /* This also checks the encoder/connector hw state with the
8193 * ->get_hw_state callbacks. */
8194 intel_connector_check_state(connector);
8195
8196 WARN(&connector->new_encoder->base != connector->base.encoder,
8197 "connector's staged encoder doesn't match current encoder\n");
8198 }
8199
8200 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8201 base.head) {
8202 bool enabled = false;
8203 bool active = false;
8204 enum pipe pipe, tracked_pipe;
8205
8206 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
8207 encoder->base.base.id,
8208 drm_get_encoder_name(&encoder->base));
8209
8210 WARN(&encoder->new_crtc->base != encoder->base.crtc,
8211 "encoder's stage crtc doesn't match current crtc\n");
8212 WARN(encoder->connectors_active && !encoder->base.crtc,
8213 "encoder's active_connectors set, but no crtc\n");
8214
8215 list_for_each_entry(connector, &dev->mode_config.connector_list,
8216 base.head) {
8217 if (connector->base.encoder != &encoder->base)
8218 continue;
8219 enabled = true;
8220 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
8221 active = true;
8222 }
8223 WARN(!!encoder->base.crtc != enabled,
8224 "encoder's enabled state mismatch "
8225 "(expected %i, found %i)\n",
8226 !!encoder->base.crtc, enabled);
8227 WARN(active && !encoder->base.crtc,
8228 "active encoder with no crtc\n");
8229
8230 WARN(encoder->connectors_active != active,
8231 "encoder's computed active state doesn't match tracked active state "
8232 "(expected %i, found %i)\n", active, encoder->connectors_active);
8233
8234 active = encoder->get_hw_state(encoder, &pipe);
8235 WARN(active != encoder->connectors_active,
8236 "encoder's hw state doesn't match sw tracking "
8237 "(expected %i, found %i)\n",
8238 encoder->connectors_active, active);
8239
8240 if (!encoder->base.crtc)
8241 continue;
8242
8243 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8244 WARN(active && pipe != tracked_pipe,
8245 "active encoder's pipe doesn't match"
8246 "(expected %i, found %i)\n",
8247 tracked_pipe, pipe);
8248
8249 }
8250
8251 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8252 base.head) {
8253 bool enabled = false;
8254 bool active = false;
8255
Jesse Barnes045ac3b2013-05-14 17:08:26 -07008256 memset(&pipe_config, 0, sizeof(pipe_config));
8257
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008258 DRM_DEBUG_KMS("[CRTC:%d]\n",
8259 crtc->base.base.id);
8260
8261 WARN(crtc->active && !crtc->base.enabled,
8262 "active crtc, but not enabled in sw tracking\n");
8263
8264 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8265 base.head) {
8266 if (encoder->base.crtc != &crtc->base)
8267 continue;
8268 enabled = true;
8269 if (encoder->connectors_active)
8270 active = true;
8271 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008272
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008273 WARN(active != crtc->active,
8274 "crtc's computed active state doesn't match tracked active state "
8275 "(expected %i, found %i)\n", active, crtc->active);
8276 WARN(enabled != crtc->base.enabled,
8277 "crtc's computed enabled state doesn't match tracked enabled state "
8278 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8279
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008280 active = dev_priv->display.get_pipe_config(crtc,
8281 &pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008282 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8283 base.head) {
8284 if (encoder->base.crtc != &crtc->base)
8285 continue;
8286 if (encoder->get_config)
8287 encoder->get_config(encoder, &pipe_config);
8288 }
8289
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008290 WARN(crtc->active != active,
8291 "crtc active state doesn't match with hw state "
8292 "(expected %i, found %i)\n", crtc->active, active);
8293
Daniel Vetterc0b03412013-05-28 12:05:54 +02008294 if (active &&
8295 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
8296 WARN(1, "pipe state doesn't match!\n");
8297 intel_dump_pipe_config(crtc, &pipe_config,
8298 "[hw state]");
8299 intel_dump_pipe_config(crtc, &crtc->config,
8300 "[sw state]");
8301 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008302 }
8303}
8304
Daniel Vetterf30da182013-04-11 20:22:50 +02008305static int __intel_set_mode(struct drm_crtc *crtc,
8306 struct drm_display_mode *mode,
8307 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02008308{
8309 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02008310 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008311 struct drm_display_mode *saved_mode, *saved_hwmode;
8312 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008313 struct intel_crtc *intel_crtc;
8314 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008315 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02008316
Tim Gardner3ac18232012-12-07 07:54:26 -07008317 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008318 if (!saved_mode)
8319 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07008320 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02008321
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008322 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02008323 &prepare_pipes, &disable_pipes);
8324
Tim Gardner3ac18232012-12-07 07:54:26 -07008325 *saved_hwmode = crtc->hwmode;
8326 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008327
Daniel Vetter25c5b262012-07-08 22:08:04 +02008328 /* Hack: Because we don't (yet) support global modeset on multiple
8329 * crtcs, we don't keep track of the new mode for more than one crtc.
8330 * Hence simply check whether any bit is set in modeset_pipes in all the
8331 * pieces of code that are not yet converted to deal with mutliple crtcs
8332 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008333 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008334 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008335 if (IS_ERR(pipe_config)) {
8336 ret = PTR_ERR(pipe_config);
8337 pipe_config = NULL;
8338
Tim Gardner3ac18232012-12-07 07:54:26 -07008339 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008340 }
Daniel Vetterc0b03412013-05-28 12:05:54 +02008341 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
8342 "[modeset]");
Daniel Vettera6778b32012-07-02 09:56:42 +02008343 }
8344
Daniel Vetter460da9162013-03-27 00:44:51 +01008345 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8346 intel_crtc_disable(&intel_crtc->base);
8347
Daniel Vetterea9d7582012-07-10 10:42:52 +02008348 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8349 if (intel_crtc->base.enabled)
8350 dev_priv->display.crtc_disable(&intel_crtc->base);
8351 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008352
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02008353 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8354 * to set it here already despite that we pass it down the callchain.
8355 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008356 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +02008357 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008358 /* mode_set/enable/disable functions rely on a correct pipe
8359 * config. */
8360 to_intel_crtc(crtc)->config = *pipe_config;
8361 }
Daniel Vetter7758a112012-07-08 19:40:39 +02008362
Daniel Vetterea9d7582012-07-10 10:42:52 +02008363 /* Only after disabling all output pipelines that will be changed can we
8364 * update the the output configuration. */
8365 intel_modeset_update_state(dev, prepare_pipes);
8366
Daniel Vetter47fab732012-10-26 10:58:18 +02008367 if (dev_priv->display.modeset_global_resources)
8368 dev_priv->display.modeset_global_resources(dev);
8369
Daniel Vettera6778b32012-07-02 09:56:42 +02008370 /* Set up the DPLL and any encoders state that needs to adjust or depend
8371 * on the DPLL.
8372 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008373 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008374 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008375 x, y, fb);
8376 if (ret)
8377 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02008378 }
8379
8380 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008381 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8382 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02008383
Daniel Vetter25c5b262012-07-08 22:08:04 +02008384 if (modeset_pipes) {
8385 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008386 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008387
Daniel Vetter25c5b262012-07-08 22:08:04 +02008388 /* Calculate and store various constants which
8389 * are later needed by vblank and swap-completion
8390 * timestamping. They are derived from true hwmode.
8391 */
8392 drm_calc_timestamping_constants(crtc);
8393 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008394
8395 /* FIXME: add subpixel order */
8396done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008397 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07008398 crtc->hwmode = *saved_hwmode;
8399 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008400 }
8401
Tim Gardner3ac18232012-12-07 07:54:26 -07008402out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008403 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07008404 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02008405 return ret;
8406}
8407
Daniel Vetterf30da182013-04-11 20:22:50 +02008408int intel_set_mode(struct drm_crtc *crtc,
8409 struct drm_display_mode *mode,
8410 int x, int y, struct drm_framebuffer *fb)
8411{
8412 int ret;
8413
8414 ret = __intel_set_mode(crtc, mode, x, y, fb);
8415
8416 if (ret == 0)
8417 intel_modeset_check_state(crtc->dev);
8418
8419 return ret;
8420}
8421
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008422void intel_crtc_restore_mode(struct drm_crtc *crtc)
8423{
8424 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8425}
8426
Daniel Vetter25c5b262012-07-08 22:08:04 +02008427#undef for_each_intel_crtc_masked
8428
Daniel Vetterd9e55602012-07-04 22:16:09 +02008429static void intel_set_config_free(struct intel_set_config *config)
8430{
8431 if (!config)
8432 return;
8433
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008434 kfree(config->save_connector_encoders);
8435 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02008436 kfree(config);
8437}
8438
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008439static int intel_set_config_save_state(struct drm_device *dev,
8440 struct intel_set_config *config)
8441{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008442 struct drm_encoder *encoder;
8443 struct drm_connector *connector;
8444 int count;
8445
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008446 config->save_encoder_crtcs =
8447 kcalloc(dev->mode_config.num_encoder,
8448 sizeof(struct drm_crtc *), GFP_KERNEL);
8449 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008450 return -ENOMEM;
8451
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008452 config->save_connector_encoders =
8453 kcalloc(dev->mode_config.num_connector,
8454 sizeof(struct drm_encoder *), GFP_KERNEL);
8455 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008456 return -ENOMEM;
8457
8458 /* Copy data. Note that driver private data is not affected.
8459 * Should anything bad happen only the expected state is
8460 * restored, not the drivers personal bookkeeping.
8461 */
8462 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008463 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008464 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008465 }
8466
8467 count = 0;
8468 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008469 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008470 }
8471
8472 return 0;
8473}
8474
8475static void intel_set_config_restore_state(struct drm_device *dev,
8476 struct intel_set_config *config)
8477{
Daniel Vetter9a935852012-07-05 22:34:27 +02008478 struct intel_encoder *encoder;
8479 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008480 int count;
8481
8482 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008483 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8484 encoder->new_crtc =
8485 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008486 }
8487
8488 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008489 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8490 connector->new_encoder =
8491 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008492 }
8493}
8494
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008495static void
8496intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8497 struct intel_set_config *config)
8498{
8499
8500 /* We should be able to check here if the fb has the same properties
8501 * and then just flip_or_move it */
8502 if (set->crtc->fb != set->fb) {
8503 /* If we have no fb then treat it as a full mode set */
8504 if (set->crtc->fb == NULL) {
8505 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
8506 config->mode_changed = true;
8507 } else if (set->fb == NULL) {
8508 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01008509 } else if (set->fb->pixel_format !=
8510 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008511 config->mode_changed = true;
8512 } else
8513 config->fb_changed = true;
8514 }
8515
Daniel Vetter835c5872012-07-10 18:11:08 +02008516 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008517 config->fb_changed = true;
8518
8519 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8520 DRM_DEBUG_KMS("modes are different, full mode set\n");
8521 drm_mode_debug_printmodeline(&set->crtc->mode);
8522 drm_mode_debug_printmodeline(set->mode);
8523 config->mode_changed = true;
8524 }
8525}
8526
Daniel Vetter2e431052012-07-04 22:42:15 +02008527static int
Daniel Vetter9a935852012-07-05 22:34:27 +02008528intel_modeset_stage_output_state(struct drm_device *dev,
8529 struct drm_mode_set *set,
8530 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02008531{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008532 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008533 struct intel_connector *connector;
8534 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02008535 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02008536
Damien Lespiau9abdda72013-02-13 13:29:23 +00008537 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02008538 * of connectors. For paranoia, double-check this. */
8539 WARN_ON(!set->fb && (set->num_connectors != 0));
8540 WARN_ON(set->fb && (set->num_connectors == 0));
8541
Daniel Vetter50f56112012-07-02 09:35:43 +02008542 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008543 list_for_each_entry(connector, &dev->mode_config.connector_list,
8544 base.head) {
8545 /* Otherwise traverse passed in connector list and get encoders
8546 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008547 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008548 if (set->connectors[ro] == &connector->base) {
8549 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02008550 break;
8551 }
8552 }
8553
Daniel Vetter9a935852012-07-05 22:34:27 +02008554 /* If we disable the crtc, disable all its connectors. Also, if
8555 * the connector is on the changing crtc but not on the new
8556 * connector list, disable it. */
8557 if ((!set->fb || ro == set->num_connectors) &&
8558 connector->base.encoder &&
8559 connector->base.encoder->crtc == set->crtc) {
8560 connector->new_encoder = NULL;
8561
8562 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8563 connector->base.base.id,
8564 drm_get_connector_name(&connector->base));
8565 }
8566
8567
8568 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008569 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008570 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008571 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008572 }
8573 /* connector->new_encoder is now updated for all connectors. */
8574
8575 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008576 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008577 list_for_each_entry(connector, &dev->mode_config.connector_list,
8578 base.head) {
8579 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02008580 continue;
8581
Daniel Vetter9a935852012-07-05 22:34:27 +02008582 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02008583
8584 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008585 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02008586 new_crtc = set->crtc;
8587 }
8588
8589 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02008590 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8591 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008592 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02008593 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008594 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8595
8596 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8597 connector->base.base.id,
8598 drm_get_connector_name(&connector->base),
8599 new_crtc->base.id);
8600 }
8601
8602 /* Check for any encoders that needs to be disabled. */
8603 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8604 base.head) {
8605 list_for_each_entry(connector,
8606 &dev->mode_config.connector_list,
8607 base.head) {
8608 if (connector->new_encoder == encoder) {
8609 WARN_ON(!connector->new_encoder->new_crtc);
8610
8611 goto next_encoder;
8612 }
8613 }
8614 encoder->new_crtc = NULL;
8615next_encoder:
8616 /* Only now check for crtc changes so we don't miss encoders
8617 * that will be disabled. */
8618 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008619 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008620 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008621 }
8622 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008623 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008624
Daniel Vetter2e431052012-07-04 22:42:15 +02008625 return 0;
8626}
8627
8628static int intel_crtc_set_config(struct drm_mode_set *set)
8629{
8630 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02008631 struct drm_mode_set save_set;
8632 struct intel_set_config *config;
8633 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02008634
Daniel Vetter8d3e3752012-07-05 16:09:09 +02008635 BUG_ON(!set);
8636 BUG_ON(!set->crtc);
8637 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02008638
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01008639 /* Enforce sane interface api - has been abused by the fb helper. */
8640 BUG_ON(!set->mode && set->fb);
8641 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02008642
Daniel Vetter2e431052012-07-04 22:42:15 +02008643 if (set->fb) {
8644 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8645 set->crtc->base.id, set->fb->base.id,
8646 (int)set->num_connectors, set->x, set->y);
8647 } else {
8648 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02008649 }
8650
8651 dev = set->crtc->dev;
8652
8653 ret = -ENOMEM;
8654 config = kzalloc(sizeof(*config), GFP_KERNEL);
8655 if (!config)
8656 goto out_config;
8657
8658 ret = intel_set_config_save_state(dev, config);
8659 if (ret)
8660 goto out_config;
8661
8662 save_set.crtc = set->crtc;
8663 save_set.mode = &set->crtc->mode;
8664 save_set.x = set->crtc->x;
8665 save_set.y = set->crtc->y;
8666 save_set.fb = set->crtc->fb;
8667
8668 /* Compute whether we need a full modeset, only an fb base update or no
8669 * change at all. In the future we might also check whether only the
8670 * mode changed, e.g. for LVDS where we only change the panel fitter in
8671 * such cases. */
8672 intel_set_config_compute_mode_changes(set, config);
8673
Daniel Vetter9a935852012-07-05 22:34:27 +02008674 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02008675 if (ret)
8676 goto fail;
8677
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008678 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008679 ret = intel_set_mode(set->crtc, set->mode,
8680 set->x, set->y, set->fb);
8681 if (ret) {
8682 DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
8683 set->crtc->base.id, ret);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008684 goto fail;
8685 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008686 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02008687 intel_crtc_wait_for_pending_flips(set->crtc);
8688
Daniel Vetter4f660f42012-07-02 09:47:37 +02008689 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008690 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008691 }
8692
Daniel Vetterd9e55602012-07-04 22:16:09 +02008693 intel_set_config_free(config);
8694
Daniel Vetter50f56112012-07-02 09:35:43 +02008695 return 0;
8696
8697fail:
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008698 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008699
8700 /* Try to restore the config */
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008701 if (config->mode_changed &&
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008702 intel_set_mode(save_set.crtc, save_set.mode,
8703 save_set.x, save_set.y, save_set.fb))
Daniel Vetter50f56112012-07-02 09:35:43 +02008704 DRM_ERROR("failed to restore config after modeset failure\n");
8705
Daniel Vetterd9e55602012-07-04 22:16:09 +02008706out_config:
8707 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008708 return ret;
8709}
8710
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008711static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008712 .cursor_set = intel_crtc_cursor_set,
8713 .cursor_move = intel_crtc_cursor_move,
8714 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008715 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008716 .destroy = intel_crtc_destroy,
8717 .page_flip = intel_crtc_page_flip,
8718};
8719
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008720static void intel_cpu_pll_init(struct drm_device *dev)
8721{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008722 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008723 intel_ddi_pll_init(dev);
8724}
8725
Daniel Vettere72f9fb2013-06-05 13:34:06 +02008726static void intel_shared_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008727{
8728 drm_i915_private_t *dev_priv = dev->dev_private;
8729 int i;
8730
Daniel Vettere72f9fb2013-06-05 13:34:06 +02008731 if (dev_priv->num_shared_dpll == 0) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008732 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
8733 return;
8734 }
8735
Daniel Vettere72f9fb2013-06-05 13:34:06 +02008736 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
8737 dev_priv->shared_dplls[i].pll_reg = _PCH_DPLL(i);
8738 dev_priv->shared_dplls[i].fp0_reg = _PCH_FP0(i);
8739 dev_priv->shared_dplls[i].fp1_reg = _PCH_FP1(i);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008740 }
8741}
8742
Hannes Ederb358d0a2008-12-18 21:18:47 +01008743static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08008744{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008745 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008746 struct intel_crtc *intel_crtc;
8747 int i;
8748
8749 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8750 if (intel_crtc == NULL)
8751 return;
8752
8753 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8754
8755 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08008756 for (i = 0; i < 256; i++) {
8757 intel_crtc->lut_r[i] = i;
8758 intel_crtc->lut_g[i] = i;
8759 intel_crtc->lut_b[i] = i;
8760 }
8761
Jesse Barnes80824002009-09-10 15:28:06 -07008762 /* Swap pipes & planes for FBC on pre-965 */
8763 intel_crtc->pipe = pipe;
8764 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01008765 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008766 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01008767 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07008768 }
8769
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008770 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8771 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8772 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8773 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8774
Jesse Barnes79e53942008-11-07 14:24:08 -08008775 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08008776}
8777
Carl Worth08d7b3d2009-04-29 14:43:54 -07008778int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00008779 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07008780{
Carl Worth08d7b3d2009-04-29 14:43:54 -07008781 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02008782 struct drm_mode_object *drmmode_obj;
8783 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008784
Daniel Vetter1cff8f62012-04-24 09:55:08 +02008785 if (!drm_core_check_feature(dev, DRIVER_MODESET))
8786 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008787
Daniel Vetterc05422d2009-08-11 16:05:30 +02008788 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8789 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07008790
Daniel Vetterc05422d2009-08-11 16:05:30 +02008791 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07008792 DRM_ERROR("no such CRTC id\n");
8793 return -EINVAL;
8794 }
8795
Daniel Vetterc05422d2009-08-11 16:05:30 +02008796 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8797 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008798
Daniel Vetterc05422d2009-08-11 16:05:30 +02008799 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008800}
8801
Daniel Vetter66a92782012-07-12 20:08:18 +02008802static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008803{
Daniel Vetter66a92782012-07-12 20:08:18 +02008804 struct drm_device *dev = encoder->base.dev;
8805 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008806 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008807 int entry = 0;
8808
Daniel Vetter66a92782012-07-12 20:08:18 +02008809 list_for_each_entry(source_encoder,
8810 &dev->mode_config.encoder_list, base.head) {
8811
8812 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008813 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02008814
8815 /* Intel hw has only one MUX where enocoders could be cloned. */
8816 if (encoder->cloneable && source_encoder->cloneable)
8817 index_mask |= (1 << entry);
8818
Jesse Barnes79e53942008-11-07 14:24:08 -08008819 entry++;
8820 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01008821
Jesse Barnes79e53942008-11-07 14:24:08 -08008822 return index_mask;
8823}
8824
Chris Wilson4d302442010-12-14 19:21:29 +00008825static bool has_edp_a(struct drm_device *dev)
8826{
8827 struct drm_i915_private *dev_priv = dev->dev_private;
8828
8829 if (!IS_MOBILE(dev))
8830 return false;
8831
8832 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8833 return false;
8834
8835 if (IS_GEN5(dev) &&
8836 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8837 return false;
8838
8839 return true;
8840}
8841
Jesse Barnes79e53942008-11-07 14:24:08 -08008842static void intel_setup_outputs(struct drm_device *dev)
8843{
Eric Anholt725e30a2009-01-22 13:01:02 -08008844 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008845 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008846 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008847 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08008848
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008849 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00008850 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8851 /* disable the panel fitter on everything but LVDS */
8852 I915_WRITE(PFIT_CONTROL, 0);
8853 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008854
Paulo Zanonic40c0f52013-04-12 18:16:53 -03008855 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02008856 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008857
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008858 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03008859 int found;
8860
8861 /* Haswell uses DDI functions to detect digital outputs */
8862 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
8863 /* DDI A only supports eDP */
8864 if (found)
8865 intel_ddi_init(dev, PORT_A);
8866
8867 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
8868 * register */
8869 found = I915_READ(SFUSE_STRAP);
8870
8871 if (found & SFUSE_STRAP_DDIB_DETECTED)
8872 intel_ddi_init(dev, PORT_B);
8873 if (found & SFUSE_STRAP_DDIC_DETECTED)
8874 intel_ddi_init(dev, PORT_C);
8875 if (found & SFUSE_STRAP_DDID_DETECTED)
8876 intel_ddi_init(dev, PORT_D);
8877 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008878 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02008879 dpd_is_edp = intel_dpd_is_edp(dev);
8880
8881 if (has_edp_a(dev))
8882 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008883
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008884 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08008885 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01008886 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008887 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03008888 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008889 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008890 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008891 }
8892
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008893 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03008894 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008895
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008896 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03008897 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008898
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008899 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008900 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008901
Daniel Vetter270b3042012-10-27 15:52:05 +02008902 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008903 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008904 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05308905 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008906 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
8907 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05308908
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008909 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03008910 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
8911 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008912 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
8913 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008914 }
Zhenyu Wang103a1962009-11-27 11:44:36 +08008915 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008916 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008917
Paulo Zanonie2debe92013-02-18 19:00:27 -03008918 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008919 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008920 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008921 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8922 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008923 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008924 }
Ma Ling27185ae2009-08-24 13:50:23 +08008925
Imre Deake7281ea2013-05-08 13:14:08 +03008926 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008927 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -08008928 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008929
8930 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008931
Paulo Zanonie2debe92013-02-18 19:00:27 -03008932 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008933 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008934 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008935 }
Ma Ling27185ae2009-08-24 13:50:23 +08008936
Paulo Zanonie2debe92013-02-18 19:00:27 -03008937 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008938
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008939 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8940 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008941 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008942 }
Imre Deake7281ea2013-05-08 13:14:08 +03008943 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008944 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -08008945 }
Ma Ling27185ae2009-08-24 13:50:23 +08008946
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008947 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +03008948 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008949 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -07008950 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008951 intel_dvo_init(dev);
8952
Zhenyu Wang103a1962009-11-27 11:44:36 +08008953 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008954 intel_tv_init(dev);
8955
Chris Wilson4ef69c72010-09-09 15:14:28 +01008956 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8957 encoder->base.possible_crtcs = encoder->crtc_mask;
8958 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02008959 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08008960 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008961
Paulo Zanonidde86e22012-12-01 12:04:25 -02008962 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02008963
8964 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008965}
8966
8967static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8968{
8969 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008970
8971 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008972 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008973
8974 kfree(intel_fb);
8975}
8976
8977static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008978 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008979 unsigned int *handle)
8980{
8981 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008982 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008983
Chris Wilson05394f32010-11-08 19:18:58 +00008984 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008985}
8986
8987static const struct drm_framebuffer_funcs intel_fb_funcs = {
8988 .destroy = intel_user_framebuffer_destroy,
8989 .create_handle = intel_user_framebuffer_create_handle,
8990};
8991
Dave Airlie38651672010-03-30 05:34:13 +00008992int intel_framebuffer_init(struct drm_device *dev,
8993 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008994 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008995 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008996{
Jesse Barnes79e53942008-11-07 14:24:08 -08008997 int ret;
8998
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008999 if (obj->tiling_mode == I915_TILING_Y) {
9000 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01009001 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009002 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009003
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009004 if (mode_cmd->pitches[0] & 63) {
9005 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
9006 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01009007 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009008 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009009
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009010 /* FIXME <= Gen4 stride limits are bit unclear */
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009011 if (mode_cmd->pitches[0] > 32768) {
9012 DRM_DEBUG("pitch (%d) must be at less than 32768\n",
9013 mode_cmd->pitches[0]);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009014 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009015 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009016
9017 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009018 mode_cmd->pitches[0] != obj->stride) {
9019 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
9020 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009021 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009022 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02009023
Ville Syrjälä57779d02012-10-31 17:50:14 +02009024 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009025 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02009026 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009027 case DRM_FORMAT_RGB565:
9028 case DRM_FORMAT_XRGB8888:
9029 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009030 break;
9031 case DRM_FORMAT_XRGB1555:
9032 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009033 if (INTEL_INFO(dev)->gen > 3) {
9034 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02009035 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009036 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02009037 break;
9038 case DRM_FORMAT_XBGR8888:
9039 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02009040 case DRM_FORMAT_XRGB2101010:
9041 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02009042 case DRM_FORMAT_XBGR2101010:
9043 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009044 if (INTEL_INFO(dev)->gen < 4) {
9045 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02009046 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009047 }
Jesse Barnesb5626742011-06-24 12:19:27 -07009048 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02009049 case DRM_FORMAT_YUYV:
9050 case DRM_FORMAT_UYVY:
9051 case DRM_FORMAT_YVYU:
9052 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009053 if (INTEL_INFO(dev)->gen < 5) {
9054 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02009055 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009056 }
Chris Wilson57cd6502010-08-08 12:34:44 +01009057 break;
9058 default:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00009059 DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01009060 return -EINVAL;
9061 }
9062
Ville Syrjälä90f9a332012-10-31 17:50:19 +02009063 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
9064 if (mode_cmd->offsets[0] != 0)
9065 return -EINVAL;
9066
Daniel Vetterc7d73f62012-12-13 23:38:38 +01009067 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
9068 intel_fb->obj = obj;
9069
Jesse Barnes79e53942008-11-07 14:24:08 -08009070 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
9071 if (ret) {
9072 DRM_ERROR("framebuffer init failed %d\n", ret);
9073 return ret;
9074 }
9075
Jesse Barnes79e53942008-11-07 14:24:08 -08009076 return 0;
9077}
9078
Jesse Barnes79e53942008-11-07 14:24:08 -08009079static struct drm_framebuffer *
9080intel_user_framebuffer_create(struct drm_device *dev,
9081 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009082 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08009083{
Chris Wilson05394f32010-11-08 19:18:58 +00009084 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08009085
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009086 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
9087 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00009088 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01009089 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08009090
Chris Wilsond2dff872011-04-19 08:36:26 +01009091 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08009092}
9093
Jesse Barnes79e53942008-11-07 14:24:08 -08009094static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08009095 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00009096 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08009097};
9098
Jesse Barnese70236a2009-09-21 10:42:27 -07009099/* Set up chip specific display functions */
9100static void intel_init_display(struct drm_device *dev)
9101{
9102 struct drm_i915_private *dev_priv = dev->dev_private;
9103
Daniel Vetteree9300b2013-06-03 22:40:22 +02009104 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
9105 dev_priv->display.find_dpll = g4x_find_best_dpll;
9106 else if (IS_VALLEYVIEW(dev))
9107 dev_priv->display.find_dpll = vlv_find_best_dpll;
9108 else if (IS_PINEVIEW(dev))
9109 dev_priv->display.find_dpll = pnv_find_best_dpll;
9110 else
9111 dev_priv->display.find_dpll = i9xx_find_best_dpll;
9112
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009113 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009114 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009115 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02009116 dev_priv->display.crtc_enable = haswell_crtc_enable;
9117 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009118 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009119 dev_priv->display.update_plane = ironlake_update_plane;
9120 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009121 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -07009122 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009123 dev_priv->display.crtc_enable = ironlake_crtc_enable;
9124 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009125 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009126 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -07009127 } else if (IS_VALLEYVIEW(dev)) {
9128 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
9129 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
9130 dev_priv->display.crtc_enable = valleyview_crtc_enable;
9131 dev_priv->display.crtc_disable = i9xx_crtc_disable;
9132 dev_priv->display.off = i9xx_crtc_off;
9133 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009134 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009135 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -07009136 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02009137 dev_priv->display.crtc_enable = i9xx_crtc_enable;
9138 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009139 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07009140 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07009141 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009142
Jesse Barnese70236a2009-09-21 10:42:27 -07009143 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07009144 if (IS_VALLEYVIEW(dev))
9145 dev_priv->display.get_display_clock_speed =
9146 valleyview_get_display_clock_speed;
9147 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07009148 dev_priv->display.get_display_clock_speed =
9149 i945_get_display_clock_speed;
9150 else if (IS_I915G(dev))
9151 dev_priv->display.get_display_clock_speed =
9152 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009153 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009154 dev_priv->display.get_display_clock_speed =
9155 i9xx_misc_get_display_clock_speed;
9156 else if (IS_I915GM(dev))
9157 dev_priv->display.get_display_clock_speed =
9158 i915gm_get_display_clock_speed;
9159 else if (IS_I865G(dev))
9160 dev_priv->display.get_display_clock_speed =
9161 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02009162 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07009163 dev_priv->display.get_display_clock_speed =
9164 i855_get_display_clock_speed;
9165 else /* 852, 830 */
9166 dev_priv->display.get_display_clock_speed =
9167 i830_get_display_clock_speed;
9168
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08009169 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01009170 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009171 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009172 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08009173 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07009174 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009175 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07009176 } else if (IS_IVYBRIDGE(dev)) {
9177 /* FIXME: detect B0+ stepping and use auto training */
9178 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08009179 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02009180 dev_priv->display.modeset_global_resources =
9181 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03009182 } else if (IS_HASWELL(dev)) {
9183 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08009184 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02009185 dev_priv->display.modeset_global_resources =
9186 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02009187 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07009188 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08009189 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07009190 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009191
9192 /* Default just returns -ENODEV to indicate unsupported */
9193 dev_priv->display.queue_flip = intel_default_queue_flip;
9194
9195 switch (INTEL_INFO(dev)->gen) {
9196 case 2:
9197 dev_priv->display.queue_flip = intel_gen2_queue_flip;
9198 break;
9199
9200 case 3:
9201 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9202 break;
9203
9204 case 4:
9205 case 5:
9206 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9207 break;
9208
9209 case 6:
9210 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9211 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009212 case 7:
9213 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9214 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009215 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009216}
9217
Jesse Barnesb690e962010-07-19 13:53:12 -07009218/*
9219 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9220 * resume, or other times. This quirk makes sure that's the case for
9221 * affected systems.
9222 */
Akshay Joshi0206e352011-08-16 15:34:10 -04009223static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07009224{
9225 struct drm_i915_private *dev_priv = dev->dev_private;
9226
9227 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009228 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009229}
9230
Keith Packard435793d2011-07-12 14:56:22 -07009231/*
9232 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9233 */
9234static void quirk_ssc_force_disable(struct drm_device *dev)
9235{
9236 struct drm_i915_private *dev_priv = dev->dev_private;
9237 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009238 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07009239}
9240
Carsten Emde4dca20e2012-03-15 15:56:26 +01009241/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01009242 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9243 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01009244 */
9245static void quirk_invert_brightness(struct drm_device *dev)
9246{
9247 struct drm_i915_private *dev_priv = dev->dev_private;
9248 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009249 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009250}
9251
9252struct intel_quirk {
9253 int device;
9254 int subsystem_vendor;
9255 int subsystem_device;
9256 void (*hook)(struct drm_device *dev);
9257};
9258
Egbert Eich5f85f172012-10-14 15:46:38 +02009259/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9260struct intel_dmi_quirk {
9261 void (*hook)(struct drm_device *dev);
9262 const struct dmi_system_id (*dmi_id_list)[];
9263};
9264
9265static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9266{
9267 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9268 return 1;
9269}
9270
9271static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9272 {
9273 .dmi_id_list = &(const struct dmi_system_id[]) {
9274 {
9275 .callback = intel_dmi_reverse_brightness,
9276 .ident = "NCR Corporation",
9277 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9278 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9279 },
9280 },
9281 { } /* terminating entry */
9282 },
9283 .hook = quirk_invert_brightness,
9284 },
9285};
9286
Ben Widawskyc43b5632012-04-16 14:07:40 -07009287static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07009288 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04009289 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07009290
Jesse Barnesb690e962010-07-19 13:53:12 -07009291 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9292 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9293
Jesse Barnesb690e962010-07-19 13:53:12 -07009294 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9295 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9296
Daniel Vetterccd0d362012-10-10 23:13:59 +02009297 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07009298 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02009299 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07009300
9301 /* Lenovo U160 cannot use SSC on LVDS */
9302 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02009303
9304 /* Sony Vaio Y cannot use SSC on LVDS */
9305 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01009306
9307 /* Acer Aspire 5734Z must invert backlight brightness */
9308 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02009309
9310 /* Acer/eMachines G725 */
9311 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02009312
9313 /* Acer/eMachines e725 */
9314 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02009315
9316 /* Acer/Packard Bell NCL20 */
9317 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Daniel Vetterac4199e2013-02-15 18:35:30 +01009318
9319 /* Acer Aspire 4736Z */
9320 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07009321};
9322
9323static void intel_init_quirks(struct drm_device *dev)
9324{
9325 struct pci_dev *d = dev->pdev;
9326 int i;
9327
9328 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9329 struct intel_quirk *q = &intel_quirks[i];
9330
9331 if (d->device == q->device &&
9332 (d->subsystem_vendor == q->subsystem_vendor ||
9333 q->subsystem_vendor == PCI_ANY_ID) &&
9334 (d->subsystem_device == q->subsystem_device ||
9335 q->subsystem_device == PCI_ANY_ID))
9336 q->hook(dev);
9337 }
Egbert Eich5f85f172012-10-14 15:46:38 +02009338 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9339 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9340 intel_dmi_quirks[i].hook(dev);
9341 }
Jesse Barnesb690e962010-07-19 13:53:12 -07009342}
9343
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009344/* Disable the VGA plane that we never use */
9345static void i915_disable_vga(struct drm_device *dev)
9346{
9347 struct drm_i915_private *dev_priv = dev->dev_private;
9348 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009349 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009350
9351 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07009352 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009353 sr1 = inb(VGA_SR_DATA);
9354 outb(sr1 | 1<<5, VGA_SR_DATA);
9355 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9356 udelay(300);
9357
9358 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9359 POSTING_READ(vga_reg);
9360}
9361
Daniel Vetterf8175862012-04-10 15:50:11 +02009362void intel_modeset_init_hw(struct drm_device *dev)
9363{
Paulo Zanonifa42e232013-01-25 16:59:11 -02009364 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03009365
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03009366 intel_prepare_ddi(dev);
9367
Daniel Vetterf8175862012-04-10 15:50:11 +02009368 intel_init_clock_gating(dev);
9369
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009370 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009371 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009372 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02009373}
9374
Imre Deak7d708ee2013-04-17 14:04:50 +03009375void intel_modeset_suspend_hw(struct drm_device *dev)
9376{
9377 intel_suspend_hw(dev);
9378}
9379
Jesse Barnes79e53942008-11-07 14:24:08 -08009380void intel_modeset_init(struct drm_device *dev)
9381{
Jesse Barnes652c3932009-08-17 13:31:43 -07009382 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009383 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009384
9385 drm_mode_config_init(dev);
9386
9387 dev->mode_config.min_width = 0;
9388 dev->mode_config.min_height = 0;
9389
Dave Airlie019d96c2011-09-29 16:20:42 +01009390 dev->mode_config.preferred_depth = 24;
9391 dev->mode_config.prefer_shadow = 1;
9392
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02009393 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08009394
Jesse Barnesb690e962010-07-19 13:53:12 -07009395 intel_init_quirks(dev);
9396
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009397 intel_init_pm(dev);
9398
Ben Widawskye3c74752013-04-05 13:12:39 -07009399 if (INTEL_INFO(dev)->num_pipes == 0)
9400 return;
9401
Jesse Barnese70236a2009-09-21 10:42:27 -07009402 intel_init_display(dev);
9403
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009404 if (IS_GEN2(dev)) {
9405 dev->mode_config.max_width = 2048;
9406 dev->mode_config.max_height = 2048;
9407 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009408 dev->mode_config.max_width = 4096;
9409 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009410 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009411 dev->mode_config.max_width = 8192;
9412 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009413 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08009414 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009415
Zhao Yakui28c97732009-10-09 11:39:41 +08009416 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009417 INTEL_INFO(dev)->num_pipes,
9418 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009419
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009420 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009421 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009422 for (j = 0; j < dev_priv->num_plane; j++) {
9423 ret = intel_plane_init(dev, i, j);
9424 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +03009425 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9426 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009427 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009428 }
9429
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009430 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02009431 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009432
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009433 /* Just disable it once at startup */
9434 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009435 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00009436
9437 /* Just in case the BIOS is doing something questionable. */
9438 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009439}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009440
Daniel Vetter24929352012-07-02 20:28:59 +02009441static void
9442intel_connector_break_all_links(struct intel_connector *connector)
9443{
9444 connector->base.dpms = DRM_MODE_DPMS_OFF;
9445 connector->base.encoder = NULL;
9446 connector->encoder->connectors_active = false;
9447 connector->encoder->base.crtc = NULL;
9448}
9449
Daniel Vetter7fad7982012-07-04 17:51:47 +02009450static void intel_enable_pipe_a(struct drm_device *dev)
9451{
9452 struct intel_connector *connector;
9453 struct drm_connector *crt = NULL;
9454 struct intel_load_detect_pipe load_detect_temp;
9455
9456 /* We can't just switch on the pipe A, we need to set things up with a
9457 * proper mode and output configuration. As a gross hack, enable pipe A
9458 * by enabling the load detect pipe once. */
9459 list_for_each_entry(connector,
9460 &dev->mode_config.connector_list,
9461 base.head) {
9462 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9463 crt = &connector->base;
9464 break;
9465 }
9466 }
9467
9468 if (!crt)
9469 return;
9470
9471 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9472 intel_release_load_detect_pipe(crt, &load_detect_temp);
9473
9474
9475}
9476
Daniel Vetterfa555832012-10-10 23:14:00 +02009477static bool
9478intel_check_plane_mapping(struct intel_crtc *crtc)
9479{
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009480 struct drm_device *dev = crtc->base.dev;
9481 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009482 u32 reg, val;
9483
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009484 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +02009485 return true;
9486
9487 reg = DSPCNTR(!crtc->plane);
9488 val = I915_READ(reg);
9489
9490 if ((val & DISPLAY_PLANE_ENABLE) &&
9491 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9492 return false;
9493
9494 return true;
9495}
9496
Daniel Vetter24929352012-07-02 20:28:59 +02009497static void intel_sanitize_crtc(struct intel_crtc *crtc)
9498{
9499 struct drm_device *dev = crtc->base.dev;
9500 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009501 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02009502
Daniel Vetter24929352012-07-02 20:28:59 +02009503 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +02009504 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02009505 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9506
9507 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02009508 * disable the crtc (and hence change the state) if it is wrong. Note
9509 * that gen4+ has a fixed plane -> pipe mapping. */
9510 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02009511 struct intel_connector *connector;
9512 bool plane;
9513
Daniel Vetter24929352012-07-02 20:28:59 +02009514 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9515 crtc->base.base.id);
9516
9517 /* Pipe has the wrong plane attached and the plane is active.
9518 * Temporarily change the plane mapping and disable everything
9519 * ... */
9520 plane = crtc->plane;
9521 crtc->plane = !plane;
9522 dev_priv->display.crtc_disable(&crtc->base);
9523 crtc->plane = plane;
9524
9525 /* ... and break all links. */
9526 list_for_each_entry(connector, &dev->mode_config.connector_list,
9527 base.head) {
9528 if (connector->encoder->base.crtc != &crtc->base)
9529 continue;
9530
9531 intel_connector_break_all_links(connector);
9532 }
9533
9534 WARN_ON(crtc->active);
9535 crtc->base.enabled = false;
9536 }
Daniel Vetter24929352012-07-02 20:28:59 +02009537
Daniel Vetter7fad7982012-07-04 17:51:47 +02009538 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9539 crtc->pipe == PIPE_A && !crtc->active) {
9540 /* BIOS forgot to enable pipe A, this mostly happens after
9541 * resume. Force-enable the pipe to fix this, the update_dpms
9542 * call below we restore the pipe to the right state, but leave
9543 * the required bits on. */
9544 intel_enable_pipe_a(dev);
9545 }
9546
Daniel Vetter24929352012-07-02 20:28:59 +02009547 /* Adjust the state of the output pipe according to whether we
9548 * have active connectors/encoders. */
9549 intel_crtc_update_dpms(&crtc->base);
9550
9551 if (crtc->active != crtc->base.enabled) {
9552 struct intel_encoder *encoder;
9553
9554 /* This can happen either due to bugs in the get_hw_state
9555 * functions or because the pipe is force-enabled due to the
9556 * pipe A quirk. */
9557 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9558 crtc->base.base.id,
9559 crtc->base.enabled ? "enabled" : "disabled",
9560 crtc->active ? "enabled" : "disabled");
9561
9562 crtc->base.enabled = crtc->active;
9563
9564 /* Because we only establish the connector -> encoder ->
9565 * crtc links if something is active, this means the
9566 * crtc is now deactivated. Break the links. connector
9567 * -> encoder links are only establish when things are
9568 * actually up, hence no need to break them. */
9569 WARN_ON(crtc->active);
9570
9571 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9572 WARN_ON(encoder->connectors_active);
9573 encoder->base.crtc = NULL;
9574 }
9575 }
9576}
9577
9578static void intel_sanitize_encoder(struct intel_encoder *encoder)
9579{
9580 struct intel_connector *connector;
9581 struct drm_device *dev = encoder->base.dev;
9582
9583 /* We need to check both for a crtc link (meaning that the
9584 * encoder is active and trying to read from a pipe) and the
9585 * pipe itself being active. */
9586 bool has_active_crtc = encoder->base.crtc &&
9587 to_intel_crtc(encoder->base.crtc)->active;
9588
9589 if (encoder->connectors_active && !has_active_crtc) {
9590 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9591 encoder->base.base.id,
9592 drm_get_encoder_name(&encoder->base));
9593
9594 /* Connector is active, but has no active pipe. This is
9595 * fallout from our resume register restoring. Disable
9596 * the encoder manually again. */
9597 if (encoder->base.crtc) {
9598 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9599 encoder->base.base.id,
9600 drm_get_encoder_name(&encoder->base));
9601 encoder->disable(encoder);
9602 }
9603
9604 /* Inconsistent output/port/pipe state happens presumably due to
9605 * a bug in one of the get_hw_state functions. Or someplace else
9606 * in our code, like the register restore mess on resume. Clamp
9607 * things to off as a safer default. */
9608 list_for_each_entry(connector,
9609 &dev->mode_config.connector_list,
9610 base.head) {
9611 if (connector->encoder != encoder)
9612 continue;
9613
9614 intel_connector_break_all_links(connector);
9615 }
9616 }
9617 /* Enabled encoders without active connectors will be fixed in
9618 * the crtc fixup. */
9619}
9620
Daniel Vetter44cec742013-01-25 17:53:21 +01009621void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009622{
9623 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009624 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009625
9626 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9627 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +02009628 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009629 }
9630}
9631
Daniel Vetter24929352012-07-02 20:28:59 +02009632/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
9633 * and i915 state tracking structures. */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009634void intel_modeset_setup_hw_state(struct drm_device *dev,
9635 bool force_restore)
Daniel Vetter24929352012-07-02 20:28:59 +02009636{
9637 struct drm_i915_private *dev_priv = dev->dev_private;
9638 enum pipe pipe;
Jesse Barnesb5644d02013-03-26 13:25:27 -07009639 struct drm_plane *plane;
Daniel Vetter24929352012-07-02 20:28:59 +02009640 struct intel_crtc *crtc;
9641 struct intel_encoder *encoder;
9642 struct intel_connector *connector;
9643
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009644 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9645 base.head) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01009646 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +02009647
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009648 crtc->active = dev_priv->display.get_pipe_config(crtc,
9649 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009650
9651 crtc->base.enabled = crtc->active;
9652
9653 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9654 crtc->base.base.id,
9655 crtc->active ? "enabled" : "disabled");
9656 }
9657
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009658 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009659 intel_ddi_setup_hw_pll_state(dev);
9660
Daniel Vetter24929352012-07-02 20:28:59 +02009661 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9662 base.head) {
9663 pipe = 0;
9664
9665 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -07009666 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9667 encoder->base.crtc = &crtc->base;
9668 if (encoder->get_config)
9669 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009670 } else {
9671 encoder->base.crtc = NULL;
9672 }
9673
9674 encoder->connectors_active = false;
9675 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9676 encoder->base.base.id,
9677 drm_get_encoder_name(&encoder->base),
9678 encoder->base.crtc ? "enabled" : "disabled",
9679 pipe);
9680 }
9681
9682 list_for_each_entry(connector, &dev->mode_config.connector_list,
9683 base.head) {
9684 if (connector->get_hw_state(connector)) {
9685 connector->base.dpms = DRM_MODE_DPMS_ON;
9686 connector->encoder->connectors_active = true;
9687 connector->base.encoder = &connector->encoder->base;
9688 } else {
9689 connector->base.dpms = DRM_MODE_DPMS_OFF;
9690 connector->base.encoder = NULL;
9691 }
9692 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9693 connector->base.base.id,
9694 drm_get_connector_name(&connector->base),
9695 connector->base.encoder ? "enabled" : "disabled");
9696 }
9697
9698 /* HW state is read out, now we need to sanitize this mess. */
9699 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9700 base.head) {
9701 intel_sanitize_encoder(encoder);
9702 }
9703
9704 for_each_pipe(pipe) {
9705 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9706 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009707 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +02009708 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009709
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009710 if (force_restore) {
Daniel Vetterf30da182013-04-11 20:22:50 +02009711 /*
9712 * We need to use raw interfaces for restoring state to avoid
9713 * checking (bogus) intermediate states.
9714 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009715 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -07009716 struct drm_crtc *crtc =
9717 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +02009718
9719 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
9720 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009721 }
Jesse Barnesb5644d02013-03-26 13:25:27 -07009722 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
9723 intel_plane_restore(plane);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009724
9725 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009726 } else {
9727 intel_modeset_update_staged_output_state(dev);
9728 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009729
9730 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +02009731
9732 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009733}
9734
9735void intel_modeset_gem_init(struct drm_device *dev)
9736{
Chris Wilson1833b132012-05-09 11:56:28 +01009737 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02009738
9739 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +02009740
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009741 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08009742}
9743
9744void intel_modeset_cleanup(struct drm_device *dev)
9745{
Jesse Barnes652c3932009-08-17 13:31:43 -07009746 struct drm_i915_private *dev_priv = dev->dev_private;
9747 struct drm_crtc *crtc;
9748 struct intel_crtc *intel_crtc;
9749
Daniel Vetterfd0c0642013-04-24 11:13:35 +02009750 /*
9751 * Interrupts and polling as the first thing to avoid creating havoc.
9752 * Too much stuff here (turning of rps, connectors, ...) would
9753 * experience fancy races otherwise.
9754 */
9755 drm_irq_uninstall(dev);
9756 cancel_work_sync(&dev_priv->hotplug_work);
9757 /*
9758 * Due to the hpd irq storm handling the hotplug work can re-arm the
9759 * poll handlers. Hence disable polling after hpd handling is shut down.
9760 */
Keith Packardf87ea762010-10-03 19:36:26 -07009761 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +02009762
Jesse Barnes652c3932009-08-17 13:31:43 -07009763 mutex_lock(&dev->struct_mutex);
9764
Jesse Barnes723bfd72010-10-07 16:01:13 -07009765 intel_unregister_dsm_handler();
9766
Jesse Barnes652c3932009-08-17 13:31:43 -07009767 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9768 /* Skip inactive CRTCs */
9769 if (!crtc->fb)
9770 continue;
9771
9772 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02009773 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009774 }
9775
Chris Wilson973d04f2011-07-08 12:22:37 +01009776 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07009777
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009778 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00009779
Daniel Vetter930ebb42012-06-29 23:32:16 +02009780 ironlake_teardown_rc6(dev);
9781
Kristian Høgsberg69341a52009-11-11 12:19:17 -05009782 mutex_unlock(&dev->struct_mutex);
9783
Chris Wilson1630fe72011-07-08 12:22:42 +01009784 /* flush any delayed tasks or pending work */
9785 flush_scheduled_work();
9786
Jani Nikuladc652f92013-04-12 15:18:38 +03009787 /* destroy backlight, if any, before the connectors */
9788 intel_panel_destroy_backlight(dev);
9789
Jesse Barnes79e53942008-11-07 14:24:08 -08009790 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +01009791
9792 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009793}
9794
Dave Airlie28d52042009-09-21 14:33:58 +10009795/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08009796 * Return which encoder is currently attached for connector.
9797 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01009798struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08009799{
Chris Wilsondf0e9242010-09-09 16:20:55 +01009800 return &intel_attached_encoder(connector)->base;
9801}
Jesse Barnes79e53942008-11-07 14:24:08 -08009802
Chris Wilsondf0e9242010-09-09 16:20:55 +01009803void intel_connector_attach_encoder(struct intel_connector *connector,
9804 struct intel_encoder *encoder)
9805{
9806 connector->encoder = encoder;
9807 drm_mode_connector_attach_encoder(&connector->base,
9808 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009809}
Dave Airlie28d52042009-09-21 14:33:58 +10009810
9811/*
9812 * set vga decode state - true == enable VGA decode
9813 */
9814int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9815{
9816 struct drm_i915_private *dev_priv = dev->dev_private;
9817 u16 gmch_ctrl;
9818
9819 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9820 if (state)
9821 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9822 else
9823 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9824 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9825 return 0;
9826}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009827
9828#ifdef CONFIG_DEBUG_FS
9829#include <linux/seq_file.h>
9830
9831struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -03009832
9833 u32 power_well_driver;
9834
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009835 struct intel_cursor_error_state {
9836 u32 control;
9837 u32 position;
9838 u32 base;
9839 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +01009840 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009841
9842 struct intel_pipe_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -03009843 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009844 u32 conf;
9845 u32 source;
9846
9847 u32 htotal;
9848 u32 hblank;
9849 u32 hsync;
9850 u32 vtotal;
9851 u32 vblank;
9852 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +01009853 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009854
9855 struct intel_plane_error_state {
9856 u32 control;
9857 u32 stride;
9858 u32 size;
9859 u32 pos;
9860 u32 addr;
9861 u32 surface;
9862 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +01009863 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009864};
9865
9866struct intel_display_error_state *
9867intel_display_capture_error_state(struct drm_device *dev)
9868{
Akshay Joshi0206e352011-08-16 15:34:10 -04009869 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009870 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009871 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009872 int i;
9873
9874 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9875 if (error == NULL)
9876 return NULL;
9877
Paulo Zanoniff57f1b2013-05-03 12:15:37 -03009878 if (HAS_POWER_WELL(dev))
9879 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
9880
Damien Lespiau52331302012-08-15 19:23:25 +01009881 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009882 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -03009883 error->pipe[i].cpu_transcoder = cpu_transcoder;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009884
Paulo Zanonia18c4c32013-03-06 20:03:12 -03009885 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
9886 error->cursor[i].control = I915_READ(CURCNTR(i));
9887 error->cursor[i].position = I915_READ(CURPOS(i));
9888 error->cursor[i].base = I915_READ(CURBASE(i));
9889 } else {
9890 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
9891 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
9892 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
9893 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009894
9895 error->plane[i].control = I915_READ(DSPCNTR(i));
9896 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009897 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -03009898 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009899 error->plane[i].pos = I915_READ(DSPPOS(i));
9900 }
Paulo Zanonica291362013-03-06 20:03:14 -03009901 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
9902 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009903 if (INTEL_INFO(dev)->gen >= 4) {
9904 error->plane[i].surface = I915_READ(DSPSURF(i));
9905 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9906 }
9907
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009908 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009909 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009910 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
9911 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
9912 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
9913 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
9914 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
9915 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009916 }
9917
Paulo Zanoni12d217c2013-05-03 12:15:38 -03009918 /* In the code above we read the registers without checking if the power
9919 * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
9920 * prevent the next I915_WRITE from detecting it and printing an error
9921 * message. */
9922 if (HAS_POWER_WELL(dev))
9923 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
9924
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009925 return error;
9926}
9927
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03009928#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
9929
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009930void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03009931intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009932 struct drm_device *dev,
9933 struct intel_display_error_state *error)
9934{
9935 int i;
9936
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03009937 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Paulo Zanoniff57f1b2013-05-03 12:15:37 -03009938 if (HAS_POWER_WELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03009939 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -03009940 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +01009941 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03009942 err_printf(m, "Pipe [%d]:\n", i);
9943 err_printf(m, " CPU transcoder: %c\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -03009944 transcoder_name(error->pipe[i].cpu_transcoder));
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03009945 err_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9946 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
9947 err_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9948 err_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9949 err_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9950 err_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9951 err_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9952 err_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009953
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03009954 err_printf(m, "Plane [%d]:\n", i);
9955 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
9956 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009957 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03009958 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
9959 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009960 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -03009961 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03009962 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009963 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03009964 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
9965 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009966 }
9967
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03009968 err_printf(m, "Cursor [%d]:\n", i);
9969 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9970 err_printf(m, " POS: %08x\n", error->cursor[i].position);
9971 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009972 }
9973}
9974#endif