blob: 6adeabda258724a5fd842718e7f03938fa769f48 [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 Barnesa0c4da22012-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 Barnesa0c4da22012-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 Barnesa0c4da22012-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 Barnesa0c4da22012-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 Barnesa0c4da22012-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 Barnesa0c4da22012-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 Barnesa0c4da22012-06-15 11:55:13 -0700441
Jesse Barnesa0c4da22012-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 Barnesa0c4da22012-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 Barnesa0c4da22012-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 Barnesa0c4da22012-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 Vetter6c2b7c122012-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 Barnesa0c4da22012-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 Barnesa0c4da22012-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
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000988/*
989 * ibx_digital_port_connected - is the specified port connected?
990 * @dev_priv: i915 private structure
991 * @port: the port to test
992 *
993 * Returns true if @port is connected, false otherwise.
994 */
995bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
996 struct intel_digital_port *port)
997{
998 u32 bit;
999
1000 /* XXX: IBX has different SDEISR bits */
1001 if (HAS_PCH_IBX(dev_priv->dev))
1002 return true;
1003
1004 switch(port->port) {
1005 case PORT_B:
1006 bit = SDE_PORTB_HOTPLUG_CPT;
1007 break;
1008 case PORT_C:
1009 bit = SDE_PORTC_HOTPLUG_CPT;
1010 break;
1011 case PORT_D:
1012 bit = SDE_PORTD_HOTPLUG_CPT;
1013 break;
1014 default:
1015 return true;
1016 }
1017
1018 return I915_READ(SDEISR) & bit;
1019}
1020
Jesse Barnesb24e7172011-01-04 15:09:30 -08001021static const char *state_string(bool enabled)
1022{
1023 return enabled ? "on" : "off";
1024}
1025
1026/* Only for pre-ILK configs */
1027static void assert_pll(struct drm_i915_private *dev_priv,
1028 enum pipe pipe, bool state)
1029{
1030 int reg;
1031 u32 val;
1032 bool cur_state;
1033
1034 reg = DPLL(pipe);
1035 val = I915_READ(reg);
1036 cur_state = !!(val & DPLL_VCO_ENABLE);
1037 WARN(cur_state != state,
1038 "PLL state assertion failure (expected %s, current %s)\n",
1039 state_string(state), state_string(cur_state));
1040}
1041#define assert_pll_enabled(d, p) assert_pll(d, p, true)
1042#define assert_pll_disabled(d, p) assert_pll(d, p, false)
1043
Jesse Barnes040484a2011-01-03 12:14:26 -08001044/* For ILK+ */
1045static void assert_pch_pll(struct drm_i915_private *dev_priv,
Chris Wilson92b27b02012-05-20 18:10:50 +01001046 struct intel_pch_pll *pll,
1047 struct intel_crtc *crtc,
1048 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001049{
Jesse Barnes040484a2011-01-03 12:14:26 -08001050 u32 val;
1051 bool cur_state;
1052
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001053 if (HAS_PCH_LPT(dev_priv->dev)) {
1054 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1055 return;
1056 }
1057
Chris Wilson92b27b02012-05-20 18:10:50 +01001058 if (WARN (!pll,
1059 "asserting PCH PLL %s with no PLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001060 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001061
Chris Wilson92b27b02012-05-20 18:10:50 +01001062 val = I915_READ(pll->pll_reg);
1063 cur_state = !!(val & DPLL_VCO_ENABLE);
1064 WARN(cur_state != state,
1065 "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
1066 pll->pll_reg, state_string(state), state_string(cur_state), val);
1067
1068 /* Make sure the selected PLL is correctly attached to the transcoder */
1069 if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001070 u32 pch_dpll;
1071
1072 pch_dpll = I915_READ(PCH_DPLL_SEL);
Chris Wilson92b27b02012-05-20 18:10:50 +01001073 cur_state = pll->pll_reg == _PCH_DPLL_B;
1074 if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
1075 "PLL[%d] not attached to this transcoder %d: %08x\n",
1076 cur_state, crtc->pipe, pch_dpll)) {
1077 cur_state = !!(val >> (4*crtc->pipe + 3));
1078 WARN(cur_state != state,
1079 "PLL[%d] not %s on this transcoder %d: %08x\n",
1080 pll->pll_reg == _PCH_DPLL_B,
1081 state_string(state),
1082 crtc->pipe,
1083 val);
1084 }
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001085 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001086}
Chris Wilson92b27b02012-05-20 18:10:50 +01001087#define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
1088#define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
Jesse Barnes040484a2011-01-03 12:14:26 -08001089
1090static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1091 enum pipe pipe, bool state)
1092{
1093 int reg;
1094 u32 val;
1095 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001096 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1097 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001098
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001099 if (HAS_DDI(dev_priv->dev)) {
1100 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001101 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001102 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001103 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001104 } else {
1105 reg = FDI_TX_CTL(pipe);
1106 val = I915_READ(reg);
1107 cur_state = !!(val & FDI_TX_ENABLE);
1108 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001109 WARN(cur_state != state,
1110 "FDI TX state assertion failure (expected %s, current %s)\n",
1111 state_string(state), state_string(cur_state));
1112}
1113#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1114#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1115
1116static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1117 enum pipe pipe, bool state)
1118{
1119 int reg;
1120 u32 val;
1121 bool cur_state;
1122
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001123 reg = FDI_RX_CTL(pipe);
1124 val = I915_READ(reg);
1125 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001126 WARN(cur_state != state,
1127 "FDI RX state assertion failure (expected %s, current %s)\n",
1128 state_string(state), state_string(cur_state));
1129}
1130#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1131#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1132
1133static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1134 enum pipe pipe)
1135{
1136 int reg;
1137 u32 val;
1138
1139 /* ILK FDI PLL is always enabled */
1140 if (dev_priv->info->gen == 5)
1141 return;
1142
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001143 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001144 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001145 return;
1146
Jesse Barnes040484a2011-01-03 12:14:26 -08001147 reg = FDI_TX_CTL(pipe);
1148 val = I915_READ(reg);
1149 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1150}
1151
1152static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1153 enum pipe pipe)
1154{
1155 int reg;
1156 u32 val;
1157
1158 reg = FDI_RX_CTL(pipe);
1159 val = I915_READ(reg);
1160 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1161}
1162
Jesse Barnesea0760c2011-01-04 15:09:32 -08001163static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1164 enum pipe pipe)
1165{
1166 int pp_reg, lvds_reg;
1167 u32 val;
1168 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001169 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001170
1171 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1172 pp_reg = PCH_PP_CONTROL;
1173 lvds_reg = PCH_LVDS;
1174 } else {
1175 pp_reg = PP_CONTROL;
1176 lvds_reg = LVDS;
1177 }
1178
1179 val = I915_READ(pp_reg);
1180 if (!(val & PANEL_POWER_ON) ||
1181 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1182 locked = false;
1183
1184 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1185 panel_pipe = PIPE_B;
1186
1187 WARN(panel_pipe == pipe && locked,
1188 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001189 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001190}
1191
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001192void assert_pipe(struct drm_i915_private *dev_priv,
1193 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001194{
1195 int reg;
1196 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001197 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001198 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1199 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001200
Daniel Vetter8e636782012-01-22 01:36:48 +01001201 /* if we need the pipe A quirk it must be always on */
1202 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1203 state = true;
1204
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001205 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001206 val = I915_READ(reg);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001207 cur_state = !!(val & PIPECONF_ENABLE);
1208 WARN(cur_state != state,
1209 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001210 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001211}
1212
Chris Wilson931872f2012-01-16 23:01:13 +00001213static void assert_plane(struct drm_i915_private *dev_priv,
1214 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001215{
1216 int reg;
1217 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001218 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001219
1220 reg = DSPCNTR(plane);
1221 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001222 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1223 WARN(cur_state != state,
1224 "plane %c assertion failure (expected %s, current %s)\n",
1225 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001226}
1227
Chris Wilson931872f2012-01-16 23:01:13 +00001228#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1229#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1230
Jesse Barnesb24e7172011-01-04 15:09:30 -08001231static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1232 enum pipe pipe)
1233{
1234 int reg, i;
1235 u32 val;
1236 int cur_pipe;
1237
Jesse Barnes19ec1352011-02-02 12:28:02 -08001238 /* Planes are fixed to pipes on ILK+ */
Adam Jackson28c057942011-10-07 14:38:42 -04001239 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1240 reg = DSPCNTR(pipe);
1241 val = I915_READ(reg);
1242 WARN((val & DISPLAY_PLANE_ENABLE),
1243 "plane %c assertion failure, should be disabled but not\n",
1244 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001245 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001246 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001247
Jesse Barnesb24e7172011-01-04 15:09:30 -08001248 /* Need to check both planes against the pipe */
1249 for (i = 0; i < 2; i++) {
1250 reg = DSPCNTR(i);
1251 val = I915_READ(reg);
1252 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1253 DISPPLANE_SEL_PIPE_SHIFT;
1254 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001255 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1256 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001257 }
1258}
1259
Jesse Barnes92f25842011-01-04 15:09:34 -08001260static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1261{
1262 u32 val;
1263 bool enabled;
1264
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001265 if (HAS_PCH_LPT(dev_priv->dev)) {
1266 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1267 return;
1268 }
1269
Jesse Barnes92f25842011-01-04 15:09:34 -08001270 val = I915_READ(PCH_DREF_CONTROL);
1271 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1272 DREF_SUPERSPREAD_SOURCE_MASK));
1273 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1274}
1275
1276static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1277 enum pipe pipe)
1278{
1279 int reg;
1280 u32 val;
1281 bool enabled;
1282
1283 reg = TRANSCONF(pipe);
1284 val = I915_READ(reg);
1285 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001286 WARN(enabled,
1287 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1288 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001289}
1290
Keith Packard4e634382011-08-06 10:39:45 -07001291static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1292 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001293{
1294 if ((val & DP_PORT_EN) == 0)
1295 return false;
1296
1297 if (HAS_PCH_CPT(dev_priv->dev)) {
1298 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1299 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1300 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1301 return false;
1302 } else {
1303 if ((val & DP_PIPE_MASK) != (pipe << 30))
1304 return false;
1305 }
1306 return true;
1307}
1308
Keith Packard1519b992011-08-06 10:35:34 -07001309static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1310 enum pipe pipe, u32 val)
1311{
1312 if ((val & PORT_ENABLE) == 0)
1313 return false;
1314
1315 if (HAS_PCH_CPT(dev_priv->dev)) {
1316 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1317 return false;
1318 } else {
1319 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1320 return false;
1321 }
1322 return true;
1323}
1324
1325static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1326 enum pipe pipe, u32 val)
1327{
1328 if ((val & LVDS_PORT_EN) == 0)
1329 return false;
1330
1331 if (HAS_PCH_CPT(dev_priv->dev)) {
1332 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1333 return false;
1334 } else {
1335 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1336 return false;
1337 }
1338 return true;
1339}
1340
1341static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1342 enum pipe pipe, u32 val)
1343{
1344 if ((val & ADPA_DAC_ENABLE) == 0)
1345 return false;
1346 if (HAS_PCH_CPT(dev_priv->dev)) {
1347 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1348 return false;
1349 } else {
1350 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1351 return false;
1352 }
1353 return true;
1354}
1355
Jesse Barnes291906f2011-02-02 12:28:03 -08001356static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001357 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001358{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001359 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001360 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001361 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001362 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001363
Daniel Vetter75c5da22012-09-10 21:58:29 +02001364 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1365 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001366 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001367}
1368
1369static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1370 enum pipe pipe, int reg)
1371{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001372 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001373 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001374 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001375 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001376
Daniel Vetter75c5da22012-09-10 21:58:29 +02001377 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & PORT_ENABLE) == 0
1378 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001379 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001380}
1381
1382static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1383 enum pipe pipe)
1384{
1385 int reg;
1386 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001387
Keith Packardf0575e92011-07-25 22:12:43 -07001388 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1389 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1390 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001391
1392 reg = PCH_ADPA;
1393 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001394 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001395 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001396 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001397
1398 reg = PCH_LVDS;
1399 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001400 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001401 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001402 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001403
1404 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1405 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1406 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1407}
1408
Jesse Barnesb24e7172011-01-04 15:09:30 -08001409/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001410 * intel_enable_pll - enable a PLL
1411 * @dev_priv: i915 private structure
1412 * @pipe: pipe PLL to enable
1413 *
1414 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1415 * make sure the PLL reg is writable first though, since the panel write
1416 * protect mechanism may be enabled.
1417 *
1418 * Note! This is for pre-ILK only.
Thomas Richter7434a252012-07-18 19:22:30 +02001419 *
1420 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001421 */
1422static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1423{
1424 int reg;
1425 u32 val;
1426
1427 /* No really, not for ILK+ */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07001428 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001429
1430 /* PLL is protected by panel, make sure we can write it */
1431 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1432 assert_panel_unlocked(dev_priv, pipe);
1433
1434 reg = DPLL(pipe);
1435 val = I915_READ(reg);
1436 val |= DPLL_VCO_ENABLE;
1437
1438 /* We do this three times for luck */
1439 I915_WRITE(reg, val);
1440 POSTING_READ(reg);
1441 udelay(150); /* wait for warmup */
1442 I915_WRITE(reg, val);
1443 POSTING_READ(reg);
1444 udelay(150); /* wait for warmup */
1445 I915_WRITE(reg, val);
1446 POSTING_READ(reg);
1447 udelay(150); /* wait for warmup */
1448}
1449
1450/**
1451 * intel_disable_pll - disable a PLL
1452 * @dev_priv: i915 private structure
1453 * @pipe: pipe PLL to disable
1454 *
1455 * Disable the PLL for @pipe, making sure the pipe is off first.
1456 *
1457 * Note! This is for pre-ILK only.
1458 */
1459static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1460{
1461 int reg;
1462 u32 val;
1463
1464 /* Don't disable pipe A or pipe A PLLs if needed */
1465 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1466 return;
1467
1468 /* Make sure the pipe isn't still relying on us */
1469 assert_pipe_disabled(dev_priv, pipe);
1470
1471 reg = DPLL(pipe);
1472 val = I915_READ(reg);
1473 val &= ~DPLL_VCO_ENABLE;
1474 I915_WRITE(reg, val);
1475 POSTING_READ(reg);
1476}
1477
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001478/* SBI access */
1479static void
1480intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value)
1481{
Daniel Vetter09153002012-12-12 14:06:44 +01001482 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001483
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001484 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001485 100)) {
1486 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001487 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001488 }
1489
1490 I915_WRITE(SBI_ADDR,
1491 (reg << 16));
1492 I915_WRITE(SBI_DATA,
1493 value);
1494 I915_WRITE(SBI_CTL_STAT,
1495 SBI_BUSY |
1496 SBI_CTL_OP_CRWR);
1497
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001498 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001499 100)) {
1500 DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001501 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001502 }
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001503}
1504
1505static u32
1506intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg)
1507{
Daniel Vetter09153002012-12-12 14:06:44 +01001508 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001509
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001510 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001511 100)) {
1512 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001513 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001514 }
1515
1516 I915_WRITE(SBI_ADDR,
1517 (reg << 16));
1518 I915_WRITE(SBI_CTL_STAT,
1519 SBI_BUSY |
1520 SBI_CTL_OP_CRRD);
1521
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001522 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001523 100)) {
1524 DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001525 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001526 }
1527
Daniel Vetter09153002012-12-12 14:06:44 +01001528 return I915_READ(SBI_DATA);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001529}
1530
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001531/**
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001532 * ironlake_enable_pch_pll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001533 * @dev_priv: i915 private structure
1534 * @pipe: pipe PLL to enable
1535 *
1536 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1537 * drives the transcoder clock.
1538 */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001539static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001540{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001541 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Chris Wilson48da64a2012-05-13 20:16:12 +01001542 struct intel_pch_pll *pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001543 int reg;
1544 u32 val;
1545
Chris Wilson48da64a2012-05-13 20:16:12 +01001546 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001547 BUG_ON(dev_priv->info->gen < 5);
Chris Wilson48da64a2012-05-13 20:16:12 +01001548 pll = intel_crtc->pch_pll;
1549 if (pll == NULL)
1550 return;
1551
1552 if (WARN_ON(pll->refcount == 0))
1553 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001554
1555 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1556 pll->pll_reg, pll->active, pll->on,
1557 intel_crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001558
1559 /* PCH refclock must be enabled first */
1560 assert_pch_refclk_enabled(dev_priv);
1561
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001562 if (pll->active++ && pll->on) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001563 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001564 return;
1565 }
1566
1567 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1568
1569 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001570 val = I915_READ(reg);
1571 val |= DPLL_VCO_ENABLE;
1572 I915_WRITE(reg, val);
1573 POSTING_READ(reg);
1574 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001575
1576 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001577}
1578
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001579static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001580{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001581 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1582 struct intel_pch_pll *pll = intel_crtc->pch_pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001583 int reg;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001584 u32 val;
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001585
Jesse Barnes92f25842011-01-04 15:09:34 -08001586 /* PCH only available on ILK+ */
1587 BUG_ON(dev_priv->info->gen < 5);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001588 if (pll == NULL)
1589 return;
1590
Chris Wilson48da64a2012-05-13 20:16:12 +01001591 if (WARN_ON(pll->refcount == 0))
1592 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001593
1594 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1595 pll->pll_reg, pll->active, pll->on,
1596 intel_crtc->base.base.id);
1597
Chris Wilson48da64a2012-05-13 20:16:12 +01001598 if (WARN_ON(pll->active == 0)) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001599 assert_pch_pll_disabled(dev_priv, pll, NULL);
Chris Wilson48da64a2012-05-13 20:16:12 +01001600 return;
1601 }
1602
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001603 if (--pll->active) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001604 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001605 return;
1606 }
1607
1608 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
Jesse Barnes92f25842011-01-04 15:09:34 -08001609
1610 /* Make sure transcoder isn't still depending on us */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001611 assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001612
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001613 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001614 val = I915_READ(reg);
1615 val &= ~DPLL_VCO_ENABLE;
1616 I915_WRITE(reg, val);
1617 POSTING_READ(reg);
1618 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001619
1620 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001621}
1622
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001623static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1624 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001625{
Daniel Vetter23670b322012-11-01 09:15:30 +01001626 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001627 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetter23670b322012-11-01 09:15:30 +01001628 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001629
1630 /* PCH only available on ILK+ */
1631 BUG_ON(dev_priv->info->gen < 5);
1632
1633 /* Make sure PCH DPLL is enabled */
Chris Wilson92b27b02012-05-20 18:10:50 +01001634 assert_pch_pll_enabled(dev_priv,
1635 to_intel_crtc(crtc)->pch_pll,
1636 to_intel_crtc(crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001637
1638 /* FDI must be feeding us bits for PCH ports */
1639 assert_fdi_tx_enabled(dev_priv, pipe);
1640 assert_fdi_rx_enabled(dev_priv, pipe);
1641
Daniel Vetter23670b322012-11-01 09:15:30 +01001642 if (HAS_PCH_CPT(dev)) {
1643 /* Workaround: Set the timing override bit before enabling the
1644 * pch transcoder. */
1645 reg = TRANS_CHICKEN2(pipe);
1646 val = I915_READ(reg);
1647 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1648 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001649 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001650
Jesse Barnes040484a2011-01-03 12:14:26 -08001651 reg = TRANSCONF(pipe);
1652 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001653 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001654
1655 if (HAS_PCH_IBX(dev_priv->dev)) {
1656 /*
1657 * make the BPC in transcoder be consistent with
1658 * that in pipeconf reg.
1659 */
1660 val &= ~PIPE_BPC_MASK;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001661 val |= pipeconf_val & PIPE_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001662 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001663
1664 val &= ~TRANS_INTERLACE_MASK;
1665 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001666 if (HAS_PCH_IBX(dev_priv->dev) &&
1667 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1668 val |= TRANS_LEGACY_INTERLACED_ILK;
1669 else
1670 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001671 else
1672 val |= TRANS_PROGRESSIVE;
1673
Jesse Barnes040484a2011-01-03 12:14:26 -08001674 I915_WRITE(reg, val | TRANS_ENABLE);
1675 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1676 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1677}
1678
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001679static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001680 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001681{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001682 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001683
1684 /* PCH only available on ILK+ */
1685 BUG_ON(dev_priv->info->gen < 5);
1686
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001687 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001688 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001689 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001690
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001691 /* Workaround: set timing override bit. */
1692 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001693 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001694 I915_WRITE(_TRANSA_CHICKEN2, val);
1695
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001696 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001697 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001698
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001699 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1700 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001701 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001702 else
1703 val |= TRANS_PROGRESSIVE;
1704
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001705 I915_WRITE(TRANSCONF(TRANSCODER_A), val);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001706 if (wait_for(I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE, 100))
1707 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001708}
1709
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001710static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1711 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001712{
Daniel Vetter23670b322012-11-01 09:15:30 +01001713 struct drm_device *dev = dev_priv->dev;
1714 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001715
1716 /* FDI relies on the transcoder */
1717 assert_fdi_tx_disabled(dev_priv, pipe);
1718 assert_fdi_rx_disabled(dev_priv, pipe);
1719
Jesse Barnes291906f2011-02-02 12:28:03 -08001720 /* Ports must be off as well */
1721 assert_pch_ports_disabled(dev_priv, pipe);
1722
Jesse Barnes040484a2011-01-03 12:14:26 -08001723 reg = TRANSCONF(pipe);
1724 val = I915_READ(reg);
1725 val &= ~TRANS_ENABLE;
1726 I915_WRITE(reg, val);
1727 /* wait for PCH transcoder off, transcoder state */
1728 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes4c9c18c2011-10-13 09:46:32 -07001729 DRM_ERROR("failed to disable transcoder %d\n", pipe);
Daniel Vetter23670b322012-11-01 09:15:30 +01001730
1731 if (!HAS_PCH_IBX(dev)) {
1732 /* Workaround: Clear the timing override chicken bit again. */
1733 reg = TRANS_CHICKEN2(pipe);
1734 val = I915_READ(reg);
1735 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1736 I915_WRITE(reg, val);
1737 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001738}
1739
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001740static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001741{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001742 u32 val;
1743
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001744 val = I915_READ(_TRANSACONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001745 val &= ~TRANS_ENABLE;
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001746 I915_WRITE(_TRANSACONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001747 /* wait for PCH transcoder off, transcoder state */
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001748 if (wait_for((I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE) == 0, 50))
1749 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001750
1751 /* Workaround: clear timing override bit. */
1752 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001753 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001754 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001755}
1756
1757/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001758 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001759 * @dev_priv: i915 private structure
1760 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001761 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001762 *
1763 * Enable @pipe, making sure that various hardware specific requirements
1764 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1765 *
1766 * @pipe should be %PIPE_A or %PIPE_B.
1767 *
1768 * Will wait until the pipe is actually running (i.e. first vblank) before
1769 * returning.
1770 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001771static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1772 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001773{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001774 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1775 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001776 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001777 int reg;
1778 u32 val;
1779
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001780 if (IS_HASWELL(dev_priv->dev))
1781 pch_transcoder = TRANSCODER_A;
1782 else
1783 pch_transcoder = pipe;
1784
Jesse Barnesb24e7172011-01-04 15:09:30 -08001785 /*
1786 * A pipe without a PLL won't actually be able to drive bits from
1787 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1788 * need the check.
1789 */
1790 if (!HAS_PCH_SPLIT(dev_priv->dev))
1791 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001792 else {
1793 if (pch_port) {
1794 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001795 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001796 assert_fdi_tx_pll_enabled(dev_priv,
1797 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001798 }
1799 /* FIXME: assert CPU port conditions for SNB+ */
1800 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001801
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001802 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001803 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001804 if (val & PIPECONF_ENABLE)
1805 return;
1806
1807 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001808 intel_wait_for_vblank(dev_priv->dev, pipe);
1809}
1810
1811/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001812 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001813 * @dev_priv: i915 private structure
1814 * @pipe: pipe to disable
1815 *
1816 * Disable @pipe, making sure that various hardware specific requirements
1817 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1818 *
1819 * @pipe should be %PIPE_A or %PIPE_B.
1820 *
1821 * Will wait until the pipe has shut down before returning.
1822 */
1823static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1824 enum pipe pipe)
1825{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001826 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1827 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001828 int reg;
1829 u32 val;
1830
1831 /*
1832 * Make sure planes won't keep trying to pump pixels to us,
1833 * or we might hang the display.
1834 */
1835 assert_planes_disabled(dev_priv, pipe);
1836
1837 /* Don't disable pipe A or pipe A PLLs if needed */
1838 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1839 return;
1840
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001841 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001842 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001843 if ((val & PIPECONF_ENABLE) == 0)
1844 return;
1845
1846 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001847 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1848}
1849
Keith Packardd74362c2011-07-28 14:47:14 -07001850/*
1851 * Plane regs are double buffered, going from enabled->disabled needs a
1852 * trigger in order to latch. The display address reg provides this.
1853 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001854void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001855 enum plane plane)
1856{
Damien Lespiau14f86142012-10-29 15:24:49 +00001857 if (dev_priv->info->gen >= 4)
1858 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1859 else
1860 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001861}
1862
Jesse Barnesb24e7172011-01-04 15:09:30 -08001863/**
1864 * intel_enable_plane - enable a display plane on a given pipe
1865 * @dev_priv: i915 private structure
1866 * @plane: plane to enable
1867 * @pipe: pipe being fed
1868 *
1869 * Enable @plane on @pipe, making sure that @pipe is running first.
1870 */
1871static void intel_enable_plane(struct drm_i915_private *dev_priv,
1872 enum plane plane, enum pipe pipe)
1873{
1874 int reg;
1875 u32 val;
1876
1877 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1878 assert_pipe_enabled(dev_priv, pipe);
1879
1880 reg = DSPCNTR(plane);
1881 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001882 if (val & DISPLAY_PLANE_ENABLE)
1883 return;
1884
1885 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001886 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001887 intel_wait_for_vblank(dev_priv->dev, pipe);
1888}
1889
Jesse Barnesb24e7172011-01-04 15:09:30 -08001890/**
1891 * intel_disable_plane - disable a display plane
1892 * @dev_priv: i915 private structure
1893 * @plane: plane to disable
1894 * @pipe: pipe consuming the data
1895 *
1896 * Disable @plane; should be an independent operation.
1897 */
1898static void intel_disable_plane(struct drm_i915_private *dev_priv,
1899 enum plane plane, enum pipe pipe)
1900{
1901 int reg;
1902 u32 val;
1903
1904 reg = DSPCNTR(plane);
1905 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001906 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1907 return;
1908
1909 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001910 intel_flush_display_plane(dev_priv, plane);
1911 intel_wait_for_vblank(dev_priv->dev, pipe);
1912}
1913
Chris Wilson127bd2a2010-07-23 23:32:05 +01001914int
Chris Wilson48b956c2010-09-14 12:50:34 +01001915intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001916 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001917 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001918{
Chris Wilsonce453d82011-02-21 14:43:56 +00001919 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001920 u32 alignment;
1921 int ret;
1922
Chris Wilson05394f32010-11-08 19:18:58 +00001923 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001924 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001925 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1926 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001927 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001928 alignment = 4 * 1024;
1929 else
1930 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001931 break;
1932 case I915_TILING_X:
1933 /* pin() will align the object as required by fence */
1934 alignment = 0;
1935 break;
1936 case I915_TILING_Y:
1937 /* FIXME: Is this true? */
1938 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1939 return -EINVAL;
1940 default:
1941 BUG();
1942 }
1943
Chris Wilsonce453d82011-02-21 14:43:56 +00001944 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001945 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001946 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001947 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001948
1949 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1950 * fence, whereas 965+ only requires a fence if using
1951 * framebuffer compression. For simplicity, we always install
1952 * a fence as the cost is not that onerous.
1953 */
Chris Wilson06d98132012-04-17 15:31:24 +01001954 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001955 if (ret)
1956 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001957
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001958 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001959
Chris Wilsonce453d82011-02-21 14:43:56 +00001960 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001961 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001962
1963err_unpin:
1964 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001965err_interruptible:
1966 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001967 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001968}
1969
Chris Wilson1690e1e2011-12-14 13:57:08 +01001970void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1971{
1972 i915_gem_object_unpin_fence(obj);
1973 i915_gem_object_unpin(obj);
1974}
1975
Daniel Vetterc2c75132012-07-05 12:17:30 +02001976/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1977 * is assumed to be a power-of-two. */
Damien Lespiau5a35e992012-10-26 18:20:12 +01001978unsigned long intel_gen4_compute_offset_xtiled(int *x, int *y,
1979 unsigned int bpp,
1980 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001981{
1982 int tile_rows, tiles;
1983
1984 tile_rows = *y / 8;
1985 *y %= 8;
1986 tiles = *x / (512/bpp);
1987 *x %= 512/bpp;
1988
1989 return tile_rows * pitch * 8 + tiles * 4096;
1990}
1991
Jesse Barnes17638cd2011-06-24 12:19:23 -07001992static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1993 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001994{
1995 struct drm_device *dev = crtc->dev;
1996 struct drm_i915_private *dev_priv = dev->dev_private;
1997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1998 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001999 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002000 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002001 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002002 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002003 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002004
2005 switch (plane) {
2006 case 0:
2007 case 1:
2008 break;
2009 default:
2010 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2011 return -EINVAL;
2012 }
2013
2014 intel_fb = to_intel_framebuffer(fb);
2015 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002016
Chris Wilson5eddb702010-09-11 13:48:45 +01002017 reg = DSPCNTR(plane);
2018 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002019 /* Mask out pixel format bits in case we change it */
2020 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002021 switch (fb->pixel_format) {
2022 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002023 dspcntr |= DISPPLANE_8BPP;
2024 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002025 case DRM_FORMAT_XRGB1555:
2026 case DRM_FORMAT_ARGB1555:
2027 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002028 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002029 case DRM_FORMAT_RGB565:
2030 dspcntr |= DISPPLANE_BGRX565;
2031 break;
2032 case DRM_FORMAT_XRGB8888:
2033 case DRM_FORMAT_ARGB8888:
2034 dspcntr |= DISPPLANE_BGRX888;
2035 break;
2036 case DRM_FORMAT_XBGR8888:
2037 case DRM_FORMAT_ABGR8888:
2038 dspcntr |= DISPPLANE_RGBX888;
2039 break;
2040 case DRM_FORMAT_XRGB2101010:
2041 case DRM_FORMAT_ARGB2101010:
2042 dspcntr |= DISPPLANE_BGRX101010;
2043 break;
2044 case DRM_FORMAT_XBGR2101010:
2045 case DRM_FORMAT_ABGR2101010:
2046 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002047 break;
2048 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002049 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes81255562010-08-02 12:07:50 -07002050 return -EINVAL;
2051 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002052
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002053 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002054 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002055 dspcntr |= DISPPLANE_TILED;
2056 else
2057 dspcntr &= ~DISPPLANE_TILED;
2058 }
2059
Chris Wilson5eddb702010-09-11 13:48:45 +01002060 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002061
Daniel Vettere506a0c2012-07-05 12:17:29 +02002062 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002063
Daniel Vetterc2c75132012-07-05 12:17:30 +02002064 if (INTEL_INFO(dev)->gen >= 4) {
2065 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002066 intel_gen4_compute_offset_xtiled(&x, &y,
2067 fb->bits_per_pixel / 8,
2068 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002069 linear_offset -= intel_crtc->dspaddr_offset;
2070 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002071 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002072 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002073
2074 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2075 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002076 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002077 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002078 I915_MODIFY_DISPBASE(DSPSURF(plane),
2079 obj->gtt_offset + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002080 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002081 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002082 } else
Daniel Vettere506a0c2012-07-05 12:17:29 +02002083 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002084 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002085
Jesse Barnes17638cd2011-06-24 12:19:23 -07002086 return 0;
2087}
2088
2089static int ironlake_update_plane(struct drm_crtc *crtc,
2090 struct drm_framebuffer *fb, int x, int y)
2091{
2092 struct drm_device *dev = crtc->dev;
2093 struct drm_i915_private *dev_priv = dev->dev_private;
2094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2095 struct intel_framebuffer *intel_fb;
2096 struct drm_i915_gem_object *obj;
2097 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002098 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002099 u32 dspcntr;
2100 u32 reg;
2101
2102 switch (plane) {
2103 case 0:
2104 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002105 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002106 break;
2107 default:
2108 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2109 return -EINVAL;
2110 }
2111
2112 intel_fb = to_intel_framebuffer(fb);
2113 obj = intel_fb->obj;
2114
2115 reg = DSPCNTR(plane);
2116 dspcntr = I915_READ(reg);
2117 /* Mask out pixel format bits in case we change it */
2118 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002119 switch (fb->pixel_format) {
2120 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002121 dspcntr |= DISPPLANE_8BPP;
2122 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002123 case DRM_FORMAT_RGB565:
2124 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002125 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002126 case DRM_FORMAT_XRGB8888:
2127 case DRM_FORMAT_ARGB8888:
2128 dspcntr |= DISPPLANE_BGRX888;
2129 break;
2130 case DRM_FORMAT_XBGR8888:
2131 case DRM_FORMAT_ABGR8888:
2132 dspcntr |= DISPPLANE_RGBX888;
2133 break;
2134 case DRM_FORMAT_XRGB2101010:
2135 case DRM_FORMAT_ARGB2101010:
2136 dspcntr |= DISPPLANE_BGRX101010;
2137 break;
2138 case DRM_FORMAT_XBGR2101010:
2139 case DRM_FORMAT_ABGR2101010:
2140 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002141 break;
2142 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002143 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002144 return -EINVAL;
2145 }
2146
2147 if (obj->tiling_mode != I915_TILING_NONE)
2148 dspcntr |= DISPPLANE_TILED;
2149 else
2150 dspcntr &= ~DISPPLANE_TILED;
2151
2152 /* must disable */
2153 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2154
2155 I915_WRITE(reg, dspcntr);
2156
Daniel Vettere506a0c2012-07-05 12:17:29 +02002157 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002158 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002159 intel_gen4_compute_offset_xtiled(&x, &y,
2160 fb->bits_per_pixel / 8,
2161 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002162 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002163
Daniel Vettere506a0c2012-07-05 12:17:29 +02002164 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2165 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002166 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002167 I915_MODIFY_DISPBASE(DSPSURF(plane),
2168 obj->gtt_offset + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002169 if (IS_HASWELL(dev)) {
2170 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2171 } else {
2172 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2173 I915_WRITE(DSPLINOFF(plane), linear_offset);
2174 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002175 POSTING_READ(reg);
2176
2177 return 0;
2178}
2179
2180/* Assume fb object is pinned & idle & fenced and just update base pointers */
2181static int
2182intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2183 int x, int y, enum mode_set_atomic state)
2184{
2185 struct drm_device *dev = crtc->dev;
2186 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002187
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002188 if (dev_priv->display.disable_fbc)
2189 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002190 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002191
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002192 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002193}
2194
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002195static int
Chris Wilson14667a42012-04-03 17:58:35 +01002196intel_finish_fb(struct drm_framebuffer *old_fb)
2197{
2198 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2199 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2200 bool was_interruptible = dev_priv->mm.interruptible;
2201 int ret;
2202
2203 wait_event(dev_priv->pending_flip_queue,
2204 atomic_read(&dev_priv->mm.wedged) ||
2205 atomic_read(&obj->pending_flip) == 0);
2206
2207 /* Big Hammer, we also need to ensure that any pending
2208 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2209 * current scanout is retired before unpinning the old
2210 * framebuffer.
2211 *
2212 * This should only fail upon a hung GPU, in which case we
2213 * can safely continue.
2214 */
2215 dev_priv->mm.interruptible = false;
2216 ret = i915_gem_object_finish_gpu(obj);
2217 dev_priv->mm.interruptible = was_interruptible;
2218
2219 return ret;
2220}
2221
Ville Syrjälä198598d2012-10-31 17:50:24 +02002222static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2223{
2224 struct drm_device *dev = crtc->dev;
2225 struct drm_i915_master_private *master_priv;
2226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2227
2228 if (!dev->primary->master)
2229 return;
2230
2231 master_priv = dev->primary->master->driver_priv;
2232 if (!master_priv->sarea_priv)
2233 return;
2234
2235 switch (intel_crtc->pipe) {
2236 case 0:
2237 master_priv->sarea_priv->pipeA_x = x;
2238 master_priv->sarea_priv->pipeA_y = y;
2239 break;
2240 case 1:
2241 master_priv->sarea_priv->pipeB_x = x;
2242 master_priv->sarea_priv->pipeB_y = y;
2243 break;
2244 default:
2245 break;
2246 }
2247}
2248
Chris Wilson14667a42012-04-03 17:58:35 +01002249static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002250intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002251 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002252{
2253 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002254 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002255 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002256 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002257 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002258
2259 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002260 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002261 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002262 return 0;
2263 }
2264
Eugeni Dodonov5826eca2012-05-09 15:37:12 -03002265 if(intel_crtc->plane > dev_priv->num_pipe) {
2266 DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
2267 intel_crtc->plane,
2268 dev_priv->num_pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002269 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002270 }
2271
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002272 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002273 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002274 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002275 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002276 if (ret != 0) {
2277 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002278 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002279 return ret;
2280 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002281
Daniel Vetter94352cf2012-07-05 22:51:56 +02002282 if (crtc->fb)
2283 intel_finish_fb(crtc->fb);
Chris Wilson265db952010-09-20 15:41:01 +01002284
Daniel Vetter94352cf2012-07-05 22:51:56 +02002285 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002286 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002287 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002288 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002289 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002290 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002291 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002292
Daniel Vetter94352cf2012-07-05 22:51:56 +02002293 old_fb = crtc->fb;
2294 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002295 crtc->x = x;
2296 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002297
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002298 if (old_fb) {
2299 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002300 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002301 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002302
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002303 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002304 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002305
Ville Syrjälä198598d2012-10-31 17:50:24 +02002306 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002307
2308 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002309}
2310
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002311static void intel_fdi_normal_train(struct drm_crtc *crtc)
2312{
2313 struct drm_device *dev = crtc->dev;
2314 struct drm_i915_private *dev_priv = dev->dev_private;
2315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2316 int pipe = intel_crtc->pipe;
2317 u32 reg, temp;
2318
2319 /* enable normal train */
2320 reg = FDI_TX_CTL(pipe);
2321 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002322 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002323 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2324 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002325 } else {
2326 temp &= ~FDI_LINK_TRAIN_NONE;
2327 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002328 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002329 I915_WRITE(reg, temp);
2330
2331 reg = FDI_RX_CTL(pipe);
2332 temp = I915_READ(reg);
2333 if (HAS_PCH_CPT(dev)) {
2334 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2335 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2336 } else {
2337 temp &= ~FDI_LINK_TRAIN_NONE;
2338 temp |= FDI_LINK_TRAIN_NONE;
2339 }
2340 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2341
2342 /* wait one idle pattern time */
2343 POSTING_READ(reg);
2344 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002345
2346 /* IVB wants error correction enabled */
2347 if (IS_IVYBRIDGE(dev))
2348 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2349 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002350}
2351
Jesse Barnes291427f2011-07-29 12:42:37 -07002352static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
2353{
2354 struct drm_i915_private *dev_priv = dev->dev_private;
2355 u32 flags = I915_READ(SOUTH_CHICKEN1);
2356
2357 flags |= FDI_PHASE_SYNC_OVR(pipe);
2358 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2359 flags |= FDI_PHASE_SYNC_EN(pipe);
2360 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2361 POSTING_READ(SOUTH_CHICKEN1);
2362}
2363
Daniel Vetter01a415f2012-10-27 15:58:40 +02002364static void ivb_modeset_global_resources(struct drm_device *dev)
2365{
2366 struct drm_i915_private *dev_priv = dev->dev_private;
2367 struct intel_crtc *pipe_B_crtc =
2368 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2369 struct intel_crtc *pipe_C_crtc =
2370 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2371 uint32_t temp;
2372
2373 /* When everything is off disable fdi C so that we could enable fdi B
2374 * with all lanes. XXX: This misses the case where a pipe is not using
2375 * any pch resources and so doesn't need any fdi lanes. */
2376 if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
2377 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2378 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2379
2380 temp = I915_READ(SOUTH_CHICKEN1);
2381 temp &= ~FDI_BC_BIFURCATION_SELECT;
2382 DRM_DEBUG_KMS("disabling fdi C rx\n");
2383 I915_WRITE(SOUTH_CHICKEN1, temp);
2384 }
2385}
2386
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002387/* The FDI link training functions for ILK/Ibexpeak. */
2388static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2389{
2390 struct drm_device *dev = crtc->dev;
2391 struct drm_i915_private *dev_priv = dev->dev_private;
2392 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2393 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002394 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002395 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002396
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002397 /* FDI needs bits from pipe & plane first */
2398 assert_pipe_enabled(dev_priv, pipe);
2399 assert_plane_enabled(dev_priv, plane);
2400
Adam Jacksone1a44742010-06-25 15:32:14 -04002401 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2402 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002403 reg = FDI_RX_IMR(pipe);
2404 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002405 temp &= ~FDI_RX_SYMBOL_LOCK;
2406 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002407 I915_WRITE(reg, temp);
2408 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002409 udelay(150);
2410
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002411 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002412 reg = FDI_TX_CTL(pipe);
2413 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002414 temp &= ~(7 << 19);
2415 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002416 temp &= ~FDI_LINK_TRAIN_NONE;
2417 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002418 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002419
Chris Wilson5eddb702010-09-11 13:48:45 +01002420 reg = FDI_RX_CTL(pipe);
2421 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002422 temp &= ~FDI_LINK_TRAIN_NONE;
2423 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002424 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2425
2426 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002427 udelay(150);
2428
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002429 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002430 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2431 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2432 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002433
Chris Wilson5eddb702010-09-11 13:48:45 +01002434 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002435 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002436 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002437 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2438
2439 if ((temp & FDI_RX_BIT_LOCK)) {
2440 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002441 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002442 break;
2443 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002444 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002445 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002446 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002447
2448 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002449 reg = FDI_TX_CTL(pipe);
2450 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002451 temp &= ~FDI_LINK_TRAIN_NONE;
2452 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002453 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002454
Chris Wilson5eddb702010-09-11 13:48:45 +01002455 reg = FDI_RX_CTL(pipe);
2456 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002457 temp &= ~FDI_LINK_TRAIN_NONE;
2458 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002459 I915_WRITE(reg, temp);
2460
2461 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002462 udelay(150);
2463
Chris Wilson5eddb702010-09-11 13:48:45 +01002464 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002465 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002466 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002467 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2468
2469 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002470 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002471 DRM_DEBUG_KMS("FDI train 2 done.\n");
2472 break;
2473 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002474 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002475 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002476 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002477
2478 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002479
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002480}
2481
Akshay Joshi0206e352011-08-16 15:34:10 -04002482static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002483 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2484 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2485 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2486 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2487};
2488
2489/* The FDI link training functions for SNB/Cougarpoint. */
2490static void gen6_fdi_link_train(struct drm_crtc *crtc)
2491{
2492 struct drm_device *dev = crtc->dev;
2493 struct drm_i915_private *dev_priv = dev->dev_private;
2494 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2495 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002496 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002497
Adam Jacksone1a44742010-06-25 15:32:14 -04002498 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2499 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002500 reg = FDI_RX_IMR(pipe);
2501 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002502 temp &= ~FDI_RX_SYMBOL_LOCK;
2503 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002504 I915_WRITE(reg, temp);
2505
2506 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002507 udelay(150);
2508
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002509 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002510 reg = FDI_TX_CTL(pipe);
2511 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002512 temp &= ~(7 << 19);
2513 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002514 temp &= ~FDI_LINK_TRAIN_NONE;
2515 temp |= FDI_LINK_TRAIN_PATTERN_1;
2516 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2517 /* SNB-B */
2518 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002519 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002520
Daniel Vetterd74cf322012-10-26 10:58:13 +02002521 I915_WRITE(FDI_RX_MISC(pipe),
2522 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2523
Chris Wilson5eddb702010-09-11 13:48:45 +01002524 reg = FDI_RX_CTL(pipe);
2525 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002526 if (HAS_PCH_CPT(dev)) {
2527 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2528 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2529 } else {
2530 temp &= ~FDI_LINK_TRAIN_NONE;
2531 temp |= FDI_LINK_TRAIN_PATTERN_1;
2532 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002533 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2534
2535 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002536 udelay(150);
2537
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002538 cpt_phase_pointer_enable(dev, pipe);
Jesse Barnes291427f2011-07-29 12:42:37 -07002539
Akshay Joshi0206e352011-08-16 15:34:10 -04002540 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002541 reg = FDI_TX_CTL(pipe);
2542 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002543 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2544 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002545 I915_WRITE(reg, temp);
2546
2547 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002548 udelay(500);
2549
Sean Paulfa37d392012-03-02 12:53:39 -05002550 for (retry = 0; retry < 5; retry++) {
2551 reg = FDI_RX_IIR(pipe);
2552 temp = I915_READ(reg);
2553 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2554 if (temp & FDI_RX_BIT_LOCK) {
2555 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2556 DRM_DEBUG_KMS("FDI train 1 done.\n");
2557 break;
2558 }
2559 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002560 }
Sean Paulfa37d392012-03-02 12:53:39 -05002561 if (retry < 5)
2562 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002563 }
2564 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002565 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002566
2567 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002568 reg = FDI_TX_CTL(pipe);
2569 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002570 temp &= ~FDI_LINK_TRAIN_NONE;
2571 temp |= FDI_LINK_TRAIN_PATTERN_2;
2572 if (IS_GEN6(dev)) {
2573 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2574 /* SNB-B */
2575 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2576 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002577 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002578
Chris Wilson5eddb702010-09-11 13:48:45 +01002579 reg = FDI_RX_CTL(pipe);
2580 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002581 if (HAS_PCH_CPT(dev)) {
2582 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2583 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2584 } else {
2585 temp &= ~FDI_LINK_TRAIN_NONE;
2586 temp |= FDI_LINK_TRAIN_PATTERN_2;
2587 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002588 I915_WRITE(reg, temp);
2589
2590 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002591 udelay(150);
2592
Akshay Joshi0206e352011-08-16 15:34:10 -04002593 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002594 reg = FDI_TX_CTL(pipe);
2595 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002596 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2597 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002598 I915_WRITE(reg, temp);
2599
2600 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002601 udelay(500);
2602
Sean Paulfa37d392012-03-02 12:53:39 -05002603 for (retry = 0; retry < 5; retry++) {
2604 reg = FDI_RX_IIR(pipe);
2605 temp = I915_READ(reg);
2606 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2607 if (temp & FDI_RX_SYMBOL_LOCK) {
2608 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2609 DRM_DEBUG_KMS("FDI train 2 done.\n");
2610 break;
2611 }
2612 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002613 }
Sean Paulfa37d392012-03-02 12:53:39 -05002614 if (retry < 5)
2615 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002616 }
2617 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002618 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002619
2620 DRM_DEBUG_KMS("FDI train done.\n");
2621}
2622
Jesse Barnes357555c2011-04-28 15:09:55 -07002623/* Manual link training for Ivy Bridge A0 parts */
2624static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2625{
2626 struct drm_device *dev = crtc->dev;
2627 struct drm_i915_private *dev_priv = dev->dev_private;
2628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2629 int pipe = intel_crtc->pipe;
2630 u32 reg, temp, i;
2631
2632 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2633 for train result */
2634 reg = FDI_RX_IMR(pipe);
2635 temp = I915_READ(reg);
2636 temp &= ~FDI_RX_SYMBOL_LOCK;
2637 temp &= ~FDI_RX_BIT_LOCK;
2638 I915_WRITE(reg, temp);
2639
2640 POSTING_READ(reg);
2641 udelay(150);
2642
Daniel Vetter01a415f2012-10-27 15:58:40 +02002643 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2644 I915_READ(FDI_RX_IIR(pipe)));
2645
Jesse Barnes357555c2011-04-28 15:09:55 -07002646 /* enable CPU FDI TX and PCH FDI RX */
2647 reg = FDI_TX_CTL(pipe);
2648 temp = I915_READ(reg);
2649 temp &= ~(7 << 19);
2650 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2651 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2652 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2653 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2654 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002655 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002656 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2657
Daniel Vetterd74cf322012-10-26 10:58:13 +02002658 I915_WRITE(FDI_RX_MISC(pipe),
2659 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2660
Jesse Barnes357555c2011-04-28 15:09:55 -07002661 reg = FDI_RX_CTL(pipe);
2662 temp = I915_READ(reg);
2663 temp &= ~FDI_LINK_TRAIN_AUTO;
2664 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2665 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002666 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002667 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2668
2669 POSTING_READ(reg);
2670 udelay(150);
2671
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002672 cpt_phase_pointer_enable(dev, pipe);
Jesse Barnes291427f2011-07-29 12:42:37 -07002673
Akshay Joshi0206e352011-08-16 15:34:10 -04002674 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002675 reg = FDI_TX_CTL(pipe);
2676 temp = I915_READ(reg);
2677 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2678 temp |= snb_b_fdi_train_param[i];
2679 I915_WRITE(reg, temp);
2680
2681 POSTING_READ(reg);
2682 udelay(500);
2683
2684 reg = FDI_RX_IIR(pipe);
2685 temp = I915_READ(reg);
2686 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2687
2688 if (temp & FDI_RX_BIT_LOCK ||
2689 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2690 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002691 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002692 break;
2693 }
2694 }
2695 if (i == 4)
2696 DRM_ERROR("FDI train 1 fail!\n");
2697
2698 /* Train 2 */
2699 reg = FDI_TX_CTL(pipe);
2700 temp = I915_READ(reg);
2701 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2702 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2703 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2704 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2705 I915_WRITE(reg, temp);
2706
2707 reg = FDI_RX_CTL(pipe);
2708 temp = I915_READ(reg);
2709 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2710 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2711 I915_WRITE(reg, temp);
2712
2713 POSTING_READ(reg);
2714 udelay(150);
2715
Akshay Joshi0206e352011-08-16 15:34:10 -04002716 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002717 reg = FDI_TX_CTL(pipe);
2718 temp = I915_READ(reg);
2719 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2720 temp |= snb_b_fdi_train_param[i];
2721 I915_WRITE(reg, temp);
2722
2723 POSTING_READ(reg);
2724 udelay(500);
2725
2726 reg = FDI_RX_IIR(pipe);
2727 temp = I915_READ(reg);
2728 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2729
2730 if (temp & FDI_RX_SYMBOL_LOCK) {
2731 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002732 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002733 break;
2734 }
2735 }
2736 if (i == 4)
2737 DRM_ERROR("FDI train 2 fail!\n");
2738
2739 DRM_DEBUG_KMS("FDI train done.\n");
2740}
2741
Daniel Vetter88cefb62012-08-12 19:27:14 +02002742static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002743{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002744 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002745 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002746 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002747 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002748
Jesse Barnesc64e3112010-09-10 11:27:03 -07002749
Jesse Barnes0e23b992010-09-10 11:10:00 -07002750 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002751 reg = FDI_RX_CTL(pipe);
2752 temp = I915_READ(reg);
2753 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002754 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Chris Wilson5eddb702010-09-11 13:48:45 +01002755 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2756 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2757
2758 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002759 udelay(200);
2760
2761 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002762 temp = I915_READ(reg);
2763 I915_WRITE(reg, temp | FDI_PCDCLK);
2764
2765 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002766 udelay(200);
2767
Paulo Zanoni20749732012-11-23 15:30:38 -02002768 /* Enable CPU FDI TX PLL, always on for Ironlake */
2769 reg = FDI_TX_CTL(pipe);
2770 temp = I915_READ(reg);
2771 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2772 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002773
Paulo Zanoni20749732012-11-23 15:30:38 -02002774 POSTING_READ(reg);
2775 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002776 }
2777}
2778
Daniel Vetter88cefb62012-08-12 19:27:14 +02002779static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2780{
2781 struct drm_device *dev = intel_crtc->base.dev;
2782 struct drm_i915_private *dev_priv = dev->dev_private;
2783 int pipe = intel_crtc->pipe;
2784 u32 reg, temp;
2785
2786 /* Switch from PCDclk to Rawclk */
2787 reg = FDI_RX_CTL(pipe);
2788 temp = I915_READ(reg);
2789 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2790
2791 /* Disable CPU FDI TX PLL */
2792 reg = FDI_TX_CTL(pipe);
2793 temp = I915_READ(reg);
2794 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2795
2796 POSTING_READ(reg);
2797 udelay(100);
2798
2799 reg = FDI_RX_CTL(pipe);
2800 temp = I915_READ(reg);
2801 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2802
2803 /* Wait for the clocks to turn off. */
2804 POSTING_READ(reg);
2805 udelay(100);
2806}
2807
Jesse Barnes291427f2011-07-29 12:42:37 -07002808static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2809{
2810 struct drm_i915_private *dev_priv = dev->dev_private;
2811 u32 flags = I915_READ(SOUTH_CHICKEN1);
2812
2813 flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2814 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2815 flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2816 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2817 POSTING_READ(SOUTH_CHICKEN1);
2818}
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002819static void ironlake_fdi_disable(struct drm_crtc *crtc)
2820{
2821 struct drm_device *dev = crtc->dev;
2822 struct drm_i915_private *dev_priv = dev->dev_private;
2823 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2824 int pipe = intel_crtc->pipe;
2825 u32 reg, temp;
2826
2827 /* disable CPU FDI tx and PCH FDI rx */
2828 reg = FDI_TX_CTL(pipe);
2829 temp = I915_READ(reg);
2830 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2831 POSTING_READ(reg);
2832
2833 reg = FDI_RX_CTL(pipe);
2834 temp = I915_READ(reg);
2835 temp &= ~(0x7 << 16);
2836 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2837 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2838
2839 POSTING_READ(reg);
2840 udelay(100);
2841
2842 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002843 if (HAS_PCH_IBX(dev)) {
2844 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes291427f2011-07-29 12:42:37 -07002845 } else if (HAS_PCH_CPT(dev)) {
2846 cpt_phase_pointer_disable(dev, pipe);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002847 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002848
2849 /* still set train pattern 1 */
2850 reg = FDI_TX_CTL(pipe);
2851 temp = I915_READ(reg);
2852 temp &= ~FDI_LINK_TRAIN_NONE;
2853 temp |= FDI_LINK_TRAIN_PATTERN_1;
2854 I915_WRITE(reg, temp);
2855
2856 reg = FDI_RX_CTL(pipe);
2857 temp = I915_READ(reg);
2858 if (HAS_PCH_CPT(dev)) {
2859 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2860 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2861 } else {
2862 temp &= ~FDI_LINK_TRAIN_NONE;
2863 temp |= FDI_LINK_TRAIN_PATTERN_1;
2864 }
2865 /* BPC in FDI rx is consistent with that in PIPECONF */
2866 temp &= ~(0x07 << 16);
2867 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2868 I915_WRITE(reg, temp);
2869
2870 POSTING_READ(reg);
2871 udelay(100);
2872}
2873
Chris Wilson5bb61642012-09-27 21:25:58 +01002874static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2875{
2876 struct drm_device *dev = crtc->dev;
2877 struct drm_i915_private *dev_priv = dev->dev_private;
2878 unsigned long flags;
2879 bool pending;
2880
2881 if (atomic_read(&dev_priv->mm.wedged))
2882 return false;
2883
2884 spin_lock_irqsave(&dev->event_lock, flags);
2885 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2886 spin_unlock_irqrestore(&dev->event_lock, flags);
2887
2888 return pending;
2889}
2890
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002891static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2892{
Chris Wilson0f911282012-04-17 10:05:38 +01002893 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002894 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002895
2896 if (crtc->fb == NULL)
2897 return;
2898
Chris Wilson5bb61642012-09-27 21:25:58 +01002899 wait_event(dev_priv->pending_flip_queue,
2900 !intel_crtc_has_pending_flip(crtc));
2901
Chris Wilson0f911282012-04-17 10:05:38 +01002902 mutex_lock(&dev->struct_mutex);
2903 intel_finish_fb(crtc->fb);
2904 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002905}
2906
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002907static bool ironlake_crtc_driving_pch(struct drm_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08002908{
2909 struct drm_device *dev = crtc->dev;
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002910 struct intel_encoder *intel_encoder;
Jesse Barnes040484a2011-01-03 12:14:26 -08002911
2912 /*
2913 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2914 * must be driven by its own crtc; no sharing is possible.
2915 */
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002916 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002917 switch (intel_encoder->type) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002918 case INTEL_OUTPUT_EDP:
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002919 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnes040484a2011-01-03 12:14:26 -08002920 return false;
2921 continue;
2922 }
2923 }
2924
2925 return true;
2926}
2927
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002928static bool haswell_crtc_driving_pch(struct drm_crtc *crtc)
2929{
2930 return intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG);
2931}
2932
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002933/* Program iCLKIP clock to the desired frequency */
2934static void lpt_program_iclkip(struct drm_crtc *crtc)
2935{
2936 struct drm_device *dev = crtc->dev;
2937 struct drm_i915_private *dev_priv = dev->dev_private;
2938 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2939 u32 temp;
2940
Daniel Vetter09153002012-12-12 14:06:44 +01002941 mutex_lock(&dev_priv->dpio_lock);
2942
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002943 /* It is necessary to ungate the pixclk gate prior to programming
2944 * the divisors, and gate it back when it is done.
2945 */
2946 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2947
2948 /* Disable SSCCTL */
2949 intel_sbi_write(dev_priv, SBI_SSCCTL6,
2950 intel_sbi_read(dev_priv, SBI_SSCCTL6) |
2951 SBI_SSCCTL_DISABLE);
2952
2953 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2954 if (crtc->mode.clock == 20000) {
2955 auxdiv = 1;
2956 divsel = 0x41;
2957 phaseinc = 0x20;
2958 } else {
2959 /* The iCLK virtual clock root frequency is in MHz,
2960 * but the crtc->mode.clock in in KHz. To get the divisors,
2961 * it is necessary to divide one by another, so we
2962 * convert the virtual clock precision to KHz here for higher
2963 * precision.
2964 */
2965 u32 iclk_virtual_root_freq = 172800 * 1000;
2966 u32 iclk_pi_range = 64;
2967 u32 desired_divisor, msb_divisor_value, pi_value;
2968
2969 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2970 msb_divisor_value = desired_divisor / iclk_pi_range;
2971 pi_value = desired_divisor % iclk_pi_range;
2972
2973 auxdiv = 0;
2974 divsel = msb_divisor_value - 2;
2975 phaseinc = pi_value;
2976 }
2977
2978 /* This should not happen with any sane values */
2979 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2980 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2981 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2982 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2983
2984 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2985 crtc->mode.clock,
2986 auxdiv,
2987 divsel,
2988 phasedir,
2989 phaseinc);
2990
2991 /* Program SSCDIVINTPHASE6 */
2992 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6);
2993 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2994 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2995 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2996 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2997 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2998 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2999
3000 intel_sbi_write(dev_priv,
3001 SBI_SSCDIVINTPHASE6,
3002 temp);
3003
3004 /* Program SSCAUXDIV */
3005 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6);
3006 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3007 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
3008 intel_sbi_write(dev_priv,
3009 SBI_SSCAUXDIV6,
3010 temp);
3011
3012
3013 /* Enable modulator and associated divider */
3014 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6);
3015 temp &= ~SBI_SSCCTL_DISABLE;
3016 intel_sbi_write(dev_priv,
3017 SBI_SSCCTL6,
3018 temp);
3019
3020 /* Wait for initialization time */
3021 udelay(24);
3022
3023 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003024
3025 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003026}
3027
Jesse Barnesf67a5592011-01-05 10:31:48 -08003028/*
3029 * Enable PCH resources required for PCH ports:
3030 * - PCH PLLs
3031 * - FDI training & RX/TX
3032 * - update transcoder timings
3033 * - DP transcoding bits
3034 * - transcoder
3035 */
3036static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003037{
3038 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003039 struct drm_i915_private *dev_priv = dev->dev_private;
3040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3041 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003042 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003043
Chris Wilsone7e164d2012-05-11 09:21:25 +01003044 assert_transcoder_disabled(dev_priv, pipe);
3045
Daniel Vettercd986ab2012-10-26 10:58:12 +02003046 /* Write the TU size bits before fdi link training, so that error
3047 * detection works. */
3048 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3049 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3050
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003051 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003052 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003053
Daniel Vetter572deb32012-10-27 18:46:14 +02003054 /* XXX: pch pll's can be enabled any time before we enable the PCH
3055 * transcoder, and we actually should do this to not upset any PCH
3056 * transcoder that already use the clock when we share it.
3057 *
3058 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
3059 * unconditionally resets the pll - we need that to have the right LVDS
3060 * enable sequence. */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02003061 ironlake_enable_pch_pll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01003062
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003063 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003064 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003065
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003066 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003067 switch (pipe) {
3068 default:
3069 case 0:
3070 temp |= TRANSA_DPLL_ENABLE;
3071 sel = TRANSA_DPLLB_SEL;
3072 break;
3073 case 1:
3074 temp |= TRANSB_DPLL_ENABLE;
3075 sel = TRANSB_DPLLB_SEL;
3076 break;
3077 case 2:
3078 temp |= TRANSC_DPLL_ENABLE;
3079 sel = TRANSC_DPLLB_SEL;
3080 break;
Jesse Barnesd64311a2011-10-12 15:01:33 -07003081 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003082 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3083 temp |= sel;
3084 else
3085 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003086 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003087 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003088
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003089 /* set transcoder timing, panel must allow it */
3090 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003091 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3092 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3093 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3094
3095 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3096 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3097 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003098 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003099
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003100 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003101
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003102 /* For PCH DP, enable TRANS_DP_CTL */
3103 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003104 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3105 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003106 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003107 reg = TRANS_DP_CTL(pipe);
3108 temp = I915_READ(reg);
3109 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003110 TRANS_DP_SYNC_MASK |
3111 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003112 temp |= (TRANS_DP_OUTPUT_ENABLE |
3113 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003114 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003115
3116 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003117 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003118 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003119 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003120
3121 switch (intel_trans_dp_port_sel(crtc)) {
3122 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003123 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003124 break;
3125 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003126 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003127 break;
3128 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003129 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003130 break;
3131 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003132 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003133 }
3134
Chris Wilson5eddb702010-09-11 13:48:45 +01003135 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003136 }
3137
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003138 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003139}
3140
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003141static void lpt_pch_enable(struct drm_crtc *crtc)
3142{
3143 struct drm_device *dev = crtc->dev;
3144 struct drm_i915_private *dev_priv = dev->dev_private;
3145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003146 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003147
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003148 assert_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003149
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003150 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003151
Paulo Zanoni0540e482012-10-31 18:12:40 -02003152 /* Set transcoder timing. */
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003153 I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
3154 I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
3155 I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003156
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003157 I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
3158 I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
3159 I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
3160 I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003161
Paulo Zanoni937bb612012-10-31 18:12:47 -02003162 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003163}
3164
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003165static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3166{
3167 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3168
3169 if (pll == NULL)
3170 return;
3171
3172 if (pll->refcount == 0) {
3173 WARN(1, "bad PCH PLL refcount\n");
3174 return;
3175 }
3176
3177 --pll->refcount;
3178 intel_crtc->pch_pll = NULL;
3179}
3180
3181static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3182{
3183 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3184 struct intel_pch_pll *pll;
3185 int i;
3186
3187 pll = intel_crtc->pch_pll;
3188 if (pll) {
3189 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3190 intel_crtc->base.base.id, pll->pll_reg);
3191 goto prepare;
3192 }
3193
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003194 if (HAS_PCH_IBX(dev_priv->dev)) {
3195 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3196 i = intel_crtc->pipe;
3197 pll = &dev_priv->pch_plls[i];
3198
3199 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3200 intel_crtc->base.base.id, pll->pll_reg);
3201
3202 goto found;
3203 }
3204
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003205 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3206 pll = &dev_priv->pch_plls[i];
3207
3208 /* Only want to check enabled timings first */
3209 if (pll->refcount == 0)
3210 continue;
3211
3212 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3213 fp == I915_READ(pll->fp0_reg)) {
3214 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3215 intel_crtc->base.base.id,
3216 pll->pll_reg, pll->refcount, pll->active);
3217
3218 goto found;
3219 }
3220 }
3221
3222 /* Ok no matching timings, maybe there's a free one? */
3223 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3224 pll = &dev_priv->pch_plls[i];
3225 if (pll->refcount == 0) {
3226 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3227 intel_crtc->base.base.id, pll->pll_reg);
3228 goto found;
3229 }
3230 }
3231
3232 return NULL;
3233
3234found:
3235 intel_crtc->pch_pll = pll;
3236 pll->refcount++;
3237 DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
3238prepare: /* separate function? */
3239 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003240
Chris Wilsone04c7352012-05-02 20:43:56 +01003241 /* Wait for the clocks to stabilize before rewriting the regs */
3242 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003243 POSTING_READ(pll->pll_reg);
3244 udelay(150);
Chris Wilsone04c7352012-05-02 20:43:56 +01003245
3246 I915_WRITE(pll->fp0_reg, fp);
3247 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003248 pll->on = false;
3249 return pll;
3250}
3251
Jesse Barnesd4270e52011-10-11 10:43:02 -07003252void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3253{
3254 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003255 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003256 u32 temp;
3257
3258 temp = I915_READ(dslreg);
3259 udelay(500);
3260 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003261 if (wait_for(I915_READ(dslreg) != temp, 5))
3262 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3263 }
3264}
3265
Jesse Barnesf67a5592011-01-05 10:31:48 -08003266static void ironlake_crtc_enable(struct drm_crtc *crtc)
3267{
3268 struct drm_device *dev = crtc->dev;
3269 struct drm_i915_private *dev_priv = dev->dev_private;
3270 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003271 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003272 int pipe = intel_crtc->pipe;
3273 int plane = intel_crtc->plane;
3274 u32 temp;
3275 bool is_pch_port;
3276
Daniel Vetter08a48462012-07-02 11:43:47 +02003277 WARN_ON(!crtc->enabled);
3278
Jesse Barnesf67a5592011-01-05 10:31:48 -08003279 if (intel_crtc->active)
3280 return;
3281
3282 intel_crtc->active = true;
3283 intel_update_watermarks(dev);
3284
3285 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3286 temp = I915_READ(PCH_LVDS);
3287 if ((temp & LVDS_PORT_EN) == 0)
3288 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3289 }
3290
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003291 is_pch_port = ironlake_crtc_driving_pch(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003292
Daniel Vetter46b6f812012-09-06 22:08:33 +02003293 if (is_pch_port) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003294 /* Note: FDI PLL enabling _must_ be done before we enable the
3295 * cpu pipes, hence this is separate from all the other fdi/pch
3296 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003297 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003298 } else {
3299 assert_fdi_tx_disabled(dev_priv, pipe);
3300 assert_fdi_rx_disabled(dev_priv, pipe);
3301 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003302
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003303 for_each_encoder_on_crtc(dev, crtc, encoder)
3304 if (encoder->pre_enable)
3305 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003306
3307 /* Enable panel fitting for LVDS */
3308 if (dev_priv->pch_pf_size &&
Jani Nikula547dc042012-11-02 11:24:03 +02003309 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3310 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnesf67a5592011-01-05 10:31:48 -08003311 /* Force use of hard-coded filter coefficients
3312 * as some pre-programmed values are broken,
3313 * e.g. x201.
3314 */
Paulo Zanoni13888d72012-11-20 13:27:41 -02003315 if (IS_IVYBRIDGE(dev))
3316 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3317 PF_PIPE_SEL_IVB(pipe));
3318 else
3319 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003320 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3321 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003322 }
3323
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003324 /*
3325 * On ILK+ LUT must be loaded before the pipe is running but with
3326 * clocks enabled
3327 */
3328 intel_crtc_load_lut(crtc);
3329
Jesse Barnesf67a5592011-01-05 10:31:48 -08003330 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3331 intel_enable_plane(dev_priv, plane, pipe);
3332
3333 if (is_pch_port)
3334 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003335
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003336 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003337 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003338 mutex_unlock(&dev->struct_mutex);
3339
Chris Wilson6b383a72010-09-13 13:54:26 +01003340 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003341
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003342 for_each_encoder_on_crtc(dev, crtc, encoder)
3343 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003344
3345 if (HAS_PCH_CPT(dev))
3346 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003347
3348 /*
3349 * There seems to be a race in PCH platform hw (at least on some
3350 * outputs) where an enabled pipe still completes any pageflip right
3351 * away (as if the pipe is off) instead of waiting for vblank. As soon
3352 * as the first vblank happend, everything works as expected. Hence just
3353 * wait for one vblank before returning to avoid strange things
3354 * happening.
3355 */
3356 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003357}
3358
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003359static void haswell_crtc_enable(struct drm_crtc *crtc)
3360{
3361 struct drm_device *dev = crtc->dev;
3362 struct drm_i915_private *dev_priv = dev->dev_private;
3363 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3364 struct intel_encoder *encoder;
3365 int pipe = intel_crtc->pipe;
3366 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003367 bool is_pch_port;
3368
3369 WARN_ON(!crtc->enabled);
3370
3371 if (intel_crtc->active)
3372 return;
3373
3374 intel_crtc->active = true;
3375 intel_update_watermarks(dev);
3376
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003377 is_pch_port = haswell_crtc_driving_pch(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003378
Paulo Zanoni83616632012-10-23 18:29:54 -02003379 if (is_pch_port)
Paulo Zanoni04945642012-11-01 21:00:59 -02003380 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003381
3382 for_each_encoder_on_crtc(dev, crtc, encoder)
3383 if (encoder->pre_enable)
3384 encoder->pre_enable(encoder);
3385
Paulo Zanoni1f544382012-10-24 11:32:00 -02003386 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003387
Paulo Zanoni1f544382012-10-24 11:32:00 -02003388 /* Enable panel fitting for eDP */
Jani Nikula547dc042012-11-02 11:24:03 +02003389 if (dev_priv->pch_pf_size &&
3390 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003391 /* Force use of hard-coded filter coefficients
3392 * as some pre-programmed values are broken,
3393 * e.g. x201.
3394 */
Paulo Zanoni54075a72012-11-20 13:27:42 -02003395 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3396 PF_PIPE_SEL_IVB(pipe));
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003397 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3398 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3399 }
3400
3401 /*
3402 * On ILK+ LUT must be loaded before the pipe is running but with
3403 * clocks enabled
3404 */
3405 intel_crtc_load_lut(crtc);
3406
Paulo Zanoni1f544382012-10-24 11:32:00 -02003407 intel_ddi_set_pipe_settings(crtc);
3408 intel_ddi_enable_pipe_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003409
3410 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3411 intel_enable_plane(dev_priv, plane, pipe);
3412
3413 if (is_pch_port)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003414 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003415
3416 mutex_lock(&dev->struct_mutex);
3417 intel_update_fbc(dev);
3418 mutex_unlock(&dev->struct_mutex);
3419
3420 intel_crtc_update_cursor(crtc, true);
3421
3422 for_each_encoder_on_crtc(dev, crtc, encoder)
3423 encoder->enable(encoder);
3424
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003425 /*
3426 * There seems to be a race in PCH platform hw (at least on some
3427 * outputs) where an enabled pipe still completes any pageflip right
3428 * away (as if the pipe is off) instead of waiting for vblank. As soon
3429 * as the first vblank happend, everything works as expected. Hence just
3430 * wait for one vblank before returning to avoid strange things
3431 * happening.
3432 */
3433 intel_wait_for_vblank(dev, intel_crtc->pipe);
3434}
3435
Jesse Barnes6be4a602010-09-10 10:26:01 -07003436static void ironlake_crtc_disable(struct drm_crtc *crtc)
3437{
3438 struct drm_device *dev = crtc->dev;
3439 struct drm_i915_private *dev_priv = dev->dev_private;
3440 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003441 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003442 int pipe = intel_crtc->pipe;
3443 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003444 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003445
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003446
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003447 if (!intel_crtc->active)
3448 return;
3449
Daniel Vetterea9d7582012-07-10 10:42:52 +02003450 for_each_encoder_on_crtc(dev, crtc, encoder)
3451 encoder->disable(encoder);
3452
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003453 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003454 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003455 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003456
Jesse Barnesb24e7172011-01-04 15:09:30 -08003457 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003458
Chris Wilson973d04f2011-07-08 12:22:37 +01003459 if (dev_priv->cfb_plane == plane)
3460 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003461
Jesse Barnesb24e7172011-01-04 15:09:30 -08003462 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003463
Jesse Barnes6be4a602010-09-10 10:26:01 -07003464 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003465 I915_WRITE(PF_CTL(pipe), 0);
3466 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003467
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003468 for_each_encoder_on_crtc(dev, crtc, encoder)
3469 if (encoder->post_disable)
3470 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003471
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003473
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003474 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003475
3476 if (HAS_PCH_CPT(dev)) {
3477 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 reg = TRANS_DP_CTL(pipe);
3479 temp = I915_READ(reg);
3480 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003481 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003482 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003483
3484 /* disable DPLL_SEL */
3485 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003486 switch (pipe) {
3487 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003488 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003489 break;
3490 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003491 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003492 break;
3493 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003494 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003495 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003496 break;
3497 default:
3498 BUG(); /* wtf */
3499 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003500 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003501 }
3502
3503 /* disable PCH DPLL */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003504 intel_disable_pch_pll(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003505
Daniel Vetter88cefb62012-08-12 19:27:14 +02003506 ironlake_fdi_pll_disable(intel_crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +01003507
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003508 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003509 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003510
3511 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003512 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003513 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003514}
3515
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003516static void haswell_crtc_disable(struct drm_crtc *crtc)
3517{
3518 struct drm_device *dev = crtc->dev;
3519 struct drm_i915_private *dev_priv = dev->dev_private;
3520 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3521 struct intel_encoder *encoder;
3522 int pipe = intel_crtc->pipe;
3523 int plane = intel_crtc->plane;
Paulo Zanoniad80a812012-10-24 16:06:19 -02003524 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni83616632012-10-23 18:29:54 -02003525 bool is_pch_port;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003526
3527 if (!intel_crtc->active)
3528 return;
3529
Paulo Zanoni83616632012-10-23 18:29:54 -02003530 is_pch_port = haswell_crtc_driving_pch(crtc);
3531
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003532 for_each_encoder_on_crtc(dev, crtc, encoder)
3533 encoder->disable(encoder);
3534
3535 intel_crtc_wait_for_pending_flips(crtc);
3536 drm_vblank_off(dev, pipe);
3537 intel_crtc_update_cursor(crtc, false);
3538
3539 intel_disable_plane(dev_priv, plane, pipe);
3540
3541 if (dev_priv->cfb_plane == plane)
3542 intel_disable_fbc(dev);
3543
3544 intel_disable_pipe(dev_priv, pipe);
3545
Paulo Zanoniad80a812012-10-24 16:06:19 -02003546 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003547
3548 /* Disable PF */
3549 I915_WRITE(PF_CTL(pipe), 0);
3550 I915_WRITE(PF_WIN_SZ(pipe), 0);
3551
Paulo Zanoni1f544382012-10-24 11:32:00 -02003552 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003553
3554 for_each_encoder_on_crtc(dev, crtc, encoder)
3555 if (encoder->post_disable)
3556 encoder->post_disable(encoder);
3557
Paulo Zanoni83616632012-10-23 18:29:54 -02003558 if (is_pch_port) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003559 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003560 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003561 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003562
3563 intel_crtc->active = false;
3564 intel_update_watermarks(dev);
3565
3566 mutex_lock(&dev->struct_mutex);
3567 intel_update_fbc(dev);
3568 mutex_unlock(&dev->struct_mutex);
3569}
3570
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003571static void ironlake_crtc_off(struct drm_crtc *crtc)
3572{
3573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3574 intel_put_pch_pll(intel_crtc);
3575}
3576
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003577static void haswell_crtc_off(struct drm_crtc *crtc)
3578{
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003579 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3580
3581 /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
3582 * start using it. */
Daniel Vetter1a240d42012-11-29 22:18:51 +01003583 intel_crtc->cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003584
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003585 intel_ddi_put_crtc_pll(crtc);
3586}
3587
Daniel Vetter02e792f2009-09-15 22:57:34 +02003588static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3589{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003590 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003591 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003592 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003593
Chris Wilson23f09ce2010-08-12 13:53:37 +01003594 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003595 dev_priv->mm.interruptible = false;
3596 (void) intel_overlay_switch_off(intel_crtc->overlay);
3597 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003598 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003599 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003600
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003601 /* Let userspace switch the overlay on again. In most cases userspace
3602 * has to recompute where to put it anyway.
3603 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003604}
3605
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003606static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003607{
3608 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003609 struct drm_i915_private *dev_priv = dev->dev_private;
3610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003611 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003612 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003613 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003614
Daniel Vetter08a48462012-07-02 11:43:47 +02003615 WARN_ON(!crtc->enabled);
3616
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003617 if (intel_crtc->active)
3618 return;
3619
3620 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003621 intel_update_watermarks(dev);
3622
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003623 intel_enable_pll(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08003624 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003625 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003626
3627 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003628 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003629
3630 /* Give the overlay scaler a chance to enable if it's on this pipe */
3631 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003632 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003633
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003634 for_each_encoder_on_crtc(dev, crtc, encoder)
3635 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003636}
3637
3638static void i9xx_crtc_disable(struct drm_crtc *crtc)
3639{
3640 struct drm_device *dev = crtc->dev;
3641 struct drm_i915_private *dev_priv = dev->dev_private;
3642 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003643 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003644 int pipe = intel_crtc->pipe;
3645 int plane = intel_crtc->plane;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003646
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003647
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003648 if (!intel_crtc->active)
3649 return;
3650
Daniel Vetterea9d7582012-07-10 10:42:52 +02003651 for_each_encoder_on_crtc(dev, crtc, encoder)
3652 encoder->disable(encoder);
3653
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003654 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003655 intel_crtc_wait_for_pending_flips(crtc);
3656 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003657 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003658 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003659
Chris Wilson973d04f2011-07-08 12:22:37 +01003660 if (dev_priv->cfb_plane == plane)
3661 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003662
Jesse Barnesb24e7172011-01-04 15:09:30 -08003663 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003664 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003665 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003666
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003667 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003668 intel_update_fbc(dev);
3669 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003670}
3671
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003672static void i9xx_crtc_off(struct drm_crtc *crtc)
3673{
3674}
3675
Daniel Vetter976f8a22012-07-08 22:34:21 +02003676static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3677 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003678{
3679 struct drm_device *dev = crtc->dev;
3680 struct drm_i915_master_private *master_priv;
3681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3682 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003683
3684 if (!dev->primary->master)
3685 return;
3686
3687 master_priv = dev->primary->master->driver_priv;
3688 if (!master_priv->sarea_priv)
3689 return;
3690
Jesse Barnes79e53942008-11-07 14:24:08 -08003691 switch (pipe) {
3692 case 0:
3693 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3694 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3695 break;
3696 case 1:
3697 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3698 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3699 break;
3700 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003701 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003702 break;
3703 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003704}
3705
Daniel Vetter976f8a22012-07-08 22:34:21 +02003706/**
3707 * Sets the power management mode of the pipe and plane.
3708 */
3709void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003710{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003711 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003712 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003713 struct intel_encoder *intel_encoder;
3714 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003715
Daniel Vetter976f8a22012-07-08 22:34:21 +02003716 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3717 enable |= intel_encoder->connectors_active;
3718
3719 if (enable)
3720 dev_priv->display.crtc_enable(crtc);
3721 else
3722 dev_priv->display.crtc_disable(crtc);
3723
3724 intel_crtc_update_sarea(crtc, enable);
3725}
3726
3727static void intel_crtc_noop(struct drm_crtc *crtc)
3728{
3729}
3730
3731static void intel_crtc_disable(struct drm_crtc *crtc)
3732{
3733 struct drm_device *dev = crtc->dev;
3734 struct drm_connector *connector;
3735 struct drm_i915_private *dev_priv = dev->dev_private;
3736
3737 /* crtc should still be enabled when we disable it. */
3738 WARN_ON(!crtc->enabled);
3739
3740 dev_priv->display.crtc_disable(crtc);
3741 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003742 dev_priv->display.off(crtc);
3743
Chris Wilson931872f2012-01-16 23:01:13 +00003744 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3745 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003746
3747 if (crtc->fb) {
3748 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003749 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003750 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003751 crtc->fb = NULL;
3752 }
3753
3754 /* Update computed state. */
3755 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3756 if (!connector->encoder || !connector->encoder->crtc)
3757 continue;
3758
3759 if (connector->encoder->crtc != crtc)
3760 continue;
3761
3762 connector->dpms = DRM_MODE_DPMS_OFF;
3763 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003764 }
3765}
3766
Daniel Vettera261b242012-07-26 19:21:47 +02003767void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003768{
Daniel Vettera261b242012-07-26 19:21:47 +02003769 struct drm_crtc *crtc;
3770
3771 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3772 if (crtc->enabled)
3773 intel_crtc_disable(crtc);
3774 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003775}
3776
Daniel Vetter1f703852012-07-11 16:51:39 +02003777void intel_encoder_noop(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003778{
Jesse Barnes79e53942008-11-07 14:24:08 -08003779}
3780
Chris Wilsonea5b2132010-08-04 13:50:23 +01003781void intel_encoder_destroy(struct drm_encoder *encoder)
3782{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003783 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003784
Chris Wilsonea5b2132010-08-04 13:50:23 +01003785 drm_encoder_cleanup(encoder);
3786 kfree(intel_encoder);
3787}
3788
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003789/* Simple dpms helper for encodres with just one connector, no cloning and only
3790 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3791 * state of the entire output pipe. */
3792void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3793{
3794 if (mode == DRM_MODE_DPMS_ON) {
3795 encoder->connectors_active = true;
3796
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003797 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003798 } else {
3799 encoder->connectors_active = false;
3800
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003801 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003802 }
3803}
3804
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003805/* Cross check the actual hw state with our own modeset state tracking (and it's
3806 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003807static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003808{
3809 if (connector->get_hw_state(connector)) {
3810 struct intel_encoder *encoder = connector->encoder;
3811 struct drm_crtc *crtc;
3812 bool encoder_enabled;
3813 enum pipe pipe;
3814
3815 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3816 connector->base.base.id,
3817 drm_get_connector_name(&connector->base));
3818
3819 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3820 "wrong connector dpms state\n");
3821 WARN(connector->base.encoder != &encoder->base,
3822 "active connector not linked to encoder\n");
3823 WARN(!encoder->connectors_active,
3824 "encoder->connectors_active not set\n");
3825
3826 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3827 WARN(!encoder_enabled, "encoder not enabled\n");
3828 if (WARN_ON(!encoder->base.crtc))
3829 return;
3830
3831 crtc = encoder->base.crtc;
3832
3833 WARN(!crtc->enabled, "crtc not enabled\n");
3834 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3835 WARN(pipe != to_intel_crtc(crtc)->pipe,
3836 "encoder active on the wrong pipe\n");
3837 }
3838}
3839
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003840/* Even simpler default implementation, if there's really no special case to
3841 * consider. */
3842void intel_connector_dpms(struct drm_connector *connector, int mode)
3843{
3844 struct intel_encoder *encoder = intel_attached_encoder(connector);
3845
3846 /* All the simple cases only support two dpms states. */
3847 if (mode != DRM_MODE_DPMS_ON)
3848 mode = DRM_MODE_DPMS_OFF;
3849
3850 if (mode == connector->dpms)
3851 return;
3852
3853 connector->dpms = mode;
3854
3855 /* Only need to change hw state when actually enabled */
3856 if (encoder->base.crtc)
3857 intel_encoder_dpms(encoder, mode);
3858 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003859 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003860
Daniel Vetterb9805142012-08-31 17:37:33 +02003861 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003862}
3863
Daniel Vetterf0947c32012-07-02 13:10:34 +02003864/* Simple connector->get_hw_state implementation for encoders that support only
3865 * one connector and no cloning and hence the encoder state determines the state
3866 * of the connector. */
3867bool intel_connector_get_hw_state(struct intel_connector *connector)
3868{
Daniel Vetter24929352012-07-02 20:28:59 +02003869 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003870 struct intel_encoder *encoder = connector->encoder;
3871
3872 return encoder->get_hw_state(encoder, &pipe);
3873}
3874
Jesse Barnes79e53942008-11-07 14:24:08 -08003875static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
Daniel Vetter35313cd2012-07-20 10:30:45 +02003876 const struct drm_display_mode *mode,
Jesse Barnes79e53942008-11-07 14:24:08 -08003877 struct drm_display_mode *adjusted_mode)
3878{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003879 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003880
Eric Anholtbad720f2009-10-22 16:11:14 -07003881 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003882 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003883 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3884 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003885 }
Chris Wilson89749352010-09-12 18:25:19 +01003886
Daniel Vetterf9bef082012-04-15 19:53:19 +02003887 /* All interlaced capable intel hw wants timings in frames. Note though
3888 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3889 * timings, so we need to be careful not to clobber these.*/
3890 if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3891 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003892
Chris Wilson44f46b422012-06-21 13:19:59 +03003893 /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
3894 * with a hsync front porch of 0.
3895 */
3896 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
3897 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
3898 return false;
3899
Jesse Barnes79e53942008-11-07 14:24:08 -08003900 return true;
3901}
3902
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07003903static int valleyview_get_display_clock_speed(struct drm_device *dev)
3904{
3905 return 400000; /* FIXME */
3906}
3907
Jesse Barnese70236a2009-09-21 10:42:27 -07003908static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003909{
Jesse Barnese70236a2009-09-21 10:42:27 -07003910 return 400000;
3911}
Jesse Barnes79e53942008-11-07 14:24:08 -08003912
Jesse Barnese70236a2009-09-21 10:42:27 -07003913static int i915_get_display_clock_speed(struct drm_device *dev)
3914{
3915 return 333000;
3916}
Jesse Barnes79e53942008-11-07 14:24:08 -08003917
Jesse Barnese70236a2009-09-21 10:42:27 -07003918static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3919{
3920 return 200000;
3921}
Jesse Barnes79e53942008-11-07 14:24:08 -08003922
Jesse Barnese70236a2009-09-21 10:42:27 -07003923static int i915gm_get_display_clock_speed(struct drm_device *dev)
3924{
3925 u16 gcfgc = 0;
3926
3927 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3928
3929 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003930 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003931 else {
3932 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3933 case GC_DISPLAY_CLOCK_333_MHZ:
3934 return 333000;
3935 default:
3936 case GC_DISPLAY_CLOCK_190_200_MHZ:
3937 return 190000;
3938 }
3939 }
3940}
Jesse Barnes79e53942008-11-07 14:24:08 -08003941
Jesse Barnese70236a2009-09-21 10:42:27 -07003942static int i865_get_display_clock_speed(struct drm_device *dev)
3943{
3944 return 266000;
3945}
3946
3947static int i855_get_display_clock_speed(struct drm_device *dev)
3948{
3949 u16 hpllcc = 0;
3950 /* Assume that the hardware is in the high speed state. This
3951 * should be the default.
3952 */
3953 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3954 case GC_CLOCK_133_200:
3955 case GC_CLOCK_100_200:
3956 return 200000;
3957 case GC_CLOCK_166_250:
3958 return 250000;
3959 case GC_CLOCK_100_133:
3960 return 133000;
3961 }
3962
3963 /* Shouldn't happen */
3964 return 0;
3965}
3966
3967static int i830_get_display_clock_speed(struct drm_device *dev)
3968{
3969 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08003970}
3971
Zhenyu Wang2c072452009-06-05 15:38:42 +08003972static void
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003973intel_reduce_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003974{
3975 while (*num > 0xffffff || *den > 0xffffff) {
3976 *num >>= 1;
3977 *den >>= 1;
3978 }
3979}
3980
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003981void
3982intel_link_compute_m_n(int bits_per_pixel, int nlanes,
3983 int pixel_clock, int link_clock,
3984 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003985{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003986 m_n->tu = 64;
Chris Wilson22ed1112010-12-04 01:01:29 +00003987 m_n->gmch_m = bits_per_pixel * pixel_clock;
3988 m_n->gmch_n = link_clock * nlanes * 8;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003989 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
Chris Wilson22ed1112010-12-04 01:01:29 +00003990 m_n->link_m = pixel_clock;
3991 m_n->link_n = link_clock;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003992 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08003993}
3994
Chris Wilsona7615032011-01-12 17:04:08 +00003995static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
3996{
Keith Packard72bbe582011-09-26 16:09:45 -07003997 if (i915_panel_use_ssc >= 0)
3998 return i915_panel_use_ssc != 0;
3999 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004000 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004001}
4002
Jesse Barnes5a354202011-06-24 12:19:22 -07004003/**
4004 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
4005 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004006 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07004007 *
4008 * A pipe may be connected to one or more outputs. Based on the depth of the
4009 * attached framebuffer, choose a good color depth to use on the pipe.
4010 *
4011 * If possible, match the pipe depth to the fb depth. In some cases, this
4012 * isn't ideal, because the connected output supports a lesser or restricted
4013 * set of depths. Resolve that here:
4014 * LVDS typically supports only 6bpc, so clamp down in that case
4015 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
4016 * Displays may support a restricted set as well, check EDID and clamp as
4017 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004018 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07004019 *
4020 * RETURNS:
4021 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
4022 * true if they don't match).
4023 */
4024static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004025 struct drm_framebuffer *fb,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004026 unsigned int *pipe_bpp,
4027 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07004028{
4029 struct drm_device *dev = crtc->dev;
4030 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes5a354202011-06-24 12:19:22 -07004031 struct drm_connector *connector;
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004032 struct intel_encoder *intel_encoder;
Jesse Barnes5a354202011-06-24 12:19:22 -07004033 unsigned int display_bpc = UINT_MAX, bpc;
4034
4035 /* Walk the encoders & connectors on this crtc, get min bpc */
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004036 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004037
4038 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
4039 unsigned int lvds_bpc;
4040
4041 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
4042 LVDS_A3_POWER_UP)
4043 lvds_bpc = 8;
4044 else
4045 lvds_bpc = 6;
4046
4047 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004048 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004049 display_bpc = lvds_bpc;
4050 }
4051 continue;
4052 }
4053
Jesse Barnes5a354202011-06-24 12:19:22 -07004054 /* Not one of the known troublemakers, check the EDID */
4055 list_for_each_entry(connector, &dev->mode_config.connector_list,
4056 head) {
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004057 if (connector->encoder != &intel_encoder->base)
Jesse Barnes5a354202011-06-24 12:19:22 -07004058 continue;
4059
Jesse Barnes62ac41a2011-07-28 12:55:14 -07004060 /* Don't use an invalid EDID bpc value */
4061 if (connector->display_info.bpc &&
4062 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004063 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 -07004064 display_bpc = connector->display_info.bpc;
4065 }
4066 }
4067
4068 /*
4069 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
4070 * through, clamp it down. (Note: >12bpc will be caught below.)
4071 */
4072 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
4073 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04004074 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004075 display_bpc = 12;
4076 } else {
Adam Jackson82820492011-10-10 16:33:34 -04004077 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004078 display_bpc = 8;
4079 }
4080 }
4081 }
4082
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004083 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4084 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
4085 display_bpc = 6;
4086 }
4087
Jesse Barnes5a354202011-06-24 12:19:22 -07004088 /*
4089 * We could just drive the pipe at the highest bpc all the time and
4090 * enable dithering as needed, but that costs bandwidth. So choose
4091 * the minimum value that expresses the full color range of the fb but
4092 * also stays within the max display bpc discovered above.
4093 */
4094
Daniel Vetter94352cf2012-07-05 22:51:56 +02004095 switch (fb->depth) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004096 case 8:
4097 bpc = 8; /* since we go through a colormap */
4098 break;
4099 case 15:
4100 case 16:
4101 bpc = 6; /* min is 18bpp */
4102 break;
4103 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07004104 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07004105 break;
4106 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07004107 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07004108 break;
4109 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07004110 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07004111 break;
4112 default:
4113 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
4114 bpc = min((unsigned int)8, display_bpc);
4115 break;
4116 }
4117
Keith Packard578393c2011-09-05 11:53:21 -07004118 display_bpc = min(display_bpc, bpc);
4119
Adam Jackson82820492011-10-10 16:33:34 -04004120 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
4121 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004122
Keith Packard578393c2011-09-05 11:53:21 -07004123 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07004124
4125 return display_bpc != bpc;
4126}
4127
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004128static int vlv_get_refclk(struct drm_crtc *crtc)
4129{
4130 struct drm_device *dev = crtc->dev;
4131 struct drm_i915_private *dev_priv = dev->dev_private;
4132 int refclk = 27000; /* for DP & HDMI */
4133
4134 return 100000; /* only one validated so far */
4135
4136 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4137 refclk = 96000;
4138 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4139 if (intel_panel_use_ssc(dev_priv))
4140 refclk = 100000;
4141 else
4142 refclk = 96000;
4143 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4144 refclk = 100000;
4145 }
4146
4147 return refclk;
4148}
4149
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004150static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4151{
4152 struct drm_device *dev = crtc->dev;
4153 struct drm_i915_private *dev_priv = dev->dev_private;
4154 int refclk;
4155
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004156 if (IS_VALLEYVIEW(dev)) {
4157 refclk = vlv_get_refclk(crtc);
4158 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004159 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4160 refclk = dev_priv->lvds_ssc_freq * 1000;
4161 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4162 refclk / 1000);
4163 } else if (!IS_GEN2(dev)) {
4164 refclk = 96000;
4165 } else {
4166 refclk = 48000;
4167 }
4168
4169 return refclk;
4170}
4171
4172static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
4173 intel_clock_t *clock)
4174{
4175 /* SDVO TV has fixed PLL values depend on its clock range,
4176 this mirrors vbios setting. */
4177 if (adjusted_mode->clock >= 100000
4178 && adjusted_mode->clock < 140500) {
4179 clock->p1 = 2;
4180 clock->p2 = 10;
4181 clock->n = 3;
4182 clock->m1 = 16;
4183 clock->m2 = 8;
4184 } else if (adjusted_mode->clock >= 140500
4185 && adjusted_mode->clock <= 200000) {
4186 clock->p1 = 1;
4187 clock->p2 = 10;
4188 clock->n = 6;
4189 clock->m1 = 12;
4190 clock->m2 = 8;
4191 }
4192}
4193
Jesse Barnesa7516a02011-12-15 12:30:37 -08004194static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
4195 intel_clock_t *clock,
4196 intel_clock_t *reduced_clock)
4197{
4198 struct drm_device *dev = crtc->dev;
4199 struct drm_i915_private *dev_priv = dev->dev_private;
4200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4201 int pipe = intel_crtc->pipe;
4202 u32 fp, fp2 = 0;
4203
4204 if (IS_PINEVIEW(dev)) {
4205 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
4206 if (reduced_clock)
4207 fp2 = (1 << reduced_clock->n) << 16 |
4208 reduced_clock->m1 << 8 | reduced_clock->m2;
4209 } else {
4210 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4211 if (reduced_clock)
4212 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4213 reduced_clock->m2;
4214 }
4215
4216 I915_WRITE(FP0(pipe), fp);
4217
4218 intel_crtc->lowfreq_avail = false;
4219 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4220 reduced_clock && i915_powersave) {
4221 I915_WRITE(FP1(pipe), fp2);
4222 intel_crtc->lowfreq_avail = true;
4223 } else {
4224 I915_WRITE(FP1(pipe), fp);
4225 }
4226}
4227
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004228static void vlv_update_pll(struct drm_crtc *crtc,
4229 struct drm_display_mode *mode,
4230 struct drm_display_mode *adjusted_mode,
4231 intel_clock_t *clock, intel_clock_t *reduced_clock,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304232 int num_connectors)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004233{
4234 struct drm_device *dev = crtc->dev;
4235 struct drm_i915_private *dev_priv = dev->dev_private;
4236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4237 int pipe = intel_crtc->pipe;
4238 u32 dpll, mdiv, pdiv;
4239 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304240 bool is_sdvo;
4241 u32 temp;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004242
Daniel Vetter09153002012-12-12 14:06:44 +01004243 mutex_lock(&dev_priv->dpio_lock);
4244
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304245 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4246 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4247
4248 dpll = DPLL_VGA_MODE_DIS;
4249 dpll |= DPLL_EXT_BUFFER_ENABLE_VLV;
4250 dpll |= DPLL_REFA_CLK_ENABLE_VLV;
4251 dpll |= DPLL_INTEGRATED_CLOCK_VLV;
4252
4253 I915_WRITE(DPLL(pipe), dpll);
4254 POSTING_READ(DPLL(pipe));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004255
4256 bestn = clock->n;
4257 bestm1 = clock->m1;
4258 bestm2 = clock->m2;
4259 bestp1 = clock->p1;
4260 bestp2 = clock->p2;
4261
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304262 /*
4263 * In Valleyview PLL and program lane counter registers are exposed
4264 * through DPIO interface
4265 */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004266 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4267 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4268 mdiv |= ((bestn << DPIO_N_SHIFT));
4269 mdiv |= (1 << DPIO_POST_DIV_SHIFT);
4270 mdiv |= (1 << DPIO_K_SHIFT);
4271 mdiv |= DPIO_ENABLE_CALIBRATION;
4272 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4273
4274 intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
4275
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304276 pdiv = (1 << DPIO_REFSEL_OVERRIDE) | (5 << DPIO_PLL_MODESEL_SHIFT) |
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004277 (3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304278 (7 << DPIO_PLL_REFCLK_SEL_SHIFT) | (8 << DPIO_DRIVER_CTL_SHIFT) |
4279 (5 << DPIO_CLK_BIAS_CTL_SHIFT);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004280 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
4281
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304282 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x005f003b);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004283
4284 dpll |= DPLL_VCO_ENABLE;
4285 I915_WRITE(DPLL(pipe), dpll);
4286 POSTING_READ(DPLL(pipe));
4287 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4288 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4289
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304290 intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x620);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004291
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304292 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4293 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4294
4295 I915_WRITE(DPLL(pipe), dpll);
4296
4297 /* Wait for the clocks to stabilize. */
4298 POSTING_READ(DPLL(pipe));
4299 udelay(150);
4300
4301 temp = 0;
4302 if (is_sdvo) {
4303 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004304 if (temp > 1)
4305 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4306 else
4307 temp = 0;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004308 }
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304309 I915_WRITE(DPLL_MD(pipe), temp);
4310 POSTING_READ(DPLL_MD(pipe));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004311
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304312 /* Now program lane control registers */
4313 if(intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)
4314 || intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
4315 {
4316 temp = 0x1000C4;
4317 if(pipe == 1)
4318 temp |= (1 << 21);
4319 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL1, temp);
4320 }
4321 if(intel_pipe_has_type(crtc,INTEL_OUTPUT_EDP))
4322 {
4323 temp = 0x1000C4;
4324 if(pipe == 1)
4325 temp |= (1 << 21);
4326 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL2, temp);
4327 }
Daniel Vetter09153002012-12-12 14:06:44 +01004328
4329 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004330}
4331
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004332static void i9xx_update_pll(struct drm_crtc *crtc,
4333 struct drm_display_mode *mode,
4334 struct drm_display_mode *adjusted_mode,
4335 intel_clock_t *clock, intel_clock_t *reduced_clock,
4336 int num_connectors)
4337{
4338 struct drm_device *dev = crtc->dev;
4339 struct drm_i915_private *dev_priv = dev->dev_private;
4340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004341 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004342 int pipe = intel_crtc->pipe;
4343 u32 dpll;
4344 bool is_sdvo;
4345
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304346 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4347
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004348 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4349 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4350
4351 dpll = DPLL_VGA_MODE_DIS;
4352
4353 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4354 dpll |= DPLLB_MODE_LVDS;
4355 else
4356 dpll |= DPLLB_MODE_DAC_SERIAL;
4357 if (is_sdvo) {
4358 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4359 if (pixel_multiplier > 1) {
4360 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4361 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4362 }
4363 dpll |= DPLL_DVO_HIGH_SPEED;
4364 }
4365 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4366 dpll |= DPLL_DVO_HIGH_SPEED;
4367
4368 /* compute bitmask from p1 value */
4369 if (IS_PINEVIEW(dev))
4370 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4371 else {
4372 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4373 if (IS_G4X(dev) && reduced_clock)
4374 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4375 }
4376 switch (clock->p2) {
4377 case 5:
4378 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4379 break;
4380 case 7:
4381 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4382 break;
4383 case 10:
4384 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4385 break;
4386 case 14:
4387 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4388 break;
4389 }
4390 if (INTEL_INFO(dev)->gen >= 4)
4391 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4392
4393 if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4394 dpll |= PLL_REF_INPUT_TVCLKINBC;
4395 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4396 /* XXX: just matching BIOS for now */
4397 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4398 dpll |= 3;
4399 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4400 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4401 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4402 else
4403 dpll |= PLL_REF_INPUT_DREFCLK;
4404
4405 dpll |= DPLL_VCO_ENABLE;
4406 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4407 POSTING_READ(DPLL(pipe));
4408 udelay(150);
4409
Daniel Vetterdafd2262012-11-26 17:22:07 +01004410 for_each_encoder_on_crtc(dev, crtc, encoder)
4411 if (encoder->pre_pll_enable)
4412 encoder->pre_pll_enable(encoder);
4413
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004414 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4415 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4416
4417 I915_WRITE(DPLL(pipe), dpll);
4418
4419 /* Wait for the clocks to stabilize. */
4420 POSTING_READ(DPLL(pipe));
4421 udelay(150);
4422
4423 if (INTEL_INFO(dev)->gen >= 4) {
4424 u32 temp = 0;
4425 if (is_sdvo) {
4426 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4427 if (temp > 1)
4428 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4429 else
4430 temp = 0;
4431 }
4432 I915_WRITE(DPLL_MD(pipe), temp);
4433 } else {
4434 /* The pixel multiplier can only be updated once the
4435 * DPLL is enabled and the clocks are stable.
4436 *
4437 * So write it again.
4438 */
4439 I915_WRITE(DPLL(pipe), dpll);
4440 }
4441}
4442
4443static void i8xx_update_pll(struct drm_crtc *crtc,
4444 struct drm_display_mode *adjusted_mode,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304445 intel_clock_t *clock, intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004446 int num_connectors)
4447{
4448 struct drm_device *dev = crtc->dev;
4449 struct drm_i915_private *dev_priv = dev->dev_private;
4450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004451 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004452 int pipe = intel_crtc->pipe;
4453 u32 dpll;
4454
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304455 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4456
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004457 dpll = DPLL_VGA_MODE_DIS;
4458
4459 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4460 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4461 } else {
4462 if (clock->p1 == 2)
4463 dpll |= PLL_P1_DIVIDE_BY_TWO;
4464 else
4465 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4466 if (clock->p2 == 4)
4467 dpll |= PLL_P2_DIVIDE_BY_4;
4468 }
4469
4470 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4471 /* XXX: just matching BIOS for now */
4472 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4473 dpll |= 3;
4474 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4475 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4476 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4477 else
4478 dpll |= PLL_REF_INPUT_DREFCLK;
4479
4480 dpll |= DPLL_VCO_ENABLE;
4481 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4482 POSTING_READ(DPLL(pipe));
4483 udelay(150);
4484
Daniel Vetterdafd2262012-11-26 17:22:07 +01004485 for_each_encoder_on_crtc(dev, crtc, encoder)
4486 if (encoder->pre_pll_enable)
4487 encoder->pre_pll_enable(encoder);
4488
Daniel Vetter5b5896e2012-09-11 12:37:55 +02004489 I915_WRITE(DPLL(pipe), dpll);
4490
4491 /* Wait for the clocks to stabilize. */
4492 POSTING_READ(DPLL(pipe));
4493 udelay(150);
4494
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004495 /* The pixel multiplier can only be updated once the
4496 * DPLL is enabled and the clocks are stable.
4497 *
4498 * So write it again.
4499 */
4500 I915_WRITE(DPLL(pipe), dpll);
4501}
4502
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004503static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
4504 struct drm_display_mode *mode,
4505 struct drm_display_mode *adjusted_mode)
4506{
4507 struct drm_device *dev = intel_crtc->base.dev;
4508 struct drm_i915_private *dev_priv = dev->dev_private;
4509 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004510 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004511 uint32_t vsyncshift;
4512
4513 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4514 /* the chip adds 2 halflines automatically */
4515 adjusted_mode->crtc_vtotal -= 1;
4516 adjusted_mode->crtc_vblank_end -= 1;
4517 vsyncshift = adjusted_mode->crtc_hsync_start
4518 - adjusted_mode->crtc_htotal / 2;
4519 } else {
4520 vsyncshift = 0;
4521 }
4522
4523 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004524 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004525
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004526 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004527 (adjusted_mode->crtc_hdisplay - 1) |
4528 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004529 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004530 (adjusted_mode->crtc_hblank_start - 1) |
4531 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004532 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004533 (adjusted_mode->crtc_hsync_start - 1) |
4534 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4535
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004536 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004537 (adjusted_mode->crtc_vdisplay - 1) |
4538 ((adjusted_mode->crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004539 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004540 (adjusted_mode->crtc_vblank_start - 1) |
4541 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004542 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004543 (adjusted_mode->crtc_vsync_start - 1) |
4544 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4545
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004546 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4547 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4548 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4549 * bits. */
4550 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4551 (pipe == PIPE_B || pipe == PIPE_C))
4552 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4553
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004554 /* pipesrc controls the size that is scaled from, which should
4555 * always be the user's requested size.
4556 */
4557 I915_WRITE(PIPESRC(pipe),
4558 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4559}
4560
Eric Anholtf564048e2011-03-30 13:01:02 -07004561static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4562 struct drm_display_mode *mode,
4563 struct drm_display_mode *adjusted_mode,
4564 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004565 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004566{
4567 struct drm_device *dev = crtc->dev;
4568 struct drm_i915_private *dev_priv = dev->dev_private;
4569 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4570 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004571 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004572 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004573 intel_clock_t clock, reduced_clock;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004574 u32 dspcntr, pipeconf;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004575 bool ok, has_reduced_clock = false, is_sdvo = false;
4576 bool is_lvds = false, is_tv = false, is_dp = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004577 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004578 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004579 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004580
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004581 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004582 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004583 case INTEL_OUTPUT_LVDS:
4584 is_lvds = true;
4585 break;
4586 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08004587 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08004588 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01004589 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004590 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004591 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004592 case INTEL_OUTPUT_TVOUT:
4593 is_tv = true;
4594 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004595 case INTEL_OUTPUT_DISPLAYPORT:
4596 is_dp = true;
4597 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004598 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004599
Eric Anholtc751ce42010-03-25 11:48:48 -07004600 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004601 }
4602
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004603 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004604
Ma Lingd4906092009-03-18 20:13:27 +08004605 /*
4606 * Returns a set of divisors for the desired target clock with the given
4607 * refclk, or FALSE. The returned values represent the clock equation:
4608 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4609 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004610 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08004611 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4612 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004613 if (!ok) {
4614 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004615 return -EINVAL;
4616 }
4617
4618 /* Ensure that the cursor is valid for the new mode before changing... */
4619 intel_crtc_update_cursor(crtc, true);
4620
4621 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004622 /*
4623 * Ensure we match the reduced clock's P to the target clock.
4624 * If the clocks don't match, we can't switch the display clock
4625 * by using the FP0/FP1. In such case we will disable the LVDS
4626 * downclock feature.
4627 */
Eric Anholtf564048e2011-03-30 13:01:02 -07004628 has_reduced_clock = limit->find_pll(limit, crtc,
4629 dev_priv->lvds_downclock,
4630 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08004631 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004632 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004633 }
4634
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004635 if (is_sdvo && is_tv)
4636 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004637
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004638 if (IS_GEN2(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304639 i8xx_update_pll(crtc, adjusted_mode, &clock,
4640 has_reduced_clock ? &reduced_clock : NULL,
4641 num_connectors);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004642 else if (IS_VALLEYVIEW(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304643 vlv_update_pll(crtc, mode, adjusted_mode, &clock,
4644 has_reduced_clock ? &reduced_clock : NULL,
4645 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004646 else
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004647 i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
4648 has_reduced_clock ? &reduced_clock : NULL,
4649 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004650
4651 /* setup pipeconf */
4652 pipeconf = I915_READ(PIPECONF(pipe));
4653
4654 /* Set up the display plane register */
4655 dspcntr = DISPPLANE_GAMMA_ENABLE;
4656
Eric Anholt929c77f2011-03-30 13:01:04 -07004657 if (pipe == 0)
4658 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4659 else
4660 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07004661
4662 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4663 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4664 * core speed.
4665 *
4666 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4667 * pipe == 0 check?
4668 */
4669 if (mode->clock >
4670 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4671 pipeconf |= PIPECONF_DOUBLE_WIDE;
4672 else
4673 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4674 }
4675
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004676 /* default to 8bpc */
4677 pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
4678 if (is_dp) {
Jani Nikula0c96c652012-09-26 18:43:10 +03004679 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004680 pipeconf |= PIPECONF_BPP_6 |
4681 PIPECONF_DITHER_EN |
4682 PIPECONF_DITHER_TYPE_SP;
4683 }
4684 }
4685
Gajanan Bhat19c03922012-09-27 19:13:07 +05304686 if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4687 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4688 pipeconf |= PIPECONF_BPP_6 |
4689 PIPECONF_ENABLE |
4690 I965_PIPECONF_ACTIVE;
4691 }
4692 }
4693
Eric Anholtf564048e2011-03-30 13:01:02 -07004694 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
4695 drm_mode_debug_printmodeline(mode);
4696
Jesse Barnesa7516a02011-12-15 12:30:37 -08004697 if (HAS_PIPE_CXSR(dev)) {
4698 if (intel_crtc->lowfreq_avail) {
Eric Anholtf564048e2011-03-30 13:01:02 -07004699 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4700 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004701 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07004702 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4703 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4704 }
4705 }
4706
Keith Packard617cf882012-02-08 13:53:38 -08004707 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetterdbb02572012-01-28 14:49:23 +01004708 if (!IS_GEN2(dev) &&
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004709 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Eric Anholtf564048e2011-03-30 13:01:02 -07004710 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004711 else
Keith Packard617cf882012-02-08 13:53:38 -08004712 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01004713
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004714 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Eric Anholtf564048e2011-03-30 13:01:02 -07004715
4716 /* pipesrc and dspsize control the size that is scaled from,
4717 * which should always be the user's requested size.
4718 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004719 I915_WRITE(DSPSIZE(plane),
4720 ((mode->vdisplay - 1) << 16) |
4721 (mode->hdisplay - 1));
4722 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004723
Eric Anholtf564048e2011-03-30 13:01:02 -07004724 I915_WRITE(PIPECONF(pipe), pipeconf);
4725 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07004726 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07004727
4728 intel_wait_for_vblank(dev, pipe);
4729
Eric Anholtf564048e2011-03-30 13:01:02 -07004730 I915_WRITE(DSPCNTR(plane), dspcntr);
4731 POSTING_READ(DSPCNTR(plane));
4732
Daniel Vetter94352cf2012-07-05 22:51:56 +02004733 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004734
4735 intel_update_watermarks(dev);
4736
Eric Anholtf564048e2011-03-30 13:01:02 -07004737 return ret;
4738}
4739
Keith Packard9fb526d2011-09-26 22:24:57 -07004740/*
4741 * Initialize reference clocks when the driver loads
4742 */
4743void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07004744{
4745 struct drm_i915_private *dev_priv = dev->dev_private;
4746 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004747 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004748 u32 temp;
4749 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004750 bool has_cpu_edp = false;
4751 bool has_pch_edp = false;
4752 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07004753 bool has_ck505 = false;
4754 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004755
4756 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07004757 list_for_each_entry(encoder, &mode_config->encoder_list,
4758 base.head) {
4759 switch (encoder->type) {
4760 case INTEL_OUTPUT_LVDS:
4761 has_panel = true;
4762 has_lvds = true;
4763 break;
4764 case INTEL_OUTPUT_EDP:
4765 has_panel = true;
4766 if (intel_encoder_is_pch_edp(&encoder->base))
4767 has_pch_edp = true;
4768 else
4769 has_cpu_edp = true;
4770 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004771 }
4772 }
4773
Keith Packard99eb6a02011-09-26 14:29:12 -07004774 if (HAS_PCH_IBX(dev)) {
4775 has_ck505 = dev_priv->display_clock_mode;
4776 can_ssc = has_ck505;
4777 } else {
4778 has_ck505 = false;
4779 can_ssc = true;
4780 }
4781
4782 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4783 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4784 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004785
4786 /* Ironlake: try to setup display ref clock before DPLL
4787 * enabling. This is only under driver's control after
4788 * PCH B stepping, previous chipset stepping should be
4789 * ignoring this setting.
4790 */
4791 temp = I915_READ(PCH_DREF_CONTROL);
4792 /* Always enable nonspread source */
4793 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004794
Keith Packard99eb6a02011-09-26 14:29:12 -07004795 if (has_ck505)
4796 temp |= DREF_NONSPREAD_CK505_ENABLE;
4797 else
4798 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004799
Keith Packard199e5d72011-09-22 12:01:57 -07004800 if (has_panel) {
4801 temp &= ~DREF_SSC_SOURCE_MASK;
4802 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004803
Keith Packard199e5d72011-09-22 12:01:57 -07004804 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07004805 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004806 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004807 temp |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02004808 } else
4809 temp &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004810
4811 /* Get SSC going before enabling the outputs */
4812 I915_WRITE(PCH_DREF_CONTROL, temp);
4813 POSTING_READ(PCH_DREF_CONTROL);
4814 udelay(200);
4815
Jesse Barnes13d83a62011-08-03 12:59:20 -07004816 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4817
4818 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07004819 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07004820 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004821 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004822 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004823 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07004824 else
4825 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004826 } else
4827 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4828
4829 I915_WRITE(PCH_DREF_CONTROL, temp);
4830 POSTING_READ(PCH_DREF_CONTROL);
4831 udelay(200);
4832 } else {
4833 DRM_DEBUG_KMS("Disabling SSC entirely\n");
4834
4835 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4836
4837 /* Turn off CPU output */
4838 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4839
4840 I915_WRITE(PCH_DREF_CONTROL, temp);
4841 POSTING_READ(PCH_DREF_CONTROL);
4842 udelay(200);
4843
4844 /* Turn off the SSC source */
4845 temp &= ~DREF_SSC_SOURCE_MASK;
4846 temp |= DREF_SSC_SOURCE_DISABLE;
4847
4848 /* Turn off SSC1 */
4849 temp &= ~ DREF_SSC1_ENABLE;
4850
Jesse Barnes13d83a62011-08-03 12:59:20 -07004851 I915_WRITE(PCH_DREF_CONTROL, temp);
4852 POSTING_READ(PCH_DREF_CONTROL);
4853 udelay(200);
4854 }
4855}
4856
Jesse Barnesd9d444c2011-09-02 13:03:05 -07004857static int ironlake_get_refclk(struct drm_crtc *crtc)
4858{
4859 struct drm_device *dev = crtc->dev;
4860 struct drm_i915_private *dev_priv = dev->dev_private;
4861 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07004862 struct intel_encoder *edp_encoder = NULL;
4863 int num_connectors = 0;
4864 bool is_lvds = false;
4865
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004866 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07004867 switch (encoder->type) {
4868 case INTEL_OUTPUT_LVDS:
4869 is_lvds = true;
4870 break;
4871 case INTEL_OUTPUT_EDP:
4872 edp_encoder = encoder;
4873 break;
4874 }
4875 num_connectors++;
4876 }
4877
4878 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4879 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4880 dev_priv->lvds_ssc_freq);
4881 return dev_priv->lvds_ssc_freq * 1000;
4882 }
4883
4884 return 120000;
4885}
4886
Paulo Zanonic8203562012-09-12 10:06:29 -03004887static void ironlake_set_pipeconf(struct drm_crtc *crtc,
4888 struct drm_display_mode *adjusted_mode,
4889 bool dither)
4890{
4891 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4892 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4893 int pipe = intel_crtc->pipe;
4894 uint32_t val;
4895
4896 val = I915_READ(PIPECONF(pipe));
4897
4898 val &= ~PIPE_BPC_MASK;
4899 switch (intel_crtc->bpp) {
4900 case 18:
4901 val |= PIPE_6BPC;
4902 break;
4903 case 24:
4904 val |= PIPE_8BPC;
4905 break;
4906 case 30:
4907 val |= PIPE_10BPC;
4908 break;
4909 case 36:
4910 val |= PIPE_12BPC;
4911 break;
4912 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03004913 /* Case prevented by intel_choose_pipe_bpp_dither. */
4914 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03004915 }
4916
4917 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4918 if (dither)
4919 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4920
4921 val &= ~PIPECONF_INTERLACE_MASK;
4922 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
4923 val |= PIPECONF_INTERLACED_ILK;
4924 else
4925 val |= PIPECONF_PROGRESSIVE;
4926
4927 I915_WRITE(PIPECONF(pipe), val);
4928 POSTING_READ(PIPECONF(pipe));
4929}
4930
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004931static void haswell_set_pipeconf(struct drm_crtc *crtc,
4932 struct drm_display_mode *adjusted_mode,
4933 bool dither)
4934{
4935 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4936 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni702e7a52012-10-23 18:29:59 -02004937 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004938 uint32_t val;
4939
Paulo Zanoni702e7a52012-10-23 18:29:59 -02004940 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004941
4942 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4943 if (dither)
4944 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4945
4946 val &= ~PIPECONF_INTERLACE_MASK_HSW;
4947 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
4948 val |= PIPECONF_INTERLACED_ILK;
4949 else
4950 val |= PIPECONF_PROGRESSIVE;
4951
Paulo Zanoni702e7a52012-10-23 18:29:59 -02004952 I915_WRITE(PIPECONF(cpu_transcoder), val);
4953 POSTING_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004954}
4955
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03004956static bool ironlake_compute_clocks(struct drm_crtc *crtc,
4957 struct drm_display_mode *adjusted_mode,
4958 intel_clock_t *clock,
4959 bool *has_reduced_clock,
4960 intel_clock_t *reduced_clock)
4961{
4962 struct drm_device *dev = crtc->dev;
4963 struct drm_i915_private *dev_priv = dev->dev_private;
4964 struct intel_encoder *intel_encoder;
4965 int refclk;
4966 const intel_limit_t *limit;
4967 bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
4968
4969 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
4970 switch (intel_encoder->type) {
4971 case INTEL_OUTPUT_LVDS:
4972 is_lvds = true;
4973 break;
4974 case INTEL_OUTPUT_SDVO:
4975 case INTEL_OUTPUT_HDMI:
4976 is_sdvo = true;
4977 if (intel_encoder->needs_tv_clock)
4978 is_tv = true;
4979 break;
4980 case INTEL_OUTPUT_TVOUT:
4981 is_tv = true;
4982 break;
4983 }
4984 }
4985
4986 refclk = ironlake_get_refclk(crtc);
4987
4988 /*
4989 * Returns a set of divisors for the desired target clock with the given
4990 * refclk, or FALSE. The returned values represent the clock equation:
4991 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4992 */
4993 limit = intel_limit(crtc, refclk);
4994 ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4995 clock);
4996 if (!ret)
4997 return false;
4998
4999 if (is_lvds && dev_priv->lvds_downclock_avail) {
5000 /*
5001 * Ensure we match the reduced clock's P to the target clock.
5002 * If the clocks don't match, we can't switch the display clock
5003 * by using the FP0/FP1. In such case we will disable the LVDS
5004 * downclock feature.
5005 */
5006 *has_reduced_clock = limit->find_pll(limit, crtc,
5007 dev_priv->lvds_downclock,
5008 refclk,
5009 clock,
5010 reduced_clock);
5011 }
5012
5013 if (is_sdvo && is_tv)
5014 i9xx_adjust_sdvo_tv_clock(adjusted_mode, clock);
5015
5016 return true;
5017}
5018
Daniel Vetter01a415f2012-10-27 15:58:40 +02005019static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5020{
5021 struct drm_i915_private *dev_priv = dev->dev_private;
5022 uint32_t temp;
5023
5024 temp = I915_READ(SOUTH_CHICKEN1);
5025 if (temp & FDI_BC_BIFURCATION_SELECT)
5026 return;
5027
5028 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5029 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5030
5031 temp |= FDI_BC_BIFURCATION_SELECT;
5032 DRM_DEBUG_KMS("enabling fdi C rx\n");
5033 I915_WRITE(SOUTH_CHICKEN1, temp);
5034 POSTING_READ(SOUTH_CHICKEN1);
5035}
5036
5037static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
5038{
5039 struct drm_device *dev = intel_crtc->base.dev;
5040 struct drm_i915_private *dev_priv = dev->dev_private;
5041 struct intel_crtc *pipe_B_crtc =
5042 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5043
5044 DRM_DEBUG_KMS("checking fdi config on pipe %i, lanes %i\n",
5045 intel_crtc->pipe, intel_crtc->fdi_lanes);
5046 if (intel_crtc->fdi_lanes > 4) {
5047 DRM_DEBUG_KMS("invalid fdi lane config on pipe %i: %i lanes\n",
5048 intel_crtc->pipe, intel_crtc->fdi_lanes);
5049 /* Clamp lanes to avoid programming the hw with bogus values. */
5050 intel_crtc->fdi_lanes = 4;
5051
5052 return false;
5053 }
5054
5055 if (dev_priv->num_pipe == 2)
5056 return true;
5057
5058 switch (intel_crtc->pipe) {
5059 case PIPE_A:
5060 return true;
5061 case PIPE_B:
5062 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5063 intel_crtc->fdi_lanes > 2) {
5064 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5065 intel_crtc->pipe, intel_crtc->fdi_lanes);
5066 /* Clamp lanes to avoid programming the hw with bogus values. */
5067 intel_crtc->fdi_lanes = 2;
5068
5069 return false;
5070 }
5071
5072 if (intel_crtc->fdi_lanes > 2)
5073 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5074 else
5075 cpt_enable_fdi_bc_bifurcation(dev);
5076
5077 return true;
5078 case PIPE_C:
5079 if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
5080 if (intel_crtc->fdi_lanes > 2) {
5081 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5082 intel_crtc->pipe, intel_crtc->fdi_lanes);
5083 /* Clamp lanes to avoid programming the hw with bogus values. */
5084 intel_crtc->fdi_lanes = 2;
5085
5086 return false;
5087 }
5088 } else {
5089 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5090 return false;
5091 }
5092
5093 cpt_enable_fdi_bc_bifurcation(dev);
5094
5095 return true;
5096 default:
5097 BUG();
5098 }
5099}
5100
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005101static void ironlake_set_m_n(struct drm_crtc *crtc,
5102 struct drm_display_mode *mode,
5103 struct drm_display_mode *adjusted_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08005104{
5105 struct drm_device *dev = crtc->dev;
5106 struct drm_i915_private *dev_priv = dev->dev_private;
5107 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005108 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005109 struct intel_encoder *intel_encoder, *edp_encoder = NULL;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005110 struct intel_link_m_n m_n = {0};
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005111 int target_clock, pixel_multiplier, lane, link_bw;
5112 bool is_dp = false, is_cpu_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005113
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005114 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5115 switch (intel_encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005116 case INTEL_OUTPUT_DISPLAYPORT:
5117 is_dp = true;
5118 break;
5119 case INTEL_OUTPUT_EDP:
Jesse Barnese3aef172012-04-10 11:58:03 -07005120 is_dp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005121 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnese3aef172012-04-10 11:58:03 -07005122 is_cpu_edp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005123 edp_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005124 break;
5125 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005126 }
5127
Zhenyu Wang2c072452009-06-05 15:38:42 +08005128 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07005129 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5130 lane = 0;
5131 /* CPU eDP doesn't require FDI link, so just set DP M/N
5132 according to current link config */
Jesse Barnese3aef172012-04-10 11:58:03 -07005133 if (is_cpu_edp) {
Jesse Barnese3aef172012-04-10 11:58:03 -07005134 intel_edp_link_config(edp_encoder, &lane, &link_bw);
Eric Anholt8febb292011-03-30 13:01:07 -07005135 } else {
Eric Anholt8febb292011-03-30 13:01:07 -07005136 /* FDI is a binary signal running at ~2.7GHz, encoding
5137 * each output octet as 10 bits. The actual frequency
5138 * is stored as a divider into a 100MHz clock, and the
5139 * mode pixel clock is stored in units of 1KHz.
5140 * Hence the bw of each lane in terms of the mode signal
5141 * is:
5142 */
5143 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005144 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08005145
Daniel Vetter94bf2ce2012-06-04 18:39:19 +02005146 /* [e]DP over FDI requires target mode clock instead of link clock. */
5147 if (edp_encoder)
5148 target_clock = intel_edp_target_clock(edp_encoder, mode);
5149 else if (is_dp)
5150 target_clock = mode->clock;
5151 else
5152 target_clock = adjusted_mode->clock;
5153
Eric Anholt8febb292011-03-30 13:01:07 -07005154 if (!lane) {
5155 /*
5156 * Account for spread spectrum to avoid
5157 * oversubscribing the link. Max center spread
5158 * is 2.5%; use 5% for safety's sake.
5159 */
Jesse Barnes5a354202011-06-24 12:19:22 -07005160 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
Eric Anholt8febb292011-03-30 13:01:07 -07005161 lane = bps / (link_bw * 8) + 1;
5162 }
5163
5164 intel_crtc->fdi_lanes = lane;
5165
5166 if (pixel_multiplier > 1)
5167 link_bw *= pixel_multiplier;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005168 intel_link_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw, &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07005169
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005170 I915_WRITE(PIPE_DATA_M1(cpu_transcoder), TU_SIZE(m_n.tu) | m_n.gmch_m);
5171 I915_WRITE(PIPE_DATA_N1(cpu_transcoder), m_n.gmch_n);
5172 I915_WRITE(PIPE_LINK_M1(cpu_transcoder), m_n.link_m);
5173 I915_WRITE(PIPE_LINK_N1(cpu_transcoder), m_n.link_n);
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005174}
5175
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005176static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5177 struct drm_display_mode *adjusted_mode,
5178 intel_clock_t *clock, u32 fp)
5179{
5180 struct drm_crtc *crtc = &intel_crtc->base;
5181 struct drm_device *dev = crtc->dev;
5182 struct drm_i915_private *dev_priv = dev->dev_private;
5183 struct intel_encoder *intel_encoder;
5184 uint32_t dpll;
5185 int factor, pixel_multiplier, num_connectors = 0;
5186 bool is_lvds = false, is_sdvo = false, is_tv = false;
5187 bool is_dp = false, is_cpu_edp = false;
5188
5189 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5190 switch (intel_encoder->type) {
5191 case INTEL_OUTPUT_LVDS:
5192 is_lvds = true;
5193 break;
5194 case INTEL_OUTPUT_SDVO:
5195 case INTEL_OUTPUT_HDMI:
5196 is_sdvo = true;
5197 if (intel_encoder->needs_tv_clock)
5198 is_tv = true;
5199 break;
5200 case INTEL_OUTPUT_TVOUT:
5201 is_tv = true;
5202 break;
5203 case INTEL_OUTPUT_DISPLAYPORT:
5204 is_dp = true;
5205 break;
5206 case INTEL_OUTPUT_EDP:
5207 is_dp = true;
5208 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
5209 is_cpu_edp = true;
5210 break;
5211 }
5212
5213 num_connectors++;
5214 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005215
Chris Wilsonc1858122010-12-03 21:35:48 +00005216 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005217 factor = 21;
5218 if (is_lvds) {
5219 if ((intel_panel_use_ssc(dev_priv) &&
5220 dev_priv->lvds_ssc_freq == 100) ||
Daniel Vetter1974cad2012-11-26 17:22:09 +01005221 intel_is_dual_link_lvds(dev))
Eric Anholt8febb292011-03-30 13:01:07 -07005222 factor = 25;
5223 } else if (is_sdvo && is_tv)
5224 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005225
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005226 if (clock->m < factor * clock->n)
Eric Anholt8febb292011-03-30 13:01:07 -07005227 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005228
Chris Wilson5eddb702010-09-11 13:48:45 +01005229 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005230
Eric Anholta07d6782011-03-30 13:01:08 -07005231 if (is_lvds)
5232 dpll |= DPLLB_MODE_LVDS;
5233 else
5234 dpll |= DPLLB_MODE_DAC_SERIAL;
5235 if (is_sdvo) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005236 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
Eric Anholta07d6782011-03-30 13:01:08 -07005237 if (pixel_multiplier > 1) {
5238 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08005239 }
Eric Anholta07d6782011-03-30 13:01:08 -07005240 dpll |= DPLL_DVO_HIGH_SPEED;
5241 }
Jesse Barnese3aef172012-04-10 11:58:03 -07005242 if (is_dp && !is_cpu_edp)
Eric Anholta07d6782011-03-30 13:01:08 -07005243 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005244
Eric Anholta07d6782011-03-30 13:01:08 -07005245 /* compute bitmask from p1 value */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005246 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005247 /* also FPA1 */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005248 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005249
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005250 switch (clock->p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005251 case 5:
5252 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5253 break;
5254 case 7:
5255 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5256 break;
5257 case 10:
5258 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5259 break;
5260 case 14:
5261 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5262 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005263 }
5264
5265 if (is_sdvo && is_tv)
5266 dpll |= PLL_REF_INPUT_TVCLKINBC;
5267 else if (is_tv)
5268 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005269 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08005270 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00005271 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005272 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005273 else
5274 dpll |= PLL_REF_INPUT_DREFCLK;
5275
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005276 return dpll;
5277}
5278
Jesse Barnes79e53942008-11-07 14:24:08 -08005279static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5280 struct drm_display_mode *mode,
5281 struct drm_display_mode *adjusted_mode,
5282 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005283 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005284{
5285 struct drm_device *dev = crtc->dev;
5286 struct drm_i915_private *dev_priv = dev->dev_private;
5287 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5288 int pipe = intel_crtc->pipe;
5289 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005290 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005291 intel_clock_t clock, reduced_clock;
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005292 u32 dpll, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005293 bool ok, has_reduced_clock = false;
5294 bool is_lvds = false, is_dp = false, is_cpu_edp = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005295 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005296 int ret;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005297 bool dither, fdi_config_ok;
Jesse Barnes79e53942008-11-07 14:24:08 -08005298
5299 for_each_encoder_on_crtc(dev, crtc, encoder) {
5300 switch (encoder->type) {
5301 case INTEL_OUTPUT_LVDS:
5302 is_lvds = true;
5303 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005304 case INTEL_OUTPUT_DISPLAYPORT:
5305 is_dp = true;
5306 break;
5307 case INTEL_OUTPUT_EDP:
5308 is_dp = true;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005309 if (!intel_encoder_is_pch_edp(&encoder->base))
Jesse Barnes79e53942008-11-07 14:24:08 -08005310 is_cpu_edp = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005311 break;
5312 }
5313
5314 num_connectors++;
5315 }
5316
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005317 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5318 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5319
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005320 ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
5321 &has_reduced_clock, &reduced_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005322 if (!ok) {
5323 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5324 return -EINVAL;
5325 }
5326
5327 /* Ensure that the cursor is valid for the new mode before changing... */
5328 intel_crtc_update_cursor(crtc, true);
5329
Jesse Barnes79e53942008-11-07 14:24:08 -08005330 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005331 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5332 adjusted_mode);
Paulo Zanonic8203562012-09-12 10:06:29 -03005333 if (is_lvds && dev_priv->lvds_dither)
5334 dither = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005335
Jesse Barnes79e53942008-11-07 14:24:08 -08005336 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5337 if (has_reduced_clock)
5338 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5339 reduced_clock.m2;
5340
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005341 dpll = ironlake_compute_dpll(intel_crtc, adjusted_mode, &clock, fp);
Jesse Barnes79e53942008-11-07 14:24:08 -08005342
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07005343 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005344 drm_mode_debug_printmodeline(mode);
5345
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005346 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5347 if (!is_cpu_edp) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005348 struct intel_pch_pll *pll;
Chris Wilson5eddb702010-09-11 13:48:45 +01005349
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005350 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5351 if (pll == NULL) {
5352 DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
5353 pipe);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005354 return -EINVAL;
5355 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005356 } else
5357 intel_put_pch_pll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005358
Daniel Vetter2f0c2ad2012-11-29 15:59:35 +01005359 if (is_dp && !is_cpu_edp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005360 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08005361
Daniel Vetterdafd2262012-11-26 17:22:07 +01005362 for_each_encoder_on_crtc(dev, crtc, encoder)
5363 if (encoder->pre_pll_enable)
5364 encoder->pre_pll_enable(encoder);
5365
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005366 if (intel_crtc->pch_pll) {
5367 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005368
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005369 /* Wait for the clocks to stabilize. */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005370 POSTING_READ(intel_crtc->pch_pll->pll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005371 udelay(150);
5372
Eric Anholt8febb292011-03-30 13:01:07 -07005373 /* The pixel multiplier can only be updated once the
5374 * DPLL is enabled and the clocks are stable.
5375 *
5376 * So write it again.
5377 */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005378 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08005379 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005380
Chris Wilson5eddb702010-09-11 13:48:45 +01005381 intel_crtc->lowfreq_avail = false;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005382 if (intel_crtc->pch_pll) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07005383 if (is_lvds && has_reduced_clock && i915_powersave) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005384 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005385 intel_crtc->lowfreq_avail = true;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005386 } else {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005387 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07005388 }
5389 }
5390
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005391 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005392
Daniel Vetter01a415f2012-10-27 15:58:40 +02005393 /* Note, this also computes intel_crtc->fdi_lanes which is used below in
5394 * ironlake_check_fdi_lanes. */
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005395 ironlake_set_m_n(crtc, mode, adjusted_mode);
Chris Wilson5eddb702010-09-11 13:48:45 +01005396
Daniel Vetter01a415f2012-10-27 15:58:40 +02005397 fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005398
Paulo Zanonic8203562012-09-12 10:06:29 -03005399 ironlake_set_pipeconf(crtc, adjusted_mode, dither);
Jesse Barnes79e53942008-11-07 14:24:08 -08005400
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005401 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005402
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005403 /* Set up the display plane register */
5404 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005405 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005406
Daniel Vetter94352cf2012-07-05 22:51:56 +02005407 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005408
5409 intel_update_watermarks(dev);
5410
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03005411 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5412
Daniel Vetter01a415f2012-10-27 15:58:40 +02005413 return fdi_config_ok ? ret : -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005414}
5415
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005416static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5417 struct drm_display_mode *mode,
5418 struct drm_display_mode *adjusted_mode,
5419 int x, int y,
5420 struct drm_framebuffer *fb)
5421{
5422 struct drm_device *dev = crtc->dev;
5423 struct drm_i915_private *dev_priv = dev->dev_private;
5424 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5425 int pipe = intel_crtc->pipe;
5426 int plane = intel_crtc->plane;
5427 int num_connectors = 0;
Daniel Vettered7ef432012-12-06 14:24:21 +01005428 bool is_dp = false, is_cpu_edp = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005429 struct intel_encoder *encoder;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005430 int ret;
5431 bool dither;
5432
5433 for_each_encoder_on_crtc(dev, crtc, encoder) {
5434 switch (encoder->type) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005435 case INTEL_OUTPUT_DISPLAYPORT:
5436 is_dp = true;
5437 break;
5438 case INTEL_OUTPUT_EDP:
5439 is_dp = true;
5440 if (!intel_encoder_is_pch_edp(&encoder->base))
5441 is_cpu_edp = true;
5442 break;
5443 }
5444
5445 num_connectors++;
5446 }
5447
Paulo Zanonia5c961d2012-10-24 15:59:34 -02005448 if (is_cpu_edp)
5449 intel_crtc->cpu_transcoder = TRANSCODER_EDP;
5450 else
5451 intel_crtc->cpu_transcoder = pipe;
5452
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005453 /* We are not sure yet this won't happen. */
5454 WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
5455 INTEL_PCH_TYPE(dev));
5456
5457 WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
5458 num_connectors, pipe_name(pipe));
5459
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005460 WARN_ON(I915_READ(PIPECONF(intel_crtc->cpu_transcoder)) &
Paulo Zanoni1ce42922012-10-05 12:06:01 -03005461 (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
5462
5463 WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
5464
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005465 if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
5466 return -EINVAL;
5467
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005468 /* Ensure that the cursor is valid for the new mode before changing... */
5469 intel_crtc_update_cursor(crtc, true);
5470
5471 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005472 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5473 adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005474
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005475 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
5476 drm_mode_debug_printmodeline(mode);
5477
Daniel Vettered7ef432012-12-06 14:24:21 +01005478 if (is_dp && !is_cpu_edp)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005479 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005480
5481 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005482
5483 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5484
Paulo Zanoni1eb8dfe2012-10-18 12:42:10 -03005485 if (!is_dp || is_cpu_edp)
5486 ironlake_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005487
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005488 haswell_set_pipeconf(crtc, adjusted_mode, dither);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005489
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005490 /* Set up the display plane register */
5491 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5492 POSTING_READ(DSPCNTR(plane));
5493
5494 ret = intel_pipe_set_base(crtc, x, y, fb);
5495
5496 intel_update_watermarks(dev);
5497
5498 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5499
Jesse Barnes79e53942008-11-07 14:24:08 -08005500 return ret;
5501}
5502
Eric Anholtf564048e2011-03-30 13:01:02 -07005503static int intel_crtc_mode_set(struct drm_crtc *crtc,
5504 struct drm_display_mode *mode,
5505 struct drm_display_mode *adjusted_mode,
5506 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005507 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07005508{
5509 struct drm_device *dev = crtc->dev;
5510 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01005511 struct drm_encoder_helper_funcs *encoder_funcs;
5512 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07005513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5514 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07005515 int ret;
5516
Eric Anholt0b701d22011-03-30 13:01:03 -07005517 drm_vblank_pre_modeset(dev, pipe);
5518
Eric Anholtf564048e2011-03-30 13:01:02 -07005519 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005520 x, y, fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08005521 drm_vblank_post_modeset(dev, pipe);
5522
Daniel Vetter9256aa12012-10-31 19:26:13 +01005523 if (ret != 0)
5524 return ret;
5525
5526 for_each_encoder_on_crtc(dev, crtc, encoder) {
5527 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
5528 encoder->base.base.id,
5529 drm_get_encoder_name(&encoder->base),
5530 mode->base.id, mode->name);
5531 encoder_funcs = encoder->base.helper_private;
5532 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
5533 }
5534
5535 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005536}
5537
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005538static bool intel_eld_uptodate(struct drm_connector *connector,
5539 int reg_eldv, uint32_t bits_eldv,
5540 int reg_elda, uint32_t bits_elda,
5541 int reg_edid)
5542{
5543 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5544 uint8_t *eld = connector->eld;
5545 uint32_t i;
5546
5547 i = I915_READ(reg_eldv);
5548 i &= bits_eldv;
5549
5550 if (!eld[0])
5551 return !i;
5552
5553 if (!i)
5554 return false;
5555
5556 i = I915_READ(reg_elda);
5557 i &= ~bits_elda;
5558 I915_WRITE(reg_elda, i);
5559
5560 for (i = 0; i < eld[2]; i++)
5561 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
5562 return false;
5563
5564 return true;
5565}
5566
Wu Fengguange0dac652011-09-05 14:25:34 +08005567static void g4x_write_eld(struct drm_connector *connector,
5568 struct drm_crtc *crtc)
5569{
5570 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5571 uint8_t *eld = connector->eld;
5572 uint32_t eldv;
5573 uint32_t len;
5574 uint32_t i;
5575
5576 i = I915_READ(G4X_AUD_VID_DID);
5577
5578 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
5579 eldv = G4X_ELDV_DEVCL_DEVBLC;
5580 else
5581 eldv = G4X_ELDV_DEVCTG;
5582
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005583 if (intel_eld_uptodate(connector,
5584 G4X_AUD_CNTL_ST, eldv,
5585 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
5586 G4X_HDMIW_HDMIEDID))
5587 return;
5588
Wu Fengguange0dac652011-09-05 14:25:34 +08005589 i = I915_READ(G4X_AUD_CNTL_ST);
5590 i &= ~(eldv | G4X_ELD_ADDR);
5591 len = (i >> 9) & 0x1f; /* ELD buffer size */
5592 I915_WRITE(G4X_AUD_CNTL_ST, i);
5593
5594 if (!eld[0])
5595 return;
5596
5597 len = min_t(uint8_t, eld[2], len);
5598 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5599 for (i = 0; i < len; i++)
5600 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
5601
5602 i = I915_READ(G4X_AUD_CNTL_ST);
5603 i |= eldv;
5604 I915_WRITE(G4X_AUD_CNTL_ST, i);
5605}
5606
Wang Xingchao83358c852012-08-16 22:43:37 +08005607static void haswell_write_eld(struct drm_connector *connector,
5608 struct drm_crtc *crtc)
5609{
5610 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5611 uint8_t *eld = connector->eld;
5612 struct drm_device *dev = crtc->dev;
5613 uint32_t eldv;
5614 uint32_t i;
5615 int len;
5616 int pipe = to_intel_crtc(crtc)->pipe;
5617 int tmp;
5618
5619 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
5620 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
5621 int aud_config = HSW_AUD_CFG(pipe);
5622 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
5623
5624
5625 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
5626
5627 /* Audio output enable */
5628 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
5629 tmp = I915_READ(aud_cntrl_st2);
5630 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
5631 I915_WRITE(aud_cntrl_st2, tmp);
5632
5633 /* Wait for 1 vertical blank */
5634 intel_wait_for_vblank(dev, pipe);
5635
5636 /* Set ELD valid state */
5637 tmp = I915_READ(aud_cntrl_st2);
5638 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
5639 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
5640 I915_WRITE(aud_cntrl_st2, tmp);
5641 tmp = I915_READ(aud_cntrl_st2);
5642 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
5643
5644 /* Enable HDMI mode */
5645 tmp = I915_READ(aud_config);
5646 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
5647 /* clear N_programing_enable and N_value_index */
5648 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
5649 I915_WRITE(aud_config, tmp);
5650
5651 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5652
5653 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
5654
5655 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5656 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5657 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
5658 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5659 } else
5660 I915_WRITE(aud_config, 0);
5661
5662 if (intel_eld_uptodate(connector,
5663 aud_cntrl_st2, eldv,
5664 aud_cntl_st, IBX_ELD_ADDRESS,
5665 hdmiw_hdmiedid))
5666 return;
5667
5668 i = I915_READ(aud_cntrl_st2);
5669 i &= ~eldv;
5670 I915_WRITE(aud_cntrl_st2, i);
5671
5672 if (!eld[0])
5673 return;
5674
5675 i = I915_READ(aud_cntl_st);
5676 i &= ~IBX_ELD_ADDRESS;
5677 I915_WRITE(aud_cntl_st, i);
5678 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
5679 DRM_DEBUG_DRIVER("port num:%d\n", i);
5680
5681 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5682 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5683 for (i = 0; i < len; i++)
5684 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5685
5686 i = I915_READ(aud_cntrl_st2);
5687 i |= eldv;
5688 I915_WRITE(aud_cntrl_st2, i);
5689
5690}
5691
Wu Fengguange0dac652011-09-05 14:25:34 +08005692static void ironlake_write_eld(struct drm_connector *connector,
5693 struct drm_crtc *crtc)
5694{
5695 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5696 uint8_t *eld = connector->eld;
5697 uint32_t eldv;
5698 uint32_t i;
5699 int len;
5700 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06005701 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08005702 int aud_cntl_st;
5703 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08005704 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08005705
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08005706 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08005707 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
5708 aud_config = IBX_AUD_CFG(pipe);
5709 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005710 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08005711 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08005712 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
5713 aud_config = CPT_AUD_CFG(pipe);
5714 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005715 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08005716 }
5717
Wang Xingchao9b138a82012-08-09 16:52:18 +08005718 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08005719
5720 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08005721 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08005722 if (!i) {
5723 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
5724 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005725 eldv = IBX_ELD_VALIDB;
5726 eldv |= IBX_ELD_VALIDB << 4;
5727 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08005728 } else {
5729 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005730 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08005731 }
5732
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005733 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5734 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5735 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06005736 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5737 } else
5738 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005739
5740 if (intel_eld_uptodate(connector,
5741 aud_cntrl_st2, eldv,
5742 aud_cntl_st, IBX_ELD_ADDRESS,
5743 hdmiw_hdmiedid))
5744 return;
5745
Wu Fengguange0dac652011-09-05 14:25:34 +08005746 i = I915_READ(aud_cntrl_st2);
5747 i &= ~eldv;
5748 I915_WRITE(aud_cntrl_st2, i);
5749
5750 if (!eld[0])
5751 return;
5752
Wu Fengguange0dac652011-09-05 14:25:34 +08005753 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005754 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08005755 I915_WRITE(aud_cntl_st, i);
5756
5757 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5758 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5759 for (i = 0; i < len; i++)
5760 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5761
5762 i = I915_READ(aud_cntrl_st2);
5763 i |= eldv;
5764 I915_WRITE(aud_cntrl_st2, i);
5765}
5766
5767void intel_write_eld(struct drm_encoder *encoder,
5768 struct drm_display_mode *mode)
5769{
5770 struct drm_crtc *crtc = encoder->crtc;
5771 struct drm_connector *connector;
5772 struct drm_device *dev = encoder->dev;
5773 struct drm_i915_private *dev_priv = dev->dev_private;
5774
5775 connector = drm_select_eld(encoder, mode);
5776 if (!connector)
5777 return;
5778
5779 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5780 connector->base.id,
5781 drm_get_connector_name(connector),
5782 connector->encoder->base.id,
5783 drm_get_encoder_name(connector->encoder));
5784
5785 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
5786
5787 if (dev_priv->display.write_eld)
5788 dev_priv->display.write_eld(connector, crtc);
5789}
5790
Jesse Barnes79e53942008-11-07 14:24:08 -08005791/** Loads the palette/gamma unit for the CRTC with the prepared values */
5792void intel_crtc_load_lut(struct drm_crtc *crtc)
5793{
5794 struct drm_device *dev = crtc->dev;
5795 struct drm_i915_private *dev_priv = dev->dev_private;
5796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005797 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005798 int i;
5799
5800 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00005801 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08005802 return;
5803
Adam Jacksonf2b115e2009-12-03 17:14:42 -05005804 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07005805 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005806 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005807
Jesse Barnes79e53942008-11-07 14:24:08 -08005808 for (i = 0; i < 256; i++) {
5809 I915_WRITE(palreg + 4 * i,
5810 (intel_crtc->lut_r[i] << 16) |
5811 (intel_crtc->lut_g[i] << 8) |
5812 intel_crtc->lut_b[i]);
5813 }
5814}
5815
Chris Wilson560b85b2010-08-07 11:01:38 +01005816static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
5817{
5818 struct drm_device *dev = crtc->dev;
5819 struct drm_i915_private *dev_priv = dev->dev_private;
5820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5821 bool visible = base != 0;
5822 u32 cntl;
5823
5824 if (intel_crtc->cursor_visible == visible)
5825 return;
5826
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005827 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01005828 if (visible) {
5829 /* On these chipsets we can only modify the base whilst
5830 * the cursor is disabled.
5831 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005832 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01005833
5834 cntl &= ~(CURSOR_FORMAT_MASK);
5835 /* XXX width must be 64, stride 256 => 0x00 << 28 */
5836 cntl |= CURSOR_ENABLE |
5837 CURSOR_GAMMA_ENABLE |
5838 CURSOR_FORMAT_ARGB;
5839 } else
5840 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005841 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01005842
5843 intel_crtc->cursor_visible = visible;
5844}
5845
5846static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
5847{
5848 struct drm_device *dev = crtc->dev;
5849 struct drm_i915_private *dev_priv = dev->dev_private;
5850 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5851 int pipe = intel_crtc->pipe;
5852 bool visible = base != 0;
5853
5854 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08005855 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01005856 if (base) {
5857 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
5858 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5859 cntl |= pipe << 28; /* Connect to correct pipe */
5860 } else {
5861 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5862 cntl |= CURSOR_MODE_DISABLE;
5863 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005864 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01005865
5866 intel_crtc->cursor_visible = visible;
5867 }
5868 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005869 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01005870}
5871
Jesse Barnes65a21cd2011-10-12 11:10:21 -07005872static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
5873{
5874 struct drm_device *dev = crtc->dev;
5875 struct drm_i915_private *dev_priv = dev->dev_private;
5876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5877 int pipe = intel_crtc->pipe;
5878 bool visible = base != 0;
5879
5880 if (intel_crtc->cursor_visible != visible) {
5881 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
5882 if (base) {
5883 cntl &= ~CURSOR_MODE;
5884 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5885 } else {
5886 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5887 cntl |= CURSOR_MODE_DISABLE;
5888 }
5889 I915_WRITE(CURCNTR_IVB(pipe), cntl);
5890
5891 intel_crtc->cursor_visible = visible;
5892 }
5893 /* and commit changes on next vblank */
5894 I915_WRITE(CURBASE_IVB(pipe), base);
5895}
5896
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005897/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01005898static void intel_crtc_update_cursor(struct drm_crtc *crtc,
5899 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005900{
5901 struct drm_device *dev = crtc->dev;
5902 struct drm_i915_private *dev_priv = dev->dev_private;
5903 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5904 int pipe = intel_crtc->pipe;
5905 int x = intel_crtc->cursor_x;
5906 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01005907 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005908 bool visible;
5909
5910 pos = 0;
5911
Chris Wilson6b383a72010-09-13 13:54:26 +01005912 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005913 base = intel_crtc->cursor_addr;
5914 if (x > (int) crtc->fb->width)
5915 base = 0;
5916
5917 if (y > (int) crtc->fb->height)
5918 base = 0;
5919 } else
5920 base = 0;
5921
5922 if (x < 0) {
5923 if (x + intel_crtc->cursor_width < 0)
5924 base = 0;
5925
5926 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
5927 x = -x;
5928 }
5929 pos |= x << CURSOR_X_SHIFT;
5930
5931 if (y < 0) {
5932 if (y + intel_crtc->cursor_height < 0)
5933 base = 0;
5934
5935 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
5936 y = -y;
5937 }
5938 pos |= y << CURSOR_Y_SHIFT;
5939
5940 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01005941 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005942 return;
5943
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03005944 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07005945 I915_WRITE(CURPOS_IVB(pipe), pos);
5946 ivb_update_cursor(crtc, base);
5947 } else {
5948 I915_WRITE(CURPOS(pipe), pos);
5949 if (IS_845G(dev) || IS_I865G(dev))
5950 i845_update_cursor(crtc, base);
5951 else
5952 i9xx_update_cursor(crtc, base);
5953 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005954}
5955
Jesse Barnes79e53942008-11-07 14:24:08 -08005956static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00005957 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08005958 uint32_t handle,
5959 uint32_t width, uint32_t height)
5960{
5961 struct drm_device *dev = crtc->dev;
5962 struct drm_i915_private *dev_priv = dev->dev_private;
5963 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00005964 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005965 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005966 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005967
Jesse Barnes79e53942008-11-07 14:24:08 -08005968 /* if we want to turn off the cursor ignore width and height */
5969 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08005970 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005971 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00005972 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10005973 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005974 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08005975 }
5976
5977 /* Currently we only support 64x64 cursors */
5978 if (width != 64 || height != 64) {
5979 DRM_ERROR("we currently only support 64x64 cursors\n");
5980 return -EINVAL;
5981 }
5982
Chris Wilson05394f32010-11-08 19:18:58 +00005983 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00005984 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08005985 return -ENOENT;
5986
Chris Wilson05394f32010-11-08 19:18:58 +00005987 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005988 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10005989 ret = -ENOMEM;
5990 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08005991 }
5992
Dave Airlie71acb5e2008-12-30 20:31:46 +10005993 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05005994 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05005995 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00005996 if (obj->tiling_mode) {
5997 DRM_ERROR("cursor cannot be tiled\n");
5998 ret = -EINVAL;
5999 goto fail_locked;
6000 }
6001
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006002 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006003 if (ret) {
6004 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006005 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006006 }
6007
Chris Wilsond9e86c02010-11-10 16:40:20 +00006008 ret = i915_gem_object_put_fence(obj);
6009 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006010 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006011 goto fail_unpin;
6012 }
6013
Chris Wilson05394f32010-11-08 19:18:58 +00006014 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006015 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006016 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006017 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006018 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6019 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006020 if (ret) {
6021 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006022 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006023 }
Chris Wilson05394f32010-11-08 19:18:58 +00006024 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006025 }
6026
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006027 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006028 I915_WRITE(CURSIZE, (height << 12) | width);
6029
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006030 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006031 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006032 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006033 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006034 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6035 } else
6036 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006037 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006038 }
Jesse Barnes80824002009-09-10 15:28:06 -07006039
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006040 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006041
6042 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006043 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006044 intel_crtc->cursor_width = width;
6045 intel_crtc->cursor_height = height;
6046
Chris Wilson6b383a72010-09-13 13:54:26 +01006047 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006048
Jesse Barnes79e53942008-11-07 14:24:08 -08006049 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006050fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006051 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006052fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006053 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006054fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006055 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006056 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006057}
6058
6059static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6060{
Jesse Barnes79e53942008-11-07 14:24:08 -08006061 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006062
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006063 intel_crtc->cursor_x = x;
6064 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006065
Chris Wilson6b383a72010-09-13 13:54:26 +01006066 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006067
6068 return 0;
6069}
6070
6071/** Sets the color ramps on behalf of RandR */
6072void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6073 u16 blue, int regno)
6074{
6075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6076
6077 intel_crtc->lut_r[regno] = red >> 8;
6078 intel_crtc->lut_g[regno] = green >> 8;
6079 intel_crtc->lut_b[regno] = blue >> 8;
6080}
6081
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006082void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6083 u16 *blue, int regno)
6084{
6085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6086
6087 *red = intel_crtc->lut_r[regno] << 8;
6088 *green = intel_crtc->lut_g[regno] << 8;
6089 *blue = intel_crtc->lut_b[regno] << 8;
6090}
6091
Jesse Barnes79e53942008-11-07 14:24:08 -08006092static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006093 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006094{
James Simmons72034252010-08-03 01:33:19 +01006095 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006097
James Simmons72034252010-08-03 01:33:19 +01006098 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006099 intel_crtc->lut_r[i] = red[i] >> 8;
6100 intel_crtc->lut_g[i] = green[i] >> 8;
6101 intel_crtc->lut_b[i] = blue[i] >> 8;
6102 }
6103
6104 intel_crtc_load_lut(crtc);
6105}
6106
6107/**
6108 * Get a pipe with a simple mode set on it for doing load-based monitor
6109 * detection.
6110 *
6111 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07006112 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08006113 *
Eric Anholtc751ce42010-03-25 11:48:48 -07006114 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08006115 * configured for it. In the future, it could choose to temporarily disable
6116 * some outputs to free up a pipe for its use.
6117 *
6118 * \return crtc, or NULL if no pipes are available.
6119 */
6120
6121/* VESA 640x480x72Hz mode to set on the pipe */
6122static struct drm_display_mode load_detect_mode = {
6123 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6124 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6125};
6126
Chris Wilsond2dff872011-04-19 08:36:26 +01006127static struct drm_framebuffer *
6128intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006129 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006130 struct drm_i915_gem_object *obj)
6131{
6132 struct intel_framebuffer *intel_fb;
6133 int ret;
6134
6135 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6136 if (!intel_fb) {
6137 drm_gem_object_unreference_unlocked(&obj->base);
6138 return ERR_PTR(-ENOMEM);
6139 }
6140
6141 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6142 if (ret) {
6143 drm_gem_object_unreference_unlocked(&obj->base);
6144 kfree(intel_fb);
6145 return ERR_PTR(ret);
6146 }
6147
6148 return &intel_fb->base;
6149}
6150
6151static u32
6152intel_framebuffer_pitch_for_width(int width, int bpp)
6153{
6154 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6155 return ALIGN(pitch, 64);
6156}
6157
6158static u32
6159intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6160{
6161 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6162 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6163}
6164
6165static struct drm_framebuffer *
6166intel_framebuffer_create_for_mode(struct drm_device *dev,
6167 struct drm_display_mode *mode,
6168 int depth, int bpp)
6169{
6170 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006171 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006172
6173 obj = i915_gem_alloc_object(dev,
6174 intel_framebuffer_size_for_mode(mode, bpp));
6175 if (obj == NULL)
6176 return ERR_PTR(-ENOMEM);
6177
6178 mode_cmd.width = mode->hdisplay;
6179 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006180 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6181 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006182 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006183
6184 return intel_framebuffer_create(dev, &mode_cmd, obj);
6185}
6186
6187static struct drm_framebuffer *
6188mode_fits_in_fbdev(struct drm_device *dev,
6189 struct drm_display_mode *mode)
6190{
6191 struct drm_i915_private *dev_priv = dev->dev_private;
6192 struct drm_i915_gem_object *obj;
6193 struct drm_framebuffer *fb;
6194
6195 if (dev_priv->fbdev == NULL)
6196 return NULL;
6197
6198 obj = dev_priv->fbdev->ifb.obj;
6199 if (obj == NULL)
6200 return NULL;
6201
6202 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006203 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6204 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006205 return NULL;
6206
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006207 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006208 return NULL;
6209
6210 return fb;
6211}
6212
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006213bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006214 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006215 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006216{
6217 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006218 struct intel_encoder *intel_encoder =
6219 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006220 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006221 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006222 struct drm_crtc *crtc = NULL;
6223 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006224 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006225 int i = -1;
6226
Chris Wilsond2dff872011-04-19 08:36:26 +01006227 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6228 connector->base.id, drm_get_connector_name(connector),
6229 encoder->base.id, drm_get_encoder_name(encoder));
6230
Jesse Barnes79e53942008-11-07 14:24:08 -08006231 /*
6232 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006233 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006234 * - if the connector already has an assigned crtc, use it (but make
6235 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006236 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006237 * - try to find the first unused crtc that can drive this connector,
6238 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006239 */
6240
6241 /* See if we already have a CRTC for this connector */
6242 if (encoder->crtc) {
6243 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006244
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 = false;
6247
6248 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006249 if (connector->dpms != DRM_MODE_DPMS_ON)
6250 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006251
Chris Wilson71731882011-04-19 23:10:58 +01006252 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006253 }
6254
6255 /* Find an unused one (if possible) */
6256 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6257 i++;
6258 if (!(encoder->possible_crtcs & (1 << i)))
6259 continue;
6260 if (!possible_crtc->enabled) {
6261 crtc = possible_crtc;
6262 break;
6263 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006264 }
6265
6266 /*
6267 * If we didn't find an unused CRTC, don't use any.
6268 */
6269 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006270 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6271 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006272 }
6273
Daniel Vetterfc303102012-07-09 10:40:58 +02006274 intel_encoder->new_crtc = to_intel_crtc(crtc);
6275 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006276
6277 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006278 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006279 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006280 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006281
Chris Wilson64927112011-04-20 07:25:26 +01006282 if (!mode)
6283 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006284
Chris Wilsond2dff872011-04-19 08:36:26 +01006285 /* We need a framebuffer large enough to accommodate all accesses
6286 * that the plane may generate whilst we perform load detection.
6287 * We can not rely on the fbcon either being present (we get called
6288 * during its initialisation to detect all boot displays, or it may
6289 * not even exist) or that it is large enough to satisfy the
6290 * requested mode.
6291 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006292 fb = mode_fits_in_fbdev(dev, mode);
6293 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006294 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006295 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6296 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006297 } else
6298 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006299 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006300 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006301 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006302 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006303
Daniel Vetter94352cf2012-07-05 22:51:56 +02006304 if (!intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006305 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006306 if (old->release_fb)
6307 old->release_fb->funcs->destroy(old->release_fb);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006308 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006309 }
Chris Wilson71731882011-04-19 23:10:58 +01006310
Jesse Barnes79e53942008-11-07 14:24:08 -08006311 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006312 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006313 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006314}
6315
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006316void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006317 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006318{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006319 struct intel_encoder *intel_encoder =
6320 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006321 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006322
Chris Wilsond2dff872011-04-19 08:36:26 +01006323 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6324 connector->base.id, drm_get_connector_name(connector),
6325 encoder->base.id, drm_get_encoder_name(encoder));
6326
Chris Wilson8261b192011-04-19 23:18:09 +01006327 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006328 struct drm_crtc *crtc = encoder->crtc;
6329
6330 to_intel_connector(connector)->new_encoder = NULL;
6331 intel_encoder->new_crtc = NULL;
6332 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006333
6334 if (old->release_fb)
6335 old->release_fb->funcs->destroy(old->release_fb);
6336
Chris Wilson0622a532011-04-21 09:32:11 +01006337 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006338 }
6339
Eric Anholtc751ce42010-03-25 11:48:48 -07006340 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006341 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6342 connector->funcs->dpms(connector, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006343}
6344
6345/* Returns the clock of the currently programmed mode of the given pipe. */
6346static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6347{
6348 struct drm_i915_private *dev_priv = dev->dev_private;
6349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6350 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006351 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006352 u32 fp;
6353 intel_clock_t clock;
6354
6355 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006356 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006357 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006358 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006359
6360 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006361 if (IS_PINEVIEW(dev)) {
6362 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6363 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006364 } else {
6365 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6366 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6367 }
6368
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006369 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006370 if (IS_PINEVIEW(dev))
6371 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6372 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006373 else
6374 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006375 DPLL_FPA01_P1_POST_DIV_SHIFT);
6376
6377 switch (dpll & DPLL_MODE_MASK) {
6378 case DPLLB_MODE_DAC_SERIAL:
6379 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6380 5 : 10;
6381 break;
6382 case DPLLB_MODE_LVDS:
6383 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6384 7 : 14;
6385 break;
6386 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006387 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006388 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6389 return 0;
6390 }
6391
6392 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08006393 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006394 } else {
6395 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6396
6397 if (is_lvds) {
6398 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6399 DPLL_FPA01_P1_POST_DIV_SHIFT);
6400 clock.p2 = 14;
6401
6402 if ((dpll & PLL_REF_INPUT_MASK) ==
6403 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6404 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08006405 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006406 } else
Shaohua Li21778322009-02-23 15:19:16 +08006407 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006408 } else {
6409 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6410 clock.p1 = 2;
6411 else {
6412 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6413 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6414 }
6415 if (dpll & PLL_P2_DIVIDE_BY_4)
6416 clock.p2 = 4;
6417 else
6418 clock.p2 = 2;
6419
Shaohua Li21778322009-02-23 15:19:16 +08006420 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006421 }
6422 }
6423
6424 /* XXX: It would be nice to validate the clocks, but we can't reuse
6425 * i830PllIsValid() because it relies on the xf86_config connector
6426 * configuration being accurate, which it isn't necessarily.
6427 */
6428
6429 return clock.dot;
6430}
6431
6432/** Returns the currently programmed mode of the given pipe. */
6433struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6434 struct drm_crtc *crtc)
6435{
Jesse Barnes548f2452011-02-17 10:40:53 -08006436 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006437 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006438 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006439 struct drm_display_mode *mode;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006440 int htot = I915_READ(HTOTAL(cpu_transcoder));
6441 int hsync = I915_READ(HSYNC(cpu_transcoder));
6442 int vtot = I915_READ(VTOTAL(cpu_transcoder));
6443 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08006444
6445 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6446 if (!mode)
6447 return NULL;
6448
6449 mode->clock = intel_crtc_clock_get(dev, crtc);
6450 mode->hdisplay = (htot & 0xffff) + 1;
6451 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6452 mode->hsync_start = (hsync & 0xffff) + 1;
6453 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6454 mode->vdisplay = (vtot & 0xffff) + 1;
6455 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6456 mode->vsync_start = (vsync & 0xffff) + 1;
6457 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6458
6459 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006460
6461 return mode;
6462}
6463
Daniel Vetter3dec0092010-08-20 21:40:52 +02006464static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07006465{
6466 struct drm_device *dev = crtc->dev;
6467 drm_i915_private_t *dev_priv = dev->dev_private;
6468 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6469 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006470 int dpll_reg = DPLL(pipe);
6471 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07006472
Eric Anholtbad720f2009-10-22 16:11:14 -07006473 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006474 return;
6475
6476 if (!dev_priv->lvds_downclock_avail)
6477 return;
6478
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006479 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006480 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08006481 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006482
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006483 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006484
6485 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6486 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006487 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006488
Jesse Barnes652c3932009-08-17 13:31:43 -07006489 dpll = I915_READ(dpll_reg);
6490 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08006491 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006492 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006493}
6494
6495static void intel_decrease_pllclock(struct drm_crtc *crtc)
6496{
6497 struct drm_device *dev = crtc->dev;
6498 drm_i915_private_t *dev_priv = dev->dev_private;
6499 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006500
Eric Anholtbad720f2009-10-22 16:11:14 -07006501 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006502 return;
6503
6504 if (!dev_priv->lvds_downclock_avail)
6505 return;
6506
6507 /*
6508 * Since this is called by a timer, we should never get here in
6509 * the manual case.
6510 */
6511 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01006512 int pipe = intel_crtc->pipe;
6513 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02006514 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01006515
Zhao Yakui44d98a62009-10-09 11:39:40 +08006516 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006517
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006518 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006519
Chris Wilson074b5e12012-05-02 12:07:06 +01006520 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006521 dpll |= DISPLAY_RATE_SELECT_FPA1;
6522 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006523 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006524 dpll = I915_READ(dpll_reg);
6525 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08006526 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006527 }
6528
6529}
6530
Chris Wilsonf047e392012-07-21 12:31:41 +01006531void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006532{
Chris Wilsonf047e392012-07-21 12:31:41 +01006533 i915_update_gfx_val(dev->dev_private);
6534}
6535
6536void intel_mark_idle(struct drm_device *dev)
6537{
Chris Wilsonf047e392012-07-21 12:31:41 +01006538}
6539
6540void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
6541{
6542 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07006543 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006544
6545 if (!i915_powersave)
6546 return;
6547
Jesse Barnes652c3932009-08-17 13:31:43 -07006548 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006549 if (!crtc->fb)
6550 continue;
6551
Chris Wilsonf047e392012-07-21 12:31:41 +01006552 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6553 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006554 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006555}
6556
Chris Wilsonf047e392012-07-21 12:31:41 +01006557void intel_mark_fb_idle(struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07006558{
Chris Wilsonf047e392012-07-21 12:31:41 +01006559 struct drm_device *dev = obj->base.dev;
6560 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006561
Chris Wilsonf047e392012-07-21 12:31:41 +01006562 if (!i915_powersave)
Chris Wilsonacb87df2012-05-03 15:47:57 +01006563 return;
6564
Jesse Barnes652c3932009-08-17 13:31:43 -07006565 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6566 if (!crtc->fb)
6567 continue;
6568
Chris Wilsonf047e392012-07-21 12:31:41 +01006569 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6570 intel_decrease_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006571 }
6572}
6573
Jesse Barnes79e53942008-11-07 14:24:08 -08006574static void intel_crtc_destroy(struct drm_crtc *crtc)
6575{
6576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006577 struct drm_device *dev = crtc->dev;
6578 struct intel_unpin_work *work;
6579 unsigned long flags;
6580
6581 spin_lock_irqsave(&dev->event_lock, flags);
6582 work = intel_crtc->unpin_work;
6583 intel_crtc->unpin_work = NULL;
6584 spin_unlock_irqrestore(&dev->event_lock, flags);
6585
6586 if (work) {
6587 cancel_work_sync(&work->work);
6588 kfree(work);
6589 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006590
6591 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006592
Jesse Barnes79e53942008-11-07 14:24:08 -08006593 kfree(intel_crtc);
6594}
6595
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006596static void intel_unpin_work_fn(struct work_struct *__work)
6597{
6598 struct intel_unpin_work *work =
6599 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006600 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006601
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006602 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01006603 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00006604 drm_gem_object_unreference(&work->pending_flip_obj->base);
6605 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00006606
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006607 intel_update_fbc(dev);
6608 mutex_unlock(&dev->struct_mutex);
6609
6610 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
6611 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
6612
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006613 kfree(work);
6614}
6615
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006616static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01006617 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006618{
6619 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6621 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00006622 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006623 unsigned long flags;
6624
6625 /* Ignore early vblank irqs */
6626 if (intel_crtc == NULL)
6627 return;
6628
6629 spin_lock_irqsave(&dev->event_lock, flags);
6630 work = intel_crtc->unpin_work;
6631 if (work == NULL || !work->pending) {
6632 spin_unlock_irqrestore(&dev->event_lock, flags);
6633 return;
6634 }
6635
6636 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006637
Rob Clark45a066e2012-10-08 14:50:40 -05006638 if (work->event)
6639 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006640
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01006641 drm_vblank_put(dev, intel_crtc->pipe);
6642
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006643 spin_unlock_irqrestore(&dev->event_lock, flags);
6644
Chris Wilson05394f32010-11-08 19:18:58 +00006645 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00006646
Chris Wilson5bb61642012-09-27 21:25:58 +01006647 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006648
6649 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07006650
6651 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006652}
6653
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006654void intel_finish_page_flip(struct drm_device *dev, int pipe)
6655{
6656 drm_i915_private_t *dev_priv = dev->dev_private;
6657 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
6658
Mario Kleiner49b14a52010-12-09 07:00:07 +01006659 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006660}
6661
6662void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
6663{
6664 drm_i915_private_t *dev_priv = dev->dev_private;
6665 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
6666
Mario Kleiner49b14a52010-12-09 07:00:07 +01006667 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006668}
6669
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006670void intel_prepare_page_flip(struct drm_device *dev, int plane)
6671{
6672 drm_i915_private_t *dev_priv = dev->dev_private;
6673 struct intel_crtc *intel_crtc =
6674 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
6675 unsigned long flags;
6676
6677 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08006678 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01006679 if ((++intel_crtc->unpin_work->pending) > 1)
6680 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08006681 } else {
6682 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
6683 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006684 spin_unlock_irqrestore(&dev->event_lock, flags);
6685}
6686
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006687static int intel_gen2_queue_flip(struct drm_device *dev,
6688 struct drm_crtc *crtc,
6689 struct drm_framebuffer *fb,
6690 struct drm_i915_gem_object *obj)
6691{
6692 struct drm_i915_private *dev_priv = dev->dev_private;
6693 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006694 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006695 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006696 int ret;
6697
Daniel Vetter6d90c952012-04-26 23:28:05 +02006698 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006699 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006700 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006701
Daniel Vetter6d90c952012-04-26 23:28:05 +02006702 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006703 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006704 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006705
6706 /* Can't queue multiple flips, so wait for the previous
6707 * one to finish before executing the next.
6708 */
6709 if (intel_crtc->plane)
6710 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6711 else
6712 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006713 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6714 intel_ring_emit(ring, MI_NOOP);
6715 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6716 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6717 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02006718 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006719 intel_ring_emit(ring, 0); /* aux display base address, unused */
6720 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006721 return 0;
6722
6723err_unpin:
6724 intel_unpin_fb_obj(obj);
6725err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006726 return ret;
6727}
6728
6729static int intel_gen3_queue_flip(struct drm_device *dev,
6730 struct drm_crtc *crtc,
6731 struct drm_framebuffer *fb,
6732 struct drm_i915_gem_object *obj)
6733{
6734 struct drm_i915_private *dev_priv = dev->dev_private;
6735 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006736 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006737 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006738 int ret;
6739
Daniel Vetter6d90c952012-04-26 23:28:05 +02006740 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006741 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006742 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006743
Daniel Vetter6d90c952012-04-26 23:28:05 +02006744 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006745 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006746 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006747
6748 if (intel_crtc->plane)
6749 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6750 else
6751 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006752 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6753 intel_ring_emit(ring, MI_NOOP);
6754 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
6755 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6756 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02006757 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006758 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006759
Daniel Vetter6d90c952012-04-26 23:28:05 +02006760 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006761 return 0;
6762
6763err_unpin:
6764 intel_unpin_fb_obj(obj);
6765err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006766 return ret;
6767}
6768
6769static int intel_gen4_queue_flip(struct drm_device *dev,
6770 struct drm_crtc *crtc,
6771 struct drm_framebuffer *fb,
6772 struct drm_i915_gem_object *obj)
6773{
6774 struct drm_i915_private *dev_priv = dev->dev_private;
6775 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6776 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006777 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006778 int ret;
6779
Daniel Vetter6d90c952012-04-26 23:28:05 +02006780 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006781 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006782 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006783
Daniel Vetter6d90c952012-04-26 23:28:05 +02006784 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006785 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006786 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006787
6788 /* i965+ uses the linear or tiled offsets from the
6789 * Display Registers (which do not change across a page-flip)
6790 * so we need only reprogram the base address.
6791 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02006792 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6793 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6794 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02006795 intel_ring_emit(ring,
6796 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
6797 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006798
6799 /* XXX Enabling the panel-fitter across page-flip is so far
6800 * untested on non-native modes, so ignore it for now.
6801 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
6802 */
6803 pf = 0;
6804 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006805 intel_ring_emit(ring, pf | pipesrc);
6806 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006807 return 0;
6808
6809err_unpin:
6810 intel_unpin_fb_obj(obj);
6811err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006812 return ret;
6813}
6814
6815static int intel_gen6_queue_flip(struct drm_device *dev,
6816 struct drm_crtc *crtc,
6817 struct drm_framebuffer *fb,
6818 struct drm_i915_gem_object *obj)
6819{
6820 struct drm_i915_private *dev_priv = dev->dev_private;
6821 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006822 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006823 uint32_t pf, pipesrc;
6824 int ret;
6825
Daniel Vetter6d90c952012-04-26 23:28:05 +02006826 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006827 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006828 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006829
Daniel Vetter6d90c952012-04-26 23:28:05 +02006830 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006831 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006832 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006833
Daniel Vetter6d90c952012-04-26 23:28:05 +02006834 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6835 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6836 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Daniel Vetterc2c75132012-07-05 12:17:30 +02006837 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006838
Chris Wilson99d9acd2012-04-17 20:37:00 +01006839 /* Contrary to the suggestions in the documentation,
6840 * "Enable Panel Fitter" does not seem to be required when page
6841 * flipping with a non-native mode, and worse causes a normal
6842 * modeset to fail.
6843 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
6844 */
6845 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006846 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006847 intel_ring_emit(ring, pf | pipesrc);
6848 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006849 return 0;
6850
6851err_unpin:
6852 intel_unpin_fb_obj(obj);
6853err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006854 return ret;
6855}
6856
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006857/*
6858 * On gen7 we currently use the blit ring because (in early silicon at least)
6859 * the render ring doesn't give us interrpts for page flip completion, which
6860 * means clients will hang after the first flip is queued. Fortunately the
6861 * blit ring generates interrupts properly, so use it instead.
6862 */
6863static int intel_gen7_queue_flip(struct drm_device *dev,
6864 struct drm_crtc *crtc,
6865 struct drm_framebuffer *fb,
6866 struct drm_i915_gem_object *obj)
6867{
6868 struct drm_i915_private *dev_priv = dev->dev_private;
6869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6870 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006871 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006872 int ret;
6873
6874 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6875 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006876 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006877
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006878 switch(intel_crtc->plane) {
6879 case PLANE_A:
6880 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
6881 break;
6882 case PLANE_B:
6883 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
6884 break;
6885 case PLANE_C:
6886 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
6887 break;
6888 default:
6889 WARN_ONCE(1, "unknown plane in flip command\n");
6890 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03006891 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006892 }
6893
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006894 ret = intel_ring_begin(ring, 4);
6895 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006896 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006897
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006898 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006899 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Daniel Vetterc2c75132012-07-05 12:17:30 +02006900 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006901 intel_ring_emit(ring, (MI_NOOP));
6902 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006903 return 0;
6904
6905err_unpin:
6906 intel_unpin_fb_obj(obj);
6907err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006908 return ret;
6909}
6910
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006911static int intel_default_queue_flip(struct drm_device *dev,
6912 struct drm_crtc *crtc,
6913 struct drm_framebuffer *fb,
6914 struct drm_i915_gem_object *obj)
6915{
6916 return -ENODEV;
6917}
6918
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006919static int intel_crtc_page_flip(struct drm_crtc *crtc,
6920 struct drm_framebuffer *fb,
6921 struct drm_pending_vblank_event *event)
6922{
6923 struct drm_device *dev = crtc->dev;
6924 struct drm_i915_private *dev_priv = dev->dev_private;
6925 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00006926 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6928 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006929 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01006930 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006931
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03006932 /* Can't change pixel format via MI display flips. */
6933 if (fb->pixel_format != crtc->fb->pixel_format)
6934 return -EINVAL;
6935
6936 /*
6937 * TILEOFF/LINOFF registers can't be changed via MI display flips.
6938 * Note that pitch changes could also affect these register.
6939 */
6940 if (INTEL_INFO(dev)->gen > 3 &&
6941 (fb->offsets[0] != crtc->fb->offsets[0] ||
6942 fb->pitches[0] != crtc->fb->pitches[0]))
6943 return -EINVAL;
6944
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006945 work = kzalloc(sizeof *work, GFP_KERNEL);
6946 if (work == NULL)
6947 return -ENOMEM;
6948
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006949 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006950 work->crtc = crtc;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006951 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08006952 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006953 INIT_WORK(&work->work, intel_unpin_work_fn);
6954
Jesse Barnes7317c75e62011-08-29 09:45:28 -07006955 ret = drm_vblank_get(dev, intel_crtc->pipe);
6956 if (ret)
6957 goto free_work;
6958
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006959 /* We borrow the event spin lock for protecting unpin_work */
6960 spin_lock_irqsave(&dev->event_lock, flags);
6961 if (intel_crtc->unpin_work) {
6962 spin_unlock_irqrestore(&dev->event_lock, flags);
6963 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07006964 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01006965
6966 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006967 return -EBUSY;
6968 }
6969 intel_crtc->unpin_work = work;
6970 spin_unlock_irqrestore(&dev->event_lock, flags);
6971
6972 intel_fb = to_intel_framebuffer(fb);
6973 obj = intel_fb->obj;
6974
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006975 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
6976 flush_workqueue(dev_priv->wq);
6977
Chris Wilson79158102012-05-23 11:13:58 +01006978 ret = i915_mutex_lock_interruptible(dev);
6979 if (ret)
6980 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006981
Jesse Barnes75dfca82010-02-10 15:09:44 -08006982 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00006983 drm_gem_object_reference(&work->old_fb_obj->base);
6984 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006985
6986 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01006987
Chris Wilsone1f99ce2010-10-27 12:45:26 +01006988 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01006989
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01006990 work->enable_stall_check = true;
6991
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006992 atomic_inc(&intel_crtc->unpin_work_count);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01006993
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006994 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
6995 if (ret)
6996 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006997
Chris Wilson7782de32011-07-08 12:22:41 +01006998 intel_disable_fbc(dev);
Chris Wilsonf047e392012-07-21 12:31:41 +01006999 intel_mark_fb_busy(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007000 mutex_unlock(&dev->struct_mutex);
7001
Jesse Barnese5510fa2010-07-01 16:48:37 -07007002 trace_i915_flip_request(intel_crtc->plane, obj);
7003
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007004 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007005
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007006cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007007 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson05394f32010-11-08 19:18:58 +00007008 drm_gem_object_unreference(&work->old_fb_obj->base);
7009 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007010 mutex_unlock(&dev->struct_mutex);
7011
Chris Wilson79158102012-05-23 11:13:58 +01007012cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007013 spin_lock_irqsave(&dev->event_lock, flags);
7014 intel_crtc->unpin_work = NULL;
7015 spin_unlock_irqrestore(&dev->event_lock, flags);
7016
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007017 drm_vblank_put(dev, intel_crtc->pipe);
7018free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007019 kfree(work);
7020
7021 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007022}
7023
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007024static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007025 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7026 .load_lut = intel_crtc_load_lut,
Daniel Vetter976f8a22012-07-08 22:34:21 +02007027 .disable = intel_crtc_noop,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007028};
7029
Daniel Vetter6ed0f792012-07-08 19:41:43 +02007030bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
7031{
7032 struct intel_encoder *other_encoder;
7033 struct drm_crtc *crtc = &encoder->new_crtc->base;
7034
7035 if (WARN_ON(!crtc))
7036 return false;
7037
7038 list_for_each_entry(other_encoder,
7039 &crtc->dev->mode_config.encoder_list,
7040 base.head) {
7041
7042 if (&other_encoder->new_crtc->base != crtc ||
7043 encoder == other_encoder)
7044 continue;
7045 else
7046 return true;
7047 }
7048
7049 return false;
7050}
7051
Daniel Vetter50f56112012-07-02 09:35:43 +02007052static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7053 struct drm_crtc *crtc)
7054{
7055 struct drm_device *dev;
7056 struct drm_crtc *tmp;
7057 int crtc_mask = 1;
7058
7059 WARN(!crtc, "checking null crtc?\n");
7060
7061 dev = crtc->dev;
7062
7063 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7064 if (tmp == crtc)
7065 break;
7066 crtc_mask <<= 1;
7067 }
7068
7069 if (encoder->possible_crtcs & crtc_mask)
7070 return true;
7071 return false;
7072}
7073
Daniel Vetter9a935852012-07-05 22:34:27 +02007074/**
7075 * intel_modeset_update_staged_output_state
7076 *
7077 * Updates the staged output configuration state, e.g. after we've read out the
7078 * current hw state.
7079 */
7080static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7081{
7082 struct intel_encoder *encoder;
7083 struct intel_connector *connector;
7084
7085 list_for_each_entry(connector, &dev->mode_config.connector_list,
7086 base.head) {
7087 connector->new_encoder =
7088 to_intel_encoder(connector->base.encoder);
7089 }
7090
7091 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7092 base.head) {
7093 encoder->new_crtc =
7094 to_intel_crtc(encoder->base.crtc);
7095 }
7096}
7097
7098/**
7099 * intel_modeset_commit_output_state
7100 *
7101 * This function copies the stage display pipe configuration to the real one.
7102 */
7103static void intel_modeset_commit_output_state(struct drm_device *dev)
7104{
7105 struct intel_encoder *encoder;
7106 struct intel_connector *connector;
7107
7108 list_for_each_entry(connector, &dev->mode_config.connector_list,
7109 base.head) {
7110 connector->base.encoder = &connector->new_encoder->base;
7111 }
7112
7113 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7114 base.head) {
7115 encoder->base.crtc = &encoder->new_crtc->base;
7116 }
7117}
7118
Daniel Vetter7758a112012-07-08 19:40:39 +02007119static struct drm_display_mode *
7120intel_modeset_adjusted_mode(struct drm_crtc *crtc,
7121 struct drm_display_mode *mode)
7122{
7123 struct drm_device *dev = crtc->dev;
7124 struct drm_display_mode *adjusted_mode;
7125 struct drm_encoder_helper_funcs *encoder_funcs;
7126 struct intel_encoder *encoder;
7127
7128 adjusted_mode = drm_mode_duplicate(dev, mode);
7129 if (!adjusted_mode)
7130 return ERR_PTR(-ENOMEM);
7131
7132 /* Pass our mode to the connectors and the CRTC to give them a chance to
7133 * adjust it according to limitations or connector properties, and also
7134 * a chance to reject the mode entirely.
7135 */
7136 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7137 base.head) {
7138
7139 if (&encoder->new_crtc->base != crtc)
7140 continue;
7141 encoder_funcs = encoder->base.helper_private;
7142 if (!(encoder_funcs->mode_fixup(&encoder->base, mode,
7143 adjusted_mode))) {
7144 DRM_DEBUG_KMS("Encoder fixup failed\n");
7145 goto fail;
7146 }
7147 }
7148
7149 if (!(intel_crtc_mode_fixup(crtc, mode, adjusted_mode))) {
7150 DRM_DEBUG_KMS("CRTC fixup failed\n");
7151 goto fail;
7152 }
7153 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
7154
7155 return adjusted_mode;
7156fail:
7157 drm_mode_destroy(dev, adjusted_mode);
7158 return ERR_PTR(-EINVAL);
7159}
7160
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007161/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7162 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7163static void
7164intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7165 unsigned *prepare_pipes, unsigned *disable_pipes)
7166{
7167 struct intel_crtc *intel_crtc;
7168 struct drm_device *dev = crtc->dev;
7169 struct intel_encoder *encoder;
7170 struct intel_connector *connector;
7171 struct drm_crtc *tmp_crtc;
7172
7173 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7174
7175 /* Check which crtcs have changed outputs connected to them, these need
7176 * to be part of the prepare_pipes mask. We don't (yet) support global
7177 * modeset across multiple crtcs, so modeset_pipes will only have one
7178 * bit set at most. */
7179 list_for_each_entry(connector, &dev->mode_config.connector_list,
7180 base.head) {
7181 if (connector->base.encoder == &connector->new_encoder->base)
7182 continue;
7183
7184 if (connector->base.encoder) {
7185 tmp_crtc = connector->base.encoder->crtc;
7186
7187 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7188 }
7189
7190 if (connector->new_encoder)
7191 *prepare_pipes |=
7192 1 << connector->new_encoder->new_crtc->pipe;
7193 }
7194
7195 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7196 base.head) {
7197 if (encoder->base.crtc == &encoder->new_crtc->base)
7198 continue;
7199
7200 if (encoder->base.crtc) {
7201 tmp_crtc = encoder->base.crtc;
7202
7203 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7204 }
7205
7206 if (encoder->new_crtc)
7207 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7208 }
7209
7210 /* Check for any pipes that will be fully disabled ... */
7211 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7212 base.head) {
7213 bool used = false;
7214
7215 /* Don't try to disable disabled crtcs. */
7216 if (!intel_crtc->base.enabled)
7217 continue;
7218
7219 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7220 base.head) {
7221 if (encoder->new_crtc == intel_crtc)
7222 used = true;
7223 }
7224
7225 if (!used)
7226 *disable_pipes |= 1 << intel_crtc->pipe;
7227 }
7228
7229
7230 /* set_mode is also used to update properties on life display pipes. */
7231 intel_crtc = to_intel_crtc(crtc);
7232 if (crtc->enabled)
7233 *prepare_pipes |= 1 << intel_crtc->pipe;
7234
7235 /* We only support modeset on one single crtc, hence we need to do that
7236 * only for the passed in crtc iff we change anything else than just
7237 * disable crtcs.
7238 *
7239 * This is actually not true, to be fully compatible with the old crtc
7240 * helper we automatically disable _any_ output (i.e. doesn't need to be
7241 * connected to the crtc we're modesetting on) if it's disconnected.
7242 * Which is a rather nutty api (since changed the output configuration
7243 * without userspace's explicit request can lead to confusion), but
7244 * alas. Hence we currently need to modeset on all pipes we prepare. */
7245 if (*prepare_pipes)
7246 *modeset_pipes = *prepare_pipes;
7247
7248 /* ... and mask these out. */
7249 *modeset_pipes &= ~(*disable_pipes);
7250 *prepare_pipes &= ~(*disable_pipes);
7251}
7252
Daniel Vetterea9d7582012-07-10 10:42:52 +02007253static bool intel_crtc_in_use(struct drm_crtc *crtc)
7254{
7255 struct drm_encoder *encoder;
7256 struct drm_device *dev = crtc->dev;
7257
7258 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
7259 if (encoder->crtc == crtc)
7260 return true;
7261
7262 return false;
7263}
7264
7265static void
7266intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
7267{
7268 struct intel_encoder *intel_encoder;
7269 struct intel_crtc *intel_crtc;
7270 struct drm_connector *connector;
7271
7272 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
7273 base.head) {
7274 if (!intel_encoder->base.crtc)
7275 continue;
7276
7277 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
7278
7279 if (prepare_pipes & (1 << intel_crtc->pipe))
7280 intel_encoder->connectors_active = false;
7281 }
7282
7283 intel_modeset_commit_output_state(dev);
7284
7285 /* Update computed state. */
7286 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7287 base.head) {
7288 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
7289 }
7290
7291 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7292 if (!connector->encoder || !connector->encoder->crtc)
7293 continue;
7294
7295 intel_crtc = to_intel_crtc(connector->encoder->crtc);
7296
7297 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02007298 struct drm_property *dpms_property =
7299 dev->mode_config.dpms_property;
7300
Daniel Vetterea9d7582012-07-10 10:42:52 +02007301 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05007302 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02007303 dpms_property,
7304 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02007305
7306 intel_encoder = to_intel_encoder(connector->encoder);
7307 intel_encoder->connectors_active = true;
7308 }
7309 }
7310
7311}
7312
Daniel Vetter25c5b262012-07-08 22:08:04 +02007313#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
7314 list_for_each_entry((intel_crtc), \
7315 &(dev)->mode_config.crtc_list, \
7316 base.head) \
7317 if (mask & (1 <<(intel_crtc)->pipe)) \
7318
Daniel Vetterb9805142012-08-31 17:37:33 +02007319void
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007320intel_modeset_check_state(struct drm_device *dev)
7321{
7322 struct intel_crtc *crtc;
7323 struct intel_encoder *encoder;
7324 struct intel_connector *connector;
7325
7326 list_for_each_entry(connector, &dev->mode_config.connector_list,
7327 base.head) {
7328 /* This also checks the encoder/connector hw state with the
7329 * ->get_hw_state callbacks. */
7330 intel_connector_check_state(connector);
7331
7332 WARN(&connector->new_encoder->base != connector->base.encoder,
7333 "connector's staged encoder doesn't match current encoder\n");
7334 }
7335
7336 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7337 base.head) {
7338 bool enabled = false;
7339 bool active = false;
7340 enum pipe pipe, tracked_pipe;
7341
7342 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
7343 encoder->base.base.id,
7344 drm_get_encoder_name(&encoder->base));
7345
7346 WARN(&encoder->new_crtc->base != encoder->base.crtc,
7347 "encoder's stage crtc doesn't match current crtc\n");
7348 WARN(encoder->connectors_active && !encoder->base.crtc,
7349 "encoder's active_connectors set, but no crtc\n");
7350
7351 list_for_each_entry(connector, &dev->mode_config.connector_list,
7352 base.head) {
7353 if (connector->base.encoder != &encoder->base)
7354 continue;
7355 enabled = true;
7356 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
7357 active = true;
7358 }
7359 WARN(!!encoder->base.crtc != enabled,
7360 "encoder's enabled state mismatch "
7361 "(expected %i, found %i)\n",
7362 !!encoder->base.crtc, enabled);
7363 WARN(active && !encoder->base.crtc,
7364 "active encoder with no crtc\n");
7365
7366 WARN(encoder->connectors_active != active,
7367 "encoder's computed active state doesn't match tracked active state "
7368 "(expected %i, found %i)\n", active, encoder->connectors_active);
7369
7370 active = encoder->get_hw_state(encoder, &pipe);
7371 WARN(active != encoder->connectors_active,
7372 "encoder's hw state doesn't match sw tracking "
7373 "(expected %i, found %i)\n",
7374 encoder->connectors_active, active);
7375
7376 if (!encoder->base.crtc)
7377 continue;
7378
7379 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
7380 WARN(active && pipe != tracked_pipe,
7381 "active encoder's pipe doesn't match"
7382 "(expected %i, found %i)\n",
7383 tracked_pipe, pipe);
7384
7385 }
7386
7387 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
7388 base.head) {
7389 bool enabled = false;
7390 bool active = false;
7391
7392 DRM_DEBUG_KMS("[CRTC:%d]\n",
7393 crtc->base.base.id);
7394
7395 WARN(crtc->active && !crtc->base.enabled,
7396 "active crtc, but not enabled in sw tracking\n");
7397
7398 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7399 base.head) {
7400 if (encoder->base.crtc != &crtc->base)
7401 continue;
7402 enabled = true;
7403 if (encoder->connectors_active)
7404 active = true;
7405 }
7406 WARN(active != crtc->active,
7407 "crtc's computed active state doesn't match tracked active state "
7408 "(expected %i, found %i)\n", active, crtc->active);
7409 WARN(enabled != crtc->base.enabled,
7410 "crtc's computed enabled state doesn't match tracked enabled state "
7411 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
7412
7413 assert_pipe(dev->dev_private, crtc->pipe, crtc->active);
7414 }
7415}
7416
Daniel Vettera6778b32012-07-02 09:56:42 +02007417bool intel_set_mode(struct drm_crtc *crtc,
7418 struct drm_display_mode *mode,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007419 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02007420{
7421 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02007422 drm_i915_private_t *dev_priv = dev->dev_private;
Tim Gardner3ac18232012-12-07 07:54:26 -07007423 struct drm_display_mode *adjusted_mode, *saved_mode, *saved_hwmode;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007424 struct intel_crtc *intel_crtc;
7425 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Daniel Vettera6778b32012-07-02 09:56:42 +02007426 bool ret = true;
7427
Tim Gardner3ac18232012-12-07 07:54:26 -07007428 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
7429 if (!saved_mode) {
7430 DRM_ERROR("i915: Could not allocate saved display mode.\n");
7431 return false;
7432 }
7433 saved_hwmode = saved_mode + 1;
7434
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007435 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02007436 &prepare_pipes, &disable_pipes);
7437
7438 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7439 modeset_pipes, prepare_pipes, disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007440
Daniel Vetter976f8a22012-07-08 22:34:21 +02007441 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
7442 intel_crtc_disable(&intel_crtc->base);
7443
Tim Gardner3ac18232012-12-07 07:54:26 -07007444 *saved_hwmode = crtc->hwmode;
7445 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007446
Daniel Vetter25c5b262012-07-08 22:08:04 +02007447 /* Hack: Because we don't (yet) support global modeset on multiple
7448 * crtcs, we don't keep track of the new mode for more than one crtc.
7449 * Hence simply check whether any bit is set in modeset_pipes in all the
7450 * pieces of code that are not yet converted to deal with mutliple crtcs
7451 * changing their mode at the same time. */
7452 adjusted_mode = NULL;
7453 if (modeset_pipes) {
7454 adjusted_mode = intel_modeset_adjusted_mode(crtc, mode);
7455 if (IS_ERR(adjusted_mode)) {
Tim Gardner3ac18232012-12-07 07:54:26 -07007456 ret = false;
7457 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007458 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007459 }
7460
Daniel Vetterea9d7582012-07-10 10:42:52 +02007461 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
7462 if (intel_crtc->base.enabled)
7463 dev_priv->display.crtc_disable(&intel_crtc->base);
7464 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007465
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02007466 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
7467 * to set it here already despite that we pass it down the callchain.
7468 */
7469 if (modeset_pipes)
Daniel Vetter25c5b262012-07-08 22:08:04 +02007470 crtc->mode = *mode;
Daniel Vetter7758a112012-07-08 19:40:39 +02007471
Daniel Vetterea9d7582012-07-10 10:42:52 +02007472 /* Only after disabling all output pipelines that will be changed can we
7473 * update the the output configuration. */
7474 intel_modeset_update_state(dev, prepare_pipes);
7475
Daniel Vetter47fab732012-10-26 10:58:18 +02007476 if (dev_priv->display.modeset_global_resources)
7477 dev_priv->display.modeset_global_resources(dev);
7478
Daniel Vettera6778b32012-07-02 09:56:42 +02007479 /* Set up the DPLL and any encoders state that needs to adjust or depend
7480 * on the DPLL.
7481 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007482 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
7483 ret = !intel_crtc_mode_set(&intel_crtc->base,
7484 mode, adjusted_mode,
7485 x, y, fb);
7486 if (!ret)
7487 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02007488 }
7489
7490 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007491 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
7492 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02007493
Daniel Vetter25c5b262012-07-08 22:08:04 +02007494 if (modeset_pipes) {
7495 /* Store real post-adjustment hardware mode. */
7496 crtc->hwmode = *adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007497
Daniel Vetter25c5b262012-07-08 22:08:04 +02007498 /* Calculate and store various constants which
7499 * are later needed by vblank and swap-completion
7500 * timestamping. They are derived from true hwmode.
7501 */
7502 drm_calc_timestamping_constants(crtc);
7503 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007504
7505 /* FIXME: add subpixel order */
7506done:
7507 drm_mode_destroy(dev, adjusted_mode);
Daniel Vetter25c5b262012-07-08 22:08:04 +02007508 if (!ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07007509 crtc->hwmode = *saved_hwmode;
7510 crtc->mode = *saved_mode;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007511 } else {
7512 intel_modeset_check_state(dev);
Daniel Vettera6778b32012-07-02 09:56:42 +02007513 }
7514
Tim Gardner3ac18232012-12-07 07:54:26 -07007515out:
7516 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02007517 return ret;
7518}
7519
Daniel Vetter25c5b262012-07-08 22:08:04 +02007520#undef for_each_intel_crtc_masked
7521
Daniel Vetterd9e55602012-07-04 22:16:09 +02007522static void intel_set_config_free(struct intel_set_config *config)
7523{
7524 if (!config)
7525 return;
7526
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007527 kfree(config->save_connector_encoders);
7528 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02007529 kfree(config);
7530}
7531
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007532static int intel_set_config_save_state(struct drm_device *dev,
7533 struct intel_set_config *config)
7534{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007535 struct drm_encoder *encoder;
7536 struct drm_connector *connector;
7537 int count;
7538
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007539 config->save_encoder_crtcs =
7540 kcalloc(dev->mode_config.num_encoder,
7541 sizeof(struct drm_crtc *), GFP_KERNEL);
7542 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007543 return -ENOMEM;
7544
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007545 config->save_connector_encoders =
7546 kcalloc(dev->mode_config.num_connector,
7547 sizeof(struct drm_encoder *), GFP_KERNEL);
7548 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007549 return -ENOMEM;
7550
7551 /* Copy data. Note that driver private data is not affected.
7552 * Should anything bad happen only the expected state is
7553 * restored, not the drivers personal bookkeeping.
7554 */
7555 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007556 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007557 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007558 }
7559
7560 count = 0;
7561 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007562 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007563 }
7564
7565 return 0;
7566}
7567
7568static void intel_set_config_restore_state(struct drm_device *dev,
7569 struct intel_set_config *config)
7570{
Daniel Vetter9a935852012-07-05 22:34:27 +02007571 struct intel_encoder *encoder;
7572 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007573 int count;
7574
7575 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007576 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7577 encoder->new_crtc =
7578 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007579 }
7580
7581 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007582 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
7583 connector->new_encoder =
7584 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007585 }
7586}
7587
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007588static void
7589intel_set_config_compute_mode_changes(struct drm_mode_set *set,
7590 struct intel_set_config *config)
7591{
7592
7593 /* We should be able to check here if the fb has the same properties
7594 * and then just flip_or_move it */
7595 if (set->crtc->fb != set->fb) {
7596 /* If we have no fb then treat it as a full mode set */
7597 if (set->crtc->fb == NULL) {
7598 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
7599 config->mode_changed = true;
7600 } else if (set->fb == NULL) {
7601 config->mode_changed = true;
7602 } else if (set->fb->depth != set->crtc->fb->depth) {
7603 config->mode_changed = true;
7604 } else if (set->fb->bits_per_pixel !=
7605 set->crtc->fb->bits_per_pixel) {
7606 config->mode_changed = true;
7607 } else
7608 config->fb_changed = true;
7609 }
7610
Daniel Vetter835c5872012-07-10 18:11:08 +02007611 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007612 config->fb_changed = true;
7613
7614 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
7615 DRM_DEBUG_KMS("modes are different, full mode set\n");
7616 drm_mode_debug_printmodeline(&set->crtc->mode);
7617 drm_mode_debug_printmodeline(set->mode);
7618 config->mode_changed = true;
7619 }
7620}
7621
Daniel Vetter2e431052012-07-04 22:42:15 +02007622static int
Daniel Vetter9a935852012-07-05 22:34:27 +02007623intel_modeset_stage_output_state(struct drm_device *dev,
7624 struct drm_mode_set *set,
7625 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02007626{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007627 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02007628 struct intel_connector *connector;
7629 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02007630 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02007631
Daniel Vetter9a935852012-07-05 22:34:27 +02007632 /* The upper layers ensure that we either disabl a crtc or have a list
7633 * of connectors. For paranoia, double-check this. */
7634 WARN_ON(!set->fb && (set->num_connectors != 0));
7635 WARN_ON(set->fb && (set->num_connectors == 0));
7636
Daniel Vetter50f56112012-07-02 09:35:43 +02007637 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007638 list_for_each_entry(connector, &dev->mode_config.connector_list,
7639 base.head) {
7640 /* Otherwise traverse passed in connector list and get encoders
7641 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007642 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02007643 if (set->connectors[ro] == &connector->base) {
7644 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02007645 break;
7646 }
7647 }
7648
Daniel Vetter9a935852012-07-05 22:34:27 +02007649 /* If we disable the crtc, disable all its connectors. Also, if
7650 * the connector is on the changing crtc but not on the new
7651 * connector list, disable it. */
7652 if ((!set->fb || ro == set->num_connectors) &&
7653 connector->base.encoder &&
7654 connector->base.encoder->crtc == set->crtc) {
7655 connector->new_encoder = NULL;
7656
7657 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
7658 connector->base.base.id,
7659 drm_get_connector_name(&connector->base));
7660 }
7661
7662
7663 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007664 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007665 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02007666 }
Daniel Vetter50f56112012-07-02 09:35:43 +02007667
Daniel Vetter9a935852012-07-05 22:34:27 +02007668 /* Disable all disconnected encoders. */
7669 if (connector->base.status == connector_status_disconnected)
7670 connector->new_encoder = NULL;
7671 }
7672 /* connector->new_encoder is now updated for all connectors. */
7673
7674 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007675 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007676 list_for_each_entry(connector, &dev->mode_config.connector_list,
7677 base.head) {
7678 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02007679 continue;
7680
Daniel Vetter9a935852012-07-05 22:34:27 +02007681 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02007682
7683 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02007684 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02007685 new_crtc = set->crtc;
7686 }
7687
7688 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02007689 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
7690 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007691 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02007692 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007693 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
7694
7695 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
7696 connector->base.base.id,
7697 drm_get_connector_name(&connector->base),
7698 new_crtc->base.id);
7699 }
7700
7701 /* Check for any encoders that needs to be disabled. */
7702 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7703 base.head) {
7704 list_for_each_entry(connector,
7705 &dev->mode_config.connector_list,
7706 base.head) {
7707 if (connector->new_encoder == encoder) {
7708 WARN_ON(!connector->new_encoder->new_crtc);
7709
7710 goto next_encoder;
7711 }
7712 }
7713 encoder->new_crtc = NULL;
7714next_encoder:
7715 /* Only now check for crtc changes so we don't miss encoders
7716 * that will be disabled. */
7717 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007718 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007719 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02007720 }
7721 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007722 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007723
Daniel Vetter2e431052012-07-04 22:42:15 +02007724 return 0;
7725}
7726
7727static int intel_crtc_set_config(struct drm_mode_set *set)
7728{
7729 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02007730 struct drm_mode_set save_set;
7731 struct intel_set_config *config;
7732 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02007733
Daniel Vetter8d3e3752012-07-05 16:09:09 +02007734 BUG_ON(!set);
7735 BUG_ON(!set->crtc);
7736 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02007737
7738 if (!set->mode)
7739 set->fb = NULL;
7740
Daniel Vetter431e50f2012-07-10 17:53:42 +02007741 /* The fb helper likes to play gross jokes with ->mode_set_config.
7742 * Unfortunately the crtc helper doesn't do much at all for this case,
7743 * so we have to cope with this madness until the fb helper is fixed up. */
7744 if (set->fb && set->num_connectors == 0)
7745 return 0;
7746
Daniel Vetter2e431052012-07-04 22:42:15 +02007747 if (set->fb) {
7748 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
7749 set->crtc->base.id, set->fb->base.id,
7750 (int)set->num_connectors, set->x, set->y);
7751 } else {
7752 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02007753 }
7754
7755 dev = set->crtc->dev;
7756
7757 ret = -ENOMEM;
7758 config = kzalloc(sizeof(*config), GFP_KERNEL);
7759 if (!config)
7760 goto out_config;
7761
7762 ret = intel_set_config_save_state(dev, config);
7763 if (ret)
7764 goto out_config;
7765
7766 save_set.crtc = set->crtc;
7767 save_set.mode = &set->crtc->mode;
7768 save_set.x = set->crtc->x;
7769 save_set.y = set->crtc->y;
7770 save_set.fb = set->crtc->fb;
7771
7772 /* Compute whether we need a full modeset, only an fb base update or no
7773 * change at all. In the future we might also check whether only the
7774 * mode changed, e.g. for LVDS where we only change the panel fitter in
7775 * such cases. */
7776 intel_set_config_compute_mode_changes(set, config);
7777
Daniel Vetter9a935852012-07-05 22:34:27 +02007778 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02007779 if (ret)
7780 goto fail;
7781
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007782 if (config->mode_changed) {
Daniel Vetter87f1faa2012-07-05 23:36:17 +02007783 if (set->mode) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007784 DRM_DEBUG_KMS("attempting to set mode from"
7785 " userspace\n");
7786 drm_mode_debug_printmodeline(set->mode);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02007787 }
7788
7789 if (!intel_set_mode(set->crtc, set->mode,
7790 set->x, set->y, set->fb)) {
7791 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
7792 set->crtc->base.id);
7793 ret = -EINVAL;
7794 goto fail;
7795 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007796 } else if (config->fb_changed) {
Daniel Vetter4f660f42012-07-02 09:47:37 +02007797 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007798 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02007799 }
7800
Daniel Vetterd9e55602012-07-04 22:16:09 +02007801 intel_set_config_free(config);
7802
Daniel Vetter50f56112012-07-02 09:35:43 +02007803 return 0;
7804
7805fail:
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007806 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02007807
7808 /* Try to restore the config */
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007809 if (config->mode_changed &&
Daniel Vettera6778b32012-07-02 09:56:42 +02007810 !intel_set_mode(save_set.crtc, save_set.mode,
7811 save_set.x, save_set.y, save_set.fb))
Daniel Vetter50f56112012-07-02 09:35:43 +02007812 DRM_ERROR("failed to restore config after modeset failure\n");
7813
Daniel Vetterd9e55602012-07-04 22:16:09 +02007814out_config:
7815 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02007816 return ret;
7817}
7818
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007819static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007820 .cursor_set = intel_crtc_cursor_set,
7821 .cursor_move = intel_crtc_cursor_move,
7822 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02007823 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007824 .destroy = intel_crtc_destroy,
7825 .page_flip = intel_crtc_page_flip,
7826};
7827
Paulo Zanoni79f689a2012-10-05 12:05:52 -03007828static void intel_cpu_pll_init(struct drm_device *dev)
7829{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02007830 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03007831 intel_ddi_pll_init(dev);
7832}
7833
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007834static void intel_pch_pll_init(struct drm_device *dev)
7835{
7836 drm_i915_private_t *dev_priv = dev->dev_private;
7837 int i;
7838
7839 if (dev_priv->num_pch_pll == 0) {
7840 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
7841 return;
7842 }
7843
7844 for (i = 0; i < dev_priv->num_pch_pll; i++) {
7845 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
7846 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
7847 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
7848 }
7849}
7850
Hannes Ederb358d0a2008-12-18 21:18:47 +01007851static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08007852{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007853 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007854 struct intel_crtc *intel_crtc;
7855 int i;
7856
7857 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
7858 if (intel_crtc == NULL)
7859 return;
7860
7861 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
7862
7863 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08007864 for (i = 0; i < 256; i++) {
7865 intel_crtc->lut_r[i] = i;
7866 intel_crtc->lut_g[i] = i;
7867 intel_crtc->lut_b[i] = i;
7868 }
7869
Jesse Barnes80824002009-09-10 15:28:06 -07007870 /* Swap pipes & planes for FBC on pre-965 */
7871 intel_crtc->pipe = pipe;
7872 intel_crtc->plane = pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02007873 intel_crtc->cpu_transcoder = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01007874 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007875 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01007876 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07007877 }
7878
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007879 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
7880 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
7881 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
7882 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
7883
Jesse Barnes5a354202011-06-24 12:19:22 -07007884 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07007885
Jesse Barnes79e53942008-11-07 14:24:08 -08007886 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08007887}
7888
Carl Worth08d7b3d2009-04-29 14:43:54 -07007889int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00007890 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07007891{
Carl Worth08d7b3d2009-04-29 14:43:54 -07007892 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02007893 struct drm_mode_object *drmmode_obj;
7894 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007895
Daniel Vetter1cff8f62012-04-24 09:55:08 +02007896 if (!drm_core_check_feature(dev, DRIVER_MODESET))
7897 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007898
Daniel Vetterc05422d2009-08-11 16:05:30 +02007899 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
7900 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07007901
Daniel Vetterc05422d2009-08-11 16:05:30 +02007902 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07007903 DRM_ERROR("no such CRTC id\n");
7904 return -EINVAL;
7905 }
7906
Daniel Vetterc05422d2009-08-11 16:05:30 +02007907 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
7908 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007909
Daniel Vetterc05422d2009-08-11 16:05:30 +02007910 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007911}
7912
Daniel Vetter66a92782012-07-12 20:08:18 +02007913static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08007914{
Daniel Vetter66a92782012-07-12 20:08:18 +02007915 struct drm_device *dev = encoder->base.dev;
7916 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007917 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007918 int entry = 0;
7919
Daniel Vetter66a92782012-07-12 20:08:18 +02007920 list_for_each_entry(source_encoder,
7921 &dev->mode_config.encoder_list, base.head) {
7922
7923 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08007924 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02007925
7926 /* Intel hw has only one MUX where enocoders could be cloned. */
7927 if (encoder->cloneable && source_encoder->cloneable)
7928 index_mask |= (1 << entry);
7929
Jesse Barnes79e53942008-11-07 14:24:08 -08007930 entry++;
7931 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01007932
Jesse Barnes79e53942008-11-07 14:24:08 -08007933 return index_mask;
7934}
7935
Chris Wilson4d302442010-12-14 19:21:29 +00007936static bool has_edp_a(struct drm_device *dev)
7937{
7938 struct drm_i915_private *dev_priv = dev->dev_private;
7939
7940 if (!IS_MOBILE(dev))
7941 return false;
7942
7943 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
7944 return false;
7945
7946 if (IS_GEN5(dev) &&
7947 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
7948 return false;
7949
7950 return true;
7951}
7952
Jesse Barnes79e53942008-11-07 14:24:08 -08007953static void intel_setup_outputs(struct drm_device *dev)
7954{
Eric Anholt725e30a2009-01-22 13:01:02 -08007955 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007956 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007957 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00007958 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08007959
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00007960 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00007961 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
7962 /* disable the panel fitter on everything but LVDS */
7963 I915_WRITE(PFIT_CONTROL, 0);
7964 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007965
Paulo Zanoniaffa9352012-11-23 15:30:39 -02007966 if (!(HAS_DDI(dev) && (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02007967 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007968
Paulo Zanoniaffa9352012-11-23 15:30:39 -02007969 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03007970 int found;
7971
7972 /* Haswell uses DDI functions to detect digital outputs */
7973 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
7974 /* DDI A only supports eDP */
7975 if (found)
7976 intel_ddi_init(dev, PORT_A);
7977
7978 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
7979 * register */
7980 found = I915_READ(SFUSE_STRAP);
7981
7982 if (found & SFUSE_STRAP_DDIB_DETECTED)
7983 intel_ddi_init(dev, PORT_B);
7984 if (found & SFUSE_STRAP_DDIC_DETECTED)
7985 intel_ddi_init(dev, PORT_C);
7986 if (found & SFUSE_STRAP_DDID_DETECTED)
7987 intel_ddi_init(dev, PORT_D);
7988 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007989 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02007990 dpd_is_edp = intel_dpd_is_edp(dev);
7991
7992 if (has_edp_a(dev))
7993 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007994
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007995 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08007996 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01007997 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007998 if (!found)
Daniel Vetter08d644a2012-07-12 20:19:59 +02007999 intel_hdmi_init(dev, HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008000 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008001 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008002 }
8003
8004 if (I915_READ(HDMIC) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008005 intel_hdmi_init(dev, HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008006
Jesse Barnesb708a1d2012-06-11 14:39:56 -04008007 if (!dpd_is_edp && I915_READ(HDMID) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008008 intel_hdmi_init(dev, HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008009
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008010 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008011 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008012
Daniel Vetter270b3042012-10-27 15:52:05 +02008013 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008014 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008015 } else if (IS_VALLEYVIEW(dev)) {
8016 int found;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008017
Gajanan Bhat19c03922012-09-27 19:13:07 +05308018 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
8019 if (I915_READ(DP_C) & DP_DETECTED)
8020 intel_dp_init(dev, DP_C, PORT_C);
8021
Jesse Barnes4a87d652012-06-15 11:55:16 -07008022 if (I915_READ(SDVOB) & PORT_DETECTED) {
8023 /* SDVOB multiplex with HDMIB */
8024 found = intel_sdvo_init(dev, SDVOB, true);
8025 if (!found)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008026 intel_hdmi_init(dev, SDVOB, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008027 if (!found && (I915_READ(DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008028 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008029 }
8030
8031 if (I915_READ(SDVOC) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008032 intel_hdmi_init(dev, SDVOC, PORT_C);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008033
Zhenyu Wang103a1962009-11-27 11:44:36 +08008034 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008035 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008036
Eric Anholt725e30a2009-01-22 13:01:02 -08008037 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008038 DRM_DEBUG_KMS("probing SDVOB\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008039 found = intel_sdvo_init(dev, SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008040 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8041 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008042 intel_hdmi_init(dev, SDVOB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008043 }
Ma Ling27185ae2009-08-24 13:50:23 +08008044
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008045 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8046 DRM_DEBUG_KMS("probing DP_B\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008047 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008048 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008049 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008050
8051 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008052
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008053 if (I915_READ(SDVOB) & SDVO_DETECTED) {
8054 DRM_DEBUG_KMS("probing SDVOC\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008055 found = intel_sdvo_init(dev, SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008056 }
Ma Ling27185ae2009-08-24 13:50:23 +08008057
8058 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
8059
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008060 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8061 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008062 intel_hdmi_init(dev, SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008063 }
8064 if (SUPPORTS_INTEGRATED_DP(dev)) {
8065 DRM_DEBUG_KMS("probing DP_C\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008066 intel_dp_init(dev, DP_C, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008067 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008068 }
Ma Ling27185ae2009-08-24 13:50:23 +08008069
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008070 if (SUPPORTS_INTEGRATED_DP(dev) &&
8071 (I915_READ(DP_D) & DP_DETECTED)) {
8072 DRM_DEBUG_KMS("probing DP_D\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008073 intel_dp_init(dev, DP_D, PORT_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008074 }
Eric Anholtbad720f2009-10-22 16:11:14 -07008075 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008076 intel_dvo_init(dev);
8077
Zhenyu Wang103a1962009-11-27 11:44:36 +08008078 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008079 intel_tv_init(dev);
8080
Chris Wilson4ef69c72010-09-09 15:14:28 +01008081 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8082 encoder->base.possible_crtcs = encoder->crtc_mask;
8083 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02008084 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08008085 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008086
Paulo Zanoni40579ab2012-07-03 15:57:33 -03008087 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Keith Packard9fb526d2011-09-26 22:24:57 -07008088 ironlake_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02008089
8090 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008091}
8092
8093static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8094{
8095 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008096
8097 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008098 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008099
8100 kfree(intel_fb);
8101}
8102
8103static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008104 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008105 unsigned int *handle)
8106{
8107 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008108 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008109
Chris Wilson05394f32010-11-08 19:18:58 +00008110 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008111}
8112
8113static const struct drm_framebuffer_funcs intel_fb_funcs = {
8114 .destroy = intel_user_framebuffer_destroy,
8115 .create_handle = intel_user_framebuffer_create_handle,
8116};
8117
Dave Airlie38651672010-03-30 05:34:13 +00008118int intel_framebuffer_init(struct drm_device *dev,
8119 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008120 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008121 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008122{
Jesse Barnes79e53942008-11-07 14:24:08 -08008123 int ret;
8124
Chris Wilson05394f32010-11-08 19:18:58 +00008125 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01008126 return -EINVAL;
8127
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008128 if (mode_cmd->pitches[0] & 63)
Chris Wilson57cd6502010-08-08 12:34:44 +01008129 return -EINVAL;
8130
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008131 /* FIXME <= Gen4 stride limits are bit unclear */
8132 if (mode_cmd->pitches[0] > 32768)
8133 return -EINVAL;
8134
8135 if (obj->tiling_mode != I915_TILING_NONE &&
8136 mode_cmd->pitches[0] != obj->stride)
8137 return -EINVAL;
8138
Ville Syrjälä57779d02012-10-31 17:50:14 +02008139 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008140 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02008141 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008142 case DRM_FORMAT_RGB565:
8143 case DRM_FORMAT_XRGB8888:
8144 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008145 break;
8146 case DRM_FORMAT_XRGB1555:
8147 case DRM_FORMAT_ARGB1555:
8148 if (INTEL_INFO(dev)->gen > 3)
8149 return -EINVAL;
8150 break;
8151 case DRM_FORMAT_XBGR8888:
8152 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008153 case DRM_FORMAT_XRGB2101010:
8154 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008155 case DRM_FORMAT_XBGR2101010:
8156 case DRM_FORMAT_ABGR2101010:
8157 if (INTEL_INFO(dev)->gen < 4)
8158 return -EINVAL;
Jesse Barnesb5626742011-06-24 12:19:27 -07008159 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02008160 case DRM_FORMAT_YUYV:
8161 case DRM_FORMAT_UYVY:
8162 case DRM_FORMAT_YVYU:
8163 case DRM_FORMAT_VYUY:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008164 if (INTEL_INFO(dev)->gen < 6)
8165 return -EINVAL;
Chris Wilson57cd6502010-08-08 12:34:44 +01008166 break;
8167 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008168 DRM_DEBUG_KMS("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01008169 return -EINVAL;
8170 }
8171
Ville Syrjälä90f9a332012-10-31 17:50:19 +02008172 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
8173 if (mode_cmd->offsets[0] != 0)
8174 return -EINVAL;
8175
Jesse Barnes79e53942008-11-07 14:24:08 -08008176 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8177 if (ret) {
8178 DRM_ERROR("framebuffer init failed %d\n", ret);
8179 return ret;
8180 }
8181
8182 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08008183 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008184 return 0;
8185}
8186
Jesse Barnes79e53942008-11-07 14:24:08 -08008187static struct drm_framebuffer *
8188intel_user_framebuffer_create(struct drm_device *dev,
8189 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008190 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08008191{
Chris Wilson05394f32010-11-08 19:18:58 +00008192 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008193
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008194 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8195 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00008196 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01008197 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08008198
Chris Wilsond2dff872011-04-19 08:36:26 +01008199 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08008200}
8201
Jesse Barnes79e53942008-11-07 14:24:08 -08008202static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08008203 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00008204 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08008205};
8206
Jesse Barnese70236a2009-09-21 10:42:27 -07008207/* Set up chip specific display functions */
8208static void intel_init_display(struct drm_device *dev)
8209{
8210 struct drm_i915_private *dev_priv = dev->dev_private;
8211
8212 /* We always want a DPMS function */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008213 if (HAS_DDI(dev)) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008214 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02008215 dev_priv->display.crtc_enable = haswell_crtc_enable;
8216 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008217 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008218 dev_priv->display.update_plane = ironlake_update_plane;
8219 } else if (HAS_PCH_SPLIT(dev)) {
Eric Anholtf564048e2011-03-30 13:01:02 -07008220 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008221 dev_priv->display.crtc_enable = ironlake_crtc_enable;
8222 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008223 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008224 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008225 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07008226 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008227 dev_priv->display.crtc_enable = i9xx_crtc_enable;
8228 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008229 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008230 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008231 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008232
Jesse Barnese70236a2009-09-21 10:42:27 -07008233 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07008234 if (IS_VALLEYVIEW(dev))
8235 dev_priv->display.get_display_clock_speed =
8236 valleyview_get_display_clock_speed;
8237 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07008238 dev_priv->display.get_display_clock_speed =
8239 i945_get_display_clock_speed;
8240 else if (IS_I915G(dev))
8241 dev_priv->display.get_display_clock_speed =
8242 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008243 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008244 dev_priv->display.get_display_clock_speed =
8245 i9xx_misc_get_display_clock_speed;
8246 else if (IS_I915GM(dev))
8247 dev_priv->display.get_display_clock_speed =
8248 i915gm_get_display_clock_speed;
8249 else if (IS_I865G(dev))
8250 dev_priv->display.get_display_clock_speed =
8251 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02008252 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008253 dev_priv->display.get_display_clock_speed =
8254 i855_get_display_clock_speed;
8255 else /* 852, 830 */
8256 dev_priv->display.get_display_clock_speed =
8257 i830_get_display_clock_speed;
8258
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008259 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01008260 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008261 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008262 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08008263 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008264 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008265 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07008266 } else if (IS_IVYBRIDGE(dev)) {
8267 /* FIXME: detect B0+ stepping and use auto training */
8268 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008269 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02008270 dev_priv->display.modeset_global_resources =
8271 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03008272 } else if (IS_HASWELL(dev)) {
8273 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08008274 dev_priv->display.write_eld = haswell_write_eld;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008275 } else
8276 dev_priv->display.update_wm = NULL;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008277 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08008278 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07008279 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008280
8281 /* Default just returns -ENODEV to indicate unsupported */
8282 dev_priv->display.queue_flip = intel_default_queue_flip;
8283
8284 switch (INTEL_INFO(dev)->gen) {
8285 case 2:
8286 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8287 break;
8288
8289 case 3:
8290 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8291 break;
8292
8293 case 4:
8294 case 5:
8295 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8296 break;
8297
8298 case 6:
8299 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8300 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008301 case 7:
8302 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8303 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008304 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008305}
8306
Jesse Barnesb690e962010-07-19 13:53:12 -07008307/*
8308 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8309 * resume, or other times. This quirk makes sure that's the case for
8310 * affected systems.
8311 */
Akshay Joshi0206e352011-08-16 15:34:10 -04008312static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07008313{
8314 struct drm_i915_private *dev_priv = dev->dev_private;
8315
8316 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008317 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008318}
8319
Keith Packard435793d2011-07-12 14:56:22 -07008320/*
8321 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8322 */
8323static void quirk_ssc_force_disable(struct drm_device *dev)
8324{
8325 struct drm_i915_private *dev_priv = dev->dev_private;
8326 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008327 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07008328}
8329
Carsten Emde4dca20e2012-03-15 15:56:26 +01008330/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01008331 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
8332 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01008333 */
8334static void quirk_invert_brightness(struct drm_device *dev)
8335{
8336 struct drm_i915_private *dev_priv = dev->dev_private;
8337 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008338 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008339}
8340
8341struct intel_quirk {
8342 int device;
8343 int subsystem_vendor;
8344 int subsystem_device;
8345 void (*hook)(struct drm_device *dev);
8346};
8347
Egbert Eich5f85f1762012-10-14 15:46:38 +02008348/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
8349struct intel_dmi_quirk {
8350 void (*hook)(struct drm_device *dev);
8351 const struct dmi_system_id (*dmi_id_list)[];
8352};
8353
8354static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
8355{
8356 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
8357 return 1;
8358}
8359
8360static const struct intel_dmi_quirk intel_dmi_quirks[] = {
8361 {
8362 .dmi_id_list = &(const struct dmi_system_id[]) {
8363 {
8364 .callback = intel_dmi_reverse_brightness,
8365 .ident = "NCR Corporation",
8366 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
8367 DMI_MATCH(DMI_PRODUCT_NAME, ""),
8368 },
8369 },
8370 { } /* terminating entry */
8371 },
8372 .hook = quirk_invert_brightness,
8373 },
8374};
8375
Ben Widawskyc43b5632012-04-16 14:07:40 -07008376static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07008377 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04008378 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07008379
Jesse Barnesb690e962010-07-19 13:53:12 -07008380 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
8381 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
8382
Jesse Barnesb690e962010-07-19 13:53:12 -07008383 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
8384 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
8385
Daniel Vetterccd0d362012-10-10 23:13:59 +02008386 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07008387 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02008388 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07008389
8390 /* Lenovo U160 cannot use SSC on LVDS */
8391 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02008392
8393 /* Sony Vaio Y cannot use SSC on LVDS */
8394 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01008395
8396 /* Acer Aspire 5734Z must invert backlight brightness */
8397 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07008398};
8399
8400static void intel_init_quirks(struct drm_device *dev)
8401{
8402 struct pci_dev *d = dev->pdev;
8403 int i;
8404
8405 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
8406 struct intel_quirk *q = &intel_quirks[i];
8407
8408 if (d->device == q->device &&
8409 (d->subsystem_vendor == q->subsystem_vendor ||
8410 q->subsystem_vendor == PCI_ANY_ID) &&
8411 (d->subsystem_device == q->subsystem_device ||
8412 q->subsystem_device == PCI_ANY_ID))
8413 q->hook(dev);
8414 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02008415 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
8416 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
8417 intel_dmi_quirks[i].hook(dev);
8418 }
Jesse Barnesb690e962010-07-19 13:53:12 -07008419}
8420
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008421/* Disable the VGA plane that we never use */
8422static void i915_disable_vga(struct drm_device *dev)
8423{
8424 struct drm_i915_private *dev_priv = dev->dev_private;
8425 u8 sr1;
8426 u32 vga_reg;
8427
8428 if (HAS_PCH_SPLIT(dev))
8429 vga_reg = CPU_VGACNTRL;
8430 else
8431 vga_reg = VGACNTRL;
8432
8433 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07008434 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008435 sr1 = inb(VGA_SR_DATA);
8436 outb(sr1 | 1<<5, VGA_SR_DATA);
8437 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
8438 udelay(300);
8439
8440 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
8441 POSTING_READ(vga_reg);
8442}
8443
Daniel Vetterf8175862012-04-10 15:50:11 +02008444void intel_modeset_init_hw(struct drm_device *dev)
8445{
Eugeni Dodonov0232e922012-07-06 15:42:36 -03008446 /* We attempt to init the necessary power wells early in the initialization
8447 * time, so the subsystems that expect power to be enabled can work.
8448 */
8449 intel_init_power_wells(dev);
8450
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03008451 intel_prepare_ddi(dev);
8452
Daniel Vetterf8175862012-04-10 15:50:11 +02008453 intel_init_clock_gating(dev);
8454
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008455 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008456 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008457 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02008458}
8459
Jesse Barnes79e53942008-11-07 14:24:08 -08008460void intel_modeset_init(struct drm_device *dev)
8461{
Jesse Barnes652c3932009-08-17 13:31:43 -07008462 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008463 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008464
8465 drm_mode_config_init(dev);
8466
8467 dev->mode_config.min_width = 0;
8468 dev->mode_config.min_height = 0;
8469
Dave Airlie019d96c2011-09-29 16:20:42 +01008470 dev->mode_config.preferred_depth = 24;
8471 dev->mode_config.prefer_shadow = 1;
8472
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02008473 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08008474
Jesse Barnesb690e962010-07-19 13:53:12 -07008475 intel_init_quirks(dev);
8476
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008477 intel_init_pm(dev);
8478
Jesse Barnese70236a2009-09-21 10:42:27 -07008479 intel_init_display(dev);
8480
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008481 if (IS_GEN2(dev)) {
8482 dev->mode_config.max_width = 2048;
8483 dev->mode_config.max_height = 2048;
8484 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07008485 dev->mode_config.max_width = 4096;
8486 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08008487 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008488 dev->mode_config.max_width = 8192;
8489 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08008490 }
Daniel Vetterdd2757f2012-06-07 15:55:57 +02008491 dev->mode_config.fb_base = dev_priv->mm.gtt_base_addr;
Jesse Barnes79e53942008-11-07 14:24:08 -08008492
Zhao Yakui28c97732009-10-09 11:39:41 +08008493 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10008494 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08008495
Dave Airliea3524f12010-06-06 18:59:41 +10008496 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008497 intel_crtc_init(dev, i);
Jesse Barnes00c2064b2012-01-13 15:48:39 -08008498 ret = intel_plane_init(dev, i);
8499 if (ret)
8500 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08008501 }
8502
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008503 intel_cpu_pll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008504 intel_pch_pll_init(dev);
8505
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008506 /* Just disable it once at startup */
8507 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008508 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00008509
8510 /* Just in case the BIOS is doing something questionable. */
8511 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01008512}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008513
Daniel Vetter24929352012-07-02 20:28:59 +02008514static void
8515intel_connector_break_all_links(struct intel_connector *connector)
8516{
8517 connector->base.dpms = DRM_MODE_DPMS_OFF;
8518 connector->base.encoder = NULL;
8519 connector->encoder->connectors_active = false;
8520 connector->encoder->base.crtc = NULL;
8521}
8522
Daniel Vetter7fad7982012-07-04 17:51:47 +02008523static void intel_enable_pipe_a(struct drm_device *dev)
8524{
8525 struct intel_connector *connector;
8526 struct drm_connector *crt = NULL;
8527 struct intel_load_detect_pipe load_detect_temp;
8528
8529 /* We can't just switch on the pipe A, we need to set things up with a
8530 * proper mode and output configuration. As a gross hack, enable pipe A
8531 * by enabling the load detect pipe once. */
8532 list_for_each_entry(connector,
8533 &dev->mode_config.connector_list,
8534 base.head) {
8535 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
8536 crt = &connector->base;
8537 break;
8538 }
8539 }
8540
8541 if (!crt)
8542 return;
8543
8544 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
8545 intel_release_load_detect_pipe(crt, &load_detect_temp);
8546
8547
8548}
8549
Daniel Vetterfa555832012-10-10 23:14:00 +02008550static bool
8551intel_check_plane_mapping(struct intel_crtc *crtc)
8552{
8553 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8554 u32 reg, val;
8555
8556 if (dev_priv->num_pipe == 1)
8557 return true;
8558
8559 reg = DSPCNTR(!crtc->plane);
8560 val = I915_READ(reg);
8561
8562 if ((val & DISPLAY_PLANE_ENABLE) &&
8563 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
8564 return false;
8565
8566 return true;
8567}
8568
Daniel Vetter24929352012-07-02 20:28:59 +02008569static void intel_sanitize_crtc(struct intel_crtc *crtc)
8570{
8571 struct drm_device *dev = crtc->base.dev;
8572 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02008573 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02008574
Daniel Vetter24929352012-07-02 20:28:59 +02008575 /* Clear any frame start delays used for debugging left by the BIOS */
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008576 reg = PIPECONF(crtc->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02008577 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
8578
8579 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02008580 * disable the crtc (and hence change the state) if it is wrong. Note
8581 * that gen4+ has a fixed plane -> pipe mapping. */
8582 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02008583 struct intel_connector *connector;
8584 bool plane;
8585
Daniel Vetter24929352012-07-02 20:28:59 +02008586 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
8587 crtc->base.base.id);
8588
8589 /* Pipe has the wrong plane attached and the plane is active.
8590 * Temporarily change the plane mapping and disable everything
8591 * ... */
8592 plane = crtc->plane;
8593 crtc->plane = !plane;
8594 dev_priv->display.crtc_disable(&crtc->base);
8595 crtc->plane = plane;
8596
8597 /* ... and break all links. */
8598 list_for_each_entry(connector, &dev->mode_config.connector_list,
8599 base.head) {
8600 if (connector->encoder->base.crtc != &crtc->base)
8601 continue;
8602
8603 intel_connector_break_all_links(connector);
8604 }
8605
8606 WARN_ON(crtc->active);
8607 crtc->base.enabled = false;
8608 }
Daniel Vetter24929352012-07-02 20:28:59 +02008609
Daniel Vetter7fad7982012-07-04 17:51:47 +02008610 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
8611 crtc->pipe == PIPE_A && !crtc->active) {
8612 /* BIOS forgot to enable pipe A, this mostly happens after
8613 * resume. Force-enable the pipe to fix this, the update_dpms
8614 * call below we restore the pipe to the right state, but leave
8615 * the required bits on. */
8616 intel_enable_pipe_a(dev);
8617 }
8618
Daniel Vetter24929352012-07-02 20:28:59 +02008619 /* Adjust the state of the output pipe according to whether we
8620 * have active connectors/encoders. */
8621 intel_crtc_update_dpms(&crtc->base);
8622
8623 if (crtc->active != crtc->base.enabled) {
8624 struct intel_encoder *encoder;
8625
8626 /* This can happen either due to bugs in the get_hw_state
8627 * functions or because the pipe is force-enabled due to the
8628 * pipe A quirk. */
8629 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
8630 crtc->base.base.id,
8631 crtc->base.enabled ? "enabled" : "disabled",
8632 crtc->active ? "enabled" : "disabled");
8633
8634 crtc->base.enabled = crtc->active;
8635
8636 /* Because we only establish the connector -> encoder ->
8637 * crtc links if something is active, this means the
8638 * crtc is now deactivated. Break the links. connector
8639 * -> encoder links are only establish when things are
8640 * actually up, hence no need to break them. */
8641 WARN_ON(crtc->active);
8642
8643 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
8644 WARN_ON(encoder->connectors_active);
8645 encoder->base.crtc = NULL;
8646 }
8647 }
8648}
8649
8650static void intel_sanitize_encoder(struct intel_encoder *encoder)
8651{
8652 struct intel_connector *connector;
8653 struct drm_device *dev = encoder->base.dev;
8654
8655 /* We need to check both for a crtc link (meaning that the
8656 * encoder is active and trying to read from a pipe) and the
8657 * pipe itself being active. */
8658 bool has_active_crtc = encoder->base.crtc &&
8659 to_intel_crtc(encoder->base.crtc)->active;
8660
8661 if (encoder->connectors_active && !has_active_crtc) {
8662 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
8663 encoder->base.base.id,
8664 drm_get_encoder_name(&encoder->base));
8665
8666 /* Connector is active, but has no active pipe. This is
8667 * fallout from our resume register restoring. Disable
8668 * the encoder manually again. */
8669 if (encoder->base.crtc) {
8670 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
8671 encoder->base.base.id,
8672 drm_get_encoder_name(&encoder->base));
8673 encoder->disable(encoder);
8674 }
8675
8676 /* Inconsistent output/port/pipe state happens presumably due to
8677 * a bug in one of the get_hw_state functions. Or someplace else
8678 * in our code, like the register restore mess on resume. Clamp
8679 * things to off as a safer default. */
8680 list_for_each_entry(connector,
8681 &dev->mode_config.connector_list,
8682 base.head) {
8683 if (connector->encoder != encoder)
8684 continue;
8685
8686 intel_connector_break_all_links(connector);
8687 }
8688 }
8689 /* Enabled encoders without active connectors will be fixed in
8690 * the crtc fixup. */
8691}
8692
8693/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
8694 * and i915 state tracking structures. */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01008695void intel_modeset_setup_hw_state(struct drm_device *dev,
8696 bool force_restore)
Daniel Vetter24929352012-07-02 20:28:59 +02008697{
8698 struct drm_i915_private *dev_priv = dev->dev_private;
8699 enum pipe pipe;
8700 u32 tmp;
8701 struct intel_crtc *crtc;
8702 struct intel_encoder *encoder;
8703 struct intel_connector *connector;
8704
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008705 if (HAS_DDI(dev)) {
Paulo Zanonie28d54c2012-10-24 16:09:25 -02008706 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8707
8708 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8709 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8710 case TRANS_DDI_EDP_INPUT_A_ON:
8711 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8712 pipe = PIPE_A;
8713 break;
8714 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8715 pipe = PIPE_B;
8716 break;
8717 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8718 pipe = PIPE_C;
8719 break;
8720 }
8721
8722 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8723 crtc->cpu_transcoder = TRANSCODER_EDP;
8724
8725 DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
8726 pipe_name(pipe));
8727 }
8728 }
8729
Daniel Vetter24929352012-07-02 20:28:59 +02008730 for_each_pipe(pipe) {
8731 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8732
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008733 tmp = I915_READ(PIPECONF(crtc->cpu_transcoder));
Daniel Vetter24929352012-07-02 20:28:59 +02008734 if (tmp & PIPECONF_ENABLE)
8735 crtc->active = true;
8736 else
8737 crtc->active = false;
8738
8739 crtc->base.enabled = crtc->active;
8740
8741 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
8742 crtc->base.base.id,
8743 crtc->active ? "enabled" : "disabled");
8744 }
8745
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008746 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008747 intel_ddi_setup_hw_pll_state(dev);
8748
Daniel Vetter24929352012-07-02 20:28:59 +02008749 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8750 base.head) {
8751 pipe = 0;
8752
8753 if (encoder->get_hw_state(encoder, &pipe)) {
8754 encoder->base.crtc =
8755 dev_priv->pipe_to_crtc_mapping[pipe];
8756 } else {
8757 encoder->base.crtc = NULL;
8758 }
8759
8760 encoder->connectors_active = false;
8761 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
8762 encoder->base.base.id,
8763 drm_get_encoder_name(&encoder->base),
8764 encoder->base.crtc ? "enabled" : "disabled",
8765 pipe);
8766 }
8767
8768 list_for_each_entry(connector, &dev->mode_config.connector_list,
8769 base.head) {
8770 if (connector->get_hw_state(connector)) {
8771 connector->base.dpms = DRM_MODE_DPMS_ON;
8772 connector->encoder->connectors_active = true;
8773 connector->base.encoder = &connector->encoder->base;
8774 } else {
8775 connector->base.dpms = DRM_MODE_DPMS_OFF;
8776 connector->base.encoder = NULL;
8777 }
8778 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
8779 connector->base.base.id,
8780 drm_get_connector_name(&connector->base),
8781 connector->base.encoder ? "enabled" : "disabled");
8782 }
8783
8784 /* HW state is read out, now we need to sanitize this mess. */
8785 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8786 base.head) {
8787 intel_sanitize_encoder(encoder);
8788 }
8789
8790 for_each_pipe(pipe) {
8791 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8792 intel_sanitize_crtc(crtc);
8793 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008794
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01008795 if (force_restore) {
8796 for_each_pipe(pipe) {
8797 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8798 intel_set_mode(&crtc->base, &crtc->base.mode,
8799 crtc->base.x, crtc->base.y, crtc->base.fb);
8800 }
8801 } else {
8802 intel_modeset_update_staged_output_state(dev);
8803 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008804
8805 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +02008806
8807 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01008808}
8809
8810void intel_modeset_gem_init(struct drm_device *dev)
8811{
Chris Wilson1833b132012-05-09 11:56:28 +01008812 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02008813
8814 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +02008815
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01008816 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08008817}
8818
8819void intel_modeset_cleanup(struct drm_device *dev)
8820{
Jesse Barnes652c3932009-08-17 13:31:43 -07008821 struct drm_i915_private *dev_priv = dev->dev_private;
8822 struct drm_crtc *crtc;
8823 struct intel_crtc *intel_crtc;
8824
Keith Packardf87ea762010-10-03 19:36:26 -07008825 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07008826 mutex_lock(&dev->struct_mutex);
8827
Jesse Barnes723bfd72010-10-07 16:01:13 -07008828 intel_unregister_dsm_handler();
8829
8830
Jesse Barnes652c3932009-08-17 13:31:43 -07008831 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8832 /* Skip inactive CRTCs */
8833 if (!crtc->fb)
8834 continue;
8835
8836 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02008837 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008838 }
8839
Chris Wilson973d04f2011-07-08 12:22:37 +01008840 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07008841
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008842 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00008843
Daniel Vetter930ebb42012-06-29 23:32:16 +02008844 ironlake_teardown_rc6(dev);
8845
Jesse Barnes57f350b2012-03-28 13:39:25 -07008846 if (IS_VALLEYVIEW(dev))
8847 vlv_init_dpio(dev);
8848
Kristian Høgsberg69341a52009-11-11 12:19:17 -05008849 mutex_unlock(&dev->struct_mutex);
8850
Daniel Vetter6c0d93502010-08-20 18:26:46 +02008851 /* Disable the irq before mode object teardown, for the irq might
8852 * enqueue unpin/hotplug work. */
8853 drm_irq_uninstall(dev);
8854 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02008855 cancel_work_sync(&dev_priv->rps.work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02008856
Chris Wilson1630fe72011-07-08 12:22:42 +01008857 /* flush any delayed tasks or pending work */
8858 flush_scheduled_work();
8859
Jesse Barnes79e53942008-11-07 14:24:08 -08008860 drm_mode_config_cleanup(dev);
8861}
8862
Dave Airlie28d52042009-09-21 14:33:58 +10008863/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08008864 * Return which encoder is currently attached for connector.
8865 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01008866struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08008867{
Chris Wilsondf0e9242010-09-09 16:20:55 +01008868 return &intel_attached_encoder(connector)->base;
8869}
Jesse Barnes79e53942008-11-07 14:24:08 -08008870
Chris Wilsondf0e9242010-09-09 16:20:55 +01008871void intel_connector_attach_encoder(struct intel_connector *connector,
8872 struct intel_encoder *encoder)
8873{
8874 connector->encoder = encoder;
8875 drm_mode_connector_attach_encoder(&connector->base,
8876 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008877}
Dave Airlie28d52042009-09-21 14:33:58 +10008878
8879/*
8880 * set vga decode state - true == enable VGA decode
8881 */
8882int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
8883{
8884 struct drm_i915_private *dev_priv = dev->dev_private;
8885 u16 gmch_ctrl;
8886
8887 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
8888 if (state)
8889 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
8890 else
8891 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
8892 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
8893 return 0;
8894}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008895
8896#ifdef CONFIG_DEBUG_FS
8897#include <linux/seq_file.h>
8898
8899struct intel_display_error_state {
8900 struct intel_cursor_error_state {
8901 u32 control;
8902 u32 position;
8903 u32 base;
8904 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +01008905 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008906
8907 struct intel_pipe_error_state {
8908 u32 conf;
8909 u32 source;
8910
8911 u32 htotal;
8912 u32 hblank;
8913 u32 hsync;
8914 u32 vtotal;
8915 u32 vblank;
8916 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +01008917 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008918
8919 struct intel_plane_error_state {
8920 u32 control;
8921 u32 stride;
8922 u32 size;
8923 u32 pos;
8924 u32 addr;
8925 u32 surface;
8926 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +01008927 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008928};
8929
8930struct intel_display_error_state *
8931intel_display_capture_error_state(struct drm_device *dev)
8932{
Akshay Joshi0206e352011-08-16 15:34:10 -04008933 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008934 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008935 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008936 int i;
8937
8938 error = kmalloc(sizeof(*error), GFP_ATOMIC);
8939 if (error == NULL)
8940 return NULL;
8941
Damien Lespiau52331302012-08-15 19:23:25 +01008942 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008943 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
8944
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008945 error->cursor[i].control = I915_READ(CURCNTR(i));
8946 error->cursor[i].position = I915_READ(CURPOS(i));
8947 error->cursor[i].base = I915_READ(CURBASE(i));
8948
8949 error->plane[i].control = I915_READ(DSPCNTR(i));
8950 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
8951 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04008952 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008953 error->plane[i].addr = I915_READ(DSPADDR(i));
8954 if (INTEL_INFO(dev)->gen >= 4) {
8955 error->plane[i].surface = I915_READ(DSPSURF(i));
8956 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
8957 }
8958
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008959 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008960 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008961 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
8962 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
8963 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
8964 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
8965 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
8966 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008967 }
8968
8969 return error;
8970}
8971
8972void
8973intel_display_print_error_state(struct seq_file *m,
8974 struct drm_device *dev,
8975 struct intel_display_error_state *error)
8976{
Damien Lespiau52331302012-08-15 19:23:25 +01008977 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008978 int i;
8979
Damien Lespiau52331302012-08-15 19:23:25 +01008980 seq_printf(m, "Num Pipes: %d\n", dev_priv->num_pipe);
8981 for_each_pipe(i) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008982 seq_printf(m, "Pipe [%d]:\n", i);
8983 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
8984 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
8985 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
8986 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
8987 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
8988 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
8989 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
8990 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
8991
8992 seq_printf(m, "Plane [%d]:\n", i);
8993 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
8994 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
8995 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
8996 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
8997 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
8998 if (INTEL_INFO(dev)->gen >= 4) {
8999 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9000 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9001 }
9002
9003 seq_printf(m, "Cursor [%d]:\n", i);
9004 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9005 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9006 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9007 }
9008}
9009#endif