blob: 2a01b09221fbd05317236402e0da0ea5c867b32b [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 /* given values */
50 int n;
51 int m1, m2;
52 int p1, p2;
53 /* derived values */
54 int dot;
55 int vco;
56 int m;
57 int p;
Jesse Barnes79e53942008-11-07 14:24:08 -080058} intel_clock_t;
59
60typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040061 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080062} intel_range_t;
63
64typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040065 int dot_limit;
66 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080067} intel_p2_t;
68
69#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080070typedef struct intel_limit intel_limit_t;
71struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040072 intel_range_t dot, vco, n, m, m1, m2, p, p1;
73 intel_p2_t p2;
74 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
Sean Paulcec2f352012-01-10 15:09:36 -080075 int, int, intel_clock_t *, intel_clock_t *);
Ma Lingd4906092009-03-18 20:13:27 +080076};
Jesse Barnes79e53942008-11-07 14:24:08 -080077
Jesse Barnes2377b742010-07-07 14:06:43 -070078/* FDI */
79#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
80
Daniel Vetterd2acd212012-10-20 20:57:43 +020081int
82intel_pch_rawclk(struct drm_device *dev)
83{
84 struct drm_i915_private *dev_priv = dev->dev_private;
85
86 WARN_ON(!HAS_PCH_SPLIT(dev));
87
88 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
89}
90
Ma Lingd4906092009-03-18 20:13:27 +080091static bool
92intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080093 int target, int refclk, intel_clock_t *match_clock,
94 intel_clock_t *best_clock);
Ma Lingd4906092009-03-18 20:13:27 +080095static bool
96intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080097 int target, int refclk, intel_clock_t *match_clock,
98 intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080099
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700100static bool
101intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800102 int target, int refclk, intel_clock_t *match_clock,
103 intel_clock_t *best_clock);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800104static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500105intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800106 int target, int refclk, intel_clock_t *match_clock,
107 intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700108
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700109static bool
110intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
111 int target, int refclk, intel_clock_t *match_clock,
112 intel_clock_t *best_clock);
113
Chris Wilson021357a2010-09-07 20:54:59 +0100114static inline u32 /* units of 100MHz */
115intel_fdi_link_freq(struct drm_device *dev)
116{
Chris Wilson8b99e682010-10-13 09:59:17 +0100117 if (IS_GEN5(dev)) {
118 struct drm_i915_private *dev_priv = dev->dev_private;
119 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
120 } else
121 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100122}
123
Keith Packarde4b36692009-06-05 19:22:17 -0700124static const intel_limit_t intel_limits_i8xx_dvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 .dot = { .min = 25000, .max = 350000 },
126 .vco = { .min = 930000, .max = 1400000 },
127 .n = { .min = 3, .max = 16 },
128 .m = { .min = 96, .max = 140 },
129 .m1 = { .min = 18, .max = 26 },
130 .m2 = { .min = 6, .max = 16 },
131 .p = { .min = 4, .max = 128 },
132 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700133 .p2 = { .dot_limit = 165000,
134 .p2_slow = 4, .p2_fast = 2 },
Ma Lingd4906092009-03-18 20:13:27 +0800135 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700136};
137
138static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400139 .dot = { .min = 25000, .max = 350000 },
140 .vco = { .min = 930000, .max = 1400000 },
141 .n = { .min = 3, .max = 16 },
142 .m = { .min = 96, .max = 140 },
143 .m1 = { .min = 18, .max = 26 },
144 .m2 = { .min = 6, .max = 16 },
145 .p = { .min = 4, .max = 128 },
146 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700147 .p2 = { .dot_limit = 165000,
148 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800149 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700150};
Eric Anholt273e27c2011-03-30 13:01:10 -0700151
Keith Packarde4b36692009-06-05 19:22:17 -0700152static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400153 .dot = { .min = 20000, .max = 400000 },
154 .vco = { .min = 1400000, .max = 2800000 },
155 .n = { .min = 1, .max = 6 },
156 .m = { .min = 70, .max = 120 },
157 .m1 = { .min = 10, .max = 22 },
158 .m2 = { .min = 5, .max = 9 },
159 .p = { .min = 5, .max = 80 },
160 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700161 .p2 = { .dot_limit = 200000,
162 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800163 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700164};
165
166static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400167 .dot = { .min = 20000, .max = 400000 },
168 .vco = { .min = 1400000, .max = 2800000 },
169 .n = { .min = 1, .max = 6 },
170 .m = { .min = 70, .max = 120 },
171 .m1 = { .min = 10, .max = 22 },
172 .m2 = { .min = 5, .max = 9 },
173 .p = { .min = 7, .max = 98 },
174 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700175 .p2 = { .dot_limit = 112000,
176 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800177 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700178};
179
Eric Anholt273e27c2011-03-30 13:01:10 -0700180
Keith Packarde4b36692009-06-05 19:22:17 -0700181static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700182 .dot = { .min = 25000, .max = 270000 },
183 .vco = { .min = 1750000, .max = 3500000},
184 .n = { .min = 1, .max = 4 },
185 .m = { .min = 104, .max = 138 },
186 .m1 = { .min = 17, .max = 23 },
187 .m2 = { .min = 5, .max = 11 },
188 .p = { .min = 10, .max = 30 },
189 .p1 = { .min = 1, .max = 3},
190 .p2 = { .dot_limit = 270000,
191 .p2_slow = 10,
192 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800193 },
Ma Lingd4906092009-03-18 20:13:27 +0800194 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700195};
196
197static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700198 .dot = { .min = 22000, .max = 400000 },
199 .vco = { .min = 1750000, .max = 3500000},
200 .n = { .min = 1, .max = 4 },
201 .m = { .min = 104, .max = 138 },
202 .m1 = { .min = 16, .max = 23 },
203 .m2 = { .min = 5, .max = 11 },
204 .p = { .min = 5, .max = 80 },
205 .p1 = { .min = 1, .max = 8},
206 .p2 = { .dot_limit = 165000,
207 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800208 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700209};
210
211static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700212 .dot = { .min = 20000, .max = 115000 },
213 .vco = { .min = 1750000, .max = 3500000 },
214 .n = { .min = 1, .max = 3 },
215 .m = { .min = 104, .max = 138 },
216 .m1 = { .min = 17, .max = 23 },
217 .m2 = { .min = 5, .max = 11 },
218 .p = { .min = 28, .max = 112 },
219 .p1 = { .min = 2, .max = 8 },
220 .p2 = { .dot_limit = 0,
221 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800222 },
Ma Lingd4906092009-03-18 20:13:27 +0800223 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700224};
225
226static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700227 .dot = { .min = 80000, .max = 224000 },
228 .vco = { .min = 1750000, .max = 3500000 },
229 .n = { .min = 1, .max = 3 },
230 .m = { .min = 104, .max = 138 },
231 .m1 = { .min = 17, .max = 23 },
232 .m2 = { .min = 5, .max = 11 },
233 .p = { .min = 14, .max = 42 },
234 .p1 = { .min = 2, .max = 6 },
235 .p2 = { .dot_limit = 0,
236 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800237 },
Ma Lingd4906092009-03-18 20:13:27 +0800238 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700239};
240
241static const intel_limit_t intel_limits_g4x_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400242 .dot = { .min = 161670, .max = 227000 },
243 .vco = { .min = 1750000, .max = 3500000},
244 .n = { .min = 1, .max = 2 },
245 .m = { .min = 97, .max = 108 },
246 .m1 = { .min = 0x10, .max = 0x12 },
247 .m2 = { .min = 0x05, .max = 0x06 },
248 .p = { .min = 10, .max = 20 },
249 .p1 = { .min = 1, .max = 2},
250 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700251 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400252 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700253};
254
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500255static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400256 .dot = { .min = 20000, .max = 400000},
257 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700258 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400259 .n = { .min = 3, .max = 6 },
260 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400262 .m1 = { .min = 0, .max = 0 },
263 .m2 = { .min = 0, .max = 254 },
264 .p = { .min = 5, .max = 80 },
265 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700266 .p2 = { .dot_limit = 200000,
267 .p2_slow = 10, .p2_fast = 5 },
Shaohua Li61157072009-04-03 15:24:43 +0800268 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700269};
270
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500271static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400272 .dot = { .min = 20000, .max = 400000 },
273 .vco = { .min = 1700000, .max = 3500000 },
274 .n = { .min = 3, .max = 6 },
275 .m = { .min = 2, .max = 256 },
276 .m1 = { .min = 0, .max = 0 },
277 .m2 = { .min = 0, .max = 254 },
278 .p = { .min = 7, .max = 112 },
279 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700280 .p2 = { .dot_limit = 112000,
281 .p2_slow = 14, .p2_fast = 14 },
Shaohua Li61157072009-04-03 15:24:43 +0800282 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700283};
284
Eric Anholt273e27c2011-03-30 13:01:10 -0700285/* Ironlake / Sandybridge
286 *
287 * We calculate clock using (register_value + 2) for N/M1/M2, so here
288 * the range value for them is (actual_value - 2).
289 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800290static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700291 .dot = { .min = 25000, .max = 350000 },
292 .vco = { .min = 1760000, .max = 3510000 },
293 .n = { .min = 1, .max = 5 },
294 .m = { .min = 79, .max = 127 },
295 .m1 = { .min = 12, .max = 22 },
296 .m2 = { .min = 5, .max = 9 },
297 .p = { .min = 5, .max = 80 },
298 .p1 = { .min = 1, .max = 8 },
299 .p2 = { .dot_limit = 225000,
300 .p2_slow = 10, .p2_fast = 5 },
Zhao Yakui45476682009-12-31 16:06:04 +0800301 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700302};
303
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800304static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700305 .dot = { .min = 25000, .max = 350000 },
306 .vco = { .min = 1760000, .max = 3510000 },
307 .n = { .min = 1, .max = 3 },
308 .m = { .min = 79, .max = 118 },
309 .m1 = { .min = 12, .max = 22 },
310 .m2 = { .min = 5, .max = 9 },
311 .p = { .min = 28, .max = 112 },
312 .p1 = { .min = 2, .max = 8 },
313 .p2 = { .dot_limit = 225000,
314 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800315 .find_pll = intel_g4x_find_best_PLL,
316};
317
318static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700319 .dot = { .min = 25000, .max = 350000 },
320 .vco = { .min = 1760000, .max = 3510000 },
321 .n = { .min = 1, .max = 3 },
322 .m = { .min = 79, .max = 127 },
323 .m1 = { .min = 12, .max = 22 },
324 .m2 = { .min = 5, .max = 9 },
325 .p = { .min = 14, .max = 56 },
326 .p1 = { .min = 2, .max = 8 },
327 .p2 = { .dot_limit = 225000,
328 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800329 .find_pll = intel_g4x_find_best_PLL,
330};
331
Eric Anholt273e27c2011-03-30 13:01:10 -0700332/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800333static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700334 .dot = { .min = 25000, .max = 350000 },
335 .vco = { .min = 1760000, .max = 3510000 },
336 .n = { .min = 1, .max = 2 },
337 .m = { .min = 79, .max = 126 },
338 .m1 = { .min = 12, .max = 22 },
339 .m2 = { .min = 5, .max = 9 },
340 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400341 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700342 .p2 = { .dot_limit = 225000,
343 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800344 .find_pll = intel_g4x_find_best_PLL,
345};
346
347static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700348 .dot = { .min = 25000, .max = 350000 },
349 .vco = { .min = 1760000, .max = 3510000 },
350 .n = { .min = 1, .max = 3 },
351 .m = { .min = 79, .max = 126 },
352 .m1 = { .min = 12, .max = 22 },
353 .m2 = { .min = 5, .max = 9 },
354 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400355 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700356 .p2 = { .dot_limit = 225000,
357 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800358 .find_pll = intel_g4x_find_best_PLL,
359};
360
361static const intel_limit_t intel_limits_ironlake_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400362 .dot = { .min = 25000, .max = 350000 },
363 .vco = { .min = 1760000, .max = 3510000},
364 .n = { .min = 1, .max = 2 },
365 .m = { .min = 81, .max = 90 },
366 .m1 = { .min = 12, .max = 22 },
367 .m2 = { .min = 5, .max = 9 },
368 .p = { .min = 10, .max = 20 },
369 .p1 = { .min = 1, .max = 2},
370 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400372 .find_pll = intel_find_pll_ironlake_dp,
Jesse Barnes79e53942008-11-07 14:24:08 -0800373};
374
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700375static const intel_limit_t intel_limits_vlv_dac = {
376 .dot = { .min = 25000, .max = 270000 },
377 .vco = { .min = 4000000, .max = 6000000 },
378 .n = { .min = 1, .max = 7 },
379 .m = { .min = 22, .max = 450 }, /* guess */
380 .m1 = { .min = 2, .max = 3 },
381 .m2 = { .min = 11, .max = 156 },
382 .p = { .min = 10, .max = 30 },
383 .p1 = { .min = 2, .max = 3 },
384 .p2 = { .dot_limit = 270000,
385 .p2_slow = 2, .p2_fast = 20 },
386 .find_pll = intel_vlv_find_best_pll,
387};
388
389static const intel_limit_t intel_limits_vlv_hdmi = {
390 .dot = { .min = 20000, .max = 165000 },
Vijay Purushothaman17dc9252012-09-27 19:13:09 +0530391 .vco = { .min = 4000000, .max = 5994000},
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700392 .n = { .min = 1, .max = 7 },
393 .m = { .min = 60, .max = 300 }, /* guess */
394 .m1 = { .min = 2, .max = 3 },
395 .m2 = { .min = 11, .max = 156 },
396 .p = { .min = 10, .max = 30 },
397 .p1 = { .min = 2, .max = 3 },
398 .p2 = { .dot_limit = 270000,
399 .p2_slow = 2, .p2_fast = 20 },
400 .find_pll = intel_vlv_find_best_pll,
401};
402
403static const intel_limit_t intel_limits_vlv_dp = {
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530404 .dot = { .min = 25000, .max = 270000 },
405 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700406 .n = { .min = 1, .max = 7 },
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530407 .m = { .min = 22, .max = 450 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700408 .m1 = { .min = 2, .max = 3 },
409 .m2 = { .min = 11, .max = 156 },
410 .p = { .min = 10, .max = 30 },
411 .p1 = { .min = 2, .max = 3 },
412 .p2 = { .dot_limit = 270000,
413 .p2_slow = 2, .p2_fast = 20 },
414 .find_pll = intel_vlv_find_best_pll,
415};
416
Jesse Barnes57f350b2012-03-28 13:39:25 -0700417u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
418{
Daniel Vetter09153002012-12-12 14:06:44 +0100419 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnes57f350b2012-03-28 13:39:25 -0700420
Jesse Barnes57f350b2012-03-28 13:39:25 -0700421 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
422 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100423 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700424 }
425
426 I915_WRITE(DPIO_REG, reg);
427 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
428 DPIO_BYTE);
429 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
430 DRM_ERROR("DPIO read wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100431 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700432 }
Jesse Barnes57f350b2012-03-28 13:39:25 -0700433
Daniel Vetter09153002012-12-12 14:06:44 +0100434 return I915_READ(DPIO_DATA);
Jesse Barnes57f350b2012-03-28 13:39:25 -0700435}
436
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700437static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
438 u32 val)
439{
Daniel Vetter09153002012-12-12 14:06:44 +0100440 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700441
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700442 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
443 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100444 return;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700445 }
446
447 I915_WRITE(DPIO_DATA, val);
448 I915_WRITE(DPIO_REG, reg);
449 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
450 DPIO_BYTE);
451 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
452 DRM_ERROR("DPIO write wait timed out\n");
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700453}
454
Jesse Barnes57f350b2012-03-28 13:39:25 -0700455static void vlv_init_dpio(struct drm_device *dev)
456{
457 struct drm_i915_private *dev_priv = dev->dev_private;
458
459 /* Reset the DPIO config */
460 I915_WRITE(DPIO_CTL, 0);
461 POSTING_READ(DPIO_CTL);
462 I915_WRITE(DPIO_CTL, 1);
463 POSTING_READ(DPIO_CTL);
464}
465
Chris Wilson1b894b52010-12-14 20:04:54 +0000466static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
467 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800468{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800469 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800470 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800471
472 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100473 if (intel_is_dual_link_lvds(dev)) {
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800474 /* LVDS dual channel */
Chris Wilson1b894b52010-12-14 20:04:54 +0000475 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800476 limit = &intel_limits_ironlake_dual_lvds_100m;
477 else
478 limit = &intel_limits_ironlake_dual_lvds;
479 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000480 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800481 limit = &intel_limits_ironlake_single_lvds_100m;
482 else
483 limit = &intel_limits_ironlake_single_lvds;
484 }
485 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Jani Nikula547dc042012-11-02 11:24:03 +0200486 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Zhao Yakui45476682009-12-31 16:06:04 +0800487 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800488 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800489 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800490
491 return limit;
492}
493
Ma Ling044c7c42009-03-18 20:13:23 +0800494static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
495{
496 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800497 const intel_limit_t *limit;
498
499 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100500 if (intel_is_dual_link_lvds(dev))
Ma Ling044c7c42009-03-18 20:13:23 +0800501 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700502 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800503 else
504 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700505 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800506 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
507 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700508 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800509 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700510 limit = &intel_limits_g4x_sdvo;
Akshay Joshi0206e352011-08-16 15:34:10 -0400511 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800513 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800515
516 return limit;
517}
518
Chris Wilson1b894b52010-12-14 20:04:54 +0000519static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800520{
521 struct drm_device *dev = crtc->dev;
522 const intel_limit_t *limit;
523
Eric Anholtbad720f2009-10-22 16:11:14 -0700524 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000525 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800526 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800527 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500528 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800529 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500530 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800531 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500532 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700533 } else if (IS_VALLEYVIEW(dev)) {
534 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
535 limit = &intel_limits_vlv_dac;
536 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
537 limit = &intel_limits_vlv_hdmi;
538 else
539 limit = &intel_limits_vlv_dp;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100540 } else if (!IS_GEN2(dev)) {
541 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
542 limit = &intel_limits_i9xx_lvds;
543 else
544 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800545 } else {
546 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700547 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800548 else
Keith Packarde4b36692009-06-05 19:22:17 -0700549 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800550 }
551 return limit;
552}
553
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500554/* m1 is reserved as 0 in Pineview, n is a ring counter */
555static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800556{
Shaohua Li21778322009-02-23 15:19:16 +0800557 clock->m = clock->m2 + 2;
558 clock->p = clock->p1 * clock->p2;
559 clock->vco = refclk * clock->m / clock->n;
560 clock->dot = clock->vco / clock->p;
561}
562
563static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
564{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500565 if (IS_PINEVIEW(dev)) {
566 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800567 return;
568 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800569 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
570 clock->p = clock->p1 * clock->p2;
571 clock->vco = refclk * clock->m / (clock->n + 2);
572 clock->dot = clock->vco / clock->p;
573}
574
Jesse Barnes79e53942008-11-07 14:24:08 -0800575/**
576 * Returns whether any output on the specified pipe is of the specified type
577 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100578bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800579{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100580 struct drm_device *dev = crtc->dev;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100581 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800582
Daniel Vetter6c2b7c12012-07-05 09:50:24 +0200583 for_each_encoder_on_crtc(dev, crtc, encoder)
584 if (encoder->type == type)
Chris Wilson4ef69c72010-09-09 15:14:28 +0100585 return true;
586
587 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800588}
589
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800590#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800591/**
592 * Returns whether the given set of divisors are valid for a given refclk with
593 * the given connectors.
594 */
595
Chris Wilson1b894b52010-12-14 20:04:54 +0000596static bool intel_PLL_is_valid(struct drm_device *dev,
597 const intel_limit_t *limit,
598 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800599{
Jesse Barnes79e53942008-11-07 14:24:08 -0800600 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400601 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800602 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400603 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800604 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400605 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800606 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400607 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500608 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400609 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400615 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
617 * connector, etc., rather than just a single range.
618 */
619 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400620 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800621
622 return true;
623}
624
Ma Lingd4906092009-03-18 20:13:27 +0800625static bool
626intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800627 int target, int refclk, intel_clock_t *match_clock,
628 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800629
Jesse Barnes79e53942008-11-07 14:24:08 -0800630{
631 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800633 int err = target;
634
Daniel Vettera210b022012-11-26 17:22:08 +0100635 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800636 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100637 * For LVDS just rely on its current settings for dual-channel.
638 * We haven't figured out how to reliably set up different
639 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800640 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100641 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800642 clock.p2 = limit->p2.p2_fast;
643 else
644 clock.p2 = limit->p2.p2_slow;
645 } else {
646 if (target < limit->p2.dot_limit)
647 clock.p2 = limit->p2.p2_slow;
648 else
649 clock.p2 = limit->p2.p2_fast;
650 }
651
Akshay Joshi0206e352011-08-16 15:34:10 -0400652 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800653
Zhao Yakui42158662009-11-20 11:24:18 +0800654 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
655 clock.m1++) {
656 for (clock.m2 = limit->m2.min;
657 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500658 /* m1 is always 0 in Pineview */
659 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800660 break;
661 for (clock.n = limit->n.min;
662 clock.n <= limit->n.max; clock.n++) {
663 for (clock.p1 = limit->p1.min;
664 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800665 int this_err;
666
Shaohua Li21778322009-02-23 15:19:16 +0800667 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000668 if (!intel_PLL_is_valid(dev, limit,
669 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800670 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800671 if (match_clock &&
672 clock.p != match_clock->p)
673 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800674
675 this_err = abs(clock.dot - target);
676 if (this_err < err) {
677 *best_clock = clock;
678 err = this_err;
679 }
680 }
681 }
682 }
683 }
684
685 return (err != target);
686}
687
Ma Lingd4906092009-03-18 20:13:27 +0800688static bool
689intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800690 int target, int refclk, intel_clock_t *match_clock,
691 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800692{
693 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800694 intel_clock_t clock;
695 int max_n;
696 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400697 /* approximately equals target * 0.00585 */
698 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800699 found = false;
700
701 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800702 int lvds_reg;
703
Eric Anholtc619eed2010-01-28 16:45:52 -0800704 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800705 lvds_reg = PCH_LVDS;
706 else
707 lvds_reg = LVDS;
Daniel Vetter1974cad2012-11-26 17:22:09 +0100708 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800709 clock.p2 = limit->p2.p2_fast;
710 else
711 clock.p2 = limit->p2.p2_slow;
712 } else {
713 if (target < limit->p2.dot_limit)
714 clock.p2 = limit->p2.p2_slow;
715 else
716 clock.p2 = limit->p2.p2_fast;
717 }
718
719 memset(best_clock, 0, sizeof(*best_clock));
720 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200721 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800722 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200723 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800724 for (clock.m1 = limit->m1.max;
725 clock.m1 >= limit->m1.min; clock.m1--) {
726 for (clock.m2 = limit->m2.max;
727 clock.m2 >= limit->m2.min; clock.m2--) {
728 for (clock.p1 = limit->p1.max;
729 clock.p1 >= limit->p1.min; clock.p1--) {
730 int this_err;
731
Shaohua Li21778322009-02-23 15:19:16 +0800732 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000733 if (!intel_PLL_is_valid(dev, limit,
734 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800735 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800736 if (match_clock &&
737 clock.p != match_clock->p)
738 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000739
740 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800741 if (this_err < err_most) {
742 *best_clock = clock;
743 err_most = this_err;
744 max_n = clock.n;
745 found = true;
746 }
747 }
748 }
749 }
750 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800751 return found;
752}
Ma Lingd4906092009-03-18 20:13:27 +0800753
Zhenyu Wang2c072452009-06-05 15:38:42 +0800754static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500755intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800756 int target, int refclk, intel_clock_t *match_clock,
757 intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800758{
759 struct drm_device *dev = crtc->dev;
760 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800761
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800762 if (target < 200000) {
763 clock.n = 1;
764 clock.p1 = 2;
765 clock.p2 = 10;
766 clock.m1 = 12;
767 clock.m2 = 9;
768 } else {
769 clock.n = 2;
770 clock.p1 = 1;
771 clock.p2 = 10;
772 clock.m1 = 14;
773 clock.m2 = 8;
774 }
775 intel_clock(dev, refclk, &clock);
776 memcpy(best_clock, &clock, sizeof(intel_clock_t));
777 return true;
778}
779
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700780/* DisplayPort has only two frequencies, 162MHz and 270MHz */
781static bool
782intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800783 int target, int refclk, intel_clock_t *match_clock,
784 intel_clock_t *best_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700785{
Chris Wilson5eddb702010-09-11 13:48:45 +0100786 intel_clock_t clock;
787 if (target < 200000) {
788 clock.p1 = 2;
789 clock.p2 = 10;
790 clock.n = 2;
791 clock.m1 = 23;
792 clock.m2 = 8;
793 } else {
794 clock.p1 = 1;
795 clock.p2 = 10;
796 clock.n = 1;
797 clock.m1 = 14;
798 clock.m2 = 2;
799 }
800 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
801 clock.p = (clock.p1 * clock.p2);
802 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
803 clock.vco = 0;
804 memcpy(best_clock, &clock, sizeof(intel_clock_t));
805 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700806}
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700807static bool
808intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
809 int target, int refclk, intel_clock_t *match_clock,
810 intel_clock_t *best_clock)
811{
812 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
813 u32 m, n, fastclk;
814 u32 updrate, minupdate, fracbits, p;
815 unsigned long bestppm, ppm, absppm;
816 int dotclk, flag;
817
Alan Coxaf447bd2012-07-25 13:49:18 +0100818 flag = 0;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700819 dotclk = target * 1000;
820 bestppm = 1000000;
821 ppm = absppm = 0;
822 fastclk = dotclk / (2*100);
823 updrate = 0;
824 minupdate = 19200;
825 fracbits = 1;
826 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
827 bestm1 = bestm2 = bestp1 = bestp2 = 0;
828
829 /* based on hardware requirement, prefer smaller n to precision */
830 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
831 updrate = refclk / n;
832 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
833 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
834 if (p2 > 10)
835 p2 = p2 - 1;
836 p = p1 * p2;
837 /* based on hardware requirement, prefer bigger m1,m2 values */
838 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
839 m2 = (((2*(fastclk * p * n / m1 )) +
840 refclk) / (2*refclk));
841 m = m1 * m2;
842 vco = updrate * m;
843 if (vco >= limit->vco.min && vco < limit->vco.max) {
844 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
845 absppm = (ppm > 0) ? ppm : (-ppm);
846 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
847 bestppm = 0;
848 flag = 1;
849 }
850 if (absppm < bestppm - 10) {
851 bestppm = absppm;
852 flag = 1;
853 }
854 if (flag) {
855 bestn = n;
856 bestm1 = m1;
857 bestm2 = m2;
858 bestp1 = p1;
859 bestp2 = p2;
860 flag = 0;
861 }
862 }
863 }
864 }
865 }
866 }
867 best_clock->n = bestn;
868 best_clock->m1 = bestm1;
869 best_clock->m2 = bestm2;
870 best_clock->p1 = bestp1;
871 best_clock->p2 = bestp2;
872
873 return true;
874}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700875
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200876enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
877 enum pipe pipe)
878{
879 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
881
882 return intel_crtc->cpu_transcoder;
883}
884
Paulo Zanonia928d532012-05-04 17:18:15 -0300885static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
886{
887 struct drm_i915_private *dev_priv = dev->dev_private;
888 u32 frame, frame_reg = PIPEFRAME(pipe);
889
890 frame = I915_READ(frame_reg);
891
892 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
893 DRM_DEBUG_KMS("vblank wait timed out\n");
894}
895
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700896/**
897 * intel_wait_for_vblank - wait for vblank on a given pipe
898 * @dev: drm device
899 * @pipe: pipe to wait for
900 *
901 * Wait for vblank to occur on a given pipe. Needed for various bits of
902 * mode setting code.
903 */
904void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800905{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700906 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800907 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700908
Paulo Zanonia928d532012-05-04 17:18:15 -0300909 if (INTEL_INFO(dev)->gen >= 5) {
910 ironlake_wait_for_vblank(dev, pipe);
911 return;
912 }
913
Chris Wilson300387c2010-09-05 20:25:43 +0100914 /* Clear existing vblank status. Note this will clear any other
915 * sticky status fields as well.
916 *
917 * This races with i915_driver_irq_handler() with the result
918 * that either function could miss a vblank event. Here it is not
919 * fatal, as we will either wait upon the next vblank interrupt or
920 * timeout. Generally speaking intel_wait_for_vblank() is only
921 * called during modeset at which time the GPU should be idle and
922 * should *not* be performing page flips and thus not waiting on
923 * vblanks...
924 * Currently, the result of us stealing a vblank from the irq
925 * handler is that a single frame will be skipped during swapbuffers.
926 */
927 I915_WRITE(pipestat_reg,
928 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
929
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700930 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100931 if (wait_for(I915_READ(pipestat_reg) &
932 PIPE_VBLANK_INTERRUPT_STATUS,
933 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700934 DRM_DEBUG_KMS("vblank wait timed out\n");
935}
936
Keith Packardab7ad7f2010-10-03 00:33:06 -0700937/*
938 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700939 * @dev: drm device
940 * @pipe: pipe to wait for
941 *
942 * After disabling a pipe, we can't wait for vblank in the usual way,
943 * spinning on the vblank interrupt status bit, since we won't actually
944 * see an interrupt when the pipe is disabled.
945 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700946 * On Gen4 and above:
947 * wait for the pipe register state bit to turn off
948 *
949 * Otherwise:
950 * wait for the display line value to settle (it usually
951 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100952 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700953 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100954void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700955{
956 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200957 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
958 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700959
Keith Packardab7ad7f2010-10-03 00:33:06 -0700960 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200961 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700962
Keith Packardab7ad7f2010-10-03 00:33:06 -0700963 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100964 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
965 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200966 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700967 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300968 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100969 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700970 unsigned long timeout = jiffies + msecs_to_jiffies(100);
971
Paulo Zanoni837ba002012-05-04 17:18:14 -0300972 if (IS_GEN2(dev))
973 line_mask = DSL_LINEMASK_GEN2;
974 else
975 line_mask = DSL_LINEMASK_GEN3;
976
Keith Packardab7ad7f2010-10-03 00:33:06 -0700977 /* Wait for the display line to settle */
978 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300979 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700980 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300981 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700982 time_after(timeout, jiffies));
983 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200984 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700985 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800986}
987
Jesse Barnesb24e7172011-01-04 15:09:30 -0800988static const char *state_string(bool enabled)
989{
990 return enabled ? "on" : "off";
991}
992
993/* Only for pre-ILK configs */
994static void assert_pll(struct drm_i915_private *dev_priv,
995 enum pipe pipe, bool state)
996{
997 int reg;
998 u32 val;
999 bool cur_state;
1000
1001 reg = DPLL(pipe);
1002 val = I915_READ(reg);
1003 cur_state = !!(val & DPLL_VCO_ENABLE);
1004 WARN(cur_state != state,
1005 "PLL state assertion failure (expected %s, current %s)\n",
1006 state_string(state), state_string(cur_state));
1007}
1008#define assert_pll_enabled(d, p) assert_pll(d, p, true)
1009#define assert_pll_disabled(d, p) assert_pll(d, p, false)
1010
Jesse Barnes040484a2011-01-03 12:14:26 -08001011/* For ILK+ */
1012static void assert_pch_pll(struct drm_i915_private *dev_priv,
Chris Wilson92b27b02012-05-20 18:10:50 +01001013 struct intel_pch_pll *pll,
1014 struct intel_crtc *crtc,
1015 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001016{
Jesse Barnes040484a2011-01-03 12:14:26 -08001017 u32 val;
1018 bool cur_state;
1019
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001020 if (HAS_PCH_LPT(dev_priv->dev)) {
1021 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1022 return;
1023 }
1024
Chris Wilson92b27b02012-05-20 18:10:50 +01001025 if (WARN (!pll,
1026 "asserting PCH PLL %s with no PLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001027 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001028
Chris Wilson92b27b02012-05-20 18:10:50 +01001029 val = I915_READ(pll->pll_reg);
1030 cur_state = !!(val & DPLL_VCO_ENABLE);
1031 WARN(cur_state != state,
1032 "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
1033 pll->pll_reg, state_string(state), state_string(cur_state), val);
1034
1035 /* Make sure the selected PLL is correctly attached to the transcoder */
1036 if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001037 u32 pch_dpll;
1038
1039 pch_dpll = I915_READ(PCH_DPLL_SEL);
Chris Wilson92b27b02012-05-20 18:10:50 +01001040 cur_state = pll->pll_reg == _PCH_DPLL_B;
1041 if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
1042 "PLL[%d] not attached to this transcoder %d: %08x\n",
1043 cur_state, crtc->pipe, pch_dpll)) {
1044 cur_state = !!(val >> (4*crtc->pipe + 3));
1045 WARN(cur_state != state,
1046 "PLL[%d] not %s on this transcoder %d: %08x\n",
1047 pll->pll_reg == _PCH_DPLL_B,
1048 state_string(state),
1049 crtc->pipe,
1050 val);
1051 }
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001052 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001053}
Chris Wilson92b27b02012-05-20 18:10:50 +01001054#define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
1055#define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
Jesse Barnes040484a2011-01-03 12:14:26 -08001056
1057static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1058 enum pipe pipe, bool state)
1059{
1060 int reg;
1061 u32 val;
1062 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001063 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1064 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001065
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001066 if (HAS_DDI(dev_priv->dev)) {
1067 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001068 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001069 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001070 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001071 } else {
1072 reg = FDI_TX_CTL(pipe);
1073 val = I915_READ(reg);
1074 cur_state = !!(val & FDI_TX_ENABLE);
1075 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001076 WARN(cur_state != state,
1077 "FDI TX state assertion failure (expected %s, current %s)\n",
1078 state_string(state), state_string(cur_state));
1079}
1080#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1081#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1082
1083static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1084 enum pipe pipe, bool state)
1085{
1086 int reg;
1087 u32 val;
1088 bool cur_state;
1089
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001090 reg = FDI_RX_CTL(pipe);
1091 val = I915_READ(reg);
1092 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001093 WARN(cur_state != state,
1094 "FDI RX state assertion failure (expected %s, current %s)\n",
1095 state_string(state), state_string(cur_state));
1096}
1097#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1098#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1099
1100static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1101 enum pipe pipe)
1102{
1103 int reg;
1104 u32 val;
1105
1106 /* ILK FDI PLL is always enabled */
1107 if (dev_priv->info->gen == 5)
1108 return;
1109
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001110 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001111 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001112 return;
1113
Jesse Barnes040484a2011-01-03 12:14:26 -08001114 reg = FDI_TX_CTL(pipe);
1115 val = I915_READ(reg);
1116 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1117}
1118
1119static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1120 enum pipe pipe)
1121{
1122 int reg;
1123 u32 val;
1124
1125 reg = FDI_RX_CTL(pipe);
1126 val = I915_READ(reg);
1127 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1128}
1129
Jesse Barnesea0760c2011-01-04 15:09:32 -08001130static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1131 enum pipe pipe)
1132{
1133 int pp_reg, lvds_reg;
1134 u32 val;
1135 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001136 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001137
1138 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1139 pp_reg = PCH_PP_CONTROL;
1140 lvds_reg = PCH_LVDS;
1141 } else {
1142 pp_reg = PP_CONTROL;
1143 lvds_reg = LVDS;
1144 }
1145
1146 val = I915_READ(pp_reg);
1147 if (!(val & PANEL_POWER_ON) ||
1148 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1149 locked = false;
1150
1151 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1152 panel_pipe = PIPE_B;
1153
1154 WARN(panel_pipe == pipe && locked,
1155 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001156 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001157}
1158
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001159void assert_pipe(struct drm_i915_private *dev_priv,
1160 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001161{
1162 int reg;
1163 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001164 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001165 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1166 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001167
Daniel Vetter8e636782012-01-22 01:36:48 +01001168 /* if we need the pipe A quirk it must be always on */
1169 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1170 state = true;
1171
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001172 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001173 val = I915_READ(reg);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001174 cur_state = !!(val & PIPECONF_ENABLE);
1175 WARN(cur_state != state,
1176 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001177 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001178}
1179
Chris Wilson931872f2012-01-16 23:01:13 +00001180static void assert_plane(struct drm_i915_private *dev_priv,
1181 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001182{
1183 int reg;
1184 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001185 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001186
1187 reg = DSPCNTR(plane);
1188 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001189 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1190 WARN(cur_state != state,
1191 "plane %c assertion failure (expected %s, current %s)\n",
1192 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001193}
1194
Chris Wilson931872f2012-01-16 23:01:13 +00001195#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1196#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1197
Jesse Barnesb24e7172011-01-04 15:09:30 -08001198static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1199 enum pipe pipe)
1200{
1201 int reg, i;
1202 u32 val;
1203 int cur_pipe;
1204
Jesse Barnes19ec1352011-02-02 12:28:02 -08001205 /* Planes are fixed to pipes on ILK+ */
Adam Jackson28c057942011-10-07 14:38:42 -04001206 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1207 reg = DSPCNTR(pipe);
1208 val = I915_READ(reg);
1209 WARN((val & DISPLAY_PLANE_ENABLE),
1210 "plane %c assertion failure, should be disabled but not\n",
1211 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001212 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001213 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001214
Jesse Barnesb24e7172011-01-04 15:09:30 -08001215 /* Need to check both planes against the pipe */
1216 for (i = 0; i < 2; i++) {
1217 reg = DSPCNTR(i);
1218 val = I915_READ(reg);
1219 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1220 DISPPLANE_SEL_PIPE_SHIFT;
1221 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001222 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1223 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001224 }
1225}
1226
Jesse Barnes92f25842011-01-04 15:09:34 -08001227static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1228{
1229 u32 val;
1230 bool enabled;
1231
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001232 if (HAS_PCH_LPT(dev_priv->dev)) {
1233 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1234 return;
1235 }
1236
Jesse Barnes92f25842011-01-04 15:09:34 -08001237 val = I915_READ(PCH_DREF_CONTROL);
1238 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1239 DREF_SUPERSPREAD_SOURCE_MASK));
1240 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1241}
1242
1243static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1244 enum pipe pipe)
1245{
1246 int reg;
1247 u32 val;
1248 bool enabled;
1249
1250 reg = TRANSCONF(pipe);
1251 val = I915_READ(reg);
1252 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001253 WARN(enabled,
1254 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1255 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001256}
1257
Keith Packard4e634382011-08-06 10:39:45 -07001258static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1259 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001260{
1261 if ((val & DP_PORT_EN) == 0)
1262 return false;
1263
1264 if (HAS_PCH_CPT(dev_priv->dev)) {
1265 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1266 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1267 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1268 return false;
1269 } else {
1270 if ((val & DP_PIPE_MASK) != (pipe << 30))
1271 return false;
1272 }
1273 return true;
1274}
1275
Keith Packard1519b992011-08-06 10:35:34 -07001276static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1277 enum pipe pipe, u32 val)
1278{
1279 if ((val & PORT_ENABLE) == 0)
1280 return false;
1281
1282 if (HAS_PCH_CPT(dev_priv->dev)) {
1283 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1284 return false;
1285 } else {
1286 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1287 return false;
1288 }
1289 return true;
1290}
1291
1292static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1293 enum pipe pipe, u32 val)
1294{
1295 if ((val & LVDS_PORT_EN) == 0)
1296 return false;
1297
1298 if (HAS_PCH_CPT(dev_priv->dev)) {
1299 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1300 return false;
1301 } else {
1302 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1303 return false;
1304 }
1305 return true;
1306}
1307
1308static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1309 enum pipe pipe, u32 val)
1310{
1311 if ((val & ADPA_DAC_ENABLE) == 0)
1312 return false;
1313 if (HAS_PCH_CPT(dev_priv->dev)) {
1314 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1315 return false;
1316 } else {
1317 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1318 return false;
1319 }
1320 return true;
1321}
1322
Jesse Barnes291906f2011-02-02 12:28:03 -08001323static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001324 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001325{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001326 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001327 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001328 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001329 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001330
Daniel Vetter75c5da22012-09-10 21:58:29 +02001331 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1332 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001333 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001334}
1335
1336static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1337 enum pipe pipe, int reg)
1338{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001339 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001340 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001341 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001342 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001343
Daniel Vetter75c5da22012-09-10 21:58:29 +02001344 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & PORT_ENABLE) == 0
1345 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001346 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001347}
1348
1349static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1350 enum pipe pipe)
1351{
1352 int reg;
1353 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001354
Keith Packardf0575e92011-07-25 22:12:43 -07001355 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1356 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1357 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001358
1359 reg = PCH_ADPA;
1360 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001361 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001362 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001363 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001364
1365 reg = PCH_LVDS;
1366 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001367 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001368 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001369 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001370
1371 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1372 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1373 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1374}
1375
Jesse Barnesb24e7172011-01-04 15:09:30 -08001376/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001377 * intel_enable_pll - enable a PLL
1378 * @dev_priv: i915 private structure
1379 * @pipe: pipe PLL to enable
1380 *
1381 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1382 * make sure the PLL reg is writable first though, since the panel write
1383 * protect mechanism may be enabled.
1384 *
1385 * Note! This is for pre-ILK only.
Thomas Richter7434a252012-07-18 19:22:30 +02001386 *
1387 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001388 */
1389static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1390{
1391 int reg;
1392 u32 val;
1393
1394 /* No really, not for ILK+ */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001395 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001396
1397 /* PLL is protected by panel, make sure we can write it */
1398 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1399 assert_panel_unlocked(dev_priv, pipe);
1400
1401 reg = DPLL(pipe);
1402 val = I915_READ(reg);
1403 val |= DPLL_VCO_ENABLE;
1404
1405 /* We do this three times for luck */
1406 I915_WRITE(reg, val);
1407 POSTING_READ(reg);
1408 udelay(150); /* wait for warmup */
1409 I915_WRITE(reg, val);
1410 POSTING_READ(reg);
1411 udelay(150); /* wait for warmup */
1412 I915_WRITE(reg, val);
1413 POSTING_READ(reg);
1414 udelay(150); /* wait for warmup */
1415}
1416
1417/**
1418 * intel_disable_pll - disable a PLL
1419 * @dev_priv: i915 private structure
1420 * @pipe: pipe PLL to disable
1421 *
1422 * Disable the PLL for @pipe, making sure the pipe is off first.
1423 *
1424 * Note! This is for pre-ILK only.
1425 */
1426static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1427{
1428 int reg;
1429 u32 val;
1430
1431 /* Don't disable pipe A or pipe A PLLs if needed */
1432 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1433 return;
1434
1435 /* Make sure the pipe isn't still relying on us */
1436 assert_pipe_disabled(dev_priv, pipe);
1437
1438 reg = DPLL(pipe);
1439 val = I915_READ(reg);
1440 val &= ~DPLL_VCO_ENABLE;
1441 I915_WRITE(reg, val);
1442 POSTING_READ(reg);
1443}
1444
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001445/* SBI access */
1446static void
1447intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value)
1448{
Daniel Vetter09153002012-12-12 14:06:44 +01001449 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001450
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001451 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001452 100)) {
1453 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001454 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001455 }
1456
1457 I915_WRITE(SBI_ADDR,
1458 (reg << 16));
1459 I915_WRITE(SBI_DATA,
1460 value);
1461 I915_WRITE(SBI_CTL_STAT,
1462 SBI_BUSY |
1463 SBI_CTL_OP_CRWR);
1464
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001465 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001466 100)) {
1467 DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001468 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001469 }
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001470}
1471
1472static u32
1473intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg)
1474{
Daniel Vetter09153002012-12-12 14:06:44 +01001475 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001476
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001477 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001478 100)) {
1479 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001480 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001481 }
1482
1483 I915_WRITE(SBI_ADDR,
1484 (reg << 16));
1485 I915_WRITE(SBI_CTL_STAT,
1486 SBI_BUSY |
1487 SBI_CTL_OP_CRRD);
1488
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001489 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001490 100)) {
1491 DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001492 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001493 }
1494
Daniel Vetter09153002012-12-12 14:06:44 +01001495 return I915_READ(SBI_DATA);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001496}
1497
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001498/**
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001499 * ironlake_enable_pch_pll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001500 * @dev_priv: i915 private structure
1501 * @pipe: pipe PLL to enable
1502 *
1503 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1504 * drives the transcoder clock.
1505 */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001506static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001507{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001508 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Chris Wilson48da64a2012-05-13 20:16:12 +01001509 struct intel_pch_pll *pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001510 int reg;
1511 u32 val;
1512
Chris Wilson48da64a2012-05-13 20:16:12 +01001513 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001514 BUG_ON(dev_priv->info->gen < 5);
Chris Wilson48da64a2012-05-13 20:16:12 +01001515 pll = intel_crtc->pch_pll;
1516 if (pll == NULL)
1517 return;
1518
1519 if (WARN_ON(pll->refcount == 0))
1520 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001521
1522 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1523 pll->pll_reg, pll->active, pll->on,
1524 intel_crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001525
1526 /* PCH refclock must be enabled first */
1527 assert_pch_refclk_enabled(dev_priv);
1528
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001529 if (pll->active++ && pll->on) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001530 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001531 return;
1532 }
1533
1534 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1535
1536 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001537 val = I915_READ(reg);
1538 val |= DPLL_VCO_ENABLE;
1539 I915_WRITE(reg, val);
1540 POSTING_READ(reg);
1541 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001542
1543 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001544}
1545
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001546static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001547{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001548 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1549 struct intel_pch_pll *pll = intel_crtc->pch_pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001550 int reg;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001551 u32 val;
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001552
Jesse Barnes92f25842011-01-04 15:09:34 -08001553 /* PCH only available on ILK+ */
1554 BUG_ON(dev_priv->info->gen < 5);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001555 if (pll == NULL)
1556 return;
1557
Chris Wilson48da64a2012-05-13 20:16:12 +01001558 if (WARN_ON(pll->refcount == 0))
1559 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001560
1561 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1562 pll->pll_reg, pll->active, pll->on,
1563 intel_crtc->base.base.id);
1564
Chris Wilson48da64a2012-05-13 20:16:12 +01001565 if (WARN_ON(pll->active == 0)) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001566 assert_pch_pll_disabled(dev_priv, pll, NULL);
Chris Wilson48da64a2012-05-13 20:16:12 +01001567 return;
1568 }
1569
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001570 if (--pll->active) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001571 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001572 return;
1573 }
1574
1575 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
Jesse Barnes92f25842011-01-04 15:09:34 -08001576
1577 /* Make sure transcoder isn't still depending on us */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001578 assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001579
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001580 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001581 val = I915_READ(reg);
1582 val &= ~DPLL_VCO_ENABLE;
1583 I915_WRITE(reg, val);
1584 POSTING_READ(reg);
1585 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001586
1587 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001588}
1589
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001590static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1591 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001592{
Daniel Vetter23670b322012-11-01 09:15:30 +01001593 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001594 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetter23670b322012-11-01 09:15:30 +01001595 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001596
1597 /* PCH only available on ILK+ */
1598 BUG_ON(dev_priv->info->gen < 5);
1599
1600 /* Make sure PCH DPLL is enabled */
Chris Wilson92b27b02012-05-20 18:10:50 +01001601 assert_pch_pll_enabled(dev_priv,
1602 to_intel_crtc(crtc)->pch_pll,
1603 to_intel_crtc(crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001604
1605 /* FDI must be feeding us bits for PCH ports */
1606 assert_fdi_tx_enabled(dev_priv, pipe);
1607 assert_fdi_rx_enabled(dev_priv, pipe);
1608
Daniel Vetter23670b322012-11-01 09:15:30 +01001609 if (HAS_PCH_CPT(dev)) {
1610 /* Workaround: Set the timing override bit before enabling the
1611 * pch transcoder. */
1612 reg = TRANS_CHICKEN2(pipe);
1613 val = I915_READ(reg);
1614 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1615 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001616 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001617
Jesse Barnes040484a2011-01-03 12:14:26 -08001618 reg = TRANSCONF(pipe);
1619 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001620 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001621
1622 if (HAS_PCH_IBX(dev_priv->dev)) {
1623 /*
1624 * make the BPC in transcoder be consistent with
1625 * that in pipeconf reg.
1626 */
1627 val &= ~PIPE_BPC_MASK;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001628 val |= pipeconf_val & PIPE_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001629 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001630
1631 val &= ~TRANS_INTERLACE_MASK;
1632 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001633 if (HAS_PCH_IBX(dev_priv->dev) &&
1634 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1635 val |= TRANS_LEGACY_INTERLACED_ILK;
1636 else
1637 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001638 else
1639 val |= TRANS_PROGRESSIVE;
1640
Jesse Barnes040484a2011-01-03 12:14:26 -08001641 I915_WRITE(reg, val | TRANS_ENABLE);
1642 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1643 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1644}
1645
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001646static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001647 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001648{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001649 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001650
1651 /* PCH only available on ILK+ */
1652 BUG_ON(dev_priv->info->gen < 5);
1653
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001654 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001655 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001656 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001657
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001658 /* Workaround: set timing override bit. */
1659 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001660 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001661 I915_WRITE(_TRANSA_CHICKEN2, val);
1662
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001663 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001664 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001665
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001666 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1667 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001668 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001669 else
1670 val |= TRANS_PROGRESSIVE;
1671
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001672 I915_WRITE(TRANSCONF(TRANSCODER_A), val);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001673 if (wait_for(I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE, 100))
1674 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001675}
1676
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001677static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1678 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001679{
Daniel Vetter23670b322012-11-01 09:15:30 +01001680 struct drm_device *dev = dev_priv->dev;
1681 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001682
1683 /* FDI relies on the transcoder */
1684 assert_fdi_tx_disabled(dev_priv, pipe);
1685 assert_fdi_rx_disabled(dev_priv, pipe);
1686
Jesse Barnes291906f2011-02-02 12:28:03 -08001687 /* Ports must be off as well */
1688 assert_pch_ports_disabled(dev_priv, pipe);
1689
Jesse Barnes040484a2011-01-03 12:14:26 -08001690 reg = TRANSCONF(pipe);
1691 val = I915_READ(reg);
1692 val &= ~TRANS_ENABLE;
1693 I915_WRITE(reg, val);
1694 /* wait for PCH transcoder off, transcoder state */
1695 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes4c9c18c2011-10-13 09:46:32 -07001696 DRM_ERROR("failed to disable transcoder %d\n", pipe);
Daniel Vetter23670b322012-11-01 09:15:30 +01001697
1698 if (!HAS_PCH_IBX(dev)) {
1699 /* Workaround: Clear the timing override chicken bit again. */
1700 reg = TRANS_CHICKEN2(pipe);
1701 val = I915_READ(reg);
1702 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1703 I915_WRITE(reg, val);
1704 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001705}
1706
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001707static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001708{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001709 u32 val;
1710
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001711 val = I915_READ(_TRANSACONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001712 val &= ~TRANS_ENABLE;
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001713 I915_WRITE(_TRANSACONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001714 /* wait for PCH transcoder off, transcoder state */
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001715 if (wait_for((I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE) == 0, 50))
1716 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001717
1718 /* Workaround: clear timing override bit. */
1719 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001720 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001721 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001722}
1723
1724/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001725 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001726 * @dev_priv: i915 private structure
1727 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001728 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001729 *
1730 * Enable @pipe, making sure that various hardware specific requirements
1731 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1732 *
1733 * @pipe should be %PIPE_A or %PIPE_B.
1734 *
1735 * Will wait until the pipe is actually running (i.e. first vblank) before
1736 * returning.
1737 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001738static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1739 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001740{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001741 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1742 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001743 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001744 int reg;
1745 u32 val;
1746
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001747 if (IS_HASWELL(dev_priv->dev))
1748 pch_transcoder = TRANSCODER_A;
1749 else
1750 pch_transcoder = pipe;
1751
Jesse Barnesb24e7172011-01-04 15:09:30 -08001752 /*
1753 * A pipe without a PLL won't actually be able to drive bits from
1754 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1755 * need the check.
1756 */
1757 if (!HAS_PCH_SPLIT(dev_priv->dev))
1758 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001759 else {
1760 if (pch_port) {
1761 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001762 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001763 assert_fdi_tx_pll_enabled(dev_priv,
1764 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001765 }
1766 /* FIXME: assert CPU port conditions for SNB+ */
1767 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001768
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001769 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001770 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001771 if (val & PIPECONF_ENABLE)
1772 return;
1773
1774 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001775 intel_wait_for_vblank(dev_priv->dev, pipe);
1776}
1777
1778/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001779 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001780 * @dev_priv: i915 private structure
1781 * @pipe: pipe to disable
1782 *
1783 * Disable @pipe, making sure that various hardware specific requirements
1784 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1785 *
1786 * @pipe should be %PIPE_A or %PIPE_B.
1787 *
1788 * Will wait until the pipe has shut down before returning.
1789 */
1790static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1791 enum pipe pipe)
1792{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001793 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1794 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001795 int reg;
1796 u32 val;
1797
1798 /*
1799 * Make sure planes won't keep trying to pump pixels to us,
1800 * or we might hang the display.
1801 */
1802 assert_planes_disabled(dev_priv, pipe);
1803
1804 /* Don't disable pipe A or pipe A PLLs if needed */
1805 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1806 return;
1807
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001808 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001809 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001810 if ((val & PIPECONF_ENABLE) == 0)
1811 return;
1812
1813 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001814 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1815}
1816
Keith Packardd74362c2011-07-28 14:47:14 -07001817/*
1818 * Plane regs are double buffered, going from enabled->disabled needs a
1819 * trigger in order to latch. The display address reg provides this.
1820 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001821void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001822 enum plane plane)
1823{
Damien Lespiau14f86142012-10-29 15:24:49 +00001824 if (dev_priv->info->gen >= 4)
1825 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1826 else
1827 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001828}
1829
Jesse Barnesb24e7172011-01-04 15:09:30 -08001830/**
1831 * intel_enable_plane - enable a display plane on a given pipe
1832 * @dev_priv: i915 private structure
1833 * @plane: plane to enable
1834 * @pipe: pipe being fed
1835 *
1836 * Enable @plane on @pipe, making sure that @pipe is running first.
1837 */
1838static void intel_enable_plane(struct drm_i915_private *dev_priv,
1839 enum plane plane, enum pipe pipe)
1840{
1841 int reg;
1842 u32 val;
1843
1844 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1845 assert_pipe_enabled(dev_priv, pipe);
1846
1847 reg = DSPCNTR(plane);
1848 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001849 if (val & DISPLAY_PLANE_ENABLE)
1850 return;
1851
1852 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001853 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001854 intel_wait_for_vblank(dev_priv->dev, pipe);
1855}
1856
Jesse Barnesb24e7172011-01-04 15:09:30 -08001857/**
1858 * intel_disable_plane - disable a display plane
1859 * @dev_priv: i915 private structure
1860 * @plane: plane to disable
1861 * @pipe: pipe consuming the data
1862 *
1863 * Disable @plane; should be an independent operation.
1864 */
1865static void intel_disable_plane(struct drm_i915_private *dev_priv,
1866 enum plane plane, enum pipe pipe)
1867{
1868 int reg;
1869 u32 val;
1870
1871 reg = DSPCNTR(plane);
1872 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001873 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1874 return;
1875
1876 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001877 intel_flush_display_plane(dev_priv, plane);
1878 intel_wait_for_vblank(dev_priv->dev, pipe);
1879}
1880
Chris Wilson127bd2a2010-07-23 23:32:05 +01001881int
Chris Wilson48b956c2010-09-14 12:50:34 +01001882intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001883 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001884 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001885{
Chris Wilsonce453d82011-02-21 14:43:56 +00001886 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001887 u32 alignment;
1888 int ret;
1889
Chris Wilson05394f32010-11-08 19:18:58 +00001890 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001891 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001892 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1893 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001894 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001895 alignment = 4 * 1024;
1896 else
1897 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001898 break;
1899 case I915_TILING_X:
1900 /* pin() will align the object as required by fence */
1901 alignment = 0;
1902 break;
1903 case I915_TILING_Y:
1904 /* FIXME: Is this true? */
1905 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1906 return -EINVAL;
1907 default:
1908 BUG();
1909 }
1910
Chris Wilsonce453d82011-02-21 14:43:56 +00001911 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001912 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001913 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001914 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001915
1916 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1917 * fence, whereas 965+ only requires a fence if using
1918 * framebuffer compression. For simplicity, we always install
1919 * a fence as the cost is not that onerous.
1920 */
Chris Wilson06d98132012-04-17 15:31:24 +01001921 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001922 if (ret)
1923 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001924
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001925 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001926
Chris Wilsonce453d82011-02-21 14:43:56 +00001927 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001928 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001929
1930err_unpin:
1931 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001932err_interruptible:
1933 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001934 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001935}
1936
Chris Wilson1690e1e2011-12-14 13:57:08 +01001937void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1938{
1939 i915_gem_object_unpin_fence(obj);
1940 i915_gem_object_unpin(obj);
1941}
1942
Daniel Vetterc2c75132012-07-05 12:17:30 +02001943/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1944 * is assumed to be a power-of-two. */
Damien Lespiau5a35e992012-10-26 18:20:12 +01001945unsigned long intel_gen4_compute_offset_xtiled(int *x, int *y,
1946 unsigned int bpp,
1947 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001948{
1949 int tile_rows, tiles;
1950
1951 tile_rows = *y / 8;
1952 *y %= 8;
1953 tiles = *x / (512/bpp);
1954 *x %= 512/bpp;
1955
1956 return tile_rows * pitch * 8 + tiles * 4096;
1957}
1958
Jesse Barnes17638cd2011-06-24 12:19:23 -07001959static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1960 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001961{
1962 struct drm_device *dev = crtc->dev;
1963 struct drm_i915_private *dev_priv = dev->dev_private;
1964 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1965 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001966 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001967 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001968 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001969 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001970 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001971
1972 switch (plane) {
1973 case 0:
1974 case 1:
1975 break;
1976 default:
1977 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1978 return -EINVAL;
1979 }
1980
1981 intel_fb = to_intel_framebuffer(fb);
1982 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001983
Chris Wilson5eddb702010-09-11 13:48:45 +01001984 reg = DSPCNTR(plane);
1985 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001986 /* Mask out pixel format bits in case we change it */
1987 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001988 switch (fb->pixel_format) {
1989 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07001990 dspcntr |= DISPPLANE_8BPP;
1991 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001992 case DRM_FORMAT_XRGB1555:
1993 case DRM_FORMAT_ARGB1555:
1994 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07001995 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02001996 case DRM_FORMAT_RGB565:
1997 dspcntr |= DISPPLANE_BGRX565;
1998 break;
1999 case DRM_FORMAT_XRGB8888:
2000 case DRM_FORMAT_ARGB8888:
2001 dspcntr |= DISPPLANE_BGRX888;
2002 break;
2003 case DRM_FORMAT_XBGR8888:
2004 case DRM_FORMAT_ABGR8888:
2005 dspcntr |= DISPPLANE_RGBX888;
2006 break;
2007 case DRM_FORMAT_XRGB2101010:
2008 case DRM_FORMAT_ARGB2101010:
2009 dspcntr |= DISPPLANE_BGRX101010;
2010 break;
2011 case DRM_FORMAT_XBGR2101010:
2012 case DRM_FORMAT_ABGR2101010:
2013 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002014 break;
2015 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002016 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes81255562010-08-02 12:07:50 -07002017 return -EINVAL;
2018 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002019
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002020 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002021 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002022 dspcntr |= DISPPLANE_TILED;
2023 else
2024 dspcntr &= ~DISPPLANE_TILED;
2025 }
2026
Chris Wilson5eddb702010-09-11 13:48:45 +01002027 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002028
Daniel Vettere506a0c2012-07-05 12:17:29 +02002029 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002030
Daniel Vetterc2c75132012-07-05 12:17:30 +02002031 if (INTEL_INFO(dev)->gen >= 4) {
2032 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002033 intel_gen4_compute_offset_xtiled(&x, &y,
2034 fb->bits_per_pixel / 8,
2035 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002036 linear_offset -= intel_crtc->dspaddr_offset;
2037 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002038 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002039 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002040
2041 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2042 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002043 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002044 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002045 I915_MODIFY_DISPBASE(DSPSURF(plane),
2046 obj->gtt_offset + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002047 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002048 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002049 } else
Daniel Vettere506a0c2012-07-05 12:17:29 +02002050 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002051 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002052
Jesse Barnes17638cd2011-06-24 12:19:23 -07002053 return 0;
2054}
2055
2056static int ironlake_update_plane(struct drm_crtc *crtc,
2057 struct drm_framebuffer *fb, int x, int y)
2058{
2059 struct drm_device *dev = crtc->dev;
2060 struct drm_i915_private *dev_priv = dev->dev_private;
2061 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2062 struct intel_framebuffer *intel_fb;
2063 struct drm_i915_gem_object *obj;
2064 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002065 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002066 u32 dspcntr;
2067 u32 reg;
2068
2069 switch (plane) {
2070 case 0:
2071 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002072 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002073 break;
2074 default:
2075 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2076 return -EINVAL;
2077 }
2078
2079 intel_fb = to_intel_framebuffer(fb);
2080 obj = intel_fb->obj;
2081
2082 reg = DSPCNTR(plane);
2083 dspcntr = I915_READ(reg);
2084 /* Mask out pixel format bits in case we change it */
2085 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002086 switch (fb->pixel_format) {
2087 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002088 dspcntr |= DISPPLANE_8BPP;
2089 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002090 case DRM_FORMAT_RGB565:
2091 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002092 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002093 case DRM_FORMAT_XRGB8888:
2094 case DRM_FORMAT_ARGB8888:
2095 dspcntr |= DISPPLANE_BGRX888;
2096 break;
2097 case DRM_FORMAT_XBGR8888:
2098 case DRM_FORMAT_ABGR8888:
2099 dspcntr |= DISPPLANE_RGBX888;
2100 break;
2101 case DRM_FORMAT_XRGB2101010:
2102 case DRM_FORMAT_ARGB2101010:
2103 dspcntr |= DISPPLANE_BGRX101010;
2104 break;
2105 case DRM_FORMAT_XBGR2101010:
2106 case DRM_FORMAT_ABGR2101010:
2107 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002108 break;
2109 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002110 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002111 return -EINVAL;
2112 }
2113
2114 if (obj->tiling_mode != I915_TILING_NONE)
2115 dspcntr |= DISPPLANE_TILED;
2116 else
2117 dspcntr &= ~DISPPLANE_TILED;
2118
2119 /* must disable */
2120 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2121
2122 I915_WRITE(reg, dspcntr);
2123
Daniel Vettere506a0c2012-07-05 12:17:29 +02002124 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002125 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002126 intel_gen4_compute_offset_xtiled(&x, &y,
2127 fb->bits_per_pixel / 8,
2128 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002129 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002130
Daniel Vettere506a0c2012-07-05 12:17:29 +02002131 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2132 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002133 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002134 I915_MODIFY_DISPBASE(DSPSURF(plane),
2135 obj->gtt_offset + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002136 if (IS_HASWELL(dev)) {
2137 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2138 } else {
2139 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2140 I915_WRITE(DSPLINOFF(plane), linear_offset);
2141 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002142 POSTING_READ(reg);
2143
2144 return 0;
2145}
2146
2147/* Assume fb object is pinned & idle & fenced and just update base pointers */
2148static int
2149intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2150 int x, int y, enum mode_set_atomic state)
2151{
2152 struct drm_device *dev = crtc->dev;
2153 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002154
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002155 if (dev_priv->display.disable_fbc)
2156 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002157 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002158
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002159 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002160}
2161
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002162static int
Chris Wilson14667a42012-04-03 17:58:35 +01002163intel_finish_fb(struct drm_framebuffer *old_fb)
2164{
2165 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2166 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2167 bool was_interruptible = dev_priv->mm.interruptible;
2168 int ret;
2169
2170 wait_event(dev_priv->pending_flip_queue,
2171 atomic_read(&dev_priv->mm.wedged) ||
2172 atomic_read(&obj->pending_flip) == 0);
2173
2174 /* Big Hammer, we also need to ensure that any pending
2175 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2176 * current scanout is retired before unpinning the old
2177 * framebuffer.
2178 *
2179 * This should only fail upon a hung GPU, in which case we
2180 * can safely continue.
2181 */
2182 dev_priv->mm.interruptible = false;
2183 ret = i915_gem_object_finish_gpu(obj);
2184 dev_priv->mm.interruptible = was_interruptible;
2185
2186 return ret;
2187}
2188
Ville Syrjälä198598d2012-10-31 17:50:24 +02002189static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2190{
2191 struct drm_device *dev = crtc->dev;
2192 struct drm_i915_master_private *master_priv;
2193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2194
2195 if (!dev->primary->master)
2196 return;
2197
2198 master_priv = dev->primary->master->driver_priv;
2199 if (!master_priv->sarea_priv)
2200 return;
2201
2202 switch (intel_crtc->pipe) {
2203 case 0:
2204 master_priv->sarea_priv->pipeA_x = x;
2205 master_priv->sarea_priv->pipeA_y = y;
2206 break;
2207 case 1:
2208 master_priv->sarea_priv->pipeB_x = x;
2209 master_priv->sarea_priv->pipeB_y = y;
2210 break;
2211 default:
2212 break;
2213 }
2214}
2215
Chris Wilson14667a42012-04-03 17:58:35 +01002216static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002217intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002218 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002219{
2220 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002221 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002222 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002223 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002224 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002225
2226 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002227 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002228 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002229 return 0;
2230 }
2231
Eugeni Dodonov5826eca2012-05-09 15:37:12 -03002232 if(intel_crtc->plane > dev_priv->num_pipe) {
2233 DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
2234 intel_crtc->plane,
2235 dev_priv->num_pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002236 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002237 }
2238
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002239 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002240 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002241 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002242 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002243 if (ret != 0) {
2244 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002245 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002246 return ret;
2247 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002248
Daniel Vetter94352cf2012-07-05 22:51:56 +02002249 if (crtc->fb)
2250 intel_finish_fb(crtc->fb);
Chris Wilson265db952010-09-20 15:41:01 +01002251
Daniel Vetter94352cf2012-07-05 22:51:56 +02002252 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002253 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002254 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002255 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002256 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002257 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002258 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002259
Daniel Vetter94352cf2012-07-05 22:51:56 +02002260 old_fb = crtc->fb;
2261 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002262 crtc->x = x;
2263 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002264
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002265 if (old_fb) {
2266 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002267 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002268 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002269
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002270 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002271 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002272
Ville Syrjälä198598d2012-10-31 17:50:24 +02002273 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002274
2275 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002276}
2277
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002278static void intel_fdi_normal_train(struct drm_crtc *crtc)
2279{
2280 struct drm_device *dev = crtc->dev;
2281 struct drm_i915_private *dev_priv = dev->dev_private;
2282 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2283 int pipe = intel_crtc->pipe;
2284 u32 reg, temp;
2285
2286 /* enable normal train */
2287 reg = FDI_TX_CTL(pipe);
2288 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002289 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002290 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2291 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002292 } else {
2293 temp &= ~FDI_LINK_TRAIN_NONE;
2294 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002295 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002296 I915_WRITE(reg, temp);
2297
2298 reg = FDI_RX_CTL(pipe);
2299 temp = I915_READ(reg);
2300 if (HAS_PCH_CPT(dev)) {
2301 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2302 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2303 } else {
2304 temp &= ~FDI_LINK_TRAIN_NONE;
2305 temp |= FDI_LINK_TRAIN_NONE;
2306 }
2307 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2308
2309 /* wait one idle pattern time */
2310 POSTING_READ(reg);
2311 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002312
2313 /* IVB wants error correction enabled */
2314 if (IS_IVYBRIDGE(dev))
2315 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2316 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002317}
2318
Jesse Barnes291427f2011-07-29 12:42:37 -07002319static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
2320{
2321 struct drm_i915_private *dev_priv = dev->dev_private;
2322 u32 flags = I915_READ(SOUTH_CHICKEN1);
2323
2324 flags |= FDI_PHASE_SYNC_OVR(pipe);
2325 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2326 flags |= FDI_PHASE_SYNC_EN(pipe);
2327 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2328 POSTING_READ(SOUTH_CHICKEN1);
2329}
2330
Daniel Vetter01a415f2012-10-27 15:58:40 +02002331static void ivb_modeset_global_resources(struct drm_device *dev)
2332{
2333 struct drm_i915_private *dev_priv = dev->dev_private;
2334 struct intel_crtc *pipe_B_crtc =
2335 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2336 struct intel_crtc *pipe_C_crtc =
2337 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2338 uint32_t temp;
2339
2340 /* When everything is off disable fdi C so that we could enable fdi B
2341 * with all lanes. XXX: This misses the case where a pipe is not using
2342 * any pch resources and so doesn't need any fdi lanes. */
2343 if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
2344 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2345 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2346
2347 temp = I915_READ(SOUTH_CHICKEN1);
2348 temp &= ~FDI_BC_BIFURCATION_SELECT;
2349 DRM_DEBUG_KMS("disabling fdi C rx\n");
2350 I915_WRITE(SOUTH_CHICKEN1, temp);
2351 }
2352}
2353
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002354/* The FDI link training functions for ILK/Ibexpeak. */
2355static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2356{
2357 struct drm_device *dev = crtc->dev;
2358 struct drm_i915_private *dev_priv = dev->dev_private;
2359 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2360 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002361 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002362 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002363
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002364 /* FDI needs bits from pipe & plane first */
2365 assert_pipe_enabled(dev_priv, pipe);
2366 assert_plane_enabled(dev_priv, plane);
2367
Adam Jacksone1a44742010-06-25 15:32:14 -04002368 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2369 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002370 reg = FDI_RX_IMR(pipe);
2371 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002372 temp &= ~FDI_RX_SYMBOL_LOCK;
2373 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002374 I915_WRITE(reg, temp);
2375 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002376 udelay(150);
2377
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002378 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002379 reg = FDI_TX_CTL(pipe);
2380 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002381 temp &= ~(7 << 19);
2382 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002383 temp &= ~FDI_LINK_TRAIN_NONE;
2384 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002385 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002386
Chris Wilson5eddb702010-09-11 13:48:45 +01002387 reg = FDI_RX_CTL(pipe);
2388 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002389 temp &= ~FDI_LINK_TRAIN_NONE;
2390 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002391 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2392
2393 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002394 udelay(150);
2395
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002396 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002397 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2398 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2399 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002400
Chris Wilson5eddb702010-09-11 13:48:45 +01002401 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002402 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002403 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002404 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2405
2406 if ((temp & FDI_RX_BIT_LOCK)) {
2407 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002408 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002409 break;
2410 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002411 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002412 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002413 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002414
2415 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002416 reg = FDI_TX_CTL(pipe);
2417 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002418 temp &= ~FDI_LINK_TRAIN_NONE;
2419 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002420 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002421
Chris Wilson5eddb702010-09-11 13:48:45 +01002422 reg = FDI_RX_CTL(pipe);
2423 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002424 temp &= ~FDI_LINK_TRAIN_NONE;
2425 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002426 I915_WRITE(reg, temp);
2427
2428 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002429 udelay(150);
2430
Chris Wilson5eddb702010-09-11 13:48:45 +01002431 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002432 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002433 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002434 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2435
2436 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002437 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002438 DRM_DEBUG_KMS("FDI train 2 done.\n");
2439 break;
2440 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002441 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002442 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002443 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002444
2445 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002446
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002447}
2448
Akshay Joshi0206e352011-08-16 15:34:10 -04002449static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002450 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2451 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2452 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2453 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2454};
2455
2456/* The FDI link training functions for SNB/Cougarpoint. */
2457static void gen6_fdi_link_train(struct drm_crtc *crtc)
2458{
2459 struct drm_device *dev = crtc->dev;
2460 struct drm_i915_private *dev_priv = dev->dev_private;
2461 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2462 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002463 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002464
Adam Jacksone1a44742010-06-25 15:32:14 -04002465 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2466 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002467 reg = FDI_RX_IMR(pipe);
2468 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002469 temp &= ~FDI_RX_SYMBOL_LOCK;
2470 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002471 I915_WRITE(reg, temp);
2472
2473 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002474 udelay(150);
2475
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002476 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002477 reg = FDI_TX_CTL(pipe);
2478 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002479 temp &= ~(7 << 19);
2480 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002481 temp &= ~FDI_LINK_TRAIN_NONE;
2482 temp |= FDI_LINK_TRAIN_PATTERN_1;
2483 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2484 /* SNB-B */
2485 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002486 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002487
Daniel Vetterd74cf322012-10-26 10:58:13 +02002488 I915_WRITE(FDI_RX_MISC(pipe),
2489 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2490
Chris Wilson5eddb702010-09-11 13:48:45 +01002491 reg = FDI_RX_CTL(pipe);
2492 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002493 if (HAS_PCH_CPT(dev)) {
2494 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2495 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2496 } else {
2497 temp &= ~FDI_LINK_TRAIN_NONE;
2498 temp |= FDI_LINK_TRAIN_PATTERN_1;
2499 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002500 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2501
2502 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002503 udelay(150);
2504
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002505 cpt_phase_pointer_enable(dev, pipe);
Jesse Barnes291427f2011-07-29 12:42:37 -07002506
Akshay Joshi0206e352011-08-16 15:34:10 -04002507 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002508 reg = FDI_TX_CTL(pipe);
2509 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002510 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2511 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002512 I915_WRITE(reg, temp);
2513
2514 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002515 udelay(500);
2516
Sean Paulfa37d392012-03-02 12:53:39 -05002517 for (retry = 0; retry < 5; retry++) {
2518 reg = FDI_RX_IIR(pipe);
2519 temp = I915_READ(reg);
2520 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2521 if (temp & FDI_RX_BIT_LOCK) {
2522 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2523 DRM_DEBUG_KMS("FDI train 1 done.\n");
2524 break;
2525 }
2526 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002527 }
Sean Paulfa37d392012-03-02 12:53:39 -05002528 if (retry < 5)
2529 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002530 }
2531 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002532 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002533
2534 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002535 reg = FDI_TX_CTL(pipe);
2536 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002537 temp &= ~FDI_LINK_TRAIN_NONE;
2538 temp |= FDI_LINK_TRAIN_PATTERN_2;
2539 if (IS_GEN6(dev)) {
2540 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2541 /* SNB-B */
2542 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2543 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002544 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002545
Chris Wilson5eddb702010-09-11 13:48:45 +01002546 reg = FDI_RX_CTL(pipe);
2547 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002548 if (HAS_PCH_CPT(dev)) {
2549 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2550 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2551 } else {
2552 temp &= ~FDI_LINK_TRAIN_NONE;
2553 temp |= FDI_LINK_TRAIN_PATTERN_2;
2554 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002555 I915_WRITE(reg, temp);
2556
2557 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002558 udelay(150);
2559
Akshay Joshi0206e352011-08-16 15:34:10 -04002560 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002561 reg = FDI_TX_CTL(pipe);
2562 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002563 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2564 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002565 I915_WRITE(reg, temp);
2566
2567 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002568 udelay(500);
2569
Sean Paulfa37d392012-03-02 12:53:39 -05002570 for (retry = 0; retry < 5; retry++) {
2571 reg = FDI_RX_IIR(pipe);
2572 temp = I915_READ(reg);
2573 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2574 if (temp & FDI_RX_SYMBOL_LOCK) {
2575 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2576 DRM_DEBUG_KMS("FDI train 2 done.\n");
2577 break;
2578 }
2579 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002580 }
Sean Paulfa37d392012-03-02 12:53:39 -05002581 if (retry < 5)
2582 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002583 }
2584 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002585 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002586
2587 DRM_DEBUG_KMS("FDI train done.\n");
2588}
2589
Jesse Barnes357555c2011-04-28 15:09:55 -07002590/* Manual link training for Ivy Bridge A0 parts */
2591static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2592{
2593 struct drm_device *dev = crtc->dev;
2594 struct drm_i915_private *dev_priv = dev->dev_private;
2595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2596 int pipe = intel_crtc->pipe;
2597 u32 reg, temp, i;
2598
2599 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2600 for train result */
2601 reg = FDI_RX_IMR(pipe);
2602 temp = I915_READ(reg);
2603 temp &= ~FDI_RX_SYMBOL_LOCK;
2604 temp &= ~FDI_RX_BIT_LOCK;
2605 I915_WRITE(reg, temp);
2606
2607 POSTING_READ(reg);
2608 udelay(150);
2609
Daniel Vetter01a415f2012-10-27 15:58:40 +02002610 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2611 I915_READ(FDI_RX_IIR(pipe)));
2612
Jesse Barnes357555c2011-04-28 15:09:55 -07002613 /* enable CPU FDI TX and PCH FDI RX */
2614 reg = FDI_TX_CTL(pipe);
2615 temp = I915_READ(reg);
2616 temp &= ~(7 << 19);
2617 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2618 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2619 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2620 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2621 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002622 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002623 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2624
Daniel Vetterd74cf322012-10-26 10:58:13 +02002625 I915_WRITE(FDI_RX_MISC(pipe),
2626 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2627
Jesse Barnes357555c2011-04-28 15:09:55 -07002628 reg = FDI_RX_CTL(pipe);
2629 temp = I915_READ(reg);
2630 temp &= ~FDI_LINK_TRAIN_AUTO;
2631 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2632 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002633 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002634 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2635
2636 POSTING_READ(reg);
2637 udelay(150);
2638
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002639 cpt_phase_pointer_enable(dev, pipe);
Jesse Barnes291427f2011-07-29 12:42:37 -07002640
Akshay Joshi0206e352011-08-16 15:34:10 -04002641 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002642 reg = FDI_TX_CTL(pipe);
2643 temp = I915_READ(reg);
2644 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2645 temp |= snb_b_fdi_train_param[i];
2646 I915_WRITE(reg, temp);
2647
2648 POSTING_READ(reg);
2649 udelay(500);
2650
2651 reg = FDI_RX_IIR(pipe);
2652 temp = I915_READ(reg);
2653 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2654
2655 if (temp & FDI_RX_BIT_LOCK ||
2656 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2657 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002658 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002659 break;
2660 }
2661 }
2662 if (i == 4)
2663 DRM_ERROR("FDI train 1 fail!\n");
2664
2665 /* Train 2 */
2666 reg = FDI_TX_CTL(pipe);
2667 temp = I915_READ(reg);
2668 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2669 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2670 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2671 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2672 I915_WRITE(reg, temp);
2673
2674 reg = FDI_RX_CTL(pipe);
2675 temp = I915_READ(reg);
2676 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2677 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2678 I915_WRITE(reg, temp);
2679
2680 POSTING_READ(reg);
2681 udelay(150);
2682
Akshay Joshi0206e352011-08-16 15:34:10 -04002683 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002684 reg = FDI_TX_CTL(pipe);
2685 temp = I915_READ(reg);
2686 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2687 temp |= snb_b_fdi_train_param[i];
2688 I915_WRITE(reg, temp);
2689
2690 POSTING_READ(reg);
2691 udelay(500);
2692
2693 reg = FDI_RX_IIR(pipe);
2694 temp = I915_READ(reg);
2695 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2696
2697 if (temp & FDI_RX_SYMBOL_LOCK) {
2698 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002699 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002700 break;
2701 }
2702 }
2703 if (i == 4)
2704 DRM_ERROR("FDI train 2 fail!\n");
2705
2706 DRM_DEBUG_KMS("FDI train done.\n");
2707}
2708
Daniel Vetter88cefb62012-08-12 19:27:14 +02002709static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002710{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002711 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002712 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002713 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002714 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002715
Jesse Barnesc64e3112010-09-10 11:27:03 -07002716
Jesse Barnes0e23b992010-09-10 11:10:00 -07002717 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002718 reg = FDI_RX_CTL(pipe);
2719 temp = I915_READ(reg);
2720 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002721 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Chris Wilson5eddb702010-09-11 13:48:45 +01002722 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2723 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2724
2725 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002726 udelay(200);
2727
2728 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002729 temp = I915_READ(reg);
2730 I915_WRITE(reg, temp | FDI_PCDCLK);
2731
2732 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002733 udelay(200);
2734
Paulo Zanoni20749732012-11-23 15:30:38 -02002735 /* Enable CPU FDI TX PLL, always on for Ironlake */
2736 reg = FDI_TX_CTL(pipe);
2737 temp = I915_READ(reg);
2738 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2739 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002740
Paulo Zanoni20749732012-11-23 15:30:38 -02002741 POSTING_READ(reg);
2742 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002743 }
2744}
2745
Daniel Vetter88cefb62012-08-12 19:27:14 +02002746static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2747{
2748 struct drm_device *dev = intel_crtc->base.dev;
2749 struct drm_i915_private *dev_priv = dev->dev_private;
2750 int pipe = intel_crtc->pipe;
2751 u32 reg, temp;
2752
2753 /* Switch from PCDclk to Rawclk */
2754 reg = FDI_RX_CTL(pipe);
2755 temp = I915_READ(reg);
2756 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2757
2758 /* Disable CPU FDI TX PLL */
2759 reg = FDI_TX_CTL(pipe);
2760 temp = I915_READ(reg);
2761 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2762
2763 POSTING_READ(reg);
2764 udelay(100);
2765
2766 reg = FDI_RX_CTL(pipe);
2767 temp = I915_READ(reg);
2768 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2769
2770 /* Wait for the clocks to turn off. */
2771 POSTING_READ(reg);
2772 udelay(100);
2773}
2774
Jesse Barnes291427f2011-07-29 12:42:37 -07002775static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2776{
2777 struct drm_i915_private *dev_priv = dev->dev_private;
2778 u32 flags = I915_READ(SOUTH_CHICKEN1);
2779
2780 flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2781 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2782 flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2783 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2784 POSTING_READ(SOUTH_CHICKEN1);
2785}
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002786static void ironlake_fdi_disable(struct drm_crtc *crtc)
2787{
2788 struct drm_device *dev = crtc->dev;
2789 struct drm_i915_private *dev_priv = dev->dev_private;
2790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2791 int pipe = intel_crtc->pipe;
2792 u32 reg, temp;
2793
2794 /* disable CPU FDI tx and PCH FDI rx */
2795 reg = FDI_TX_CTL(pipe);
2796 temp = I915_READ(reg);
2797 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2798 POSTING_READ(reg);
2799
2800 reg = FDI_RX_CTL(pipe);
2801 temp = I915_READ(reg);
2802 temp &= ~(0x7 << 16);
2803 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2804 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2805
2806 POSTING_READ(reg);
2807 udelay(100);
2808
2809 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002810 if (HAS_PCH_IBX(dev)) {
2811 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes291427f2011-07-29 12:42:37 -07002812 } else if (HAS_PCH_CPT(dev)) {
2813 cpt_phase_pointer_disable(dev, pipe);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002814 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002815
2816 /* still set train pattern 1 */
2817 reg = FDI_TX_CTL(pipe);
2818 temp = I915_READ(reg);
2819 temp &= ~FDI_LINK_TRAIN_NONE;
2820 temp |= FDI_LINK_TRAIN_PATTERN_1;
2821 I915_WRITE(reg, temp);
2822
2823 reg = FDI_RX_CTL(pipe);
2824 temp = I915_READ(reg);
2825 if (HAS_PCH_CPT(dev)) {
2826 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2827 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2828 } else {
2829 temp &= ~FDI_LINK_TRAIN_NONE;
2830 temp |= FDI_LINK_TRAIN_PATTERN_1;
2831 }
2832 /* BPC in FDI rx is consistent with that in PIPECONF */
2833 temp &= ~(0x07 << 16);
2834 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2835 I915_WRITE(reg, temp);
2836
2837 POSTING_READ(reg);
2838 udelay(100);
2839}
2840
Chris Wilson5bb61642012-09-27 21:25:58 +01002841static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2842{
2843 struct drm_device *dev = crtc->dev;
2844 struct drm_i915_private *dev_priv = dev->dev_private;
2845 unsigned long flags;
2846 bool pending;
2847
2848 if (atomic_read(&dev_priv->mm.wedged))
2849 return false;
2850
2851 spin_lock_irqsave(&dev->event_lock, flags);
2852 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2853 spin_unlock_irqrestore(&dev->event_lock, flags);
2854
2855 return pending;
2856}
2857
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002858static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2859{
Chris Wilson0f911282012-04-17 10:05:38 +01002860 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002861 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002862
2863 if (crtc->fb == NULL)
2864 return;
2865
Chris Wilson5bb61642012-09-27 21:25:58 +01002866 wait_event(dev_priv->pending_flip_queue,
2867 !intel_crtc_has_pending_flip(crtc));
2868
Chris Wilson0f911282012-04-17 10:05:38 +01002869 mutex_lock(&dev->struct_mutex);
2870 intel_finish_fb(crtc->fb);
2871 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002872}
2873
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002874static bool ironlake_crtc_driving_pch(struct drm_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08002875{
2876 struct drm_device *dev = crtc->dev;
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002877 struct intel_encoder *intel_encoder;
Jesse Barnes040484a2011-01-03 12:14:26 -08002878
2879 /*
2880 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2881 * must be driven by its own crtc; no sharing is possible.
2882 */
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002883 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002884 switch (intel_encoder->type) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002885 case INTEL_OUTPUT_EDP:
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002886 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnes040484a2011-01-03 12:14:26 -08002887 return false;
2888 continue;
2889 }
2890 }
2891
2892 return true;
2893}
2894
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002895static bool haswell_crtc_driving_pch(struct drm_crtc *crtc)
2896{
2897 return intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG);
2898}
2899
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002900/* Program iCLKIP clock to the desired frequency */
2901static void lpt_program_iclkip(struct drm_crtc *crtc)
2902{
2903 struct drm_device *dev = crtc->dev;
2904 struct drm_i915_private *dev_priv = dev->dev_private;
2905 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2906 u32 temp;
2907
Daniel Vetter09153002012-12-12 14:06:44 +01002908 mutex_lock(&dev_priv->dpio_lock);
2909
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002910 /* It is necessary to ungate the pixclk gate prior to programming
2911 * the divisors, and gate it back when it is done.
2912 */
2913 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2914
2915 /* Disable SSCCTL */
2916 intel_sbi_write(dev_priv, SBI_SSCCTL6,
2917 intel_sbi_read(dev_priv, SBI_SSCCTL6) |
2918 SBI_SSCCTL_DISABLE);
2919
2920 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2921 if (crtc->mode.clock == 20000) {
2922 auxdiv = 1;
2923 divsel = 0x41;
2924 phaseinc = 0x20;
2925 } else {
2926 /* The iCLK virtual clock root frequency is in MHz,
2927 * but the crtc->mode.clock in in KHz. To get the divisors,
2928 * it is necessary to divide one by another, so we
2929 * convert the virtual clock precision to KHz here for higher
2930 * precision.
2931 */
2932 u32 iclk_virtual_root_freq = 172800 * 1000;
2933 u32 iclk_pi_range = 64;
2934 u32 desired_divisor, msb_divisor_value, pi_value;
2935
2936 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2937 msb_divisor_value = desired_divisor / iclk_pi_range;
2938 pi_value = desired_divisor % iclk_pi_range;
2939
2940 auxdiv = 0;
2941 divsel = msb_divisor_value - 2;
2942 phaseinc = pi_value;
2943 }
2944
2945 /* This should not happen with any sane values */
2946 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2947 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2948 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2949 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2950
2951 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2952 crtc->mode.clock,
2953 auxdiv,
2954 divsel,
2955 phasedir,
2956 phaseinc);
2957
2958 /* Program SSCDIVINTPHASE6 */
2959 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6);
2960 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2961 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2962 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2963 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2964 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2965 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2966
2967 intel_sbi_write(dev_priv,
2968 SBI_SSCDIVINTPHASE6,
2969 temp);
2970
2971 /* Program SSCAUXDIV */
2972 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6);
2973 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2974 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
2975 intel_sbi_write(dev_priv,
2976 SBI_SSCAUXDIV6,
2977 temp);
2978
2979
2980 /* Enable modulator and associated divider */
2981 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6);
2982 temp &= ~SBI_SSCCTL_DISABLE;
2983 intel_sbi_write(dev_priv,
2984 SBI_SSCCTL6,
2985 temp);
2986
2987 /* Wait for initialization time */
2988 udelay(24);
2989
2990 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01002991
2992 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002993}
2994
Jesse Barnesf67a5592011-01-05 10:31:48 -08002995/*
2996 * Enable PCH resources required for PCH ports:
2997 * - PCH PLLs
2998 * - FDI training & RX/TX
2999 * - update transcoder timings
3000 * - DP transcoding bits
3001 * - transcoder
3002 */
3003static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003004{
3005 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003006 struct drm_i915_private *dev_priv = dev->dev_private;
3007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3008 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003009 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003010
Chris Wilsone7e164d2012-05-11 09:21:25 +01003011 assert_transcoder_disabled(dev_priv, pipe);
3012
Daniel Vettercd986ab2012-10-26 10:58:12 +02003013 /* Write the TU size bits before fdi link training, so that error
3014 * detection works. */
3015 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3016 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3017
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003018 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003019 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003020
Daniel Vetter572deb32012-10-27 18:46:14 +02003021 /* XXX: pch pll's can be enabled any time before we enable the PCH
3022 * transcoder, and we actually should do this to not upset any PCH
3023 * transcoder that already use the clock when we share it.
3024 *
3025 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
3026 * unconditionally resets the pll - we need that to have the right LVDS
3027 * enable sequence. */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02003028 ironlake_enable_pch_pll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01003029
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003030 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003031 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003032
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003033 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003034 switch (pipe) {
3035 default:
3036 case 0:
3037 temp |= TRANSA_DPLL_ENABLE;
3038 sel = TRANSA_DPLLB_SEL;
3039 break;
3040 case 1:
3041 temp |= TRANSB_DPLL_ENABLE;
3042 sel = TRANSB_DPLLB_SEL;
3043 break;
3044 case 2:
3045 temp |= TRANSC_DPLL_ENABLE;
3046 sel = TRANSC_DPLLB_SEL;
3047 break;
Jesse Barnesd64311a2011-10-12 15:01:33 -07003048 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003049 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3050 temp |= sel;
3051 else
3052 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003053 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003054 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003055
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003056 /* set transcoder timing, panel must allow it */
3057 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003058 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3059 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3060 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3061
3062 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3063 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3064 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003065 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003066
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003067 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003068
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003069 /* For PCH DP, enable TRANS_DP_CTL */
3070 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003071 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3072 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003073 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003074 reg = TRANS_DP_CTL(pipe);
3075 temp = I915_READ(reg);
3076 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003077 TRANS_DP_SYNC_MASK |
3078 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003079 temp |= (TRANS_DP_OUTPUT_ENABLE |
3080 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003081 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003082
3083 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003084 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003085 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003086 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003087
3088 switch (intel_trans_dp_port_sel(crtc)) {
3089 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003090 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003091 break;
3092 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003093 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003094 break;
3095 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003096 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003097 break;
3098 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003099 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003100 }
3101
Chris Wilson5eddb702010-09-11 13:48:45 +01003102 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003103 }
3104
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003105 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003106}
3107
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003108static void lpt_pch_enable(struct drm_crtc *crtc)
3109{
3110 struct drm_device *dev = crtc->dev;
3111 struct drm_i915_private *dev_priv = dev->dev_private;
3112 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003113 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003114
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003115 assert_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003116
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003117 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003118
Paulo Zanoni0540e482012-10-31 18:12:40 -02003119 /* Set transcoder timing. */
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003120 I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
3121 I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
3122 I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003123
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003124 I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
3125 I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
3126 I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
3127 I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003128
Paulo Zanoni937bb612012-10-31 18:12:47 -02003129 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003130}
3131
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003132static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3133{
3134 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3135
3136 if (pll == NULL)
3137 return;
3138
3139 if (pll->refcount == 0) {
3140 WARN(1, "bad PCH PLL refcount\n");
3141 return;
3142 }
3143
3144 --pll->refcount;
3145 intel_crtc->pch_pll = NULL;
3146}
3147
3148static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3149{
3150 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3151 struct intel_pch_pll *pll;
3152 int i;
3153
3154 pll = intel_crtc->pch_pll;
3155 if (pll) {
3156 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3157 intel_crtc->base.base.id, pll->pll_reg);
3158 goto prepare;
3159 }
3160
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003161 if (HAS_PCH_IBX(dev_priv->dev)) {
3162 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3163 i = intel_crtc->pipe;
3164 pll = &dev_priv->pch_plls[i];
3165
3166 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3167 intel_crtc->base.base.id, pll->pll_reg);
3168
3169 goto found;
3170 }
3171
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003172 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3173 pll = &dev_priv->pch_plls[i];
3174
3175 /* Only want to check enabled timings first */
3176 if (pll->refcount == 0)
3177 continue;
3178
3179 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3180 fp == I915_READ(pll->fp0_reg)) {
3181 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3182 intel_crtc->base.base.id,
3183 pll->pll_reg, pll->refcount, pll->active);
3184
3185 goto found;
3186 }
3187 }
3188
3189 /* Ok no matching timings, maybe there's a free one? */
3190 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3191 pll = &dev_priv->pch_plls[i];
3192 if (pll->refcount == 0) {
3193 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3194 intel_crtc->base.base.id, pll->pll_reg);
3195 goto found;
3196 }
3197 }
3198
3199 return NULL;
3200
3201found:
3202 intel_crtc->pch_pll = pll;
3203 pll->refcount++;
3204 DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
3205prepare: /* separate function? */
3206 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003207
Chris Wilsone04c7352012-05-02 20:43:56 +01003208 /* Wait for the clocks to stabilize before rewriting the regs */
3209 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003210 POSTING_READ(pll->pll_reg);
3211 udelay(150);
Chris Wilsone04c7352012-05-02 20:43:56 +01003212
3213 I915_WRITE(pll->fp0_reg, fp);
3214 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003215 pll->on = false;
3216 return pll;
3217}
3218
Jesse Barnesd4270e52011-10-11 10:43:02 -07003219void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3220{
3221 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003222 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003223 u32 temp;
3224
3225 temp = I915_READ(dslreg);
3226 udelay(500);
3227 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003228 if (wait_for(I915_READ(dslreg) != temp, 5))
3229 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3230 }
3231}
3232
Jesse Barnesf67a5592011-01-05 10:31:48 -08003233static void ironlake_crtc_enable(struct drm_crtc *crtc)
3234{
3235 struct drm_device *dev = crtc->dev;
3236 struct drm_i915_private *dev_priv = dev->dev_private;
3237 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003238 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003239 int pipe = intel_crtc->pipe;
3240 int plane = intel_crtc->plane;
3241 u32 temp;
3242 bool is_pch_port;
3243
Daniel Vetter08a48462012-07-02 11:43:47 +02003244 WARN_ON(!crtc->enabled);
3245
Jesse Barnesf67a5592011-01-05 10:31:48 -08003246 if (intel_crtc->active)
3247 return;
3248
3249 intel_crtc->active = true;
3250 intel_update_watermarks(dev);
3251
3252 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3253 temp = I915_READ(PCH_LVDS);
3254 if ((temp & LVDS_PORT_EN) == 0)
3255 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3256 }
3257
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003258 is_pch_port = ironlake_crtc_driving_pch(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003259
Daniel Vetter46b6f812012-09-06 22:08:33 +02003260 if (is_pch_port) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003261 /* Note: FDI PLL enabling _must_ be done before we enable the
3262 * cpu pipes, hence this is separate from all the other fdi/pch
3263 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003264 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003265 } else {
3266 assert_fdi_tx_disabled(dev_priv, pipe);
3267 assert_fdi_rx_disabled(dev_priv, pipe);
3268 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003269
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003270 for_each_encoder_on_crtc(dev, crtc, encoder)
3271 if (encoder->pre_enable)
3272 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003273
3274 /* Enable panel fitting for LVDS */
3275 if (dev_priv->pch_pf_size &&
Jani Nikula547dc042012-11-02 11:24:03 +02003276 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3277 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnesf67a5592011-01-05 10:31:48 -08003278 /* Force use of hard-coded filter coefficients
3279 * as some pre-programmed values are broken,
3280 * e.g. x201.
3281 */
Paulo Zanoni13888d72012-11-20 13:27:41 -02003282 if (IS_IVYBRIDGE(dev))
3283 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3284 PF_PIPE_SEL_IVB(pipe));
3285 else
3286 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003287 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3288 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003289 }
3290
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003291 /*
3292 * On ILK+ LUT must be loaded before the pipe is running but with
3293 * clocks enabled
3294 */
3295 intel_crtc_load_lut(crtc);
3296
Jesse Barnesf67a5592011-01-05 10:31:48 -08003297 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3298 intel_enable_plane(dev_priv, plane, pipe);
3299
3300 if (is_pch_port)
3301 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003302
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003303 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003304 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003305 mutex_unlock(&dev->struct_mutex);
3306
Chris Wilson6b383a72010-09-13 13:54:26 +01003307 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003308
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003309 for_each_encoder_on_crtc(dev, crtc, encoder)
3310 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003311
3312 if (HAS_PCH_CPT(dev))
3313 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003314
3315 /*
3316 * There seems to be a race in PCH platform hw (at least on some
3317 * outputs) where an enabled pipe still completes any pageflip right
3318 * away (as if the pipe is off) instead of waiting for vblank. As soon
3319 * as the first vblank happend, everything works as expected. Hence just
3320 * wait for one vblank before returning to avoid strange things
3321 * happening.
3322 */
3323 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003324}
3325
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003326static void haswell_crtc_enable(struct drm_crtc *crtc)
3327{
3328 struct drm_device *dev = crtc->dev;
3329 struct drm_i915_private *dev_priv = dev->dev_private;
3330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3331 struct intel_encoder *encoder;
3332 int pipe = intel_crtc->pipe;
3333 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003334 bool is_pch_port;
3335
3336 WARN_ON(!crtc->enabled);
3337
3338 if (intel_crtc->active)
3339 return;
3340
3341 intel_crtc->active = true;
3342 intel_update_watermarks(dev);
3343
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003344 is_pch_port = haswell_crtc_driving_pch(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003345
Paulo Zanoni83616632012-10-23 18:29:54 -02003346 if (is_pch_port)
Paulo Zanoni04945642012-11-01 21:00:59 -02003347 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003348
3349 for_each_encoder_on_crtc(dev, crtc, encoder)
3350 if (encoder->pre_enable)
3351 encoder->pre_enable(encoder);
3352
Paulo Zanoni1f544382012-10-24 11:32:00 -02003353 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003354
Paulo Zanoni1f544382012-10-24 11:32:00 -02003355 /* Enable panel fitting for eDP */
Jani Nikula547dc042012-11-02 11:24:03 +02003356 if (dev_priv->pch_pf_size &&
3357 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003358 /* Force use of hard-coded filter coefficients
3359 * as some pre-programmed values are broken,
3360 * e.g. x201.
3361 */
Paulo Zanoni54075a72012-11-20 13:27:42 -02003362 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3363 PF_PIPE_SEL_IVB(pipe));
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003364 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3365 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3366 }
3367
3368 /*
3369 * On ILK+ LUT must be loaded before the pipe is running but with
3370 * clocks enabled
3371 */
3372 intel_crtc_load_lut(crtc);
3373
Paulo Zanoni1f544382012-10-24 11:32:00 -02003374 intel_ddi_set_pipe_settings(crtc);
3375 intel_ddi_enable_pipe_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003376
3377 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3378 intel_enable_plane(dev_priv, plane, pipe);
3379
3380 if (is_pch_port)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003381 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003382
3383 mutex_lock(&dev->struct_mutex);
3384 intel_update_fbc(dev);
3385 mutex_unlock(&dev->struct_mutex);
3386
3387 intel_crtc_update_cursor(crtc, true);
3388
3389 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
Jesse Barnes6be4a602010-09-10 10:26:01 -07003403static void ironlake_crtc_disable(struct drm_crtc *crtc)
3404{
3405 struct drm_device *dev = crtc->dev;
3406 struct drm_i915_private *dev_priv = dev->dev_private;
3407 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003408 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003409 int pipe = intel_crtc->pipe;
3410 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003412
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003413
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003414 if (!intel_crtc->active)
3415 return;
3416
Daniel Vetterea9d7582012-07-10 10:42:52 +02003417 for_each_encoder_on_crtc(dev, crtc, encoder)
3418 encoder->disable(encoder);
3419
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003420 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003421 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003422 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003423
Jesse Barnesb24e7172011-01-04 15:09:30 -08003424 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003425
Chris Wilson973d04f2011-07-08 12:22:37 +01003426 if (dev_priv->cfb_plane == plane)
3427 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003428
Jesse Barnesb24e7172011-01-04 15:09:30 -08003429 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003430
Jesse Barnes6be4a602010-09-10 10:26:01 -07003431 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003432 I915_WRITE(PF_CTL(pipe), 0);
3433 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003434
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003435 for_each_encoder_on_crtc(dev, crtc, encoder)
3436 if (encoder->post_disable)
3437 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003438
Chris Wilson5eddb702010-09-11 13:48:45 +01003439 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003440
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003441 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003442
3443 if (HAS_PCH_CPT(dev)) {
3444 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003445 reg = TRANS_DP_CTL(pipe);
3446 temp = I915_READ(reg);
3447 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003448 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003449 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003450
3451 /* disable DPLL_SEL */
3452 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003453 switch (pipe) {
3454 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003455 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003456 break;
3457 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003458 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003459 break;
3460 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003461 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003462 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003463 break;
3464 default:
3465 BUG(); /* wtf */
3466 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003467 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003468 }
3469
3470 /* disable PCH DPLL */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003471 intel_disable_pch_pll(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003472
Daniel Vetter88cefb62012-08-12 19:27:14 +02003473 ironlake_fdi_pll_disable(intel_crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +01003474
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003475 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003476 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003477
3478 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003479 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003480 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003481}
3482
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003483static void haswell_crtc_disable(struct drm_crtc *crtc)
3484{
3485 struct drm_device *dev = crtc->dev;
3486 struct drm_i915_private *dev_priv = dev->dev_private;
3487 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3488 struct intel_encoder *encoder;
3489 int pipe = intel_crtc->pipe;
3490 int plane = intel_crtc->plane;
Paulo Zanoniad80a812012-10-24 16:06:19 -02003491 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni83616632012-10-23 18:29:54 -02003492 bool is_pch_port;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003493
3494 if (!intel_crtc->active)
3495 return;
3496
Paulo Zanoni83616632012-10-23 18:29:54 -02003497 is_pch_port = haswell_crtc_driving_pch(crtc);
3498
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003499 for_each_encoder_on_crtc(dev, crtc, encoder)
3500 encoder->disable(encoder);
3501
3502 intel_crtc_wait_for_pending_flips(crtc);
3503 drm_vblank_off(dev, pipe);
3504 intel_crtc_update_cursor(crtc, false);
3505
3506 intel_disable_plane(dev_priv, plane, pipe);
3507
3508 if (dev_priv->cfb_plane == plane)
3509 intel_disable_fbc(dev);
3510
3511 intel_disable_pipe(dev_priv, pipe);
3512
Paulo Zanoniad80a812012-10-24 16:06:19 -02003513 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003514
3515 /* Disable PF */
3516 I915_WRITE(PF_CTL(pipe), 0);
3517 I915_WRITE(PF_WIN_SZ(pipe), 0);
3518
Paulo Zanoni1f544382012-10-24 11:32:00 -02003519 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003520
3521 for_each_encoder_on_crtc(dev, crtc, encoder)
3522 if (encoder->post_disable)
3523 encoder->post_disable(encoder);
3524
Paulo Zanoni83616632012-10-23 18:29:54 -02003525 if (is_pch_port) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003526 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003527 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003528 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003529
3530 intel_crtc->active = false;
3531 intel_update_watermarks(dev);
3532
3533 mutex_lock(&dev->struct_mutex);
3534 intel_update_fbc(dev);
3535 mutex_unlock(&dev->struct_mutex);
3536}
3537
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003538static void ironlake_crtc_off(struct drm_crtc *crtc)
3539{
3540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3541 intel_put_pch_pll(intel_crtc);
3542}
3543
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003544static void haswell_crtc_off(struct drm_crtc *crtc)
3545{
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003546 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3547
3548 /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
3549 * start using it. */
Daniel Vetter1a240d42012-11-29 22:18:51 +01003550 intel_crtc->cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003551
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003552 intel_ddi_put_crtc_pll(crtc);
3553}
3554
Daniel Vetter02e792f2009-09-15 22:57:34 +02003555static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3556{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003557 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003558 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003559 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003560
Chris Wilson23f09ce2010-08-12 13:53:37 +01003561 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003562 dev_priv->mm.interruptible = false;
3563 (void) intel_overlay_switch_off(intel_crtc->overlay);
3564 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003565 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003566 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003567
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003568 /* Let userspace switch the overlay on again. In most cases userspace
3569 * has to recompute where to put it anyway.
3570 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003571}
3572
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003573static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003574{
3575 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003576 struct drm_i915_private *dev_priv = dev->dev_private;
3577 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003578 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003579 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003580 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003581
Daniel Vetter08a48462012-07-02 11:43:47 +02003582 WARN_ON(!crtc->enabled);
3583
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003584 if (intel_crtc->active)
3585 return;
3586
3587 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003588 intel_update_watermarks(dev);
3589
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003590 intel_enable_pll(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08003591 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003592 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003593
3594 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003595 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003596
3597 /* Give the overlay scaler a chance to enable if it's on this pipe */
3598 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003599 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003600
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003601 for_each_encoder_on_crtc(dev, crtc, encoder)
3602 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003603}
3604
3605static void i9xx_crtc_disable(struct drm_crtc *crtc)
3606{
3607 struct drm_device *dev = crtc->dev;
3608 struct drm_i915_private *dev_priv = dev->dev_private;
3609 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003610 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003611 int pipe = intel_crtc->pipe;
3612 int plane = intel_crtc->plane;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003613
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003614
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003615 if (!intel_crtc->active)
3616 return;
3617
Daniel Vetterea9d7582012-07-10 10:42:52 +02003618 for_each_encoder_on_crtc(dev, crtc, encoder)
3619 encoder->disable(encoder);
3620
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003621 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003622 intel_crtc_wait_for_pending_flips(crtc);
3623 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003624 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003625 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003626
Chris Wilson973d04f2011-07-08 12:22:37 +01003627 if (dev_priv->cfb_plane == plane)
3628 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003629
Jesse Barnesb24e7172011-01-04 15:09:30 -08003630 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003631 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003632 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003633
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003634 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003635 intel_update_fbc(dev);
3636 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003637}
3638
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003639static void i9xx_crtc_off(struct drm_crtc *crtc)
3640{
3641}
3642
Daniel Vetter976f8a22012-07-08 22:34:21 +02003643static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3644 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003645{
3646 struct drm_device *dev = crtc->dev;
3647 struct drm_i915_master_private *master_priv;
3648 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3649 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003650
3651 if (!dev->primary->master)
3652 return;
3653
3654 master_priv = dev->primary->master->driver_priv;
3655 if (!master_priv->sarea_priv)
3656 return;
3657
Jesse Barnes79e53942008-11-07 14:24:08 -08003658 switch (pipe) {
3659 case 0:
3660 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3661 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3662 break;
3663 case 1:
3664 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3665 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3666 break;
3667 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003668 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003669 break;
3670 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003671}
3672
Daniel Vetter976f8a22012-07-08 22:34:21 +02003673/**
3674 * Sets the power management mode of the pipe and plane.
3675 */
3676void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003677{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003678 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003679 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003680 struct intel_encoder *intel_encoder;
3681 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003682
Daniel Vetter976f8a22012-07-08 22:34:21 +02003683 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3684 enable |= intel_encoder->connectors_active;
3685
3686 if (enable)
3687 dev_priv->display.crtc_enable(crtc);
3688 else
3689 dev_priv->display.crtc_disable(crtc);
3690
3691 intel_crtc_update_sarea(crtc, enable);
3692}
3693
3694static void intel_crtc_noop(struct drm_crtc *crtc)
3695{
3696}
3697
3698static void intel_crtc_disable(struct drm_crtc *crtc)
3699{
3700 struct drm_device *dev = crtc->dev;
3701 struct drm_connector *connector;
3702 struct drm_i915_private *dev_priv = dev->dev_private;
3703
3704 /* crtc should still be enabled when we disable it. */
3705 WARN_ON(!crtc->enabled);
3706
3707 dev_priv->display.crtc_disable(crtc);
3708 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003709 dev_priv->display.off(crtc);
3710
Chris Wilson931872f2012-01-16 23:01:13 +00003711 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3712 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003713
3714 if (crtc->fb) {
3715 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003716 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003717 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003718 crtc->fb = NULL;
3719 }
3720
3721 /* Update computed state. */
3722 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3723 if (!connector->encoder || !connector->encoder->crtc)
3724 continue;
3725
3726 if (connector->encoder->crtc != crtc)
3727 continue;
3728
3729 connector->dpms = DRM_MODE_DPMS_OFF;
3730 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003731 }
3732}
3733
Daniel Vettera261b242012-07-26 19:21:47 +02003734void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003735{
Daniel Vettera261b242012-07-26 19:21:47 +02003736 struct drm_crtc *crtc;
3737
3738 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3739 if (crtc->enabled)
3740 intel_crtc_disable(crtc);
3741 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003742}
3743
Daniel Vetter1f703852012-07-11 16:51:39 +02003744void intel_encoder_noop(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003745{
Jesse Barnes79e53942008-11-07 14:24:08 -08003746}
3747
Chris Wilsonea5b2132010-08-04 13:50:23 +01003748void intel_encoder_destroy(struct drm_encoder *encoder)
3749{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003750 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003751
Chris Wilsonea5b2132010-08-04 13:50:23 +01003752 drm_encoder_cleanup(encoder);
3753 kfree(intel_encoder);
3754}
3755
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003756/* Simple dpms helper for encodres with just one connector, no cloning and only
3757 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3758 * state of the entire output pipe. */
3759void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3760{
3761 if (mode == DRM_MODE_DPMS_ON) {
3762 encoder->connectors_active = true;
3763
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003764 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003765 } else {
3766 encoder->connectors_active = false;
3767
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003768 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003769 }
3770}
3771
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003772/* Cross check the actual hw state with our own modeset state tracking (and it's
3773 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003774static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003775{
3776 if (connector->get_hw_state(connector)) {
3777 struct intel_encoder *encoder = connector->encoder;
3778 struct drm_crtc *crtc;
3779 bool encoder_enabled;
3780 enum pipe pipe;
3781
3782 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3783 connector->base.base.id,
3784 drm_get_connector_name(&connector->base));
3785
3786 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3787 "wrong connector dpms state\n");
3788 WARN(connector->base.encoder != &encoder->base,
3789 "active connector not linked to encoder\n");
3790 WARN(!encoder->connectors_active,
3791 "encoder->connectors_active not set\n");
3792
3793 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3794 WARN(!encoder_enabled, "encoder not enabled\n");
3795 if (WARN_ON(!encoder->base.crtc))
3796 return;
3797
3798 crtc = encoder->base.crtc;
3799
3800 WARN(!crtc->enabled, "crtc not enabled\n");
3801 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3802 WARN(pipe != to_intel_crtc(crtc)->pipe,
3803 "encoder active on the wrong pipe\n");
3804 }
3805}
3806
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003807/* Even simpler default implementation, if there's really no special case to
3808 * consider. */
3809void intel_connector_dpms(struct drm_connector *connector, int mode)
3810{
3811 struct intel_encoder *encoder = intel_attached_encoder(connector);
3812
3813 /* All the simple cases only support two dpms states. */
3814 if (mode != DRM_MODE_DPMS_ON)
3815 mode = DRM_MODE_DPMS_OFF;
3816
3817 if (mode == connector->dpms)
3818 return;
3819
3820 connector->dpms = mode;
3821
3822 /* Only need to change hw state when actually enabled */
3823 if (encoder->base.crtc)
3824 intel_encoder_dpms(encoder, mode);
3825 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003826 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003827
Daniel Vetterb9805142012-08-31 17:37:33 +02003828 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003829}
3830
Daniel Vetterf0947c32012-07-02 13:10:34 +02003831/* Simple connector->get_hw_state implementation for encoders that support only
3832 * one connector and no cloning and hence the encoder state determines the state
3833 * of the connector. */
3834bool intel_connector_get_hw_state(struct intel_connector *connector)
3835{
Daniel Vetter24929352012-07-02 20:28:59 +02003836 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003837 struct intel_encoder *encoder = connector->encoder;
3838
3839 return encoder->get_hw_state(encoder, &pipe);
3840}
3841
Jesse Barnes79e53942008-11-07 14:24:08 -08003842static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
Daniel Vetter35313cd2012-07-20 10:30:45 +02003843 const struct drm_display_mode *mode,
Jesse Barnes79e53942008-11-07 14:24:08 -08003844 struct drm_display_mode *adjusted_mode)
3845{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003846 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003847
Eric Anholtbad720f2009-10-22 16:11:14 -07003848 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003849 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003850 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3851 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003852 }
Chris Wilson89749352010-09-12 18:25:19 +01003853
Daniel Vetterf9bef082012-04-15 19:53:19 +02003854 /* All interlaced capable intel hw wants timings in frames. Note though
3855 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3856 * timings, so we need to be careful not to clobber these.*/
3857 if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3858 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003859
Chris Wilson44f46b422012-06-21 13:19:59 +03003860 /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
3861 * with a hsync front porch of 0.
3862 */
3863 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
3864 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
3865 return false;
3866
Jesse Barnes79e53942008-11-07 14:24:08 -08003867 return true;
3868}
3869
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07003870static int valleyview_get_display_clock_speed(struct drm_device *dev)
3871{
3872 return 400000; /* FIXME */
3873}
3874
Jesse Barnese70236a2009-09-21 10:42:27 -07003875static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003876{
Jesse Barnese70236a2009-09-21 10:42:27 -07003877 return 400000;
3878}
Jesse Barnes79e53942008-11-07 14:24:08 -08003879
Jesse Barnese70236a2009-09-21 10:42:27 -07003880static int i915_get_display_clock_speed(struct drm_device *dev)
3881{
3882 return 333000;
3883}
Jesse Barnes79e53942008-11-07 14:24:08 -08003884
Jesse Barnese70236a2009-09-21 10:42:27 -07003885static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3886{
3887 return 200000;
3888}
Jesse Barnes79e53942008-11-07 14:24:08 -08003889
Jesse Barnese70236a2009-09-21 10:42:27 -07003890static int i915gm_get_display_clock_speed(struct drm_device *dev)
3891{
3892 u16 gcfgc = 0;
3893
3894 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3895
3896 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003897 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003898 else {
3899 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3900 case GC_DISPLAY_CLOCK_333_MHZ:
3901 return 333000;
3902 default:
3903 case GC_DISPLAY_CLOCK_190_200_MHZ:
3904 return 190000;
3905 }
3906 }
3907}
Jesse Barnes79e53942008-11-07 14:24:08 -08003908
Jesse Barnese70236a2009-09-21 10:42:27 -07003909static int i865_get_display_clock_speed(struct drm_device *dev)
3910{
3911 return 266000;
3912}
3913
3914static int i855_get_display_clock_speed(struct drm_device *dev)
3915{
3916 u16 hpllcc = 0;
3917 /* Assume that the hardware is in the high speed state. This
3918 * should be the default.
3919 */
3920 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3921 case GC_CLOCK_133_200:
3922 case GC_CLOCK_100_200:
3923 return 200000;
3924 case GC_CLOCK_166_250:
3925 return 250000;
3926 case GC_CLOCK_100_133:
3927 return 133000;
3928 }
3929
3930 /* Shouldn't happen */
3931 return 0;
3932}
3933
3934static int i830_get_display_clock_speed(struct drm_device *dev)
3935{
3936 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08003937}
3938
Zhenyu Wang2c072452009-06-05 15:38:42 +08003939static void
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003940intel_reduce_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003941{
3942 while (*num > 0xffffff || *den > 0xffffff) {
3943 *num >>= 1;
3944 *den >>= 1;
3945 }
3946}
3947
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003948void
3949intel_link_compute_m_n(int bits_per_pixel, int nlanes,
3950 int pixel_clock, int link_clock,
3951 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003952{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003953 m_n->tu = 64;
Chris Wilson22ed1112010-12-04 01:01:29 +00003954 m_n->gmch_m = bits_per_pixel * pixel_clock;
3955 m_n->gmch_n = link_clock * nlanes * 8;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003956 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
Chris Wilson22ed1112010-12-04 01:01:29 +00003957 m_n->link_m = pixel_clock;
3958 m_n->link_n = link_clock;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003959 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08003960}
3961
Chris Wilsona7615032011-01-12 17:04:08 +00003962static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
3963{
Keith Packard72bbe582011-09-26 16:09:45 -07003964 if (i915_panel_use_ssc >= 0)
3965 return i915_panel_use_ssc != 0;
3966 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07003967 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00003968}
3969
Jesse Barnes5a354202011-06-24 12:19:22 -07003970/**
3971 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
3972 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08003973 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07003974 *
3975 * A pipe may be connected to one or more outputs. Based on the depth of the
3976 * attached framebuffer, choose a good color depth to use on the pipe.
3977 *
3978 * If possible, match the pipe depth to the fb depth. In some cases, this
3979 * isn't ideal, because the connected output supports a lesser or restricted
3980 * set of depths. Resolve that here:
3981 * LVDS typically supports only 6bpc, so clamp down in that case
3982 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
3983 * Displays may support a restricted set as well, check EDID and clamp as
3984 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08003985 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07003986 *
3987 * RETURNS:
3988 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
3989 * true if they don't match).
3990 */
3991static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02003992 struct drm_framebuffer *fb,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08003993 unsigned int *pipe_bpp,
3994 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07003995{
3996 struct drm_device *dev = crtc->dev;
3997 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes5a354202011-06-24 12:19:22 -07003998 struct drm_connector *connector;
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02003999 struct intel_encoder *intel_encoder;
Jesse Barnes5a354202011-06-24 12:19:22 -07004000 unsigned int display_bpc = UINT_MAX, bpc;
4001
4002 /* Walk the encoders & connectors on this crtc, get min bpc */
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004003 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004004
4005 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
4006 unsigned int lvds_bpc;
4007
4008 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
4009 LVDS_A3_POWER_UP)
4010 lvds_bpc = 8;
4011 else
4012 lvds_bpc = 6;
4013
4014 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004015 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004016 display_bpc = lvds_bpc;
4017 }
4018 continue;
4019 }
4020
Jesse Barnes5a354202011-06-24 12:19:22 -07004021 /* Not one of the known troublemakers, check the EDID */
4022 list_for_each_entry(connector, &dev->mode_config.connector_list,
4023 head) {
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004024 if (connector->encoder != &intel_encoder->base)
Jesse Barnes5a354202011-06-24 12:19:22 -07004025 continue;
4026
Jesse Barnes62ac41a2011-07-28 12:55:14 -07004027 /* Don't use an invalid EDID bpc value */
4028 if (connector->display_info.bpc &&
4029 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004030 DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004031 display_bpc = connector->display_info.bpc;
4032 }
4033 }
4034
4035 /*
4036 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
4037 * through, clamp it down. (Note: >12bpc will be caught below.)
4038 */
4039 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
4040 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04004041 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004042 display_bpc = 12;
4043 } else {
Adam Jackson82820492011-10-10 16:33:34 -04004044 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004045 display_bpc = 8;
4046 }
4047 }
4048 }
4049
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004050 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4051 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
4052 display_bpc = 6;
4053 }
4054
Jesse Barnes5a354202011-06-24 12:19:22 -07004055 /*
4056 * We could just drive the pipe at the highest bpc all the time and
4057 * enable dithering as needed, but that costs bandwidth. So choose
4058 * the minimum value that expresses the full color range of the fb but
4059 * also stays within the max display bpc discovered above.
4060 */
4061
Daniel Vetter94352cf2012-07-05 22:51:56 +02004062 switch (fb->depth) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004063 case 8:
4064 bpc = 8; /* since we go through a colormap */
4065 break;
4066 case 15:
4067 case 16:
4068 bpc = 6; /* min is 18bpp */
4069 break;
4070 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07004071 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07004072 break;
4073 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07004074 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07004075 break;
4076 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07004077 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07004078 break;
4079 default:
4080 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
4081 bpc = min((unsigned int)8, display_bpc);
4082 break;
4083 }
4084
Keith Packard578393c2011-09-05 11:53:21 -07004085 display_bpc = min(display_bpc, bpc);
4086
Adam Jackson82820492011-10-10 16:33:34 -04004087 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
4088 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004089
Keith Packard578393c2011-09-05 11:53:21 -07004090 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07004091
4092 return display_bpc != bpc;
4093}
4094
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004095static int vlv_get_refclk(struct drm_crtc *crtc)
4096{
4097 struct drm_device *dev = crtc->dev;
4098 struct drm_i915_private *dev_priv = dev->dev_private;
4099 int refclk = 27000; /* for DP & HDMI */
4100
4101 return 100000; /* only one validated so far */
4102
4103 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4104 refclk = 96000;
4105 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4106 if (intel_panel_use_ssc(dev_priv))
4107 refclk = 100000;
4108 else
4109 refclk = 96000;
4110 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4111 refclk = 100000;
4112 }
4113
4114 return refclk;
4115}
4116
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004117static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4118{
4119 struct drm_device *dev = crtc->dev;
4120 struct drm_i915_private *dev_priv = dev->dev_private;
4121 int refclk;
4122
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004123 if (IS_VALLEYVIEW(dev)) {
4124 refclk = vlv_get_refclk(crtc);
4125 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004126 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4127 refclk = dev_priv->lvds_ssc_freq * 1000;
4128 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4129 refclk / 1000);
4130 } else if (!IS_GEN2(dev)) {
4131 refclk = 96000;
4132 } else {
4133 refclk = 48000;
4134 }
4135
4136 return refclk;
4137}
4138
4139static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
4140 intel_clock_t *clock)
4141{
4142 /* SDVO TV has fixed PLL values depend on its clock range,
4143 this mirrors vbios setting. */
4144 if (adjusted_mode->clock >= 100000
4145 && adjusted_mode->clock < 140500) {
4146 clock->p1 = 2;
4147 clock->p2 = 10;
4148 clock->n = 3;
4149 clock->m1 = 16;
4150 clock->m2 = 8;
4151 } else if (adjusted_mode->clock >= 140500
4152 && adjusted_mode->clock <= 200000) {
4153 clock->p1 = 1;
4154 clock->p2 = 10;
4155 clock->n = 6;
4156 clock->m1 = 12;
4157 clock->m2 = 8;
4158 }
4159}
4160
Jesse Barnesa7516a02011-12-15 12:30:37 -08004161static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
4162 intel_clock_t *clock,
4163 intel_clock_t *reduced_clock)
4164{
4165 struct drm_device *dev = crtc->dev;
4166 struct drm_i915_private *dev_priv = dev->dev_private;
4167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4168 int pipe = intel_crtc->pipe;
4169 u32 fp, fp2 = 0;
4170
4171 if (IS_PINEVIEW(dev)) {
4172 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
4173 if (reduced_clock)
4174 fp2 = (1 << reduced_clock->n) << 16 |
4175 reduced_clock->m1 << 8 | reduced_clock->m2;
4176 } else {
4177 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4178 if (reduced_clock)
4179 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4180 reduced_clock->m2;
4181 }
4182
4183 I915_WRITE(FP0(pipe), fp);
4184
4185 intel_crtc->lowfreq_avail = false;
4186 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4187 reduced_clock && i915_powersave) {
4188 I915_WRITE(FP1(pipe), fp2);
4189 intel_crtc->lowfreq_avail = true;
4190 } else {
4191 I915_WRITE(FP1(pipe), fp);
4192 }
4193}
4194
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004195static void vlv_update_pll(struct drm_crtc *crtc,
4196 struct drm_display_mode *mode,
4197 struct drm_display_mode *adjusted_mode,
4198 intel_clock_t *clock, intel_clock_t *reduced_clock,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304199 int num_connectors)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004200{
4201 struct drm_device *dev = crtc->dev;
4202 struct drm_i915_private *dev_priv = dev->dev_private;
4203 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4204 int pipe = intel_crtc->pipe;
4205 u32 dpll, mdiv, pdiv;
4206 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304207 bool is_sdvo;
4208 u32 temp;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004209
Daniel Vetter09153002012-12-12 14:06:44 +01004210 mutex_lock(&dev_priv->dpio_lock);
4211
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304212 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4213 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4214
4215 dpll = DPLL_VGA_MODE_DIS;
4216 dpll |= DPLL_EXT_BUFFER_ENABLE_VLV;
4217 dpll |= DPLL_REFA_CLK_ENABLE_VLV;
4218 dpll |= DPLL_INTEGRATED_CLOCK_VLV;
4219
4220 I915_WRITE(DPLL(pipe), dpll);
4221 POSTING_READ(DPLL(pipe));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004222
4223 bestn = clock->n;
4224 bestm1 = clock->m1;
4225 bestm2 = clock->m2;
4226 bestp1 = clock->p1;
4227 bestp2 = clock->p2;
4228
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304229 /*
4230 * In Valleyview PLL and program lane counter registers are exposed
4231 * through DPIO interface
4232 */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004233 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4234 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4235 mdiv |= ((bestn << DPIO_N_SHIFT));
4236 mdiv |= (1 << DPIO_POST_DIV_SHIFT);
4237 mdiv |= (1 << DPIO_K_SHIFT);
4238 mdiv |= DPIO_ENABLE_CALIBRATION;
4239 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4240
4241 intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
4242
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304243 pdiv = (1 << DPIO_REFSEL_OVERRIDE) | (5 << DPIO_PLL_MODESEL_SHIFT) |
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004244 (3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304245 (7 << DPIO_PLL_REFCLK_SEL_SHIFT) | (8 << DPIO_DRIVER_CTL_SHIFT) |
4246 (5 << DPIO_CLK_BIAS_CTL_SHIFT);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004247 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
4248
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304249 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x005f003b);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004250
4251 dpll |= DPLL_VCO_ENABLE;
4252 I915_WRITE(DPLL(pipe), dpll);
4253 POSTING_READ(DPLL(pipe));
4254 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4255 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4256
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304257 intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x620);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004258
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304259 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4260 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4261
4262 I915_WRITE(DPLL(pipe), dpll);
4263
4264 /* Wait for the clocks to stabilize. */
4265 POSTING_READ(DPLL(pipe));
4266 udelay(150);
4267
4268 temp = 0;
4269 if (is_sdvo) {
4270 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004271 if (temp > 1)
4272 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4273 else
4274 temp = 0;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004275 }
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304276 I915_WRITE(DPLL_MD(pipe), temp);
4277 POSTING_READ(DPLL_MD(pipe));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004278
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304279 /* Now program lane control registers */
4280 if(intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)
4281 || intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
4282 {
4283 temp = 0x1000C4;
4284 if(pipe == 1)
4285 temp |= (1 << 21);
4286 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL1, temp);
4287 }
4288 if(intel_pipe_has_type(crtc,INTEL_OUTPUT_EDP))
4289 {
4290 temp = 0x1000C4;
4291 if(pipe == 1)
4292 temp |= (1 << 21);
4293 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL2, temp);
4294 }
Daniel Vetter09153002012-12-12 14:06:44 +01004295
4296 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004297}
4298
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004299static void i9xx_update_pll(struct drm_crtc *crtc,
4300 struct drm_display_mode *mode,
4301 struct drm_display_mode *adjusted_mode,
4302 intel_clock_t *clock, intel_clock_t *reduced_clock,
4303 int num_connectors)
4304{
4305 struct drm_device *dev = crtc->dev;
4306 struct drm_i915_private *dev_priv = dev->dev_private;
4307 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004308 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004309 int pipe = intel_crtc->pipe;
4310 u32 dpll;
4311 bool is_sdvo;
4312
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304313 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4314
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004315 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4316 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4317
4318 dpll = DPLL_VGA_MODE_DIS;
4319
4320 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4321 dpll |= DPLLB_MODE_LVDS;
4322 else
4323 dpll |= DPLLB_MODE_DAC_SERIAL;
4324 if (is_sdvo) {
4325 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4326 if (pixel_multiplier > 1) {
4327 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4328 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4329 }
4330 dpll |= DPLL_DVO_HIGH_SPEED;
4331 }
4332 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4333 dpll |= DPLL_DVO_HIGH_SPEED;
4334
4335 /* compute bitmask from p1 value */
4336 if (IS_PINEVIEW(dev))
4337 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4338 else {
4339 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4340 if (IS_G4X(dev) && reduced_clock)
4341 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4342 }
4343 switch (clock->p2) {
4344 case 5:
4345 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4346 break;
4347 case 7:
4348 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4349 break;
4350 case 10:
4351 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4352 break;
4353 case 14:
4354 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4355 break;
4356 }
4357 if (INTEL_INFO(dev)->gen >= 4)
4358 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4359
4360 if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4361 dpll |= PLL_REF_INPUT_TVCLKINBC;
4362 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4363 /* XXX: just matching BIOS for now */
4364 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4365 dpll |= 3;
4366 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4367 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4368 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4369 else
4370 dpll |= PLL_REF_INPUT_DREFCLK;
4371
4372 dpll |= DPLL_VCO_ENABLE;
4373 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4374 POSTING_READ(DPLL(pipe));
4375 udelay(150);
4376
Daniel Vetterdafd2262012-11-26 17:22:07 +01004377 for_each_encoder_on_crtc(dev, crtc, encoder)
4378 if (encoder->pre_pll_enable)
4379 encoder->pre_pll_enable(encoder);
4380
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004381 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4382 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4383
4384 I915_WRITE(DPLL(pipe), dpll);
4385
4386 /* Wait for the clocks to stabilize. */
4387 POSTING_READ(DPLL(pipe));
4388 udelay(150);
4389
4390 if (INTEL_INFO(dev)->gen >= 4) {
4391 u32 temp = 0;
4392 if (is_sdvo) {
4393 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4394 if (temp > 1)
4395 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4396 else
4397 temp = 0;
4398 }
4399 I915_WRITE(DPLL_MD(pipe), temp);
4400 } else {
4401 /* The pixel multiplier can only be updated once the
4402 * DPLL is enabled and the clocks are stable.
4403 *
4404 * So write it again.
4405 */
4406 I915_WRITE(DPLL(pipe), dpll);
4407 }
4408}
4409
4410static void i8xx_update_pll(struct drm_crtc *crtc,
4411 struct drm_display_mode *adjusted_mode,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304412 intel_clock_t *clock, intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004413 int num_connectors)
4414{
4415 struct drm_device *dev = crtc->dev;
4416 struct drm_i915_private *dev_priv = dev->dev_private;
4417 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004418 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004419 int pipe = intel_crtc->pipe;
4420 u32 dpll;
4421
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304422 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4423
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004424 dpll = DPLL_VGA_MODE_DIS;
4425
4426 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4427 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4428 } else {
4429 if (clock->p1 == 2)
4430 dpll |= PLL_P1_DIVIDE_BY_TWO;
4431 else
4432 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4433 if (clock->p2 == 4)
4434 dpll |= PLL_P2_DIVIDE_BY_4;
4435 }
4436
4437 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4438 /* XXX: just matching BIOS for now */
4439 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4440 dpll |= 3;
4441 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4442 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4443 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4444 else
4445 dpll |= PLL_REF_INPUT_DREFCLK;
4446
4447 dpll |= DPLL_VCO_ENABLE;
4448 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4449 POSTING_READ(DPLL(pipe));
4450 udelay(150);
4451
Daniel Vetterdafd2262012-11-26 17:22:07 +01004452 for_each_encoder_on_crtc(dev, crtc, encoder)
4453 if (encoder->pre_pll_enable)
4454 encoder->pre_pll_enable(encoder);
4455
Daniel Vetter5b5896e2012-09-11 12:37:55 +02004456 I915_WRITE(DPLL(pipe), dpll);
4457
4458 /* Wait for the clocks to stabilize. */
4459 POSTING_READ(DPLL(pipe));
4460 udelay(150);
4461
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004462 /* The pixel multiplier can only be updated once the
4463 * DPLL is enabled and the clocks are stable.
4464 *
4465 * So write it again.
4466 */
4467 I915_WRITE(DPLL(pipe), dpll);
4468}
4469
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004470static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
4471 struct drm_display_mode *mode,
4472 struct drm_display_mode *adjusted_mode)
4473{
4474 struct drm_device *dev = intel_crtc->base.dev;
4475 struct drm_i915_private *dev_priv = dev->dev_private;
4476 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004477 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004478 uint32_t vsyncshift;
4479
4480 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4481 /* the chip adds 2 halflines automatically */
4482 adjusted_mode->crtc_vtotal -= 1;
4483 adjusted_mode->crtc_vblank_end -= 1;
4484 vsyncshift = adjusted_mode->crtc_hsync_start
4485 - adjusted_mode->crtc_htotal / 2;
4486 } else {
4487 vsyncshift = 0;
4488 }
4489
4490 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004491 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004492
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004493 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004494 (adjusted_mode->crtc_hdisplay - 1) |
4495 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004496 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004497 (adjusted_mode->crtc_hblank_start - 1) |
4498 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004499 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004500 (adjusted_mode->crtc_hsync_start - 1) |
4501 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4502
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004503 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004504 (adjusted_mode->crtc_vdisplay - 1) |
4505 ((adjusted_mode->crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004506 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004507 (adjusted_mode->crtc_vblank_start - 1) |
4508 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004509 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004510 (adjusted_mode->crtc_vsync_start - 1) |
4511 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4512
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004513 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4514 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4515 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4516 * bits. */
4517 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4518 (pipe == PIPE_B || pipe == PIPE_C))
4519 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4520
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004521 /* pipesrc controls the size that is scaled from, which should
4522 * always be the user's requested size.
4523 */
4524 I915_WRITE(PIPESRC(pipe),
4525 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4526}
4527
Eric Anholtf564048e2011-03-30 13:01:02 -07004528static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4529 struct drm_display_mode *mode,
4530 struct drm_display_mode *adjusted_mode,
4531 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004532 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004533{
4534 struct drm_device *dev = crtc->dev;
4535 struct drm_i915_private *dev_priv = dev->dev_private;
4536 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4537 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004538 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004539 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004540 intel_clock_t clock, reduced_clock;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004541 u32 dspcntr, pipeconf;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004542 bool ok, has_reduced_clock = false, is_sdvo = false;
4543 bool is_lvds = false, is_tv = false, is_dp = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004544 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004545 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004546 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004547
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004548 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004549 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004550 case INTEL_OUTPUT_LVDS:
4551 is_lvds = true;
4552 break;
4553 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08004554 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08004555 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01004556 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004557 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004558 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004559 case INTEL_OUTPUT_TVOUT:
4560 is_tv = true;
4561 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004562 case INTEL_OUTPUT_DISPLAYPORT:
4563 is_dp = true;
4564 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004565 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004566
Eric Anholtc751ce42010-03-25 11:48:48 -07004567 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004568 }
4569
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004570 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004571
Ma Lingd4906092009-03-18 20:13:27 +08004572 /*
4573 * Returns a set of divisors for the desired target clock with the given
4574 * refclk, or FALSE. The returned values represent the clock equation:
4575 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4576 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004577 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08004578 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4579 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004580 if (!ok) {
4581 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004582 return -EINVAL;
4583 }
4584
4585 /* Ensure that the cursor is valid for the new mode before changing... */
4586 intel_crtc_update_cursor(crtc, true);
4587
4588 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004589 /*
4590 * Ensure we match the reduced clock's P to the target clock.
4591 * If the clocks don't match, we can't switch the display clock
4592 * by using the FP0/FP1. In such case we will disable the LVDS
4593 * downclock feature.
4594 */
Eric Anholtf564048e2011-03-30 13:01:02 -07004595 has_reduced_clock = limit->find_pll(limit, crtc,
4596 dev_priv->lvds_downclock,
4597 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08004598 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004599 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004600 }
4601
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004602 if (is_sdvo && is_tv)
4603 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004604
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004605 if (IS_GEN2(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304606 i8xx_update_pll(crtc, adjusted_mode, &clock,
4607 has_reduced_clock ? &reduced_clock : NULL,
4608 num_connectors);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004609 else if (IS_VALLEYVIEW(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304610 vlv_update_pll(crtc, mode, adjusted_mode, &clock,
4611 has_reduced_clock ? &reduced_clock : NULL,
4612 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004613 else
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004614 i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
4615 has_reduced_clock ? &reduced_clock : NULL,
4616 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004617
4618 /* setup pipeconf */
4619 pipeconf = I915_READ(PIPECONF(pipe));
4620
4621 /* Set up the display plane register */
4622 dspcntr = DISPPLANE_GAMMA_ENABLE;
4623
Eric Anholt929c77f2011-03-30 13:01:04 -07004624 if (pipe == 0)
4625 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4626 else
4627 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07004628
4629 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4630 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4631 * core speed.
4632 *
4633 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4634 * pipe == 0 check?
4635 */
4636 if (mode->clock >
4637 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4638 pipeconf |= PIPECONF_DOUBLE_WIDE;
4639 else
4640 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4641 }
4642
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004643 /* default to 8bpc */
4644 pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
4645 if (is_dp) {
Jani Nikula0c96c652012-09-26 18:43:10 +03004646 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004647 pipeconf |= PIPECONF_BPP_6 |
4648 PIPECONF_DITHER_EN |
4649 PIPECONF_DITHER_TYPE_SP;
4650 }
4651 }
4652
Gajanan Bhat19c03922012-09-27 19:13:07 +05304653 if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4654 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4655 pipeconf |= PIPECONF_BPP_6 |
4656 PIPECONF_ENABLE |
4657 I965_PIPECONF_ACTIVE;
4658 }
4659 }
4660
Eric Anholtf564048e2011-03-30 13:01:02 -07004661 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
4662 drm_mode_debug_printmodeline(mode);
4663
Jesse Barnesa7516a02011-12-15 12:30:37 -08004664 if (HAS_PIPE_CXSR(dev)) {
4665 if (intel_crtc->lowfreq_avail) {
Eric Anholtf564048e2011-03-30 13:01:02 -07004666 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4667 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004668 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07004669 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4670 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4671 }
4672 }
4673
Keith Packard617cf882012-02-08 13:53:38 -08004674 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetterdbb02572012-01-28 14:49:23 +01004675 if (!IS_GEN2(dev) &&
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004676 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Eric Anholtf564048e2011-03-30 13:01:02 -07004677 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004678 else
Keith Packard617cf882012-02-08 13:53:38 -08004679 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01004680
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004681 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Eric Anholtf564048e2011-03-30 13:01:02 -07004682
4683 /* pipesrc and dspsize control the size that is scaled from,
4684 * which should always be the user's requested size.
4685 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004686 I915_WRITE(DSPSIZE(plane),
4687 ((mode->vdisplay - 1) << 16) |
4688 (mode->hdisplay - 1));
4689 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004690
Eric Anholtf564048e2011-03-30 13:01:02 -07004691 I915_WRITE(PIPECONF(pipe), pipeconf);
4692 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07004693 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07004694
4695 intel_wait_for_vblank(dev, pipe);
4696
Eric Anholtf564048e2011-03-30 13:01:02 -07004697 I915_WRITE(DSPCNTR(plane), dspcntr);
4698 POSTING_READ(DSPCNTR(plane));
4699
Daniel Vetter94352cf2012-07-05 22:51:56 +02004700 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004701
4702 intel_update_watermarks(dev);
4703
Eric Anholtf564048e2011-03-30 13:01:02 -07004704 return ret;
4705}
4706
Keith Packard9fb526d2011-09-26 22:24:57 -07004707/*
4708 * Initialize reference clocks when the driver loads
4709 */
4710void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07004711{
4712 struct drm_i915_private *dev_priv = dev->dev_private;
4713 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004714 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004715 u32 temp;
4716 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004717 bool has_cpu_edp = false;
4718 bool has_pch_edp = false;
4719 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07004720 bool has_ck505 = false;
4721 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004722
4723 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07004724 list_for_each_entry(encoder, &mode_config->encoder_list,
4725 base.head) {
4726 switch (encoder->type) {
4727 case INTEL_OUTPUT_LVDS:
4728 has_panel = true;
4729 has_lvds = true;
4730 break;
4731 case INTEL_OUTPUT_EDP:
4732 has_panel = true;
4733 if (intel_encoder_is_pch_edp(&encoder->base))
4734 has_pch_edp = true;
4735 else
4736 has_cpu_edp = true;
4737 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004738 }
4739 }
4740
Keith Packard99eb6a02011-09-26 14:29:12 -07004741 if (HAS_PCH_IBX(dev)) {
4742 has_ck505 = dev_priv->display_clock_mode;
4743 can_ssc = has_ck505;
4744 } else {
4745 has_ck505 = false;
4746 can_ssc = true;
4747 }
4748
4749 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4750 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4751 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004752
4753 /* Ironlake: try to setup display ref clock before DPLL
4754 * enabling. This is only under driver's control after
4755 * PCH B stepping, previous chipset stepping should be
4756 * ignoring this setting.
4757 */
4758 temp = I915_READ(PCH_DREF_CONTROL);
4759 /* Always enable nonspread source */
4760 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004761
Keith Packard99eb6a02011-09-26 14:29:12 -07004762 if (has_ck505)
4763 temp |= DREF_NONSPREAD_CK505_ENABLE;
4764 else
4765 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004766
Keith Packard199e5d72011-09-22 12:01:57 -07004767 if (has_panel) {
4768 temp &= ~DREF_SSC_SOURCE_MASK;
4769 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004770
Keith Packard199e5d72011-09-22 12:01:57 -07004771 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07004772 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004773 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004774 temp |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02004775 } else
4776 temp &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004777
4778 /* Get SSC going before enabling the outputs */
4779 I915_WRITE(PCH_DREF_CONTROL, temp);
4780 POSTING_READ(PCH_DREF_CONTROL);
4781 udelay(200);
4782
Jesse Barnes13d83a62011-08-03 12:59:20 -07004783 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4784
4785 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07004786 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07004787 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004788 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004789 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004790 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07004791 else
4792 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004793 } else
4794 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4795
4796 I915_WRITE(PCH_DREF_CONTROL, temp);
4797 POSTING_READ(PCH_DREF_CONTROL);
4798 udelay(200);
4799 } else {
4800 DRM_DEBUG_KMS("Disabling SSC entirely\n");
4801
4802 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4803
4804 /* Turn off CPU output */
4805 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4806
4807 I915_WRITE(PCH_DREF_CONTROL, temp);
4808 POSTING_READ(PCH_DREF_CONTROL);
4809 udelay(200);
4810
4811 /* Turn off the SSC source */
4812 temp &= ~DREF_SSC_SOURCE_MASK;
4813 temp |= DREF_SSC_SOURCE_DISABLE;
4814
4815 /* Turn off SSC1 */
4816 temp &= ~ DREF_SSC1_ENABLE;
4817
Jesse Barnes13d83a62011-08-03 12:59:20 -07004818 I915_WRITE(PCH_DREF_CONTROL, temp);
4819 POSTING_READ(PCH_DREF_CONTROL);
4820 udelay(200);
4821 }
4822}
4823
Jesse Barnesd9d444c2011-09-02 13:03:05 -07004824static int ironlake_get_refclk(struct drm_crtc *crtc)
4825{
4826 struct drm_device *dev = crtc->dev;
4827 struct drm_i915_private *dev_priv = dev->dev_private;
4828 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07004829 struct intel_encoder *edp_encoder = NULL;
4830 int num_connectors = 0;
4831 bool is_lvds = false;
4832
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004833 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07004834 switch (encoder->type) {
4835 case INTEL_OUTPUT_LVDS:
4836 is_lvds = true;
4837 break;
4838 case INTEL_OUTPUT_EDP:
4839 edp_encoder = encoder;
4840 break;
4841 }
4842 num_connectors++;
4843 }
4844
4845 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4846 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4847 dev_priv->lvds_ssc_freq);
4848 return dev_priv->lvds_ssc_freq * 1000;
4849 }
4850
4851 return 120000;
4852}
4853
Paulo Zanonic8203562012-09-12 10:06:29 -03004854static void ironlake_set_pipeconf(struct drm_crtc *crtc,
4855 struct drm_display_mode *adjusted_mode,
4856 bool dither)
4857{
4858 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4860 int pipe = intel_crtc->pipe;
4861 uint32_t val;
4862
4863 val = I915_READ(PIPECONF(pipe));
4864
4865 val &= ~PIPE_BPC_MASK;
4866 switch (intel_crtc->bpp) {
4867 case 18:
4868 val |= PIPE_6BPC;
4869 break;
4870 case 24:
4871 val |= PIPE_8BPC;
4872 break;
4873 case 30:
4874 val |= PIPE_10BPC;
4875 break;
4876 case 36:
4877 val |= PIPE_12BPC;
4878 break;
4879 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03004880 /* Case prevented by intel_choose_pipe_bpp_dither. */
4881 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03004882 }
4883
4884 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4885 if (dither)
4886 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4887
4888 val &= ~PIPECONF_INTERLACE_MASK;
4889 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
4890 val |= PIPECONF_INTERLACED_ILK;
4891 else
4892 val |= PIPECONF_PROGRESSIVE;
4893
4894 I915_WRITE(PIPECONF(pipe), val);
4895 POSTING_READ(PIPECONF(pipe));
4896}
4897
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004898static void haswell_set_pipeconf(struct drm_crtc *crtc,
4899 struct drm_display_mode *adjusted_mode,
4900 bool dither)
4901{
4902 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4903 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni702e7a52012-10-23 18:29:59 -02004904 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004905 uint32_t val;
4906
Paulo Zanoni702e7a52012-10-23 18:29:59 -02004907 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004908
4909 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4910 if (dither)
4911 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4912
4913 val &= ~PIPECONF_INTERLACE_MASK_HSW;
4914 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
4915 val |= PIPECONF_INTERLACED_ILK;
4916 else
4917 val |= PIPECONF_PROGRESSIVE;
4918
Paulo Zanoni702e7a52012-10-23 18:29:59 -02004919 I915_WRITE(PIPECONF(cpu_transcoder), val);
4920 POSTING_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004921}
4922
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03004923static bool ironlake_compute_clocks(struct drm_crtc *crtc,
4924 struct drm_display_mode *adjusted_mode,
4925 intel_clock_t *clock,
4926 bool *has_reduced_clock,
4927 intel_clock_t *reduced_clock)
4928{
4929 struct drm_device *dev = crtc->dev;
4930 struct drm_i915_private *dev_priv = dev->dev_private;
4931 struct intel_encoder *intel_encoder;
4932 int refclk;
4933 const intel_limit_t *limit;
4934 bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
4935
4936 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
4937 switch (intel_encoder->type) {
4938 case INTEL_OUTPUT_LVDS:
4939 is_lvds = true;
4940 break;
4941 case INTEL_OUTPUT_SDVO:
4942 case INTEL_OUTPUT_HDMI:
4943 is_sdvo = true;
4944 if (intel_encoder->needs_tv_clock)
4945 is_tv = true;
4946 break;
4947 case INTEL_OUTPUT_TVOUT:
4948 is_tv = true;
4949 break;
4950 }
4951 }
4952
4953 refclk = ironlake_get_refclk(crtc);
4954
4955 /*
4956 * Returns a set of divisors for the desired target clock with the given
4957 * refclk, or FALSE. The returned values represent the clock equation:
4958 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4959 */
4960 limit = intel_limit(crtc, refclk);
4961 ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4962 clock);
4963 if (!ret)
4964 return false;
4965
4966 if (is_lvds && dev_priv->lvds_downclock_avail) {
4967 /*
4968 * Ensure we match the reduced clock's P to the target clock.
4969 * If the clocks don't match, we can't switch the display clock
4970 * by using the FP0/FP1. In such case we will disable the LVDS
4971 * downclock feature.
4972 */
4973 *has_reduced_clock = limit->find_pll(limit, crtc,
4974 dev_priv->lvds_downclock,
4975 refclk,
4976 clock,
4977 reduced_clock);
4978 }
4979
4980 if (is_sdvo && is_tv)
4981 i9xx_adjust_sdvo_tv_clock(adjusted_mode, clock);
4982
4983 return true;
4984}
4985
Daniel Vetter01a415f2012-10-27 15:58:40 +02004986static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
4987{
4988 struct drm_i915_private *dev_priv = dev->dev_private;
4989 uint32_t temp;
4990
4991 temp = I915_READ(SOUTH_CHICKEN1);
4992 if (temp & FDI_BC_BIFURCATION_SELECT)
4993 return;
4994
4995 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4996 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4997
4998 temp |= FDI_BC_BIFURCATION_SELECT;
4999 DRM_DEBUG_KMS("enabling fdi C rx\n");
5000 I915_WRITE(SOUTH_CHICKEN1, temp);
5001 POSTING_READ(SOUTH_CHICKEN1);
5002}
5003
5004static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
5005{
5006 struct drm_device *dev = intel_crtc->base.dev;
5007 struct drm_i915_private *dev_priv = dev->dev_private;
5008 struct intel_crtc *pipe_B_crtc =
5009 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5010
5011 DRM_DEBUG_KMS("checking fdi config on pipe %i, lanes %i\n",
5012 intel_crtc->pipe, intel_crtc->fdi_lanes);
5013 if (intel_crtc->fdi_lanes > 4) {
5014 DRM_DEBUG_KMS("invalid fdi lane config on pipe %i: %i lanes\n",
5015 intel_crtc->pipe, intel_crtc->fdi_lanes);
5016 /* Clamp lanes to avoid programming the hw with bogus values. */
5017 intel_crtc->fdi_lanes = 4;
5018
5019 return false;
5020 }
5021
5022 if (dev_priv->num_pipe == 2)
5023 return true;
5024
5025 switch (intel_crtc->pipe) {
5026 case PIPE_A:
5027 return true;
5028 case PIPE_B:
5029 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5030 intel_crtc->fdi_lanes > 2) {
5031 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5032 intel_crtc->pipe, intel_crtc->fdi_lanes);
5033 /* Clamp lanes to avoid programming the hw with bogus values. */
5034 intel_crtc->fdi_lanes = 2;
5035
5036 return false;
5037 }
5038
5039 if (intel_crtc->fdi_lanes > 2)
5040 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5041 else
5042 cpt_enable_fdi_bc_bifurcation(dev);
5043
5044 return true;
5045 case PIPE_C:
5046 if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
5047 if (intel_crtc->fdi_lanes > 2) {
5048 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5049 intel_crtc->pipe, intel_crtc->fdi_lanes);
5050 /* Clamp lanes to avoid programming the hw with bogus values. */
5051 intel_crtc->fdi_lanes = 2;
5052
5053 return false;
5054 }
5055 } else {
5056 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5057 return false;
5058 }
5059
5060 cpt_enable_fdi_bc_bifurcation(dev);
5061
5062 return true;
5063 default:
5064 BUG();
5065 }
5066}
5067
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005068static void ironlake_set_m_n(struct drm_crtc *crtc,
5069 struct drm_display_mode *mode,
5070 struct drm_display_mode *adjusted_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08005071{
5072 struct drm_device *dev = crtc->dev;
5073 struct drm_i915_private *dev_priv = dev->dev_private;
5074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005075 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005076 struct intel_encoder *intel_encoder, *edp_encoder = NULL;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005077 struct intel_link_m_n m_n = {0};
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005078 int target_clock, pixel_multiplier, lane, link_bw;
5079 bool is_dp = false, is_cpu_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005080
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005081 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5082 switch (intel_encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005083 case INTEL_OUTPUT_DISPLAYPORT:
5084 is_dp = true;
5085 break;
5086 case INTEL_OUTPUT_EDP:
Jesse Barnese3aef172012-04-10 11:58:03 -07005087 is_dp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005088 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnese3aef172012-04-10 11:58:03 -07005089 is_cpu_edp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005090 edp_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005091 break;
5092 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005093 }
5094
Zhenyu Wang2c072452009-06-05 15:38:42 +08005095 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07005096 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5097 lane = 0;
5098 /* CPU eDP doesn't require FDI link, so just set DP M/N
5099 according to current link config */
Jesse Barnese3aef172012-04-10 11:58:03 -07005100 if (is_cpu_edp) {
Jesse Barnese3aef172012-04-10 11:58:03 -07005101 intel_edp_link_config(edp_encoder, &lane, &link_bw);
Eric Anholt8febb292011-03-30 13:01:07 -07005102 } else {
Eric Anholt8febb292011-03-30 13:01:07 -07005103 /* FDI is a binary signal running at ~2.7GHz, encoding
5104 * each output octet as 10 bits. The actual frequency
5105 * is stored as a divider into a 100MHz clock, and the
5106 * mode pixel clock is stored in units of 1KHz.
5107 * Hence the bw of each lane in terms of the mode signal
5108 * is:
5109 */
5110 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005111 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08005112
Daniel Vetter94bf2ce2012-06-04 18:39:19 +02005113 /* [e]DP over FDI requires target mode clock instead of link clock. */
5114 if (edp_encoder)
5115 target_clock = intel_edp_target_clock(edp_encoder, mode);
5116 else if (is_dp)
5117 target_clock = mode->clock;
5118 else
5119 target_clock = adjusted_mode->clock;
5120
Eric Anholt8febb292011-03-30 13:01:07 -07005121 if (!lane) {
5122 /*
5123 * Account for spread spectrum to avoid
5124 * oversubscribing the link. Max center spread
5125 * is 2.5%; use 5% for safety's sake.
5126 */
Jesse Barnes5a354202011-06-24 12:19:22 -07005127 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
Eric Anholt8febb292011-03-30 13:01:07 -07005128 lane = bps / (link_bw * 8) + 1;
5129 }
5130
5131 intel_crtc->fdi_lanes = lane;
5132
5133 if (pixel_multiplier > 1)
5134 link_bw *= pixel_multiplier;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005135 intel_link_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw, &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07005136
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005137 I915_WRITE(PIPE_DATA_M1(cpu_transcoder), TU_SIZE(m_n.tu) | m_n.gmch_m);
5138 I915_WRITE(PIPE_DATA_N1(cpu_transcoder), m_n.gmch_n);
5139 I915_WRITE(PIPE_LINK_M1(cpu_transcoder), m_n.link_m);
5140 I915_WRITE(PIPE_LINK_N1(cpu_transcoder), m_n.link_n);
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005141}
5142
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005143static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5144 struct drm_display_mode *adjusted_mode,
5145 intel_clock_t *clock, u32 fp)
5146{
5147 struct drm_crtc *crtc = &intel_crtc->base;
5148 struct drm_device *dev = crtc->dev;
5149 struct drm_i915_private *dev_priv = dev->dev_private;
5150 struct intel_encoder *intel_encoder;
5151 uint32_t dpll;
5152 int factor, pixel_multiplier, num_connectors = 0;
5153 bool is_lvds = false, is_sdvo = false, is_tv = false;
5154 bool is_dp = false, is_cpu_edp = false;
5155
5156 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5157 switch (intel_encoder->type) {
5158 case INTEL_OUTPUT_LVDS:
5159 is_lvds = true;
5160 break;
5161 case INTEL_OUTPUT_SDVO:
5162 case INTEL_OUTPUT_HDMI:
5163 is_sdvo = true;
5164 if (intel_encoder->needs_tv_clock)
5165 is_tv = true;
5166 break;
5167 case INTEL_OUTPUT_TVOUT:
5168 is_tv = true;
5169 break;
5170 case INTEL_OUTPUT_DISPLAYPORT:
5171 is_dp = true;
5172 break;
5173 case INTEL_OUTPUT_EDP:
5174 is_dp = true;
5175 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
5176 is_cpu_edp = true;
5177 break;
5178 }
5179
5180 num_connectors++;
5181 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005182
Chris Wilsonc1858122010-12-03 21:35:48 +00005183 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005184 factor = 21;
5185 if (is_lvds) {
5186 if ((intel_panel_use_ssc(dev_priv) &&
5187 dev_priv->lvds_ssc_freq == 100) ||
Daniel Vetter1974cad2012-11-26 17:22:09 +01005188 intel_is_dual_link_lvds(dev))
Eric Anholt8febb292011-03-30 13:01:07 -07005189 factor = 25;
5190 } else if (is_sdvo && is_tv)
5191 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005192
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005193 if (clock->m < factor * clock->n)
Eric Anholt8febb292011-03-30 13:01:07 -07005194 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005195
Chris Wilson5eddb702010-09-11 13:48:45 +01005196 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005197
Eric Anholta07d6782011-03-30 13:01:08 -07005198 if (is_lvds)
5199 dpll |= DPLLB_MODE_LVDS;
5200 else
5201 dpll |= DPLLB_MODE_DAC_SERIAL;
5202 if (is_sdvo) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005203 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
Eric Anholta07d6782011-03-30 13:01:08 -07005204 if (pixel_multiplier > 1) {
5205 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08005206 }
Eric Anholta07d6782011-03-30 13:01:08 -07005207 dpll |= DPLL_DVO_HIGH_SPEED;
5208 }
Jesse Barnese3aef172012-04-10 11:58:03 -07005209 if (is_dp && !is_cpu_edp)
Eric Anholta07d6782011-03-30 13:01:08 -07005210 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005211
Eric Anholta07d6782011-03-30 13:01:08 -07005212 /* compute bitmask from p1 value */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005213 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005214 /* also FPA1 */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005215 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005216
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005217 switch (clock->p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005218 case 5:
5219 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5220 break;
5221 case 7:
5222 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5223 break;
5224 case 10:
5225 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5226 break;
5227 case 14:
5228 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5229 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005230 }
5231
5232 if (is_sdvo && is_tv)
5233 dpll |= PLL_REF_INPUT_TVCLKINBC;
5234 else if (is_tv)
5235 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005236 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08005237 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00005238 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005239 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005240 else
5241 dpll |= PLL_REF_INPUT_DREFCLK;
5242
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005243 return dpll;
5244}
5245
Jesse Barnes79e53942008-11-07 14:24:08 -08005246static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5247 struct drm_display_mode *mode,
5248 struct drm_display_mode *adjusted_mode,
5249 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005250 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005251{
5252 struct drm_device *dev = crtc->dev;
5253 struct drm_i915_private *dev_priv = dev->dev_private;
5254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5255 int pipe = intel_crtc->pipe;
5256 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005257 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005258 intel_clock_t clock, reduced_clock;
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005259 u32 dpll, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005260 bool ok, has_reduced_clock = false;
5261 bool is_lvds = false, is_dp = false, is_cpu_edp = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005262 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005263 int ret;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005264 bool dither, fdi_config_ok;
Jesse Barnes79e53942008-11-07 14:24:08 -08005265
5266 for_each_encoder_on_crtc(dev, crtc, encoder) {
5267 switch (encoder->type) {
5268 case INTEL_OUTPUT_LVDS:
5269 is_lvds = true;
5270 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005271 case INTEL_OUTPUT_DISPLAYPORT:
5272 is_dp = true;
5273 break;
5274 case INTEL_OUTPUT_EDP:
5275 is_dp = true;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005276 if (!intel_encoder_is_pch_edp(&encoder->base))
Jesse Barnes79e53942008-11-07 14:24:08 -08005277 is_cpu_edp = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005278 break;
5279 }
5280
5281 num_connectors++;
5282 }
5283
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005284 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5285 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5286
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005287 ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
5288 &has_reduced_clock, &reduced_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005289 if (!ok) {
5290 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5291 return -EINVAL;
5292 }
5293
5294 /* Ensure that the cursor is valid for the new mode before changing... */
5295 intel_crtc_update_cursor(crtc, true);
5296
Jesse Barnes79e53942008-11-07 14:24:08 -08005297 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005298 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5299 adjusted_mode);
Paulo Zanonic8203562012-09-12 10:06:29 -03005300 if (is_lvds && dev_priv->lvds_dither)
5301 dither = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005302
Jesse Barnes79e53942008-11-07 14:24:08 -08005303 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5304 if (has_reduced_clock)
5305 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5306 reduced_clock.m2;
5307
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005308 dpll = ironlake_compute_dpll(intel_crtc, adjusted_mode, &clock, fp);
Jesse Barnes79e53942008-11-07 14:24:08 -08005309
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07005310 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005311 drm_mode_debug_printmodeline(mode);
5312
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005313 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5314 if (!is_cpu_edp) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005315 struct intel_pch_pll *pll;
Chris Wilson5eddb702010-09-11 13:48:45 +01005316
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005317 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5318 if (pll == NULL) {
5319 DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
5320 pipe);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005321 return -EINVAL;
5322 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005323 } else
5324 intel_put_pch_pll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005325
Daniel Vetter2f0c2ad2012-11-29 15:59:35 +01005326 if (is_dp && !is_cpu_edp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005327 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08005328
Daniel Vetterdafd2262012-11-26 17:22:07 +01005329 for_each_encoder_on_crtc(dev, crtc, encoder)
5330 if (encoder->pre_pll_enable)
5331 encoder->pre_pll_enable(encoder);
5332
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005333 if (intel_crtc->pch_pll) {
5334 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005335
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005336 /* Wait for the clocks to stabilize. */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005337 POSTING_READ(intel_crtc->pch_pll->pll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005338 udelay(150);
5339
Eric Anholt8febb292011-03-30 13:01:07 -07005340 /* The pixel multiplier can only be updated once the
5341 * DPLL is enabled and the clocks are stable.
5342 *
5343 * So write it again.
5344 */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005345 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08005346 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005347
Chris Wilson5eddb702010-09-11 13:48:45 +01005348 intel_crtc->lowfreq_avail = false;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005349 if (intel_crtc->pch_pll) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07005350 if (is_lvds && has_reduced_clock && i915_powersave) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005351 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005352 intel_crtc->lowfreq_avail = true;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005353 } else {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005354 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07005355 }
5356 }
5357
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005358 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005359
Daniel Vetter01a415f2012-10-27 15:58:40 +02005360 /* Note, this also computes intel_crtc->fdi_lanes which is used below in
5361 * ironlake_check_fdi_lanes. */
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005362 ironlake_set_m_n(crtc, mode, adjusted_mode);
Chris Wilson5eddb702010-09-11 13:48:45 +01005363
Daniel Vetter01a415f2012-10-27 15:58:40 +02005364 fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005365
Paulo Zanonic8203562012-09-12 10:06:29 -03005366 ironlake_set_pipeconf(crtc, adjusted_mode, dither);
Jesse Barnes79e53942008-11-07 14:24:08 -08005367
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005368 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005369
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005370 /* Set up the display plane register */
5371 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005372 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005373
Daniel Vetter94352cf2012-07-05 22:51:56 +02005374 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005375
5376 intel_update_watermarks(dev);
5377
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03005378 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5379
Daniel Vetter01a415f2012-10-27 15:58:40 +02005380 return fdi_config_ok ? ret : -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005381}
5382
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005383static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5384 struct drm_display_mode *mode,
5385 struct drm_display_mode *adjusted_mode,
5386 int x, int y,
5387 struct drm_framebuffer *fb)
5388{
5389 struct drm_device *dev = crtc->dev;
5390 struct drm_i915_private *dev_priv = dev->dev_private;
5391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5392 int pipe = intel_crtc->pipe;
5393 int plane = intel_crtc->plane;
5394 int num_connectors = 0;
Daniel Vettered7ef432012-12-06 14:24:21 +01005395 bool is_dp = false, is_cpu_edp = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005396 struct intel_encoder *encoder;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005397 int ret;
5398 bool dither;
5399
5400 for_each_encoder_on_crtc(dev, crtc, encoder) {
5401 switch (encoder->type) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005402 case INTEL_OUTPUT_DISPLAYPORT:
5403 is_dp = true;
5404 break;
5405 case INTEL_OUTPUT_EDP:
5406 is_dp = true;
5407 if (!intel_encoder_is_pch_edp(&encoder->base))
5408 is_cpu_edp = true;
5409 break;
5410 }
5411
5412 num_connectors++;
5413 }
5414
Paulo Zanonia5c961d2012-10-24 15:59:34 -02005415 if (is_cpu_edp)
5416 intel_crtc->cpu_transcoder = TRANSCODER_EDP;
5417 else
5418 intel_crtc->cpu_transcoder = pipe;
5419
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005420 /* We are not sure yet this won't happen. */
5421 WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
5422 INTEL_PCH_TYPE(dev));
5423
5424 WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
5425 num_connectors, pipe_name(pipe));
5426
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005427 WARN_ON(I915_READ(PIPECONF(intel_crtc->cpu_transcoder)) &
Paulo Zanoni1ce42922012-10-05 12:06:01 -03005428 (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
5429
5430 WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
5431
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005432 if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
5433 return -EINVAL;
5434
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005435 /* Ensure that the cursor is valid for the new mode before changing... */
5436 intel_crtc_update_cursor(crtc, true);
5437
5438 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005439 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5440 adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005441
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005442 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
5443 drm_mode_debug_printmodeline(mode);
5444
Daniel Vettered7ef432012-12-06 14:24:21 +01005445 if (is_dp && !is_cpu_edp)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005446 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005447
5448 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005449
5450 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5451
Paulo Zanoni1eb8dfe2012-10-18 12:42:10 -03005452 if (!is_dp || is_cpu_edp)
5453 ironlake_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005454
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005455 haswell_set_pipeconf(crtc, adjusted_mode, dither);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005456
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005457 /* Set up the display plane register */
5458 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5459 POSTING_READ(DSPCNTR(plane));
5460
5461 ret = intel_pipe_set_base(crtc, x, y, fb);
5462
5463 intel_update_watermarks(dev);
5464
5465 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5466
Jesse Barnes79e53942008-11-07 14:24:08 -08005467 return ret;
5468}
5469
Eric Anholtf564048e2011-03-30 13:01:02 -07005470static int intel_crtc_mode_set(struct drm_crtc *crtc,
5471 struct drm_display_mode *mode,
5472 struct drm_display_mode *adjusted_mode,
5473 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005474 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07005475{
5476 struct drm_device *dev = crtc->dev;
5477 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01005478 struct drm_encoder_helper_funcs *encoder_funcs;
5479 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07005480 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5481 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07005482 int ret;
5483
Eric Anholt0b701d22011-03-30 13:01:03 -07005484 drm_vblank_pre_modeset(dev, pipe);
5485
Eric Anholtf564048e2011-03-30 13:01:02 -07005486 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005487 x, y, fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08005488 drm_vblank_post_modeset(dev, pipe);
5489
Daniel Vetter9256aa12012-10-31 19:26:13 +01005490 if (ret != 0)
5491 return ret;
5492
5493 for_each_encoder_on_crtc(dev, crtc, encoder) {
5494 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
5495 encoder->base.base.id,
5496 drm_get_encoder_name(&encoder->base),
5497 mode->base.id, mode->name);
5498 encoder_funcs = encoder->base.helper_private;
5499 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
5500 }
5501
5502 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005503}
5504
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005505static bool intel_eld_uptodate(struct drm_connector *connector,
5506 int reg_eldv, uint32_t bits_eldv,
5507 int reg_elda, uint32_t bits_elda,
5508 int reg_edid)
5509{
5510 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5511 uint8_t *eld = connector->eld;
5512 uint32_t i;
5513
5514 i = I915_READ(reg_eldv);
5515 i &= bits_eldv;
5516
5517 if (!eld[0])
5518 return !i;
5519
5520 if (!i)
5521 return false;
5522
5523 i = I915_READ(reg_elda);
5524 i &= ~bits_elda;
5525 I915_WRITE(reg_elda, i);
5526
5527 for (i = 0; i < eld[2]; i++)
5528 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
5529 return false;
5530
5531 return true;
5532}
5533
Wu Fengguange0dac652011-09-05 14:25:34 +08005534static void g4x_write_eld(struct drm_connector *connector,
5535 struct drm_crtc *crtc)
5536{
5537 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5538 uint8_t *eld = connector->eld;
5539 uint32_t eldv;
5540 uint32_t len;
5541 uint32_t i;
5542
5543 i = I915_READ(G4X_AUD_VID_DID);
5544
5545 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
5546 eldv = G4X_ELDV_DEVCL_DEVBLC;
5547 else
5548 eldv = G4X_ELDV_DEVCTG;
5549
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005550 if (intel_eld_uptodate(connector,
5551 G4X_AUD_CNTL_ST, eldv,
5552 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
5553 G4X_HDMIW_HDMIEDID))
5554 return;
5555
Wu Fengguange0dac652011-09-05 14:25:34 +08005556 i = I915_READ(G4X_AUD_CNTL_ST);
5557 i &= ~(eldv | G4X_ELD_ADDR);
5558 len = (i >> 9) & 0x1f; /* ELD buffer size */
5559 I915_WRITE(G4X_AUD_CNTL_ST, i);
5560
5561 if (!eld[0])
5562 return;
5563
5564 len = min_t(uint8_t, eld[2], len);
5565 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5566 for (i = 0; i < len; i++)
5567 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
5568
5569 i = I915_READ(G4X_AUD_CNTL_ST);
5570 i |= eldv;
5571 I915_WRITE(G4X_AUD_CNTL_ST, i);
5572}
5573
Wang Xingchao83358c852012-08-16 22:43:37 +08005574static void haswell_write_eld(struct drm_connector *connector,
5575 struct drm_crtc *crtc)
5576{
5577 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5578 uint8_t *eld = connector->eld;
5579 struct drm_device *dev = crtc->dev;
5580 uint32_t eldv;
5581 uint32_t i;
5582 int len;
5583 int pipe = to_intel_crtc(crtc)->pipe;
5584 int tmp;
5585
5586 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
5587 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
5588 int aud_config = HSW_AUD_CFG(pipe);
5589 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
5590
5591
5592 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
5593
5594 /* Audio output enable */
5595 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
5596 tmp = I915_READ(aud_cntrl_st2);
5597 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
5598 I915_WRITE(aud_cntrl_st2, tmp);
5599
5600 /* Wait for 1 vertical blank */
5601 intel_wait_for_vblank(dev, pipe);
5602
5603 /* Set ELD valid state */
5604 tmp = I915_READ(aud_cntrl_st2);
5605 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
5606 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
5607 I915_WRITE(aud_cntrl_st2, tmp);
5608 tmp = I915_READ(aud_cntrl_st2);
5609 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
5610
5611 /* Enable HDMI mode */
5612 tmp = I915_READ(aud_config);
5613 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
5614 /* clear N_programing_enable and N_value_index */
5615 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
5616 I915_WRITE(aud_config, tmp);
5617
5618 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5619
5620 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
5621
5622 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5623 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5624 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
5625 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5626 } else
5627 I915_WRITE(aud_config, 0);
5628
5629 if (intel_eld_uptodate(connector,
5630 aud_cntrl_st2, eldv,
5631 aud_cntl_st, IBX_ELD_ADDRESS,
5632 hdmiw_hdmiedid))
5633 return;
5634
5635 i = I915_READ(aud_cntrl_st2);
5636 i &= ~eldv;
5637 I915_WRITE(aud_cntrl_st2, i);
5638
5639 if (!eld[0])
5640 return;
5641
5642 i = I915_READ(aud_cntl_st);
5643 i &= ~IBX_ELD_ADDRESS;
5644 I915_WRITE(aud_cntl_st, i);
5645 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
5646 DRM_DEBUG_DRIVER("port num:%d\n", i);
5647
5648 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5649 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5650 for (i = 0; i < len; i++)
5651 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5652
5653 i = I915_READ(aud_cntrl_st2);
5654 i |= eldv;
5655 I915_WRITE(aud_cntrl_st2, i);
5656
5657}
5658
Wu Fengguange0dac652011-09-05 14:25:34 +08005659static void ironlake_write_eld(struct drm_connector *connector,
5660 struct drm_crtc *crtc)
5661{
5662 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5663 uint8_t *eld = connector->eld;
5664 uint32_t eldv;
5665 uint32_t i;
5666 int len;
5667 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06005668 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08005669 int aud_cntl_st;
5670 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08005671 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08005672
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08005673 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08005674 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
5675 aud_config = IBX_AUD_CFG(pipe);
5676 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005677 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08005678 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08005679 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
5680 aud_config = CPT_AUD_CFG(pipe);
5681 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005682 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08005683 }
5684
Wang Xingchao9b138a82012-08-09 16:52:18 +08005685 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08005686
5687 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08005688 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08005689 if (!i) {
5690 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
5691 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005692 eldv = IBX_ELD_VALIDB;
5693 eldv |= IBX_ELD_VALIDB << 4;
5694 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08005695 } else {
5696 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005697 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08005698 }
5699
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005700 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5701 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5702 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06005703 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5704 } else
5705 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005706
5707 if (intel_eld_uptodate(connector,
5708 aud_cntrl_st2, eldv,
5709 aud_cntl_st, IBX_ELD_ADDRESS,
5710 hdmiw_hdmiedid))
5711 return;
5712
Wu Fengguange0dac652011-09-05 14:25:34 +08005713 i = I915_READ(aud_cntrl_st2);
5714 i &= ~eldv;
5715 I915_WRITE(aud_cntrl_st2, i);
5716
5717 if (!eld[0])
5718 return;
5719
Wu Fengguange0dac652011-09-05 14:25:34 +08005720 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005721 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08005722 I915_WRITE(aud_cntl_st, i);
5723
5724 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5725 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5726 for (i = 0; i < len; i++)
5727 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5728
5729 i = I915_READ(aud_cntrl_st2);
5730 i |= eldv;
5731 I915_WRITE(aud_cntrl_st2, i);
5732}
5733
5734void intel_write_eld(struct drm_encoder *encoder,
5735 struct drm_display_mode *mode)
5736{
5737 struct drm_crtc *crtc = encoder->crtc;
5738 struct drm_connector *connector;
5739 struct drm_device *dev = encoder->dev;
5740 struct drm_i915_private *dev_priv = dev->dev_private;
5741
5742 connector = drm_select_eld(encoder, mode);
5743 if (!connector)
5744 return;
5745
5746 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5747 connector->base.id,
5748 drm_get_connector_name(connector),
5749 connector->encoder->base.id,
5750 drm_get_encoder_name(connector->encoder));
5751
5752 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
5753
5754 if (dev_priv->display.write_eld)
5755 dev_priv->display.write_eld(connector, crtc);
5756}
5757
Jesse Barnes79e53942008-11-07 14:24:08 -08005758/** Loads the palette/gamma unit for the CRTC with the prepared values */
5759void intel_crtc_load_lut(struct drm_crtc *crtc)
5760{
5761 struct drm_device *dev = crtc->dev;
5762 struct drm_i915_private *dev_priv = dev->dev_private;
5763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005764 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005765 int i;
5766
5767 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00005768 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08005769 return;
5770
Adam Jacksonf2b115e2009-12-03 17:14:42 -05005771 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07005772 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005773 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005774
Jesse Barnes79e53942008-11-07 14:24:08 -08005775 for (i = 0; i < 256; i++) {
5776 I915_WRITE(palreg + 4 * i,
5777 (intel_crtc->lut_r[i] << 16) |
5778 (intel_crtc->lut_g[i] << 8) |
5779 intel_crtc->lut_b[i]);
5780 }
5781}
5782
Chris Wilson560b85b2010-08-07 11:01:38 +01005783static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
5784{
5785 struct drm_device *dev = crtc->dev;
5786 struct drm_i915_private *dev_priv = dev->dev_private;
5787 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5788 bool visible = base != 0;
5789 u32 cntl;
5790
5791 if (intel_crtc->cursor_visible == visible)
5792 return;
5793
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005794 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01005795 if (visible) {
5796 /* On these chipsets we can only modify the base whilst
5797 * the cursor is disabled.
5798 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005799 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01005800
5801 cntl &= ~(CURSOR_FORMAT_MASK);
5802 /* XXX width must be 64, stride 256 => 0x00 << 28 */
5803 cntl |= CURSOR_ENABLE |
5804 CURSOR_GAMMA_ENABLE |
5805 CURSOR_FORMAT_ARGB;
5806 } else
5807 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005808 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01005809
5810 intel_crtc->cursor_visible = visible;
5811}
5812
5813static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
5814{
5815 struct drm_device *dev = crtc->dev;
5816 struct drm_i915_private *dev_priv = dev->dev_private;
5817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5818 int pipe = intel_crtc->pipe;
5819 bool visible = base != 0;
5820
5821 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08005822 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01005823 if (base) {
5824 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
5825 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5826 cntl |= pipe << 28; /* Connect to correct pipe */
5827 } else {
5828 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5829 cntl |= CURSOR_MODE_DISABLE;
5830 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005831 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01005832
5833 intel_crtc->cursor_visible = visible;
5834 }
5835 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005836 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01005837}
5838
Jesse Barnes65a21cd2011-10-12 11:10:21 -07005839static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
5840{
5841 struct drm_device *dev = crtc->dev;
5842 struct drm_i915_private *dev_priv = dev->dev_private;
5843 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5844 int pipe = intel_crtc->pipe;
5845 bool visible = base != 0;
5846
5847 if (intel_crtc->cursor_visible != visible) {
5848 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
5849 if (base) {
5850 cntl &= ~CURSOR_MODE;
5851 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5852 } else {
5853 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5854 cntl |= CURSOR_MODE_DISABLE;
5855 }
5856 I915_WRITE(CURCNTR_IVB(pipe), cntl);
5857
5858 intel_crtc->cursor_visible = visible;
5859 }
5860 /* and commit changes on next vblank */
5861 I915_WRITE(CURBASE_IVB(pipe), base);
5862}
5863
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005864/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01005865static void intel_crtc_update_cursor(struct drm_crtc *crtc,
5866 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005867{
5868 struct drm_device *dev = crtc->dev;
5869 struct drm_i915_private *dev_priv = dev->dev_private;
5870 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5871 int pipe = intel_crtc->pipe;
5872 int x = intel_crtc->cursor_x;
5873 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01005874 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005875 bool visible;
5876
5877 pos = 0;
5878
Chris Wilson6b383a72010-09-13 13:54:26 +01005879 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005880 base = intel_crtc->cursor_addr;
5881 if (x > (int) crtc->fb->width)
5882 base = 0;
5883
5884 if (y > (int) crtc->fb->height)
5885 base = 0;
5886 } else
5887 base = 0;
5888
5889 if (x < 0) {
5890 if (x + intel_crtc->cursor_width < 0)
5891 base = 0;
5892
5893 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
5894 x = -x;
5895 }
5896 pos |= x << CURSOR_X_SHIFT;
5897
5898 if (y < 0) {
5899 if (y + intel_crtc->cursor_height < 0)
5900 base = 0;
5901
5902 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
5903 y = -y;
5904 }
5905 pos |= y << CURSOR_Y_SHIFT;
5906
5907 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01005908 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005909 return;
5910
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03005911 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07005912 I915_WRITE(CURPOS_IVB(pipe), pos);
5913 ivb_update_cursor(crtc, base);
5914 } else {
5915 I915_WRITE(CURPOS(pipe), pos);
5916 if (IS_845G(dev) || IS_I865G(dev))
5917 i845_update_cursor(crtc, base);
5918 else
5919 i9xx_update_cursor(crtc, base);
5920 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005921}
5922
Jesse Barnes79e53942008-11-07 14:24:08 -08005923static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00005924 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08005925 uint32_t handle,
5926 uint32_t width, uint32_t height)
5927{
5928 struct drm_device *dev = crtc->dev;
5929 struct drm_i915_private *dev_priv = dev->dev_private;
5930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00005931 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005932 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005933 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005934
Jesse Barnes79e53942008-11-07 14:24:08 -08005935 /* if we want to turn off the cursor ignore width and height */
5936 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08005937 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005938 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00005939 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10005940 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005941 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08005942 }
5943
5944 /* Currently we only support 64x64 cursors */
5945 if (width != 64 || height != 64) {
5946 DRM_ERROR("we currently only support 64x64 cursors\n");
5947 return -EINVAL;
5948 }
5949
Chris Wilson05394f32010-11-08 19:18:58 +00005950 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00005951 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08005952 return -ENOENT;
5953
Chris Wilson05394f32010-11-08 19:18:58 +00005954 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005955 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10005956 ret = -ENOMEM;
5957 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08005958 }
5959
Dave Airlie71acb5e2008-12-30 20:31:46 +10005960 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05005961 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05005962 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00005963 if (obj->tiling_mode) {
5964 DRM_ERROR("cursor cannot be tiled\n");
5965 ret = -EINVAL;
5966 goto fail_locked;
5967 }
5968
Chris Wilson2da3b9b2011-04-14 09:41:17 +01005969 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01005970 if (ret) {
5971 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01005972 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01005973 }
5974
Chris Wilsond9e86c02010-11-10 16:40:20 +00005975 ret = i915_gem_object_put_fence(obj);
5976 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01005977 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00005978 goto fail_unpin;
5979 }
5980
Chris Wilson05394f32010-11-08 19:18:58 +00005981 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005982 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005983 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00005984 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005985 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
5986 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005987 if (ret) {
5988 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05005989 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005990 }
Chris Wilson05394f32010-11-08 19:18:58 +00005991 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005992 }
5993
Chris Wilsona6c45cf2010-09-17 00:32:17 +01005994 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04005995 I915_WRITE(CURSIZE, (height << 12) | width);
5996
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005997 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005998 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05005999 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006000 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006001 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6002 } else
6003 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006004 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006005 }
Jesse Barnes80824002009-09-10 15:28:06 -07006006
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006007 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006008
6009 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006010 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006011 intel_crtc->cursor_width = width;
6012 intel_crtc->cursor_height = height;
6013
Chris Wilson6b383a72010-09-13 13:54:26 +01006014 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006015
Jesse Barnes79e53942008-11-07 14:24:08 -08006016 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006017fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006018 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006019fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006020 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006021fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006022 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006023 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006024}
6025
6026static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6027{
Jesse Barnes79e53942008-11-07 14:24:08 -08006028 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006029
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006030 intel_crtc->cursor_x = x;
6031 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006032
Chris Wilson6b383a72010-09-13 13:54:26 +01006033 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006034
6035 return 0;
6036}
6037
6038/** Sets the color ramps on behalf of RandR */
6039void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6040 u16 blue, int regno)
6041{
6042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6043
6044 intel_crtc->lut_r[regno] = red >> 8;
6045 intel_crtc->lut_g[regno] = green >> 8;
6046 intel_crtc->lut_b[regno] = blue >> 8;
6047}
6048
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006049void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6050 u16 *blue, int regno)
6051{
6052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6053
6054 *red = intel_crtc->lut_r[regno] << 8;
6055 *green = intel_crtc->lut_g[regno] << 8;
6056 *blue = intel_crtc->lut_b[regno] << 8;
6057}
6058
Jesse Barnes79e53942008-11-07 14:24:08 -08006059static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006060 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006061{
James Simmons72034252010-08-03 01:33:19 +01006062 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006064
James Simmons72034252010-08-03 01:33:19 +01006065 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006066 intel_crtc->lut_r[i] = red[i] >> 8;
6067 intel_crtc->lut_g[i] = green[i] >> 8;
6068 intel_crtc->lut_b[i] = blue[i] >> 8;
6069 }
6070
6071 intel_crtc_load_lut(crtc);
6072}
6073
6074/**
6075 * Get a pipe with a simple mode set on it for doing load-based monitor
6076 * detection.
6077 *
6078 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07006079 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08006080 *
Eric Anholtc751ce42010-03-25 11:48:48 -07006081 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08006082 * configured for it. In the future, it could choose to temporarily disable
6083 * some outputs to free up a pipe for its use.
6084 *
6085 * \return crtc, or NULL if no pipes are available.
6086 */
6087
6088/* VESA 640x480x72Hz mode to set on the pipe */
6089static struct drm_display_mode load_detect_mode = {
6090 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6091 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6092};
6093
Chris Wilsond2dff872011-04-19 08:36:26 +01006094static struct drm_framebuffer *
6095intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006096 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006097 struct drm_i915_gem_object *obj)
6098{
6099 struct intel_framebuffer *intel_fb;
6100 int ret;
6101
6102 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6103 if (!intel_fb) {
6104 drm_gem_object_unreference_unlocked(&obj->base);
6105 return ERR_PTR(-ENOMEM);
6106 }
6107
6108 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6109 if (ret) {
6110 drm_gem_object_unreference_unlocked(&obj->base);
6111 kfree(intel_fb);
6112 return ERR_PTR(ret);
6113 }
6114
6115 return &intel_fb->base;
6116}
6117
6118static u32
6119intel_framebuffer_pitch_for_width(int width, int bpp)
6120{
6121 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6122 return ALIGN(pitch, 64);
6123}
6124
6125static u32
6126intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6127{
6128 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6129 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6130}
6131
6132static struct drm_framebuffer *
6133intel_framebuffer_create_for_mode(struct drm_device *dev,
6134 struct drm_display_mode *mode,
6135 int depth, int bpp)
6136{
6137 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006138 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006139
6140 obj = i915_gem_alloc_object(dev,
6141 intel_framebuffer_size_for_mode(mode, bpp));
6142 if (obj == NULL)
6143 return ERR_PTR(-ENOMEM);
6144
6145 mode_cmd.width = mode->hdisplay;
6146 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006147 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6148 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006149 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006150
6151 return intel_framebuffer_create(dev, &mode_cmd, obj);
6152}
6153
6154static struct drm_framebuffer *
6155mode_fits_in_fbdev(struct drm_device *dev,
6156 struct drm_display_mode *mode)
6157{
6158 struct drm_i915_private *dev_priv = dev->dev_private;
6159 struct drm_i915_gem_object *obj;
6160 struct drm_framebuffer *fb;
6161
6162 if (dev_priv->fbdev == NULL)
6163 return NULL;
6164
6165 obj = dev_priv->fbdev->ifb.obj;
6166 if (obj == NULL)
6167 return NULL;
6168
6169 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006170 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6171 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006172 return NULL;
6173
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006174 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006175 return NULL;
6176
6177 return fb;
6178}
6179
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006180bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006181 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006182 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006183{
6184 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006185 struct intel_encoder *intel_encoder =
6186 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006187 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006188 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006189 struct drm_crtc *crtc = NULL;
6190 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006191 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006192 int i = -1;
6193
Chris Wilsond2dff872011-04-19 08:36:26 +01006194 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6195 connector->base.id, drm_get_connector_name(connector),
6196 encoder->base.id, drm_get_encoder_name(encoder));
6197
Jesse Barnes79e53942008-11-07 14:24:08 -08006198 /*
6199 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006200 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006201 * - if the connector already has an assigned crtc, use it (but make
6202 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006203 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006204 * - try to find the first unused crtc that can drive this connector,
6205 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006206 */
6207
6208 /* See if we already have a CRTC for this connector */
6209 if (encoder->crtc) {
6210 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006211
Daniel Vetter24218aa2012-08-12 19:27:11 +02006212 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006213 old->load_detect_temp = false;
6214
6215 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006216 if (connector->dpms != DRM_MODE_DPMS_ON)
6217 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006218
Chris Wilson71731882011-04-19 23:10:58 +01006219 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006220 }
6221
6222 /* Find an unused one (if possible) */
6223 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6224 i++;
6225 if (!(encoder->possible_crtcs & (1 << i)))
6226 continue;
6227 if (!possible_crtc->enabled) {
6228 crtc = possible_crtc;
6229 break;
6230 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006231 }
6232
6233 /*
6234 * If we didn't find an unused CRTC, don't use any.
6235 */
6236 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006237 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6238 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006239 }
6240
Daniel Vetterfc303102012-07-09 10:40:58 +02006241 intel_encoder->new_crtc = to_intel_crtc(crtc);
6242 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006243
6244 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006245 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006246 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006247 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006248
Chris Wilson64927112011-04-20 07:25:26 +01006249 if (!mode)
6250 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006251
Chris Wilsond2dff872011-04-19 08:36:26 +01006252 /* We need a framebuffer large enough to accommodate all accesses
6253 * that the plane may generate whilst we perform load detection.
6254 * We can not rely on the fbcon either being present (we get called
6255 * during its initialisation to detect all boot displays, or it may
6256 * not even exist) or that it is large enough to satisfy the
6257 * requested mode.
6258 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006259 fb = mode_fits_in_fbdev(dev, mode);
6260 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006261 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006262 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6263 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006264 } else
6265 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006266 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006267 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006268 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006269 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006270
Daniel Vetter94352cf2012-07-05 22:51:56 +02006271 if (!intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006272 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006273 if (old->release_fb)
6274 old->release_fb->funcs->destroy(old->release_fb);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006275 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006276 }
Chris Wilson71731882011-04-19 23:10:58 +01006277
Jesse Barnes79e53942008-11-07 14:24:08 -08006278 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006279 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006280 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006281}
6282
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006283void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006284 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006285{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006286 struct intel_encoder *intel_encoder =
6287 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006288 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006289
Chris Wilsond2dff872011-04-19 08:36:26 +01006290 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6291 connector->base.id, drm_get_connector_name(connector),
6292 encoder->base.id, drm_get_encoder_name(encoder));
6293
Chris Wilson8261b192011-04-19 23:18:09 +01006294 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006295 struct drm_crtc *crtc = encoder->crtc;
6296
6297 to_intel_connector(connector)->new_encoder = NULL;
6298 intel_encoder->new_crtc = NULL;
6299 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006300
6301 if (old->release_fb)
6302 old->release_fb->funcs->destroy(old->release_fb);
6303
Chris Wilson0622a532011-04-21 09:32:11 +01006304 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006305 }
6306
Eric Anholtc751ce42010-03-25 11:48:48 -07006307 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006308 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6309 connector->funcs->dpms(connector, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006310}
6311
6312/* Returns the clock of the currently programmed mode of the given pipe. */
6313static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6314{
6315 struct drm_i915_private *dev_priv = dev->dev_private;
6316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6317 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006318 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006319 u32 fp;
6320 intel_clock_t clock;
6321
6322 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006323 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006324 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006325 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006326
6327 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006328 if (IS_PINEVIEW(dev)) {
6329 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6330 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006331 } else {
6332 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6333 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6334 }
6335
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006336 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006337 if (IS_PINEVIEW(dev))
6338 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6339 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006340 else
6341 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006342 DPLL_FPA01_P1_POST_DIV_SHIFT);
6343
6344 switch (dpll & DPLL_MODE_MASK) {
6345 case DPLLB_MODE_DAC_SERIAL:
6346 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6347 5 : 10;
6348 break;
6349 case DPLLB_MODE_LVDS:
6350 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6351 7 : 14;
6352 break;
6353 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006354 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006355 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6356 return 0;
6357 }
6358
6359 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08006360 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006361 } else {
6362 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6363
6364 if (is_lvds) {
6365 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6366 DPLL_FPA01_P1_POST_DIV_SHIFT);
6367 clock.p2 = 14;
6368
6369 if ((dpll & PLL_REF_INPUT_MASK) ==
6370 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6371 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08006372 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006373 } else
Shaohua Li21778322009-02-23 15:19:16 +08006374 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006375 } else {
6376 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6377 clock.p1 = 2;
6378 else {
6379 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6380 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6381 }
6382 if (dpll & PLL_P2_DIVIDE_BY_4)
6383 clock.p2 = 4;
6384 else
6385 clock.p2 = 2;
6386
Shaohua Li21778322009-02-23 15:19:16 +08006387 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006388 }
6389 }
6390
6391 /* XXX: It would be nice to validate the clocks, but we can't reuse
6392 * i830PllIsValid() because it relies on the xf86_config connector
6393 * configuration being accurate, which it isn't necessarily.
6394 */
6395
6396 return clock.dot;
6397}
6398
6399/** Returns the currently programmed mode of the given pipe. */
6400struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6401 struct drm_crtc *crtc)
6402{
Jesse Barnes548f2452011-02-17 10:40:53 -08006403 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006405 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006406 struct drm_display_mode *mode;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006407 int htot = I915_READ(HTOTAL(cpu_transcoder));
6408 int hsync = I915_READ(HSYNC(cpu_transcoder));
6409 int vtot = I915_READ(VTOTAL(cpu_transcoder));
6410 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08006411
6412 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6413 if (!mode)
6414 return NULL;
6415
6416 mode->clock = intel_crtc_clock_get(dev, crtc);
6417 mode->hdisplay = (htot & 0xffff) + 1;
6418 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6419 mode->hsync_start = (hsync & 0xffff) + 1;
6420 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6421 mode->vdisplay = (vtot & 0xffff) + 1;
6422 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6423 mode->vsync_start = (vsync & 0xffff) + 1;
6424 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6425
6426 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006427
6428 return mode;
6429}
6430
Daniel Vetter3dec0092010-08-20 21:40:52 +02006431static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07006432{
6433 struct drm_device *dev = crtc->dev;
6434 drm_i915_private_t *dev_priv = dev->dev_private;
6435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6436 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006437 int dpll_reg = DPLL(pipe);
6438 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07006439
Eric Anholtbad720f2009-10-22 16:11:14 -07006440 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006441 return;
6442
6443 if (!dev_priv->lvds_downclock_avail)
6444 return;
6445
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006446 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006447 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08006448 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006449
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006450 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006451
6452 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6453 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006454 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006455
Jesse Barnes652c3932009-08-17 13:31:43 -07006456 dpll = I915_READ(dpll_reg);
6457 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08006458 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006459 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006460}
6461
6462static void intel_decrease_pllclock(struct drm_crtc *crtc)
6463{
6464 struct drm_device *dev = crtc->dev;
6465 drm_i915_private_t *dev_priv = dev->dev_private;
6466 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006467
Eric Anholtbad720f2009-10-22 16:11:14 -07006468 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006469 return;
6470
6471 if (!dev_priv->lvds_downclock_avail)
6472 return;
6473
6474 /*
6475 * Since this is called by a timer, we should never get here in
6476 * the manual case.
6477 */
6478 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01006479 int pipe = intel_crtc->pipe;
6480 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02006481 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01006482
Zhao Yakui44d98a62009-10-09 11:39:40 +08006483 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006484
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006485 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006486
Chris Wilson074b5e12012-05-02 12:07:06 +01006487 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006488 dpll |= DISPLAY_RATE_SELECT_FPA1;
6489 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006490 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006491 dpll = I915_READ(dpll_reg);
6492 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08006493 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006494 }
6495
6496}
6497
Chris Wilsonf047e392012-07-21 12:31:41 +01006498void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006499{
Chris Wilsonf047e392012-07-21 12:31:41 +01006500 i915_update_gfx_val(dev->dev_private);
6501}
6502
6503void intel_mark_idle(struct drm_device *dev)
6504{
Chris Wilsonf047e392012-07-21 12:31:41 +01006505}
6506
6507void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
6508{
6509 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07006510 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006511
6512 if (!i915_powersave)
6513 return;
6514
Jesse Barnes652c3932009-08-17 13:31:43 -07006515 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006516 if (!crtc->fb)
6517 continue;
6518
Chris Wilsonf047e392012-07-21 12:31:41 +01006519 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6520 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006521 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006522}
6523
Chris Wilsonf047e392012-07-21 12:31:41 +01006524void intel_mark_fb_idle(struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07006525{
Chris Wilsonf047e392012-07-21 12:31:41 +01006526 struct drm_device *dev = obj->base.dev;
6527 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006528
Chris Wilsonf047e392012-07-21 12:31:41 +01006529 if (!i915_powersave)
Chris Wilsonacb87df2012-05-03 15:47:57 +01006530 return;
6531
Jesse Barnes652c3932009-08-17 13:31:43 -07006532 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6533 if (!crtc->fb)
6534 continue;
6535
Chris Wilsonf047e392012-07-21 12:31:41 +01006536 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6537 intel_decrease_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006538 }
6539}
6540
Jesse Barnes79e53942008-11-07 14:24:08 -08006541static void intel_crtc_destroy(struct drm_crtc *crtc)
6542{
6543 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006544 struct drm_device *dev = crtc->dev;
6545 struct intel_unpin_work *work;
6546 unsigned long flags;
6547
6548 spin_lock_irqsave(&dev->event_lock, flags);
6549 work = intel_crtc->unpin_work;
6550 intel_crtc->unpin_work = NULL;
6551 spin_unlock_irqrestore(&dev->event_lock, flags);
6552
6553 if (work) {
6554 cancel_work_sync(&work->work);
6555 kfree(work);
6556 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006557
6558 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006559
Jesse Barnes79e53942008-11-07 14:24:08 -08006560 kfree(intel_crtc);
6561}
6562
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006563static void intel_unpin_work_fn(struct work_struct *__work)
6564{
6565 struct intel_unpin_work *work =
6566 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006567 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006568
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006569 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01006570 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00006571 drm_gem_object_unreference(&work->pending_flip_obj->base);
6572 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00006573
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006574 intel_update_fbc(dev);
6575 mutex_unlock(&dev->struct_mutex);
6576
6577 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
6578 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
6579
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006580 kfree(work);
6581}
6582
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006583static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01006584 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006585{
6586 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006587 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6588 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00006589 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006590 unsigned long flags;
6591
6592 /* Ignore early vblank irqs */
6593 if (intel_crtc == NULL)
6594 return;
6595
6596 spin_lock_irqsave(&dev->event_lock, flags);
6597 work = intel_crtc->unpin_work;
6598 if (work == NULL || !work->pending) {
6599 spin_unlock_irqrestore(&dev->event_lock, flags);
6600 return;
6601 }
6602
6603 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006604
Rob Clark45a066e2012-10-08 14:50:40 -05006605 if (work->event)
6606 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006607
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01006608 drm_vblank_put(dev, intel_crtc->pipe);
6609
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006610 spin_unlock_irqrestore(&dev->event_lock, flags);
6611
Chris Wilson05394f32010-11-08 19:18:58 +00006612 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00006613
Chris Wilson5bb61642012-09-27 21:25:58 +01006614 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006615
6616 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07006617
6618 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006619}
6620
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006621void intel_finish_page_flip(struct drm_device *dev, int pipe)
6622{
6623 drm_i915_private_t *dev_priv = dev->dev_private;
6624 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
6625
Mario Kleiner49b14a52010-12-09 07:00:07 +01006626 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006627}
6628
6629void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
6630{
6631 drm_i915_private_t *dev_priv = dev->dev_private;
6632 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
6633
Mario Kleiner49b14a52010-12-09 07:00:07 +01006634 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006635}
6636
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006637void intel_prepare_page_flip(struct drm_device *dev, int plane)
6638{
6639 drm_i915_private_t *dev_priv = dev->dev_private;
6640 struct intel_crtc *intel_crtc =
6641 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
6642 unsigned long flags;
6643
6644 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08006645 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01006646 if ((++intel_crtc->unpin_work->pending) > 1)
6647 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08006648 } else {
6649 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
6650 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006651 spin_unlock_irqrestore(&dev->event_lock, flags);
6652}
6653
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006654static int intel_gen2_queue_flip(struct drm_device *dev,
6655 struct drm_crtc *crtc,
6656 struct drm_framebuffer *fb,
6657 struct drm_i915_gem_object *obj)
6658{
6659 struct drm_i915_private *dev_priv = dev->dev_private;
6660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006661 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006662 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006663 int ret;
6664
Daniel Vetter6d90c952012-04-26 23:28:05 +02006665 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006666 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006667 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006668
Daniel Vetter6d90c952012-04-26 23:28:05 +02006669 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006670 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006671 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006672
6673 /* Can't queue multiple flips, so wait for the previous
6674 * one to finish before executing the next.
6675 */
6676 if (intel_crtc->plane)
6677 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6678 else
6679 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006680 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6681 intel_ring_emit(ring, MI_NOOP);
6682 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6683 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6684 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02006685 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006686 intel_ring_emit(ring, 0); /* aux display base address, unused */
6687 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006688 return 0;
6689
6690err_unpin:
6691 intel_unpin_fb_obj(obj);
6692err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006693 return ret;
6694}
6695
6696static int intel_gen3_queue_flip(struct drm_device *dev,
6697 struct drm_crtc *crtc,
6698 struct drm_framebuffer *fb,
6699 struct drm_i915_gem_object *obj)
6700{
6701 struct drm_i915_private *dev_priv = dev->dev_private;
6702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006703 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006704 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006705 int ret;
6706
Daniel Vetter6d90c952012-04-26 23:28:05 +02006707 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006708 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006709 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006710
Daniel Vetter6d90c952012-04-26 23:28:05 +02006711 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006712 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006713 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006714
6715 if (intel_crtc->plane)
6716 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6717 else
6718 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006719 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6720 intel_ring_emit(ring, MI_NOOP);
6721 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
6722 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6723 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02006724 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006725 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006726
Daniel Vetter6d90c952012-04-26 23:28:05 +02006727 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006728 return 0;
6729
6730err_unpin:
6731 intel_unpin_fb_obj(obj);
6732err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006733 return ret;
6734}
6735
6736static int intel_gen4_queue_flip(struct drm_device *dev,
6737 struct drm_crtc *crtc,
6738 struct drm_framebuffer *fb,
6739 struct drm_i915_gem_object *obj)
6740{
6741 struct drm_i915_private *dev_priv = dev->dev_private;
6742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6743 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006744 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006745 int ret;
6746
Daniel Vetter6d90c952012-04-26 23:28:05 +02006747 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006748 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006749 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006750
Daniel Vetter6d90c952012-04-26 23:28:05 +02006751 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006752 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006753 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006754
6755 /* i965+ uses the linear or tiled offsets from the
6756 * Display Registers (which do not change across a page-flip)
6757 * so we need only reprogram the base address.
6758 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02006759 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6760 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6761 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02006762 intel_ring_emit(ring,
6763 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
6764 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006765
6766 /* XXX Enabling the panel-fitter across page-flip is so far
6767 * untested on non-native modes, so ignore it for now.
6768 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
6769 */
6770 pf = 0;
6771 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006772 intel_ring_emit(ring, pf | pipesrc);
6773 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006774 return 0;
6775
6776err_unpin:
6777 intel_unpin_fb_obj(obj);
6778err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006779 return ret;
6780}
6781
6782static int intel_gen6_queue_flip(struct drm_device *dev,
6783 struct drm_crtc *crtc,
6784 struct drm_framebuffer *fb,
6785 struct drm_i915_gem_object *obj)
6786{
6787 struct drm_i915_private *dev_priv = dev->dev_private;
6788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006789 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006790 uint32_t pf, pipesrc;
6791 int ret;
6792
Daniel Vetter6d90c952012-04-26 23:28:05 +02006793 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006794 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006795 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006796
Daniel Vetter6d90c952012-04-26 23:28:05 +02006797 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006798 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006799 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006800
Daniel Vetter6d90c952012-04-26 23:28:05 +02006801 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6802 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6803 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Daniel Vetterc2c75132012-07-05 12:17:30 +02006804 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006805
Chris Wilson99d9acd2012-04-17 20:37:00 +01006806 /* Contrary to the suggestions in the documentation,
6807 * "Enable Panel Fitter" does not seem to be required when page
6808 * flipping with a non-native mode, and worse causes a normal
6809 * modeset to fail.
6810 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
6811 */
6812 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006813 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006814 intel_ring_emit(ring, pf | pipesrc);
6815 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006816 return 0;
6817
6818err_unpin:
6819 intel_unpin_fb_obj(obj);
6820err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006821 return ret;
6822}
6823
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006824/*
6825 * On gen7 we currently use the blit ring because (in early silicon at least)
6826 * the render ring doesn't give us interrpts for page flip completion, which
6827 * means clients will hang after the first flip is queued. Fortunately the
6828 * blit ring generates interrupts properly, so use it instead.
6829 */
6830static int intel_gen7_queue_flip(struct drm_device *dev,
6831 struct drm_crtc *crtc,
6832 struct drm_framebuffer *fb,
6833 struct drm_i915_gem_object *obj)
6834{
6835 struct drm_i915_private *dev_priv = dev->dev_private;
6836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6837 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006838 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006839 int ret;
6840
6841 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6842 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006843 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006844
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006845 switch(intel_crtc->plane) {
6846 case PLANE_A:
6847 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
6848 break;
6849 case PLANE_B:
6850 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
6851 break;
6852 case PLANE_C:
6853 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
6854 break;
6855 default:
6856 WARN_ONCE(1, "unknown plane in flip command\n");
6857 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03006858 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006859 }
6860
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006861 ret = intel_ring_begin(ring, 4);
6862 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006863 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006864
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006865 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006866 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Daniel Vetterc2c75132012-07-05 12:17:30 +02006867 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006868 intel_ring_emit(ring, (MI_NOOP));
6869 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006870 return 0;
6871
6872err_unpin:
6873 intel_unpin_fb_obj(obj);
6874err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006875 return ret;
6876}
6877
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006878static int intel_default_queue_flip(struct drm_device *dev,
6879 struct drm_crtc *crtc,
6880 struct drm_framebuffer *fb,
6881 struct drm_i915_gem_object *obj)
6882{
6883 return -ENODEV;
6884}
6885
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006886static int intel_crtc_page_flip(struct drm_crtc *crtc,
6887 struct drm_framebuffer *fb,
6888 struct drm_pending_vblank_event *event)
6889{
6890 struct drm_device *dev = crtc->dev;
6891 struct drm_i915_private *dev_priv = dev->dev_private;
6892 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00006893 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006894 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6895 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006896 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01006897 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006898
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03006899 /* Can't change pixel format via MI display flips. */
6900 if (fb->pixel_format != crtc->fb->pixel_format)
6901 return -EINVAL;
6902
6903 /*
6904 * TILEOFF/LINOFF registers can't be changed via MI display flips.
6905 * Note that pitch changes could also affect these register.
6906 */
6907 if (INTEL_INFO(dev)->gen > 3 &&
6908 (fb->offsets[0] != crtc->fb->offsets[0] ||
6909 fb->pitches[0] != crtc->fb->pitches[0]))
6910 return -EINVAL;
6911
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006912 work = kzalloc(sizeof *work, GFP_KERNEL);
6913 if (work == NULL)
6914 return -ENOMEM;
6915
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006916 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006917 work->crtc = crtc;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006918 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08006919 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006920 INIT_WORK(&work->work, intel_unpin_work_fn);
6921
Jesse Barnes7317c75e62011-08-29 09:45:28 -07006922 ret = drm_vblank_get(dev, intel_crtc->pipe);
6923 if (ret)
6924 goto free_work;
6925
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006926 /* We borrow the event spin lock for protecting unpin_work */
6927 spin_lock_irqsave(&dev->event_lock, flags);
6928 if (intel_crtc->unpin_work) {
6929 spin_unlock_irqrestore(&dev->event_lock, flags);
6930 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07006931 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01006932
6933 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006934 return -EBUSY;
6935 }
6936 intel_crtc->unpin_work = work;
6937 spin_unlock_irqrestore(&dev->event_lock, flags);
6938
6939 intel_fb = to_intel_framebuffer(fb);
6940 obj = intel_fb->obj;
6941
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006942 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
6943 flush_workqueue(dev_priv->wq);
6944
Chris Wilson79158102012-05-23 11:13:58 +01006945 ret = i915_mutex_lock_interruptible(dev);
6946 if (ret)
6947 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006948
Jesse Barnes75dfca82010-02-10 15:09:44 -08006949 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00006950 drm_gem_object_reference(&work->old_fb_obj->base);
6951 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006952
6953 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01006954
Chris Wilsone1f99ce2010-10-27 12:45:26 +01006955 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01006956
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01006957 work->enable_stall_check = true;
6958
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006959 atomic_inc(&intel_crtc->unpin_work_count);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01006960
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006961 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
6962 if (ret)
6963 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006964
Chris Wilson7782de32011-07-08 12:22:41 +01006965 intel_disable_fbc(dev);
Chris Wilsonf047e392012-07-21 12:31:41 +01006966 intel_mark_fb_busy(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006967 mutex_unlock(&dev->struct_mutex);
6968
Jesse Barnese5510fa2010-07-01 16:48:37 -07006969 trace_i915_flip_request(intel_crtc->plane, obj);
6970
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006971 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01006972
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006973cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006974 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson05394f32010-11-08 19:18:58 +00006975 drm_gem_object_unreference(&work->old_fb_obj->base);
6976 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01006977 mutex_unlock(&dev->struct_mutex);
6978
Chris Wilson79158102012-05-23 11:13:58 +01006979cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01006980 spin_lock_irqsave(&dev->event_lock, flags);
6981 intel_crtc->unpin_work = NULL;
6982 spin_unlock_irqrestore(&dev->event_lock, flags);
6983
Jesse Barnes7317c75e62011-08-29 09:45:28 -07006984 drm_vblank_put(dev, intel_crtc->pipe);
6985free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01006986 kfree(work);
6987
6988 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006989}
6990
Chris Wilsonf6e5b162011-04-12 18:06:51 +01006991static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01006992 .mode_set_base_atomic = intel_pipe_set_base_atomic,
6993 .load_lut = intel_crtc_load_lut,
Daniel Vetter976f8a22012-07-08 22:34:21 +02006994 .disable = intel_crtc_noop,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01006995};
6996
Daniel Vetter6ed0f792012-07-08 19:41:43 +02006997bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
6998{
6999 struct intel_encoder *other_encoder;
7000 struct drm_crtc *crtc = &encoder->new_crtc->base;
7001
7002 if (WARN_ON(!crtc))
7003 return false;
7004
7005 list_for_each_entry(other_encoder,
7006 &crtc->dev->mode_config.encoder_list,
7007 base.head) {
7008
7009 if (&other_encoder->new_crtc->base != crtc ||
7010 encoder == other_encoder)
7011 continue;
7012 else
7013 return true;
7014 }
7015
7016 return false;
7017}
7018
Daniel Vetter50f56112012-07-02 09:35:43 +02007019static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7020 struct drm_crtc *crtc)
7021{
7022 struct drm_device *dev;
7023 struct drm_crtc *tmp;
7024 int crtc_mask = 1;
7025
7026 WARN(!crtc, "checking null crtc?\n");
7027
7028 dev = crtc->dev;
7029
7030 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7031 if (tmp == crtc)
7032 break;
7033 crtc_mask <<= 1;
7034 }
7035
7036 if (encoder->possible_crtcs & crtc_mask)
7037 return true;
7038 return false;
7039}
7040
Daniel Vetter9a935852012-07-05 22:34:27 +02007041/**
7042 * intel_modeset_update_staged_output_state
7043 *
7044 * Updates the staged output configuration state, e.g. after we've read out the
7045 * current hw state.
7046 */
7047static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7048{
7049 struct intel_encoder *encoder;
7050 struct intel_connector *connector;
7051
7052 list_for_each_entry(connector, &dev->mode_config.connector_list,
7053 base.head) {
7054 connector->new_encoder =
7055 to_intel_encoder(connector->base.encoder);
7056 }
7057
7058 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7059 base.head) {
7060 encoder->new_crtc =
7061 to_intel_crtc(encoder->base.crtc);
7062 }
7063}
7064
7065/**
7066 * intel_modeset_commit_output_state
7067 *
7068 * This function copies the stage display pipe configuration to the real one.
7069 */
7070static void intel_modeset_commit_output_state(struct drm_device *dev)
7071{
7072 struct intel_encoder *encoder;
7073 struct intel_connector *connector;
7074
7075 list_for_each_entry(connector, &dev->mode_config.connector_list,
7076 base.head) {
7077 connector->base.encoder = &connector->new_encoder->base;
7078 }
7079
7080 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7081 base.head) {
7082 encoder->base.crtc = &encoder->new_crtc->base;
7083 }
7084}
7085
Daniel Vetter7758a112012-07-08 19:40:39 +02007086static struct drm_display_mode *
7087intel_modeset_adjusted_mode(struct drm_crtc *crtc,
7088 struct drm_display_mode *mode)
7089{
7090 struct drm_device *dev = crtc->dev;
7091 struct drm_display_mode *adjusted_mode;
7092 struct drm_encoder_helper_funcs *encoder_funcs;
7093 struct intel_encoder *encoder;
7094
7095 adjusted_mode = drm_mode_duplicate(dev, mode);
7096 if (!adjusted_mode)
7097 return ERR_PTR(-ENOMEM);
7098
7099 /* Pass our mode to the connectors and the CRTC to give them a chance to
7100 * adjust it according to limitations or connector properties, and also
7101 * a chance to reject the mode entirely.
7102 */
7103 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7104 base.head) {
7105
7106 if (&encoder->new_crtc->base != crtc)
7107 continue;
7108 encoder_funcs = encoder->base.helper_private;
7109 if (!(encoder_funcs->mode_fixup(&encoder->base, mode,
7110 adjusted_mode))) {
7111 DRM_DEBUG_KMS("Encoder fixup failed\n");
7112 goto fail;
7113 }
7114 }
7115
7116 if (!(intel_crtc_mode_fixup(crtc, mode, adjusted_mode))) {
7117 DRM_DEBUG_KMS("CRTC fixup failed\n");
7118 goto fail;
7119 }
7120 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
7121
7122 return adjusted_mode;
7123fail:
7124 drm_mode_destroy(dev, adjusted_mode);
7125 return ERR_PTR(-EINVAL);
7126}
7127
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007128/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7129 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7130static void
7131intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7132 unsigned *prepare_pipes, unsigned *disable_pipes)
7133{
7134 struct intel_crtc *intel_crtc;
7135 struct drm_device *dev = crtc->dev;
7136 struct intel_encoder *encoder;
7137 struct intel_connector *connector;
7138 struct drm_crtc *tmp_crtc;
7139
7140 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7141
7142 /* Check which crtcs have changed outputs connected to them, these need
7143 * to be part of the prepare_pipes mask. We don't (yet) support global
7144 * modeset across multiple crtcs, so modeset_pipes will only have one
7145 * bit set at most. */
7146 list_for_each_entry(connector, &dev->mode_config.connector_list,
7147 base.head) {
7148 if (connector->base.encoder == &connector->new_encoder->base)
7149 continue;
7150
7151 if (connector->base.encoder) {
7152 tmp_crtc = connector->base.encoder->crtc;
7153
7154 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7155 }
7156
7157 if (connector->new_encoder)
7158 *prepare_pipes |=
7159 1 << connector->new_encoder->new_crtc->pipe;
7160 }
7161
7162 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7163 base.head) {
7164 if (encoder->base.crtc == &encoder->new_crtc->base)
7165 continue;
7166
7167 if (encoder->base.crtc) {
7168 tmp_crtc = encoder->base.crtc;
7169
7170 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7171 }
7172
7173 if (encoder->new_crtc)
7174 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7175 }
7176
7177 /* Check for any pipes that will be fully disabled ... */
7178 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7179 base.head) {
7180 bool used = false;
7181
7182 /* Don't try to disable disabled crtcs. */
7183 if (!intel_crtc->base.enabled)
7184 continue;
7185
7186 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7187 base.head) {
7188 if (encoder->new_crtc == intel_crtc)
7189 used = true;
7190 }
7191
7192 if (!used)
7193 *disable_pipes |= 1 << intel_crtc->pipe;
7194 }
7195
7196
7197 /* set_mode is also used to update properties on life display pipes. */
7198 intel_crtc = to_intel_crtc(crtc);
7199 if (crtc->enabled)
7200 *prepare_pipes |= 1 << intel_crtc->pipe;
7201
7202 /* We only support modeset on one single crtc, hence we need to do that
7203 * only for the passed in crtc iff we change anything else than just
7204 * disable crtcs.
7205 *
7206 * This is actually not true, to be fully compatible with the old crtc
7207 * helper we automatically disable _any_ output (i.e. doesn't need to be
7208 * connected to the crtc we're modesetting on) if it's disconnected.
7209 * Which is a rather nutty api (since changed the output configuration
7210 * without userspace's explicit request can lead to confusion), but
7211 * alas. Hence we currently need to modeset on all pipes we prepare. */
7212 if (*prepare_pipes)
7213 *modeset_pipes = *prepare_pipes;
7214
7215 /* ... and mask these out. */
7216 *modeset_pipes &= ~(*disable_pipes);
7217 *prepare_pipes &= ~(*disable_pipes);
7218}
7219
Daniel Vetterea9d7582012-07-10 10:42:52 +02007220static bool intel_crtc_in_use(struct drm_crtc *crtc)
7221{
7222 struct drm_encoder *encoder;
7223 struct drm_device *dev = crtc->dev;
7224
7225 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
7226 if (encoder->crtc == crtc)
7227 return true;
7228
7229 return false;
7230}
7231
7232static void
7233intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
7234{
7235 struct intel_encoder *intel_encoder;
7236 struct intel_crtc *intel_crtc;
7237 struct drm_connector *connector;
7238
7239 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
7240 base.head) {
7241 if (!intel_encoder->base.crtc)
7242 continue;
7243
7244 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
7245
7246 if (prepare_pipes & (1 << intel_crtc->pipe))
7247 intel_encoder->connectors_active = false;
7248 }
7249
7250 intel_modeset_commit_output_state(dev);
7251
7252 /* Update computed state. */
7253 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7254 base.head) {
7255 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
7256 }
7257
7258 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7259 if (!connector->encoder || !connector->encoder->crtc)
7260 continue;
7261
7262 intel_crtc = to_intel_crtc(connector->encoder->crtc);
7263
7264 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02007265 struct drm_property *dpms_property =
7266 dev->mode_config.dpms_property;
7267
Daniel Vetterea9d7582012-07-10 10:42:52 +02007268 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05007269 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02007270 dpms_property,
7271 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02007272
7273 intel_encoder = to_intel_encoder(connector->encoder);
7274 intel_encoder->connectors_active = true;
7275 }
7276 }
7277
7278}
7279
Daniel Vetter25c5b262012-07-08 22:08:04 +02007280#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
7281 list_for_each_entry((intel_crtc), \
7282 &(dev)->mode_config.crtc_list, \
7283 base.head) \
7284 if (mask & (1 <<(intel_crtc)->pipe)) \
7285
Daniel Vetterb9805142012-08-31 17:37:33 +02007286void
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007287intel_modeset_check_state(struct drm_device *dev)
7288{
7289 struct intel_crtc *crtc;
7290 struct intel_encoder *encoder;
7291 struct intel_connector *connector;
7292
7293 list_for_each_entry(connector, &dev->mode_config.connector_list,
7294 base.head) {
7295 /* This also checks the encoder/connector hw state with the
7296 * ->get_hw_state callbacks. */
7297 intel_connector_check_state(connector);
7298
7299 WARN(&connector->new_encoder->base != connector->base.encoder,
7300 "connector's staged encoder doesn't match current encoder\n");
7301 }
7302
7303 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7304 base.head) {
7305 bool enabled = false;
7306 bool active = false;
7307 enum pipe pipe, tracked_pipe;
7308
7309 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
7310 encoder->base.base.id,
7311 drm_get_encoder_name(&encoder->base));
7312
7313 WARN(&encoder->new_crtc->base != encoder->base.crtc,
7314 "encoder's stage crtc doesn't match current crtc\n");
7315 WARN(encoder->connectors_active && !encoder->base.crtc,
7316 "encoder's active_connectors set, but no crtc\n");
7317
7318 list_for_each_entry(connector, &dev->mode_config.connector_list,
7319 base.head) {
7320 if (connector->base.encoder != &encoder->base)
7321 continue;
7322 enabled = true;
7323 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
7324 active = true;
7325 }
7326 WARN(!!encoder->base.crtc != enabled,
7327 "encoder's enabled state mismatch "
7328 "(expected %i, found %i)\n",
7329 !!encoder->base.crtc, enabled);
7330 WARN(active && !encoder->base.crtc,
7331 "active encoder with no crtc\n");
7332
7333 WARN(encoder->connectors_active != active,
7334 "encoder's computed active state doesn't match tracked active state "
7335 "(expected %i, found %i)\n", active, encoder->connectors_active);
7336
7337 active = encoder->get_hw_state(encoder, &pipe);
7338 WARN(active != encoder->connectors_active,
7339 "encoder's hw state doesn't match sw tracking "
7340 "(expected %i, found %i)\n",
7341 encoder->connectors_active, active);
7342
7343 if (!encoder->base.crtc)
7344 continue;
7345
7346 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
7347 WARN(active && pipe != tracked_pipe,
7348 "active encoder's pipe doesn't match"
7349 "(expected %i, found %i)\n",
7350 tracked_pipe, pipe);
7351
7352 }
7353
7354 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
7355 base.head) {
7356 bool enabled = false;
7357 bool active = false;
7358
7359 DRM_DEBUG_KMS("[CRTC:%d]\n",
7360 crtc->base.base.id);
7361
7362 WARN(crtc->active && !crtc->base.enabled,
7363 "active crtc, but not enabled in sw tracking\n");
7364
7365 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7366 base.head) {
7367 if (encoder->base.crtc != &crtc->base)
7368 continue;
7369 enabled = true;
7370 if (encoder->connectors_active)
7371 active = true;
7372 }
7373 WARN(active != crtc->active,
7374 "crtc's computed active state doesn't match tracked active state "
7375 "(expected %i, found %i)\n", active, crtc->active);
7376 WARN(enabled != crtc->base.enabled,
7377 "crtc's computed enabled state doesn't match tracked enabled state "
7378 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
7379
7380 assert_pipe(dev->dev_private, crtc->pipe, crtc->active);
7381 }
7382}
7383
Daniel Vettera6778b32012-07-02 09:56:42 +02007384bool intel_set_mode(struct drm_crtc *crtc,
7385 struct drm_display_mode *mode,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007386 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02007387{
7388 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02007389 drm_i915_private_t *dev_priv = dev->dev_private;
Tim Gardner3ac18232012-12-07 07:54:26 -07007390 struct drm_display_mode *adjusted_mode, *saved_mode, *saved_hwmode;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007391 struct intel_crtc *intel_crtc;
7392 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Daniel Vettera6778b32012-07-02 09:56:42 +02007393 bool ret = true;
7394
Tim Gardner3ac18232012-12-07 07:54:26 -07007395 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
7396 if (!saved_mode) {
7397 DRM_ERROR("i915: Could not allocate saved display mode.\n");
7398 return false;
7399 }
7400 saved_hwmode = saved_mode + 1;
7401
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007402 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02007403 &prepare_pipes, &disable_pipes);
7404
7405 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7406 modeset_pipes, prepare_pipes, disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007407
Daniel Vetter976f8a22012-07-08 22:34:21 +02007408 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
7409 intel_crtc_disable(&intel_crtc->base);
7410
Tim Gardner3ac18232012-12-07 07:54:26 -07007411 *saved_hwmode = crtc->hwmode;
7412 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007413
Daniel Vetter25c5b262012-07-08 22:08:04 +02007414 /* Hack: Because we don't (yet) support global modeset on multiple
7415 * crtcs, we don't keep track of the new mode for more than one crtc.
7416 * Hence simply check whether any bit is set in modeset_pipes in all the
7417 * pieces of code that are not yet converted to deal with mutliple crtcs
7418 * changing their mode at the same time. */
7419 adjusted_mode = NULL;
7420 if (modeset_pipes) {
7421 adjusted_mode = intel_modeset_adjusted_mode(crtc, mode);
7422 if (IS_ERR(adjusted_mode)) {
Tim Gardner3ac18232012-12-07 07:54:26 -07007423 ret = false;
7424 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007425 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007426 }
7427
Daniel Vetterea9d7582012-07-10 10:42:52 +02007428 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
7429 if (intel_crtc->base.enabled)
7430 dev_priv->display.crtc_disable(&intel_crtc->base);
7431 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007432
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02007433 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
7434 * to set it here already despite that we pass it down the callchain.
7435 */
7436 if (modeset_pipes)
Daniel Vetter25c5b262012-07-08 22:08:04 +02007437 crtc->mode = *mode;
Daniel Vetter7758a112012-07-08 19:40:39 +02007438
Daniel Vetterea9d7582012-07-10 10:42:52 +02007439 /* Only after disabling all output pipelines that will be changed can we
7440 * update the the output configuration. */
7441 intel_modeset_update_state(dev, prepare_pipes);
7442
Daniel Vetter47fab732012-10-26 10:58:18 +02007443 if (dev_priv->display.modeset_global_resources)
7444 dev_priv->display.modeset_global_resources(dev);
7445
Daniel Vettera6778b32012-07-02 09:56:42 +02007446 /* Set up the DPLL and any encoders state that needs to adjust or depend
7447 * on the DPLL.
7448 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007449 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
7450 ret = !intel_crtc_mode_set(&intel_crtc->base,
7451 mode, adjusted_mode,
7452 x, y, fb);
7453 if (!ret)
7454 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02007455 }
7456
7457 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007458 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
7459 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02007460
Daniel Vetter25c5b262012-07-08 22:08:04 +02007461 if (modeset_pipes) {
7462 /* Store real post-adjustment hardware mode. */
7463 crtc->hwmode = *adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007464
Daniel Vetter25c5b262012-07-08 22:08:04 +02007465 /* Calculate and store various constants which
7466 * are later needed by vblank and swap-completion
7467 * timestamping. They are derived from true hwmode.
7468 */
7469 drm_calc_timestamping_constants(crtc);
7470 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007471
7472 /* FIXME: add subpixel order */
7473done:
7474 drm_mode_destroy(dev, adjusted_mode);
Daniel Vetter25c5b262012-07-08 22:08:04 +02007475 if (!ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07007476 crtc->hwmode = *saved_hwmode;
7477 crtc->mode = *saved_mode;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007478 } else {
7479 intel_modeset_check_state(dev);
Daniel Vettera6778b32012-07-02 09:56:42 +02007480 }
7481
Tim Gardner3ac18232012-12-07 07:54:26 -07007482out:
7483 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02007484 return ret;
7485}
7486
Daniel Vetter25c5b262012-07-08 22:08:04 +02007487#undef for_each_intel_crtc_masked
7488
Daniel Vetterd9e55602012-07-04 22:16:09 +02007489static void intel_set_config_free(struct intel_set_config *config)
7490{
7491 if (!config)
7492 return;
7493
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007494 kfree(config->save_connector_encoders);
7495 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02007496 kfree(config);
7497}
7498
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007499static int intel_set_config_save_state(struct drm_device *dev,
7500 struct intel_set_config *config)
7501{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007502 struct drm_encoder *encoder;
7503 struct drm_connector *connector;
7504 int count;
7505
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007506 config->save_encoder_crtcs =
7507 kcalloc(dev->mode_config.num_encoder,
7508 sizeof(struct drm_crtc *), GFP_KERNEL);
7509 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007510 return -ENOMEM;
7511
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007512 config->save_connector_encoders =
7513 kcalloc(dev->mode_config.num_connector,
7514 sizeof(struct drm_encoder *), GFP_KERNEL);
7515 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007516 return -ENOMEM;
7517
7518 /* Copy data. Note that driver private data is not affected.
7519 * Should anything bad happen only the expected state is
7520 * restored, not the drivers personal bookkeeping.
7521 */
7522 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007523 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007524 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007525 }
7526
7527 count = 0;
7528 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007529 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007530 }
7531
7532 return 0;
7533}
7534
7535static void intel_set_config_restore_state(struct drm_device *dev,
7536 struct intel_set_config *config)
7537{
Daniel Vetter9a935852012-07-05 22:34:27 +02007538 struct intel_encoder *encoder;
7539 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007540 int count;
7541
7542 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007543 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7544 encoder->new_crtc =
7545 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007546 }
7547
7548 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007549 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
7550 connector->new_encoder =
7551 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007552 }
7553}
7554
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007555static void
7556intel_set_config_compute_mode_changes(struct drm_mode_set *set,
7557 struct intel_set_config *config)
7558{
7559
7560 /* We should be able to check here if the fb has the same properties
7561 * and then just flip_or_move it */
7562 if (set->crtc->fb != set->fb) {
7563 /* If we have no fb then treat it as a full mode set */
7564 if (set->crtc->fb == NULL) {
7565 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
7566 config->mode_changed = true;
7567 } else if (set->fb == NULL) {
7568 config->mode_changed = true;
7569 } else if (set->fb->depth != set->crtc->fb->depth) {
7570 config->mode_changed = true;
7571 } else if (set->fb->bits_per_pixel !=
7572 set->crtc->fb->bits_per_pixel) {
7573 config->mode_changed = true;
7574 } else
7575 config->fb_changed = true;
7576 }
7577
Daniel Vetter835c5872012-07-10 18:11:08 +02007578 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007579 config->fb_changed = true;
7580
7581 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
7582 DRM_DEBUG_KMS("modes are different, full mode set\n");
7583 drm_mode_debug_printmodeline(&set->crtc->mode);
7584 drm_mode_debug_printmodeline(set->mode);
7585 config->mode_changed = true;
7586 }
7587}
7588
Daniel Vetter2e431052012-07-04 22:42:15 +02007589static int
Daniel Vetter9a935852012-07-05 22:34:27 +02007590intel_modeset_stage_output_state(struct drm_device *dev,
7591 struct drm_mode_set *set,
7592 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02007593{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007594 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02007595 struct intel_connector *connector;
7596 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02007597 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02007598
Daniel Vetter9a935852012-07-05 22:34:27 +02007599 /* The upper layers ensure that we either disabl a crtc or have a list
7600 * of connectors. For paranoia, double-check this. */
7601 WARN_ON(!set->fb && (set->num_connectors != 0));
7602 WARN_ON(set->fb && (set->num_connectors == 0));
7603
Daniel Vetter50f56112012-07-02 09:35:43 +02007604 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007605 list_for_each_entry(connector, &dev->mode_config.connector_list,
7606 base.head) {
7607 /* Otherwise traverse passed in connector list and get encoders
7608 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007609 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02007610 if (set->connectors[ro] == &connector->base) {
7611 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02007612 break;
7613 }
7614 }
7615
Daniel Vetter9a935852012-07-05 22:34:27 +02007616 /* If we disable the crtc, disable all its connectors. Also, if
7617 * the connector is on the changing crtc but not on the new
7618 * connector list, disable it. */
7619 if ((!set->fb || ro == set->num_connectors) &&
7620 connector->base.encoder &&
7621 connector->base.encoder->crtc == set->crtc) {
7622 connector->new_encoder = NULL;
7623
7624 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
7625 connector->base.base.id,
7626 drm_get_connector_name(&connector->base));
7627 }
7628
7629
7630 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007631 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007632 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02007633 }
Daniel Vetter50f56112012-07-02 09:35:43 +02007634
Daniel Vetter9a935852012-07-05 22:34:27 +02007635 /* Disable all disconnected encoders. */
7636 if (connector->base.status == connector_status_disconnected)
7637 connector->new_encoder = NULL;
7638 }
7639 /* connector->new_encoder is now updated for all connectors. */
7640
7641 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007642 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007643 list_for_each_entry(connector, &dev->mode_config.connector_list,
7644 base.head) {
7645 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02007646 continue;
7647
Daniel Vetter9a935852012-07-05 22:34:27 +02007648 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02007649
7650 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02007651 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02007652 new_crtc = set->crtc;
7653 }
7654
7655 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02007656 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
7657 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007658 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02007659 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007660 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
7661
7662 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
7663 connector->base.base.id,
7664 drm_get_connector_name(&connector->base),
7665 new_crtc->base.id);
7666 }
7667
7668 /* Check for any encoders that needs to be disabled. */
7669 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7670 base.head) {
7671 list_for_each_entry(connector,
7672 &dev->mode_config.connector_list,
7673 base.head) {
7674 if (connector->new_encoder == encoder) {
7675 WARN_ON(!connector->new_encoder->new_crtc);
7676
7677 goto next_encoder;
7678 }
7679 }
7680 encoder->new_crtc = NULL;
7681next_encoder:
7682 /* Only now check for crtc changes so we don't miss encoders
7683 * that will be disabled. */
7684 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007685 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007686 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02007687 }
7688 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007689 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007690
Daniel Vetter2e431052012-07-04 22:42:15 +02007691 return 0;
7692}
7693
7694static int intel_crtc_set_config(struct drm_mode_set *set)
7695{
7696 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02007697 struct drm_mode_set save_set;
7698 struct intel_set_config *config;
7699 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02007700
Daniel Vetter8d3e3752012-07-05 16:09:09 +02007701 BUG_ON(!set);
7702 BUG_ON(!set->crtc);
7703 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02007704
7705 if (!set->mode)
7706 set->fb = NULL;
7707
Daniel Vetter431e50f2012-07-10 17:53:42 +02007708 /* The fb helper likes to play gross jokes with ->mode_set_config.
7709 * Unfortunately the crtc helper doesn't do much at all for this case,
7710 * so we have to cope with this madness until the fb helper is fixed up. */
7711 if (set->fb && set->num_connectors == 0)
7712 return 0;
7713
Daniel Vetter2e431052012-07-04 22:42:15 +02007714 if (set->fb) {
7715 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
7716 set->crtc->base.id, set->fb->base.id,
7717 (int)set->num_connectors, set->x, set->y);
7718 } else {
7719 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02007720 }
7721
7722 dev = set->crtc->dev;
7723
7724 ret = -ENOMEM;
7725 config = kzalloc(sizeof(*config), GFP_KERNEL);
7726 if (!config)
7727 goto out_config;
7728
7729 ret = intel_set_config_save_state(dev, config);
7730 if (ret)
7731 goto out_config;
7732
7733 save_set.crtc = set->crtc;
7734 save_set.mode = &set->crtc->mode;
7735 save_set.x = set->crtc->x;
7736 save_set.y = set->crtc->y;
7737 save_set.fb = set->crtc->fb;
7738
7739 /* Compute whether we need a full modeset, only an fb base update or no
7740 * change at all. In the future we might also check whether only the
7741 * mode changed, e.g. for LVDS where we only change the panel fitter in
7742 * such cases. */
7743 intel_set_config_compute_mode_changes(set, config);
7744
Daniel Vetter9a935852012-07-05 22:34:27 +02007745 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02007746 if (ret)
7747 goto fail;
7748
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007749 if (config->mode_changed) {
Daniel Vetter87f1faa2012-07-05 23:36:17 +02007750 if (set->mode) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007751 DRM_DEBUG_KMS("attempting to set mode from"
7752 " userspace\n");
7753 drm_mode_debug_printmodeline(set->mode);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02007754 }
7755
7756 if (!intel_set_mode(set->crtc, set->mode,
7757 set->x, set->y, set->fb)) {
7758 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
7759 set->crtc->base.id);
7760 ret = -EINVAL;
7761 goto fail;
7762 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007763 } else if (config->fb_changed) {
Daniel Vetter4f660f42012-07-02 09:47:37 +02007764 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007765 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02007766 }
7767
Daniel Vetterd9e55602012-07-04 22:16:09 +02007768 intel_set_config_free(config);
7769
Daniel Vetter50f56112012-07-02 09:35:43 +02007770 return 0;
7771
7772fail:
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007773 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02007774
7775 /* Try to restore the config */
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007776 if (config->mode_changed &&
Daniel Vettera6778b32012-07-02 09:56:42 +02007777 !intel_set_mode(save_set.crtc, save_set.mode,
7778 save_set.x, save_set.y, save_set.fb))
Daniel Vetter50f56112012-07-02 09:35:43 +02007779 DRM_ERROR("failed to restore config after modeset failure\n");
7780
Daniel Vetterd9e55602012-07-04 22:16:09 +02007781out_config:
7782 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02007783 return ret;
7784}
7785
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007786static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007787 .cursor_set = intel_crtc_cursor_set,
7788 .cursor_move = intel_crtc_cursor_move,
7789 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02007790 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007791 .destroy = intel_crtc_destroy,
7792 .page_flip = intel_crtc_page_flip,
7793};
7794
Paulo Zanoni79f689a2012-10-05 12:05:52 -03007795static void intel_cpu_pll_init(struct drm_device *dev)
7796{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02007797 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03007798 intel_ddi_pll_init(dev);
7799}
7800
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007801static void intel_pch_pll_init(struct drm_device *dev)
7802{
7803 drm_i915_private_t *dev_priv = dev->dev_private;
7804 int i;
7805
7806 if (dev_priv->num_pch_pll == 0) {
7807 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
7808 return;
7809 }
7810
7811 for (i = 0; i < dev_priv->num_pch_pll; i++) {
7812 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
7813 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
7814 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
7815 }
7816}
7817
Hannes Ederb358d0a2008-12-18 21:18:47 +01007818static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08007819{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007820 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007821 struct intel_crtc *intel_crtc;
7822 int i;
7823
7824 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
7825 if (intel_crtc == NULL)
7826 return;
7827
7828 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
7829
7830 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08007831 for (i = 0; i < 256; i++) {
7832 intel_crtc->lut_r[i] = i;
7833 intel_crtc->lut_g[i] = i;
7834 intel_crtc->lut_b[i] = i;
7835 }
7836
Jesse Barnes80824002009-09-10 15:28:06 -07007837 /* Swap pipes & planes for FBC on pre-965 */
7838 intel_crtc->pipe = pipe;
7839 intel_crtc->plane = pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02007840 intel_crtc->cpu_transcoder = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01007841 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007842 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01007843 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07007844 }
7845
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007846 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
7847 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
7848 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
7849 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
7850
Jesse Barnes5a354202011-06-24 12:19:22 -07007851 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07007852
Jesse Barnes79e53942008-11-07 14:24:08 -08007853 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08007854}
7855
Carl Worth08d7b3d2009-04-29 14:43:54 -07007856int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00007857 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07007858{
Carl Worth08d7b3d2009-04-29 14:43:54 -07007859 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02007860 struct drm_mode_object *drmmode_obj;
7861 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007862
Daniel Vetter1cff8f62012-04-24 09:55:08 +02007863 if (!drm_core_check_feature(dev, DRIVER_MODESET))
7864 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007865
Daniel Vetterc05422d2009-08-11 16:05:30 +02007866 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
7867 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07007868
Daniel Vetterc05422d2009-08-11 16:05:30 +02007869 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07007870 DRM_ERROR("no such CRTC id\n");
7871 return -EINVAL;
7872 }
7873
Daniel Vetterc05422d2009-08-11 16:05:30 +02007874 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
7875 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007876
Daniel Vetterc05422d2009-08-11 16:05:30 +02007877 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007878}
7879
Daniel Vetter66a92782012-07-12 20:08:18 +02007880static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08007881{
Daniel Vetter66a92782012-07-12 20:08:18 +02007882 struct drm_device *dev = encoder->base.dev;
7883 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007884 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007885 int entry = 0;
7886
Daniel Vetter66a92782012-07-12 20:08:18 +02007887 list_for_each_entry(source_encoder,
7888 &dev->mode_config.encoder_list, base.head) {
7889
7890 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08007891 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02007892
7893 /* Intel hw has only one MUX where enocoders could be cloned. */
7894 if (encoder->cloneable && source_encoder->cloneable)
7895 index_mask |= (1 << entry);
7896
Jesse Barnes79e53942008-11-07 14:24:08 -08007897 entry++;
7898 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01007899
Jesse Barnes79e53942008-11-07 14:24:08 -08007900 return index_mask;
7901}
7902
Chris Wilson4d302442010-12-14 19:21:29 +00007903static bool has_edp_a(struct drm_device *dev)
7904{
7905 struct drm_i915_private *dev_priv = dev->dev_private;
7906
7907 if (!IS_MOBILE(dev))
7908 return false;
7909
7910 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
7911 return false;
7912
7913 if (IS_GEN5(dev) &&
7914 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
7915 return false;
7916
7917 return true;
7918}
7919
Jesse Barnes79e53942008-11-07 14:24:08 -08007920static void intel_setup_outputs(struct drm_device *dev)
7921{
Eric Anholt725e30a2009-01-22 13:01:02 -08007922 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007923 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007924 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00007925 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08007926
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00007927 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00007928 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
7929 /* disable the panel fitter on everything but LVDS */
7930 I915_WRITE(PFIT_CONTROL, 0);
7931 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007932
Paulo Zanoniaffa9352012-11-23 15:30:39 -02007933 if (!(HAS_DDI(dev) && (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02007934 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007935
Paulo Zanoniaffa9352012-11-23 15:30:39 -02007936 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03007937 int found;
7938
7939 /* Haswell uses DDI functions to detect digital outputs */
7940 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
7941 /* DDI A only supports eDP */
7942 if (found)
7943 intel_ddi_init(dev, PORT_A);
7944
7945 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
7946 * register */
7947 found = I915_READ(SFUSE_STRAP);
7948
7949 if (found & SFUSE_STRAP_DDIB_DETECTED)
7950 intel_ddi_init(dev, PORT_B);
7951 if (found & SFUSE_STRAP_DDIC_DETECTED)
7952 intel_ddi_init(dev, PORT_C);
7953 if (found & SFUSE_STRAP_DDID_DETECTED)
7954 intel_ddi_init(dev, PORT_D);
7955 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007956 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02007957 dpd_is_edp = intel_dpd_is_edp(dev);
7958
7959 if (has_edp_a(dev))
7960 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007961
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007962 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08007963 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01007964 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007965 if (!found)
Daniel Vetter08d644a2012-07-12 20:19:59 +02007966 intel_hdmi_init(dev, HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08007967 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03007968 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007969 }
7970
7971 if (I915_READ(HDMIC) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02007972 intel_hdmi_init(dev, HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007973
Jesse Barnesb708a1d2012-06-11 14:39:56 -04007974 if (!dpd_is_edp && I915_READ(HDMID) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02007975 intel_hdmi_init(dev, HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007976
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08007977 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03007978 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08007979
Daniel Vetter270b3042012-10-27 15:52:05 +02007980 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03007981 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07007982 } else if (IS_VALLEYVIEW(dev)) {
7983 int found;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08007984
Gajanan Bhat19c03922012-09-27 19:13:07 +05307985 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
7986 if (I915_READ(DP_C) & DP_DETECTED)
7987 intel_dp_init(dev, DP_C, PORT_C);
7988
Jesse Barnes4a87d652012-06-15 11:55:16 -07007989 if (I915_READ(SDVOB) & PORT_DETECTED) {
7990 /* SDVOB multiplex with HDMIB */
7991 found = intel_sdvo_init(dev, SDVOB, true);
7992 if (!found)
Daniel Vetter08d644a2012-07-12 20:19:59 +02007993 intel_hdmi_init(dev, SDVOB, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07007994 if (!found && (I915_READ(DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03007995 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07007996 }
7997
7998 if (I915_READ(SDVOC) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02007999 intel_hdmi_init(dev, SDVOC, PORT_C);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008000
Zhenyu Wang103a1962009-11-27 11:44:36 +08008001 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008002 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008003
Eric Anholt725e30a2009-01-22 13:01:02 -08008004 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008005 DRM_DEBUG_KMS("probing SDVOB\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008006 found = intel_sdvo_init(dev, SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008007 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8008 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008009 intel_hdmi_init(dev, SDVOB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008010 }
Ma Ling27185ae2009-08-24 13:50:23 +08008011
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008012 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8013 DRM_DEBUG_KMS("probing DP_B\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008014 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008015 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008016 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008017
8018 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008019
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008020 if (I915_READ(SDVOB) & SDVO_DETECTED) {
8021 DRM_DEBUG_KMS("probing SDVOC\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008022 found = intel_sdvo_init(dev, SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008023 }
Ma Ling27185ae2009-08-24 13:50:23 +08008024
8025 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
8026
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008027 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8028 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008029 intel_hdmi_init(dev, SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008030 }
8031 if (SUPPORTS_INTEGRATED_DP(dev)) {
8032 DRM_DEBUG_KMS("probing DP_C\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008033 intel_dp_init(dev, DP_C, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008034 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008035 }
Ma Ling27185ae2009-08-24 13:50:23 +08008036
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008037 if (SUPPORTS_INTEGRATED_DP(dev) &&
8038 (I915_READ(DP_D) & DP_DETECTED)) {
8039 DRM_DEBUG_KMS("probing DP_D\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008040 intel_dp_init(dev, DP_D, PORT_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008041 }
Eric Anholtbad720f2009-10-22 16:11:14 -07008042 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008043 intel_dvo_init(dev);
8044
Zhenyu Wang103a1962009-11-27 11:44:36 +08008045 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008046 intel_tv_init(dev);
8047
Chris Wilson4ef69c72010-09-09 15:14:28 +01008048 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8049 encoder->base.possible_crtcs = encoder->crtc_mask;
8050 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02008051 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08008052 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008053
Paulo Zanoni40579ab2012-07-03 15:57:33 -03008054 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Keith Packard9fb526d2011-09-26 22:24:57 -07008055 ironlake_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02008056
8057 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008058}
8059
8060static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8061{
8062 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008063
8064 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008065 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008066
8067 kfree(intel_fb);
8068}
8069
8070static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008071 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008072 unsigned int *handle)
8073{
8074 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008075 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008076
Chris Wilson05394f32010-11-08 19:18:58 +00008077 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008078}
8079
8080static const struct drm_framebuffer_funcs intel_fb_funcs = {
8081 .destroy = intel_user_framebuffer_destroy,
8082 .create_handle = intel_user_framebuffer_create_handle,
8083};
8084
Dave Airlie38651672010-03-30 05:34:13 +00008085int intel_framebuffer_init(struct drm_device *dev,
8086 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008087 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008088 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008089{
Jesse Barnes79e53942008-11-07 14:24:08 -08008090 int ret;
8091
Chris Wilson05394f32010-11-08 19:18:58 +00008092 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01008093 return -EINVAL;
8094
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008095 if (mode_cmd->pitches[0] & 63)
Chris Wilson57cd6502010-08-08 12:34:44 +01008096 return -EINVAL;
8097
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008098 /* FIXME <= Gen4 stride limits are bit unclear */
8099 if (mode_cmd->pitches[0] > 32768)
8100 return -EINVAL;
8101
8102 if (obj->tiling_mode != I915_TILING_NONE &&
8103 mode_cmd->pitches[0] != obj->stride)
8104 return -EINVAL;
8105
Ville Syrjälä57779d02012-10-31 17:50:14 +02008106 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008107 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02008108 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008109 case DRM_FORMAT_RGB565:
8110 case DRM_FORMAT_XRGB8888:
8111 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008112 break;
8113 case DRM_FORMAT_XRGB1555:
8114 case DRM_FORMAT_ARGB1555:
8115 if (INTEL_INFO(dev)->gen > 3)
8116 return -EINVAL;
8117 break;
8118 case DRM_FORMAT_XBGR8888:
8119 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008120 case DRM_FORMAT_XRGB2101010:
8121 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008122 case DRM_FORMAT_XBGR2101010:
8123 case DRM_FORMAT_ABGR2101010:
8124 if (INTEL_INFO(dev)->gen < 4)
8125 return -EINVAL;
Jesse Barnesb5626742011-06-24 12:19:27 -07008126 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02008127 case DRM_FORMAT_YUYV:
8128 case DRM_FORMAT_UYVY:
8129 case DRM_FORMAT_YVYU:
8130 case DRM_FORMAT_VYUY:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008131 if (INTEL_INFO(dev)->gen < 6)
8132 return -EINVAL;
Chris Wilson57cd6502010-08-08 12:34:44 +01008133 break;
8134 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008135 DRM_DEBUG_KMS("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01008136 return -EINVAL;
8137 }
8138
Ville Syrjälä90f9a332012-10-31 17:50:19 +02008139 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
8140 if (mode_cmd->offsets[0] != 0)
8141 return -EINVAL;
8142
Jesse Barnes79e53942008-11-07 14:24:08 -08008143 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8144 if (ret) {
8145 DRM_ERROR("framebuffer init failed %d\n", ret);
8146 return ret;
8147 }
8148
8149 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08008150 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008151 return 0;
8152}
8153
Jesse Barnes79e53942008-11-07 14:24:08 -08008154static struct drm_framebuffer *
8155intel_user_framebuffer_create(struct drm_device *dev,
8156 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008157 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08008158{
Chris Wilson05394f32010-11-08 19:18:58 +00008159 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008160
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008161 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8162 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00008163 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01008164 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08008165
Chris Wilsond2dff872011-04-19 08:36:26 +01008166 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08008167}
8168
Jesse Barnes79e53942008-11-07 14:24:08 -08008169static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08008170 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00008171 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08008172};
8173
Jesse Barnese70236a2009-09-21 10:42:27 -07008174/* Set up chip specific display functions */
8175static void intel_init_display(struct drm_device *dev)
8176{
8177 struct drm_i915_private *dev_priv = dev->dev_private;
8178
8179 /* We always want a DPMS function */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008180 if (HAS_DDI(dev)) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008181 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02008182 dev_priv->display.crtc_enable = haswell_crtc_enable;
8183 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008184 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008185 dev_priv->display.update_plane = ironlake_update_plane;
8186 } else if (HAS_PCH_SPLIT(dev)) {
Eric Anholtf564048e2011-03-30 13:01:02 -07008187 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008188 dev_priv->display.crtc_enable = ironlake_crtc_enable;
8189 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008190 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008191 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008192 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07008193 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008194 dev_priv->display.crtc_enable = i9xx_crtc_enable;
8195 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008196 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008197 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008198 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008199
Jesse Barnese70236a2009-09-21 10:42:27 -07008200 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07008201 if (IS_VALLEYVIEW(dev))
8202 dev_priv->display.get_display_clock_speed =
8203 valleyview_get_display_clock_speed;
8204 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07008205 dev_priv->display.get_display_clock_speed =
8206 i945_get_display_clock_speed;
8207 else if (IS_I915G(dev))
8208 dev_priv->display.get_display_clock_speed =
8209 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008210 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008211 dev_priv->display.get_display_clock_speed =
8212 i9xx_misc_get_display_clock_speed;
8213 else if (IS_I915GM(dev))
8214 dev_priv->display.get_display_clock_speed =
8215 i915gm_get_display_clock_speed;
8216 else if (IS_I865G(dev))
8217 dev_priv->display.get_display_clock_speed =
8218 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02008219 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008220 dev_priv->display.get_display_clock_speed =
8221 i855_get_display_clock_speed;
8222 else /* 852, 830 */
8223 dev_priv->display.get_display_clock_speed =
8224 i830_get_display_clock_speed;
8225
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008226 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01008227 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008228 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008229 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08008230 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008231 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008232 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07008233 } else if (IS_IVYBRIDGE(dev)) {
8234 /* FIXME: detect B0+ stepping and use auto training */
8235 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008236 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02008237 dev_priv->display.modeset_global_resources =
8238 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03008239 } else if (IS_HASWELL(dev)) {
8240 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08008241 dev_priv->display.write_eld = haswell_write_eld;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008242 } else
8243 dev_priv->display.update_wm = NULL;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008244 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08008245 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07008246 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008247
8248 /* Default just returns -ENODEV to indicate unsupported */
8249 dev_priv->display.queue_flip = intel_default_queue_flip;
8250
8251 switch (INTEL_INFO(dev)->gen) {
8252 case 2:
8253 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8254 break;
8255
8256 case 3:
8257 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8258 break;
8259
8260 case 4:
8261 case 5:
8262 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8263 break;
8264
8265 case 6:
8266 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8267 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008268 case 7:
8269 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8270 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008271 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008272}
8273
Jesse Barnesb690e962010-07-19 13:53:12 -07008274/*
8275 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8276 * resume, or other times. This quirk makes sure that's the case for
8277 * affected systems.
8278 */
Akshay Joshi0206e352011-08-16 15:34:10 -04008279static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07008280{
8281 struct drm_i915_private *dev_priv = dev->dev_private;
8282
8283 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008284 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008285}
8286
Keith Packard435793d2011-07-12 14:56:22 -07008287/*
8288 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8289 */
8290static void quirk_ssc_force_disable(struct drm_device *dev)
8291{
8292 struct drm_i915_private *dev_priv = dev->dev_private;
8293 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008294 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07008295}
8296
Carsten Emde4dca20e2012-03-15 15:56:26 +01008297/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01008298 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
8299 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01008300 */
8301static void quirk_invert_brightness(struct drm_device *dev)
8302{
8303 struct drm_i915_private *dev_priv = dev->dev_private;
8304 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008305 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008306}
8307
8308struct intel_quirk {
8309 int device;
8310 int subsystem_vendor;
8311 int subsystem_device;
8312 void (*hook)(struct drm_device *dev);
8313};
8314
Egbert Eich5f85f1762012-10-14 15:46:38 +02008315/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
8316struct intel_dmi_quirk {
8317 void (*hook)(struct drm_device *dev);
8318 const struct dmi_system_id (*dmi_id_list)[];
8319};
8320
8321static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
8322{
8323 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
8324 return 1;
8325}
8326
8327static const struct intel_dmi_quirk intel_dmi_quirks[] = {
8328 {
8329 .dmi_id_list = &(const struct dmi_system_id[]) {
8330 {
8331 .callback = intel_dmi_reverse_brightness,
8332 .ident = "NCR Corporation",
8333 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
8334 DMI_MATCH(DMI_PRODUCT_NAME, ""),
8335 },
8336 },
8337 { } /* terminating entry */
8338 },
8339 .hook = quirk_invert_brightness,
8340 },
8341};
8342
Ben Widawskyc43b5632012-04-16 14:07:40 -07008343static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07008344 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04008345 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07008346
Jesse Barnesb690e962010-07-19 13:53:12 -07008347 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
8348 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
8349
Jesse Barnesb690e962010-07-19 13:53:12 -07008350 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
8351 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
8352
Daniel Vetterccd0d362012-10-10 23:13:59 +02008353 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07008354 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02008355 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07008356
8357 /* Lenovo U160 cannot use SSC on LVDS */
8358 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02008359
8360 /* Sony Vaio Y cannot use SSC on LVDS */
8361 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01008362
8363 /* Acer Aspire 5734Z must invert backlight brightness */
8364 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07008365};
8366
8367static void intel_init_quirks(struct drm_device *dev)
8368{
8369 struct pci_dev *d = dev->pdev;
8370 int i;
8371
8372 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
8373 struct intel_quirk *q = &intel_quirks[i];
8374
8375 if (d->device == q->device &&
8376 (d->subsystem_vendor == q->subsystem_vendor ||
8377 q->subsystem_vendor == PCI_ANY_ID) &&
8378 (d->subsystem_device == q->subsystem_device ||
8379 q->subsystem_device == PCI_ANY_ID))
8380 q->hook(dev);
8381 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02008382 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
8383 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
8384 intel_dmi_quirks[i].hook(dev);
8385 }
Jesse Barnesb690e962010-07-19 13:53:12 -07008386}
8387
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008388/* Disable the VGA plane that we never use */
8389static void i915_disable_vga(struct drm_device *dev)
8390{
8391 struct drm_i915_private *dev_priv = dev->dev_private;
8392 u8 sr1;
8393 u32 vga_reg;
8394
8395 if (HAS_PCH_SPLIT(dev))
8396 vga_reg = CPU_VGACNTRL;
8397 else
8398 vga_reg = VGACNTRL;
8399
8400 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07008401 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008402 sr1 = inb(VGA_SR_DATA);
8403 outb(sr1 | 1<<5, VGA_SR_DATA);
8404 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
8405 udelay(300);
8406
8407 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
8408 POSTING_READ(vga_reg);
8409}
8410
Daniel Vetterf8175862012-04-10 15:50:11 +02008411void intel_modeset_init_hw(struct drm_device *dev)
8412{
Eugeni Dodonov0232e922012-07-06 15:42:36 -03008413 /* We attempt to init the necessary power wells early in the initialization
8414 * time, so the subsystems that expect power to be enabled can work.
8415 */
8416 intel_init_power_wells(dev);
8417
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03008418 intel_prepare_ddi(dev);
8419
Daniel Vetterf8175862012-04-10 15:50:11 +02008420 intel_init_clock_gating(dev);
8421
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008422 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008423 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008424 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02008425}
8426
Jesse Barnes79e53942008-11-07 14:24:08 -08008427void intel_modeset_init(struct drm_device *dev)
8428{
Jesse Barnes652c3932009-08-17 13:31:43 -07008429 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008430 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008431
8432 drm_mode_config_init(dev);
8433
8434 dev->mode_config.min_width = 0;
8435 dev->mode_config.min_height = 0;
8436
Dave Airlie019d96c2011-09-29 16:20:42 +01008437 dev->mode_config.preferred_depth = 24;
8438 dev->mode_config.prefer_shadow = 1;
8439
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02008440 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08008441
Jesse Barnesb690e962010-07-19 13:53:12 -07008442 intel_init_quirks(dev);
8443
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008444 intel_init_pm(dev);
8445
Jesse Barnese70236a2009-09-21 10:42:27 -07008446 intel_init_display(dev);
8447
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008448 if (IS_GEN2(dev)) {
8449 dev->mode_config.max_width = 2048;
8450 dev->mode_config.max_height = 2048;
8451 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07008452 dev->mode_config.max_width = 4096;
8453 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08008454 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008455 dev->mode_config.max_width = 8192;
8456 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08008457 }
Daniel Vetterdd2757f2012-06-07 15:55:57 +02008458 dev->mode_config.fb_base = dev_priv->mm.gtt_base_addr;
Jesse Barnes79e53942008-11-07 14:24:08 -08008459
Zhao Yakui28c97732009-10-09 11:39:41 +08008460 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10008461 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08008462
Dave Airliea3524f12010-06-06 18:59:41 +10008463 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008464 intel_crtc_init(dev, i);
Jesse Barnes00c2064b2012-01-13 15:48:39 -08008465 ret = intel_plane_init(dev, i);
8466 if (ret)
8467 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08008468 }
8469
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008470 intel_cpu_pll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008471 intel_pch_pll_init(dev);
8472
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008473 /* Just disable it once at startup */
8474 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008475 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00008476
8477 /* Just in case the BIOS is doing something questionable. */
8478 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01008479}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008480
Daniel Vetter24929352012-07-02 20:28:59 +02008481static void
8482intel_connector_break_all_links(struct intel_connector *connector)
8483{
8484 connector->base.dpms = DRM_MODE_DPMS_OFF;
8485 connector->base.encoder = NULL;
8486 connector->encoder->connectors_active = false;
8487 connector->encoder->base.crtc = NULL;
8488}
8489
Daniel Vetter7fad7982012-07-04 17:51:47 +02008490static void intel_enable_pipe_a(struct drm_device *dev)
8491{
8492 struct intel_connector *connector;
8493 struct drm_connector *crt = NULL;
8494 struct intel_load_detect_pipe load_detect_temp;
8495
8496 /* We can't just switch on the pipe A, we need to set things up with a
8497 * proper mode and output configuration. As a gross hack, enable pipe A
8498 * by enabling the load detect pipe once. */
8499 list_for_each_entry(connector,
8500 &dev->mode_config.connector_list,
8501 base.head) {
8502 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
8503 crt = &connector->base;
8504 break;
8505 }
8506 }
8507
8508 if (!crt)
8509 return;
8510
8511 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
8512 intel_release_load_detect_pipe(crt, &load_detect_temp);
8513
8514
8515}
8516
Daniel Vetterfa555832012-10-10 23:14:00 +02008517static bool
8518intel_check_plane_mapping(struct intel_crtc *crtc)
8519{
8520 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8521 u32 reg, val;
8522
8523 if (dev_priv->num_pipe == 1)
8524 return true;
8525
8526 reg = DSPCNTR(!crtc->plane);
8527 val = I915_READ(reg);
8528
8529 if ((val & DISPLAY_PLANE_ENABLE) &&
8530 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
8531 return false;
8532
8533 return true;
8534}
8535
Daniel Vetter24929352012-07-02 20:28:59 +02008536static void intel_sanitize_crtc(struct intel_crtc *crtc)
8537{
8538 struct drm_device *dev = crtc->base.dev;
8539 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02008540 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02008541
Daniel Vetter24929352012-07-02 20:28:59 +02008542 /* Clear any frame start delays used for debugging left by the BIOS */
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008543 reg = PIPECONF(crtc->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02008544 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
8545
8546 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02008547 * disable the crtc (and hence change the state) if it is wrong. Note
8548 * that gen4+ has a fixed plane -> pipe mapping. */
8549 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02008550 struct intel_connector *connector;
8551 bool plane;
8552
Daniel Vetter24929352012-07-02 20:28:59 +02008553 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
8554 crtc->base.base.id);
8555
8556 /* Pipe has the wrong plane attached and the plane is active.
8557 * Temporarily change the plane mapping and disable everything
8558 * ... */
8559 plane = crtc->plane;
8560 crtc->plane = !plane;
8561 dev_priv->display.crtc_disable(&crtc->base);
8562 crtc->plane = plane;
8563
8564 /* ... and break all links. */
8565 list_for_each_entry(connector, &dev->mode_config.connector_list,
8566 base.head) {
8567 if (connector->encoder->base.crtc != &crtc->base)
8568 continue;
8569
8570 intel_connector_break_all_links(connector);
8571 }
8572
8573 WARN_ON(crtc->active);
8574 crtc->base.enabled = false;
8575 }
Daniel Vetter24929352012-07-02 20:28:59 +02008576
Daniel Vetter7fad7982012-07-04 17:51:47 +02008577 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
8578 crtc->pipe == PIPE_A && !crtc->active) {
8579 /* BIOS forgot to enable pipe A, this mostly happens after
8580 * resume. Force-enable the pipe to fix this, the update_dpms
8581 * call below we restore the pipe to the right state, but leave
8582 * the required bits on. */
8583 intel_enable_pipe_a(dev);
8584 }
8585
Daniel Vetter24929352012-07-02 20:28:59 +02008586 /* Adjust the state of the output pipe according to whether we
8587 * have active connectors/encoders. */
8588 intel_crtc_update_dpms(&crtc->base);
8589
8590 if (crtc->active != crtc->base.enabled) {
8591 struct intel_encoder *encoder;
8592
8593 /* This can happen either due to bugs in the get_hw_state
8594 * functions or because the pipe is force-enabled due to the
8595 * pipe A quirk. */
8596 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
8597 crtc->base.base.id,
8598 crtc->base.enabled ? "enabled" : "disabled",
8599 crtc->active ? "enabled" : "disabled");
8600
8601 crtc->base.enabled = crtc->active;
8602
8603 /* Because we only establish the connector -> encoder ->
8604 * crtc links if something is active, this means the
8605 * crtc is now deactivated. Break the links. connector
8606 * -> encoder links are only establish when things are
8607 * actually up, hence no need to break them. */
8608 WARN_ON(crtc->active);
8609
8610 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
8611 WARN_ON(encoder->connectors_active);
8612 encoder->base.crtc = NULL;
8613 }
8614 }
8615}
8616
8617static void intel_sanitize_encoder(struct intel_encoder *encoder)
8618{
8619 struct intel_connector *connector;
8620 struct drm_device *dev = encoder->base.dev;
8621
8622 /* We need to check both for a crtc link (meaning that the
8623 * encoder is active and trying to read from a pipe) and the
8624 * pipe itself being active. */
8625 bool has_active_crtc = encoder->base.crtc &&
8626 to_intel_crtc(encoder->base.crtc)->active;
8627
8628 if (encoder->connectors_active && !has_active_crtc) {
8629 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
8630 encoder->base.base.id,
8631 drm_get_encoder_name(&encoder->base));
8632
8633 /* Connector is active, but has no active pipe. This is
8634 * fallout from our resume register restoring. Disable
8635 * the encoder manually again. */
8636 if (encoder->base.crtc) {
8637 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
8638 encoder->base.base.id,
8639 drm_get_encoder_name(&encoder->base));
8640 encoder->disable(encoder);
8641 }
8642
8643 /* Inconsistent output/port/pipe state happens presumably due to
8644 * a bug in one of the get_hw_state functions. Or someplace else
8645 * in our code, like the register restore mess on resume. Clamp
8646 * things to off as a safer default. */
8647 list_for_each_entry(connector,
8648 &dev->mode_config.connector_list,
8649 base.head) {
8650 if (connector->encoder != encoder)
8651 continue;
8652
8653 intel_connector_break_all_links(connector);
8654 }
8655 }
8656 /* Enabled encoders without active connectors will be fixed in
8657 * the crtc fixup. */
8658}
8659
8660/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
8661 * and i915 state tracking structures. */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01008662void intel_modeset_setup_hw_state(struct drm_device *dev,
8663 bool force_restore)
Daniel Vetter24929352012-07-02 20:28:59 +02008664{
8665 struct drm_i915_private *dev_priv = dev->dev_private;
8666 enum pipe pipe;
8667 u32 tmp;
8668 struct intel_crtc *crtc;
8669 struct intel_encoder *encoder;
8670 struct intel_connector *connector;
8671
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008672 if (HAS_DDI(dev)) {
Paulo Zanonie28d54c2012-10-24 16:09:25 -02008673 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8674
8675 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8676 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8677 case TRANS_DDI_EDP_INPUT_A_ON:
8678 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8679 pipe = PIPE_A;
8680 break;
8681 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8682 pipe = PIPE_B;
8683 break;
8684 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8685 pipe = PIPE_C;
8686 break;
8687 }
8688
8689 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8690 crtc->cpu_transcoder = TRANSCODER_EDP;
8691
8692 DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
8693 pipe_name(pipe));
8694 }
8695 }
8696
Daniel Vetter24929352012-07-02 20:28:59 +02008697 for_each_pipe(pipe) {
8698 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8699
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008700 tmp = I915_READ(PIPECONF(crtc->cpu_transcoder));
Daniel Vetter24929352012-07-02 20:28:59 +02008701 if (tmp & PIPECONF_ENABLE)
8702 crtc->active = true;
8703 else
8704 crtc->active = false;
8705
8706 crtc->base.enabled = crtc->active;
8707
8708 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
8709 crtc->base.base.id,
8710 crtc->active ? "enabled" : "disabled");
8711 }
8712
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008713 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008714 intel_ddi_setup_hw_pll_state(dev);
8715
Daniel Vetter24929352012-07-02 20:28:59 +02008716 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8717 base.head) {
8718 pipe = 0;
8719
8720 if (encoder->get_hw_state(encoder, &pipe)) {
8721 encoder->base.crtc =
8722 dev_priv->pipe_to_crtc_mapping[pipe];
8723 } else {
8724 encoder->base.crtc = NULL;
8725 }
8726
8727 encoder->connectors_active = false;
8728 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
8729 encoder->base.base.id,
8730 drm_get_encoder_name(&encoder->base),
8731 encoder->base.crtc ? "enabled" : "disabled",
8732 pipe);
8733 }
8734
8735 list_for_each_entry(connector, &dev->mode_config.connector_list,
8736 base.head) {
8737 if (connector->get_hw_state(connector)) {
8738 connector->base.dpms = DRM_MODE_DPMS_ON;
8739 connector->encoder->connectors_active = true;
8740 connector->base.encoder = &connector->encoder->base;
8741 } else {
8742 connector->base.dpms = DRM_MODE_DPMS_OFF;
8743 connector->base.encoder = NULL;
8744 }
8745 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
8746 connector->base.base.id,
8747 drm_get_connector_name(&connector->base),
8748 connector->base.encoder ? "enabled" : "disabled");
8749 }
8750
8751 /* HW state is read out, now we need to sanitize this mess. */
8752 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8753 base.head) {
8754 intel_sanitize_encoder(encoder);
8755 }
8756
8757 for_each_pipe(pipe) {
8758 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8759 intel_sanitize_crtc(crtc);
8760 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008761
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01008762 if (force_restore) {
8763 for_each_pipe(pipe) {
8764 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8765 intel_set_mode(&crtc->base, &crtc->base.mode,
8766 crtc->base.x, crtc->base.y, crtc->base.fb);
8767 }
8768 } else {
8769 intel_modeset_update_staged_output_state(dev);
8770 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008771
8772 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +02008773
8774 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01008775}
8776
8777void intel_modeset_gem_init(struct drm_device *dev)
8778{
Chris Wilson1833b132012-05-09 11:56:28 +01008779 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02008780
8781 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +02008782
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01008783 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08008784}
8785
8786void intel_modeset_cleanup(struct drm_device *dev)
8787{
Jesse Barnes652c3932009-08-17 13:31:43 -07008788 struct drm_i915_private *dev_priv = dev->dev_private;
8789 struct drm_crtc *crtc;
8790 struct intel_crtc *intel_crtc;
8791
Keith Packardf87ea762010-10-03 19:36:26 -07008792 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07008793 mutex_lock(&dev->struct_mutex);
8794
Jesse Barnes723bfd72010-10-07 16:01:13 -07008795 intel_unregister_dsm_handler();
8796
8797
Jesse Barnes652c3932009-08-17 13:31:43 -07008798 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8799 /* Skip inactive CRTCs */
8800 if (!crtc->fb)
8801 continue;
8802
8803 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02008804 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008805 }
8806
Chris Wilson973d04f2011-07-08 12:22:37 +01008807 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07008808
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008809 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00008810
Daniel Vetter930ebb42012-06-29 23:32:16 +02008811 ironlake_teardown_rc6(dev);
8812
Jesse Barnes57f350b2012-03-28 13:39:25 -07008813 if (IS_VALLEYVIEW(dev))
8814 vlv_init_dpio(dev);
8815
Kristian Høgsberg69341a52009-11-11 12:19:17 -05008816 mutex_unlock(&dev->struct_mutex);
8817
Daniel Vetter6c0d93502010-08-20 18:26:46 +02008818 /* Disable the irq before mode object teardown, for the irq might
8819 * enqueue unpin/hotplug work. */
8820 drm_irq_uninstall(dev);
8821 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02008822 cancel_work_sync(&dev_priv->rps.work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02008823
Chris Wilson1630fe72011-07-08 12:22:42 +01008824 /* flush any delayed tasks or pending work */
8825 flush_scheduled_work();
8826
Jesse Barnes79e53942008-11-07 14:24:08 -08008827 drm_mode_config_cleanup(dev);
8828}
8829
Dave Airlie28d52042009-09-21 14:33:58 +10008830/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08008831 * Return which encoder is currently attached for connector.
8832 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01008833struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08008834{
Chris Wilsondf0e9242010-09-09 16:20:55 +01008835 return &intel_attached_encoder(connector)->base;
8836}
Jesse Barnes79e53942008-11-07 14:24:08 -08008837
Chris Wilsondf0e9242010-09-09 16:20:55 +01008838void intel_connector_attach_encoder(struct intel_connector *connector,
8839 struct intel_encoder *encoder)
8840{
8841 connector->encoder = encoder;
8842 drm_mode_connector_attach_encoder(&connector->base,
8843 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008844}
Dave Airlie28d52042009-09-21 14:33:58 +10008845
8846/*
8847 * set vga decode state - true == enable VGA decode
8848 */
8849int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
8850{
8851 struct drm_i915_private *dev_priv = dev->dev_private;
8852 u16 gmch_ctrl;
8853
8854 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
8855 if (state)
8856 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
8857 else
8858 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
8859 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
8860 return 0;
8861}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008862
8863#ifdef CONFIG_DEBUG_FS
8864#include <linux/seq_file.h>
8865
8866struct intel_display_error_state {
8867 struct intel_cursor_error_state {
8868 u32 control;
8869 u32 position;
8870 u32 base;
8871 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +01008872 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008873
8874 struct intel_pipe_error_state {
8875 u32 conf;
8876 u32 source;
8877
8878 u32 htotal;
8879 u32 hblank;
8880 u32 hsync;
8881 u32 vtotal;
8882 u32 vblank;
8883 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +01008884 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008885
8886 struct intel_plane_error_state {
8887 u32 control;
8888 u32 stride;
8889 u32 size;
8890 u32 pos;
8891 u32 addr;
8892 u32 surface;
8893 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +01008894 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008895};
8896
8897struct intel_display_error_state *
8898intel_display_capture_error_state(struct drm_device *dev)
8899{
Akshay Joshi0206e352011-08-16 15:34:10 -04008900 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008901 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008902 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008903 int i;
8904
8905 error = kmalloc(sizeof(*error), GFP_ATOMIC);
8906 if (error == NULL)
8907 return NULL;
8908
Damien Lespiau52331302012-08-15 19:23:25 +01008909 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008910 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
8911
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008912 error->cursor[i].control = I915_READ(CURCNTR(i));
8913 error->cursor[i].position = I915_READ(CURPOS(i));
8914 error->cursor[i].base = I915_READ(CURBASE(i));
8915
8916 error->plane[i].control = I915_READ(DSPCNTR(i));
8917 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
8918 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04008919 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008920 error->plane[i].addr = I915_READ(DSPADDR(i));
8921 if (INTEL_INFO(dev)->gen >= 4) {
8922 error->plane[i].surface = I915_READ(DSPSURF(i));
8923 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
8924 }
8925
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008926 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008927 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008928 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
8929 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
8930 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
8931 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
8932 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
8933 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008934 }
8935
8936 return error;
8937}
8938
8939void
8940intel_display_print_error_state(struct seq_file *m,
8941 struct drm_device *dev,
8942 struct intel_display_error_state *error)
8943{
Damien Lespiau52331302012-08-15 19:23:25 +01008944 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008945 int i;
8946
Damien Lespiau52331302012-08-15 19:23:25 +01008947 seq_printf(m, "Num Pipes: %d\n", dev_priv->num_pipe);
8948 for_each_pipe(i) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008949 seq_printf(m, "Pipe [%d]:\n", i);
8950 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
8951 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
8952 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
8953 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
8954 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
8955 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
8956 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
8957 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
8958
8959 seq_printf(m, "Plane [%d]:\n", i);
8960 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
8961 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
8962 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
8963 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
8964 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
8965 if (INTEL_INFO(dev)->gen >= 4) {
8966 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
8967 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
8968 }
8969
8970 seq_printf(m, "Cursor [%d]:\n", i);
8971 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
8972 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
8973 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
8974 }
8975}
8976#endif