blob: f7f4ef17cbe40c65b0d2c83a66aec2e23fe65d0f [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
Damien Lespiauc36346e2012-12-13 16:09:03 +00001000 if (HAS_PCH_IBX(dev_priv->dev)) {
1001 switch(port->port) {
1002 case PORT_B:
1003 bit = SDE_PORTB_HOTPLUG;
1004 break;
1005 case PORT_C:
1006 bit = SDE_PORTC_HOTPLUG;
1007 break;
1008 case PORT_D:
1009 bit = SDE_PORTD_HOTPLUG;
1010 break;
1011 default:
1012 return true;
1013 }
1014 } else {
1015 switch(port->port) {
1016 case PORT_B:
1017 bit = SDE_PORTB_HOTPLUG_CPT;
1018 break;
1019 case PORT_C:
1020 bit = SDE_PORTC_HOTPLUG_CPT;
1021 break;
1022 case PORT_D:
1023 bit = SDE_PORTD_HOTPLUG_CPT;
1024 break;
1025 default:
1026 return true;
1027 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001028 }
1029
1030 return I915_READ(SDEISR) & bit;
1031}
1032
Jesse Barnesb24e7172011-01-04 15:09:30 -08001033static const char *state_string(bool enabled)
1034{
1035 return enabled ? "on" : "off";
1036}
1037
1038/* Only for pre-ILK configs */
1039static void assert_pll(struct drm_i915_private *dev_priv,
1040 enum pipe pipe, bool state)
1041{
1042 int reg;
1043 u32 val;
1044 bool cur_state;
1045
1046 reg = DPLL(pipe);
1047 val = I915_READ(reg);
1048 cur_state = !!(val & DPLL_VCO_ENABLE);
1049 WARN(cur_state != state,
1050 "PLL state assertion failure (expected %s, current %s)\n",
1051 state_string(state), state_string(cur_state));
1052}
1053#define assert_pll_enabled(d, p) assert_pll(d, p, true)
1054#define assert_pll_disabled(d, p) assert_pll(d, p, false)
1055
Jesse Barnes040484a2011-01-03 12:14:26 -08001056/* For ILK+ */
1057static void assert_pch_pll(struct drm_i915_private *dev_priv,
Chris Wilson92b27b02012-05-20 18:10:50 +01001058 struct intel_pch_pll *pll,
1059 struct intel_crtc *crtc,
1060 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001061{
Jesse Barnes040484a2011-01-03 12:14:26 -08001062 u32 val;
1063 bool cur_state;
1064
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001065 if (HAS_PCH_LPT(dev_priv->dev)) {
1066 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1067 return;
1068 }
1069
Chris Wilson92b27b02012-05-20 18:10:50 +01001070 if (WARN (!pll,
1071 "asserting PCH PLL %s with no PLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001072 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001073
Chris Wilson92b27b02012-05-20 18:10:50 +01001074 val = I915_READ(pll->pll_reg);
1075 cur_state = !!(val & DPLL_VCO_ENABLE);
1076 WARN(cur_state != state,
1077 "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
1078 pll->pll_reg, state_string(state), state_string(cur_state), val);
1079
1080 /* Make sure the selected PLL is correctly attached to the transcoder */
1081 if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001082 u32 pch_dpll;
1083
1084 pch_dpll = I915_READ(PCH_DPLL_SEL);
Chris Wilson92b27b02012-05-20 18:10:50 +01001085 cur_state = pll->pll_reg == _PCH_DPLL_B;
1086 if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
1087 "PLL[%d] not attached to this transcoder %d: %08x\n",
1088 cur_state, crtc->pipe, pch_dpll)) {
1089 cur_state = !!(val >> (4*crtc->pipe + 3));
1090 WARN(cur_state != state,
1091 "PLL[%d] not %s on this transcoder %d: %08x\n",
1092 pll->pll_reg == _PCH_DPLL_B,
1093 state_string(state),
1094 crtc->pipe,
1095 val);
1096 }
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001097 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001098}
Chris Wilson92b27b02012-05-20 18:10:50 +01001099#define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
1100#define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
Jesse Barnes040484a2011-01-03 12:14:26 -08001101
1102static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1103 enum pipe pipe, bool state)
1104{
1105 int reg;
1106 u32 val;
1107 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001108 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1109 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001110
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001111 if (HAS_DDI(dev_priv->dev)) {
1112 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001113 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001114 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001115 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001116 } else {
1117 reg = FDI_TX_CTL(pipe);
1118 val = I915_READ(reg);
1119 cur_state = !!(val & FDI_TX_ENABLE);
1120 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001121 WARN(cur_state != state,
1122 "FDI TX state assertion failure (expected %s, current %s)\n",
1123 state_string(state), state_string(cur_state));
1124}
1125#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1126#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1127
1128static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1129 enum pipe pipe, bool state)
1130{
1131 int reg;
1132 u32 val;
1133 bool cur_state;
1134
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001135 reg = FDI_RX_CTL(pipe);
1136 val = I915_READ(reg);
1137 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001138 WARN(cur_state != state,
1139 "FDI RX state assertion failure (expected %s, current %s)\n",
1140 state_string(state), state_string(cur_state));
1141}
1142#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1143#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1144
1145static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1146 enum pipe pipe)
1147{
1148 int reg;
1149 u32 val;
1150
1151 /* ILK FDI PLL is always enabled */
1152 if (dev_priv->info->gen == 5)
1153 return;
1154
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001155 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001156 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001157 return;
1158
Jesse Barnes040484a2011-01-03 12:14:26 -08001159 reg = FDI_TX_CTL(pipe);
1160 val = I915_READ(reg);
1161 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1162}
1163
1164static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1165 enum pipe pipe)
1166{
1167 int reg;
1168 u32 val;
1169
1170 reg = FDI_RX_CTL(pipe);
1171 val = I915_READ(reg);
1172 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1173}
1174
Jesse Barnesea0760c2011-01-04 15:09:32 -08001175static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1176 enum pipe pipe)
1177{
1178 int pp_reg, lvds_reg;
1179 u32 val;
1180 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001181 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001182
1183 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1184 pp_reg = PCH_PP_CONTROL;
1185 lvds_reg = PCH_LVDS;
1186 } else {
1187 pp_reg = PP_CONTROL;
1188 lvds_reg = LVDS;
1189 }
1190
1191 val = I915_READ(pp_reg);
1192 if (!(val & PANEL_POWER_ON) ||
1193 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1194 locked = false;
1195
1196 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1197 panel_pipe = PIPE_B;
1198
1199 WARN(panel_pipe == pipe && locked,
1200 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001201 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001202}
1203
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001204void assert_pipe(struct drm_i915_private *dev_priv,
1205 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001206{
1207 int reg;
1208 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001209 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001210 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1211 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001212
Daniel Vetter8e636782012-01-22 01:36:48 +01001213 /* if we need the pipe A quirk it must be always on */
1214 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1215 state = true;
1216
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001217 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001218 val = I915_READ(reg);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001219 cur_state = !!(val & PIPECONF_ENABLE);
1220 WARN(cur_state != state,
1221 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001222 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001223}
1224
Chris Wilson931872f2012-01-16 23:01:13 +00001225static void assert_plane(struct drm_i915_private *dev_priv,
1226 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001227{
1228 int reg;
1229 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001230 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001231
1232 reg = DSPCNTR(plane);
1233 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001234 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1235 WARN(cur_state != state,
1236 "plane %c assertion failure (expected %s, current %s)\n",
1237 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001238}
1239
Chris Wilson931872f2012-01-16 23:01:13 +00001240#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1241#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1242
Jesse Barnesb24e7172011-01-04 15:09:30 -08001243static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1244 enum pipe pipe)
1245{
1246 int reg, i;
1247 u32 val;
1248 int cur_pipe;
1249
Jesse Barnes19ec1352011-02-02 12:28:02 -08001250 /* Planes are fixed to pipes on ILK+ */
Adam Jackson28c057942011-10-07 14:38:42 -04001251 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1252 reg = DSPCNTR(pipe);
1253 val = I915_READ(reg);
1254 WARN((val & DISPLAY_PLANE_ENABLE),
1255 "plane %c assertion failure, should be disabled but not\n",
1256 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001257 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001258 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001259
Jesse Barnesb24e7172011-01-04 15:09:30 -08001260 /* Need to check both planes against the pipe */
1261 for (i = 0; i < 2; i++) {
1262 reg = DSPCNTR(i);
1263 val = I915_READ(reg);
1264 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1265 DISPPLANE_SEL_PIPE_SHIFT;
1266 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001267 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1268 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001269 }
1270}
1271
Jesse Barnes92f25842011-01-04 15:09:34 -08001272static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1273{
1274 u32 val;
1275 bool enabled;
1276
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001277 if (HAS_PCH_LPT(dev_priv->dev)) {
1278 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1279 return;
1280 }
1281
Jesse Barnes92f25842011-01-04 15:09:34 -08001282 val = I915_READ(PCH_DREF_CONTROL);
1283 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1284 DREF_SUPERSPREAD_SOURCE_MASK));
1285 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1286}
1287
1288static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1289 enum pipe pipe)
1290{
1291 int reg;
1292 u32 val;
1293 bool enabled;
1294
1295 reg = TRANSCONF(pipe);
1296 val = I915_READ(reg);
1297 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001298 WARN(enabled,
1299 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1300 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001301}
1302
Keith Packard4e634382011-08-06 10:39:45 -07001303static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1304 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001305{
1306 if ((val & DP_PORT_EN) == 0)
1307 return false;
1308
1309 if (HAS_PCH_CPT(dev_priv->dev)) {
1310 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1311 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1312 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1313 return false;
1314 } else {
1315 if ((val & DP_PIPE_MASK) != (pipe << 30))
1316 return false;
1317 }
1318 return true;
1319}
1320
Keith Packard1519b992011-08-06 10:35:34 -07001321static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1322 enum pipe pipe, u32 val)
1323{
1324 if ((val & PORT_ENABLE) == 0)
1325 return false;
1326
1327 if (HAS_PCH_CPT(dev_priv->dev)) {
1328 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1329 return false;
1330 } else {
1331 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1332 return false;
1333 }
1334 return true;
1335}
1336
1337static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1338 enum pipe pipe, u32 val)
1339{
1340 if ((val & LVDS_PORT_EN) == 0)
1341 return false;
1342
1343 if (HAS_PCH_CPT(dev_priv->dev)) {
1344 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1345 return false;
1346 } else {
1347 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1348 return false;
1349 }
1350 return true;
1351}
1352
1353static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1354 enum pipe pipe, u32 val)
1355{
1356 if ((val & ADPA_DAC_ENABLE) == 0)
1357 return false;
1358 if (HAS_PCH_CPT(dev_priv->dev)) {
1359 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1360 return false;
1361 } else {
1362 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1363 return false;
1364 }
1365 return true;
1366}
1367
Jesse Barnes291906f2011-02-02 12:28:03 -08001368static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001369 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001370{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001371 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001372 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001373 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001374 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001375
Daniel Vetter75c5da22012-09-10 21:58:29 +02001376 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1377 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001378 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001379}
1380
1381static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1382 enum pipe pipe, int reg)
1383{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001384 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001385 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001386 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001387 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001388
Daniel Vetter75c5da22012-09-10 21:58:29 +02001389 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & PORT_ENABLE) == 0
1390 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001391 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001392}
1393
1394static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1395 enum pipe pipe)
1396{
1397 int reg;
1398 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001399
Keith Packardf0575e92011-07-25 22:12:43 -07001400 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1401 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1402 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001403
1404 reg = PCH_ADPA;
1405 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001406 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001407 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001408 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001409
1410 reg = PCH_LVDS;
1411 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001412 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001413 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001414 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001415
1416 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1417 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1418 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1419}
1420
Jesse Barnesb24e7172011-01-04 15:09:30 -08001421/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001422 * intel_enable_pll - enable a PLL
1423 * @dev_priv: i915 private structure
1424 * @pipe: pipe PLL to enable
1425 *
1426 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1427 * make sure the PLL reg is writable first though, since the panel write
1428 * protect mechanism may be enabled.
1429 *
1430 * Note! This is for pre-ILK only.
Thomas Richter7434a252012-07-18 19:22:30 +02001431 *
1432 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001433 */
1434static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1435{
1436 int reg;
1437 u32 val;
1438
1439 /* No really, not for ILK+ */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07001440 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001441
1442 /* PLL is protected by panel, make sure we can write it */
1443 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1444 assert_panel_unlocked(dev_priv, pipe);
1445
1446 reg = DPLL(pipe);
1447 val = I915_READ(reg);
1448 val |= DPLL_VCO_ENABLE;
1449
1450 /* We do this three times for luck */
1451 I915_WRITE(reg, val);
1452 POSTING_READ(reg);
1453 udelay(150); /* wait for warmup */
1454 I915_WRITE(reg, val);
1455 POSTING_READ(reg);
1456 udelay(150); /* wait for warmup */
1457 I915_WRITE(reg, val);
1458 POSTING_READ(reg);
1459 udelay(150); /* wait for warmup */
1460}
1461
1462/**
1463 * intel_disable_pll - disable a PLL
1464 * @dev_priv: i915 private structure
1465 * @pipe: pipe PLL to disable
1466 *
1467 * Disable the PLL for @pipe, making sure the pipe is off first.
1468 *
1469 * Note! This is for pre-ILK only.
1470 */
1471static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1472{
1473 int reg;
1474 u32 val;
1475
1476 /* Don't disable pipe A or pipe A PLLs if needed */
1477 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1478 return;
1479
1480 /* Make sure the pipe isn't still relying on us */
1481 assert_pipe_disabled(dev_priv, pipe);
1482
1483 reg = DPLL(pipe);
1484 val = I915_READ(reg);
1485 val &= ~DPLL_VCO_ENABLE;
1486 I915_WRITE(reg, val);
1487 POSTING_READ(reg);
1488}
1489
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001490/* SBI access */
1491static void
1492intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value)
1493{
Daniel Vetter09153002012-12-12 14:06:44 +01001494 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001495
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001496 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001497 100)) {
1498 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001499 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001500 }
1501
1502 I915_WRITE(SBI_ADDR,
1503 (reg << 16));
1504 I915_WRITE(SBI_DATA,
1505 value);
1506 I915_WRITE(SBI_CTL_STAT,
1507 SBI_BUSY |
1508 SBI_CTL_OP_CRWR);
1509
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001510 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001511 100)) {
1512 DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001513 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001514 }
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001515}
1516
1517static u32
1518intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg)
1519{
Daniel Vetter09153002012-12-12 14:06:44 +01001520 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001521
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001522 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001523 100)) {
1524 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001525 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001526 }
1527
1528 I915_WRITE(SBI_ADDR,
1529 (reg << 16));
1530 I915_WRITE(SBI_CTL_STAT,
1531 SBI_BUSY |
1532 SBI_CTL_OP_CRRD);
1533
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001534 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001535 100)) {
1536 DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001537 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001538 }
1539
Daniel Vetter09153002012-12-12 14:06:44 +01001540 return I915_READ(SBI_DATA);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001541}
1542
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001543/**
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001544 * ironlake_enable_pch_pll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001545 * @dev_priv: i915 private structure
1546 * @pipe: pipe PLL to enable
1547 *
1548 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1549 * drives the transcoder clock.
1550 */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001551static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001552{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001553 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Chris Wilson48da64a2012-05-13 20:16:12 +01001554 struct intel_pch_pll *pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001555 int reg;
1556 u32 val;
1557
Chris Wilson48da64a2012-05-13 20:16:12 +01001558 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001559 BUG_ON(dev_priv->info->gen < 5);
Chris Wilson48da64a2012-05-13 20:16:12 +01001560 pll = intel_crtc->pch_pll;
1561 if (pll == NULL)
1562 return;
1563
1564 if (WARN_ON(pll->refcount == 0))
1565 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001566
1567 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1568 pll->pll_reg, pll->active, pll->on,
1569 intel_crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001570
1571 /* PCH refclock must be enabled first */
1572 assert_pch_refclk_enabled(dev_priv);
1573
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001574 if (pll->active++ && pll->on) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001575 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001576 return;
1577 }
1578
1579 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1580
1581 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001582 val = I915_READ(reg);
1583 val |= DPLL_VCO_ENABLE;
1584 I915_WRITE(reg, val);
1585 POSTING_READ(reg);
1586 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001587
1588 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001589}
1590
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001591static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001592{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001593 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1594 struct intel_pch_pll *pll = intel_crtc->pch_pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001595 int reg;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001596 u32 val;
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001597
Jesse Barnes92f25842011-01-04 15:09:34 -08001598 /* PCH only available on ILK+ */
1599 BUG_ON(dev_priv->info->gen < 5);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001600 if (pll == NULL)
1601 return;
1602
Chris Wilson48da64a2012-05-13 20:16:12 +01001603 if (WARN_ON(pll->refcount == 0))
1604 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001605
1606 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1607 pll->pll_reg, pll->active, pll->on,
1608 intel_crtc->base.base.id);
1609
Chris Wilson48da64a2012-05-13 20:16:12 +01001610 if (WARN_ON(pll->active == 0)) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001611 assert_pch_pll_disabled(dev_priv, pll, NULL);
Chris Wilson48da64a2012-05-13 20:16:12 +01001612 return;
1613 }
1614
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001615 if (--pll->active) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001616 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001617 return;
1618 }
1619
1620 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
Jesse Barnes92f25842011-01-04 15:09:34 -08001621
1622 /* Make sure transcoder isn't still depending on us */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001623 assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001624
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001625 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001626 val = I915_READ(reg);
1627 val &= ~DPLL_VCO_ENABLE;
1628 I915_WRITE(reg, val);
1629 POSTING_READ(reg);
1630 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001631
1632 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001633}
1634
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001635static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1636 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001637{
Daniel Vetter23670b322012-11-01 09:15:30 +01001638 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001639 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetter23670b322012-11-01 09:15:30 +01001640 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001641
1642 /* PCH only available on ILK+ */
1643 BUG_ON(dev_priv->info->gen < 5);
1644
1645 /* Make sure PCH DPLL is enabled */
Chris Wilson92b27b02012-05-20 18:10:50 +01001646 assert_pch_pll_enabled(dev_priv,
1647 to_intel_crtc(crtc)->pch_pll,
1648 to_intel_crtc(crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001649
1650 /* FDI must be feeding us bits for PCH ports */
1651 assert_fdi_tx_enabled(dev_priv, pipe);
1652 assert_fdi_rx_enabled(dev_priv, pipe);
1653
Daniel Vetter23670b322012-11-01 09:15:30 +01001654 if (HAS_PCH_CPT(dev)) {
1655 /* Workaround: Set the timing override bit before enabling the
1656 * pch transcoder. */
1657 reg = TRANS_CHICKEN2(pipe);
1658 val = I915_READ(reg);
1659 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1660 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001661 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001662
Jesse Barnes040484a2011-01-03 12:14:26 -08001663 reg = TRANSCONF(pipe);
1664 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001665 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001666
1667 if (HAS_PCH_IBX(dev_priv->dev)) {
1668 /*
1669 * make the BPC in transcoder be consistent with
1670 * that in pipeconf reg.
1671 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001672 val &= ~PIPECONF_BPC_MASK;
1673 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001674 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001675
1676 val &= ~TRANS_INTERLACE_MASK;
1677 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001678 if (HAS_PCH_IBX(dev_priv->dev) &&
1679 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1680 val |= TRANS_LEGACY_INTERLACED_ILK;
1681 else
1682 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001683 else
1684 val |= TRANS_PROGRESSIVE;
1685
Jesse Barnes040484a2011-01-03 12:14:26 -08001686 I915_WRITE(reg, val | TRANS_ENABLE);
1687 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1688 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1689}
1690
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001691static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001692 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001693{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001694 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001695
1696 /* PCH only available on ILK+ */
1697 BUG_ON(dev_priv->info->gen < 5);
1698
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001699 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001700 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001701 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001702
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001703 /* Workaround: set timing override bit. */
1704 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001705 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001706 I915_WRITE(_TRANSA_CHICKEN2, val);
1707
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001708 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001709 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001710
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001711 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1712 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001713 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001714 else
1715 val |= TRANS_PROGRESSIVE;
1716
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001717 I915_WRITE(TRANSCONF(TRANSCODER_A), val);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001718 if (wait_for(I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE, 100))
1719 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001720}
1721
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001722static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1723 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001724{
Daniel Vetter23670b322012-11-01 09:15:30 +01001725 struct drm_device *dev = dev_priv->dev;
1726 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001727
1728 /* FDI relies on the transcoder */
1729 assert_fdi_tx_disabled(dev_priv, pipe);
1730 assert_fdi_rx_disabled(dev_priv, pipe);
1731
Jesse Barnes291906f2011-02-02 12:28:03 -08001732 /* Ports must be off as well */
1733 assert_pch_ports_disabled(dev_priv, pipe);
1734
Jesse Barnes040484a2011-01-03 12:14:26 -08001735 reg = TRANSCONF(pipe);
1736 val = I915_READ(reg);
1737 val &= ~TRANS_ENABLE;
1738 I915_WRITE(reg, val);
1739 /* wait for PCH transcoder off, transcoder state */
1740 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes4c9c18c2011-10-13 09:46:32 -07001741 DRM_ERROR("failed to disable transcoder %d\n", pipe);
Daniel Vetter23670b322012-11-01 09:15:30 +01001742
1743 if (!HAS_PCH_IBX(dev)) {
1744 /* Workaround: Clear the timing override chicken bit again. */
1745 reg = TRANS_CHICKEN2(pipe);
1746 val = I915_READ(reg);
1747 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1748 I915_WRITE(reg, val);
1749 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001750}
1751
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001752static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001753{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001754 u32 val;
1755
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001756 val = I915_READ(_TRANSACONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001757 val &= ~TRANS_ENABLE;
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001758 I915_WRITE(_TRANSACONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001759 /* wait for PCH transcoder off, transcoder state */
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001760 if (wait_for((I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE) == 0, 50))
1761 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001762
1763 /* Workaround: clear timing override bit. */
1764 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001765 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001766 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001767}
1768
1769/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001770 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001771 * @dev_priv: i915 private structure
1772 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001773 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001774 *
1775 * Enable @pipe, making sure that various hardware specific requirements
1776 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1777 *
1778 * @pipe should be %PIPE_A or %PIPE_B.
1779 *
1780 * Will wait until the pipe is actually running (i.e. first vblank) before
1781 * returning.
1782 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001783static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1784 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001785{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001786 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1787 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001788 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001789 int reg;
1790 u32 val;
1791
Paulo Zanoni681e5812012-12-06 11:12:38 -02001792 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001793 pch_transcoder = TRANSCODER_A;
1794 else
1795 pch_transcoder = pipe;
1796
Jesse Barnesb24e7172011-01-04 15:09:30 -08001797 /*
1798 * A pipe without a PLL won't actually be able to drive bits from
1799 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1800 * need the check.
1801 */
1802 if (!HAS_PCH_SPLIT(dev_priv->dev))
1803 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001804 else {
1805 if (pch_port) {
1806 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001807 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001808 assert_fdi_tx_pll_enabled(dev_priv,
1809 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001810 }
1811 /* FIXME: assert CPU port conditions for SNB+ */
1812 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001813
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001814 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001815 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001816 if (val & PIPECONF_ENABLE)
1817 return;
1818
1819 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001820 intel_wait_for_vblank(dev_priv->dev, pipe);
1821}
1822
1823/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001824 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001825 * @dev_priv: i915 private structure
1826 * @pipe: pipe to disable
1827 *
1828 * Disable @pipe, making sure that various hardware specific requirements
1829 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1830 *
1831 * @pipe should be %PIPE_A or %PIPE_B.
1832 *
1833 * Will wait until the pipe has shut down before returning.
1834 */
1835static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1836 enum pipe pipe)
1837{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001838 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1839 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001840 int reg;
1841 u32 val;
1842
1843 /*
1844 * Make sure planes won't keep trying to pump pixels to us,
1845 * or we might hang the display.
1846 */
1847 assert_planes_disabled(dev_priv, pipe);
1848
1849 /* Don't disable pipe A or pipe A PLLs if needed */
1850 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1851 return;
1852
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001853 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001854 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001855 if ((val & PIPECONF_ENABLE) == 0)
1856 return;
1857
1858 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001859 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1860}
1861
Keith Packardd74362c2011-07-28 14:47:14 -07001862/*
1863 * Plane regs are double buffered, going from enabled->disabled needs a
1864 * trigger in order to latch. The display address reg provides this.
1865 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001866void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001867 enum plane plane)
1868{
Damien Lespiau14f86142012-10-29 15:24:49 +00001869 if (dev_priv->info->gen >= 4)
1870 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1871 else
1872 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001873}
1874
Jesse Barnesb24e7172011-01-04 15:09:30 -08001875/**
1876 * intel_enable_plane - enable a display plane on a given pipe
1877 * @dev_priv: i915 private structure
1878 * @plane: plane to enable
1879 * @pipe: pipe being fed
1880 *
1881 * Enable @plane on @pipe, making sure that @pipe is running first.
1882 */
1883static void intel_enable_plane(struct drm_i915_private *dev_priv,
1884 enum plane plane, enum pipe pipe)
1885{
1886 int reg;
1887 u32 val;
1888
1889 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1890 assert_pipe_enabled(dev_priv, pipe);
1891
1892 reg = DSPCNTR(plane);
1893 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001894 if (val & DISPLAY_PLANE_ENABLE)
1895 return;
1896
1897 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001898 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001899 intel_wait_for_vblank(dev_priv->dev, pipe);
1900}
1901
Jesse Barnesb24e7172011-01-04 15:09:30 -08001902/**
1903 * intel_disable_plane - disable a display plane
1904 * @dev_priv: i915 private structure
1905 * @plane: plane to disable
1906 * @pipe: pipe consuming the data
1907 *
1908 * Disable @plane; should be an independent operation.
1909 */
1910static void intel_disable_plane(struct drm_i915_private *dev_priv,
1911 enum plane plane, enum pipe pipe)
1912{
1913 int reg;
1914 u32 val;
1915
1916 reg = DSPCNTR(plane);
1917 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001918 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1919 return;
1920
1921 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001922 intel_flush_display_plane(dev_priv, plane);
1923 intel_wait_for_vblank(dev_priv->dev, pipe);
1924}
1925
Chris Wilson127bd2a2010-07-23 23:32:05 +01001926int
Chris Wilson48b956c2010-09-14 12:50:34 +01001927intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001928 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001929 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001930{
Chris Wilsonce453d82011-02-21 14:43:56 +00001931 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001932 u32 alignment;
1933 int ret;
1934
Chris Wilson05394f32010-11-08 19:18:58 +00001935 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001936 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001937 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1938 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001939 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001940 alignment = 4 * 1024;
1941 else
1942 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001943 break;
1944 case I915_TILING_X:
1945 /* pin() will align the object as required by fence */
1946 alignment = 0;
1947 break;
1948 case I915_TILING_Y:
1949 /* FIXME: Is this true? */
1950 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1951 return -EINVAL;
1952 default:
1953 BUG();
1954 }
1955
Chris Wilsonce453d82011-02-21 14:43:56 +00001956 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001957 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001958 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001959 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001960
1961 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1962 * fence, whereas 965+ only requires a fence if using
1963 * framebuffer compression. For simplicity, we always install
1964 * a fence as the cost is not that onerous.
1965 */
Chris Wilson06d98132012-04-17 15:31:24 +01001966 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001967 if (ret)
1968 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001969
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001970 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001971
Chris Wilsonce453d82011-02-21 14:43:56 +00001972 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001973 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001974
1975err_unpin:
1976 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001977err_interruptible:
1978 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001979 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001980}
1981
Chris Wilson1690e1e2011-12-14 13:57:08 +01001982void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1983{
1984 i915_gem_object_unpin_fence(obj);
1985 i915_gem_object_unpin(obj);
1986}
1987
Daniel Vetterc2c75132012-07-05 12:17:30 +02001988/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1989 * is assumed to be a power-of-two. */
Damien Lespiau5a35e992012-10-26 18:20:12 +01001990unsigned long intel_gen4_compute_offset_xtiled(int *x, int *y,
1991 unsigned int bpp,
1992 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001993{
1994 int tile_rows, tiles;
1995
1996 tile_rows = *y / 8;
1997 *y %= 8;
1998 tiles = *x / (512/bpp);
1999 *x %= 512/bpp;
2000
2001 return tile_rows * pitch * 8 + tiles * 4096;
2002}
2003
Jesse Barnes17638cd2011-06-24 12:19:23 -07002004static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2005 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002006{
2007 struct drm_device *dev = crtc->dev;
2008 struct drm_i915_private *dev_priv = dev->dev_private;
2009 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2010 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002011 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002012 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002013 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002014 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002015 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002016
2017 switch (plane) {
2018 case 0:
2019 case 1:
2020 break;
2021 default:
2022 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2023 return -EINVAL;
2024 }
2025
2026 intel_fb = to_intel_framebuffer(fb);
2027 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002028
Chris Wilson5eddb702010-09-11 13:48:45 +01002029 reg = DSPCNTR(plane);
2030 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002031 /* Mask out pixel format bits in case we change it */
2032 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002033 switch (fb->pixel_format) {
2034 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002035 dspcntr |= DISPPLANE_8BPP;
2036 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002037 case DRM_FORMAT_XRGB1555:
2038 case DRM_FORMAT_ARGB1555:
2039 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002040 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002041 case DRM_FORMAT_RGB565:
2042 dspcntr |= DISPPLANE_BGRX565;
2043 break;
2044 case DRM_FORMAT_XRGB8888:
2045 case DRM_FORMAT_ARGB8888:
2046 dspcntr |= DISPPLANE_BGRX888;
2047 break;
2048 case DRM_FORMAT_XBGR8888:
2049 case DRM_FORMAT_ABGR8888:
2050 dspcntr |= DISPPLANE_RGBX888;
2051 break;
2052 case DRM_FORMAT_XRGB2101010:
2053 case DRM_FORMAT_ARGB2101010:
2054 dspcntr |= DISPPLANE_BGRX101010;
2055 break;
2056 case DRM_FORMAT_XBGR2101010:
2057 case DRM_FORMAT_ABGR2101010:
2058 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002059 break;
2060 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002061 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes81255562010-08-02 12:07:50 -07002062 return -EINVAL;
2063 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002064
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002065 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002066 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002067 dspcntr |= DISPPLANE_TILED;
2068 else
2069 dspcntr &= ~DISPPLANE_TILED;
2070 }
2071
Chris Wilson5eddb702010-09-11 13:48:45 +01002072 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002073
Daniel Vettere506a0c2012-07-05 12:17:29 +02002074 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002075
Daniel Vetterc2c75132012-07-05 12:17:30 +02002076 if (INTEL_INFO(dev)->gen >= 4) {
2077 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002078 intel_gen4_compute_offset_xtiled(&x, &y,
2079 fb->bits_per_pixel / 8,
2080 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002081 linear_offset -= intel_crtc->dspaddr_offset;
2082 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002083 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002084 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002085
2086 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2087 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002088 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002089 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002090 I915_MODIFY_DISPBASE(DSPSURF(plane),
2091 obj->gtt_offset + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002092 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002093 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002094 } else
Daniel Vettere506a0c2012-07-05 12:17:29 +02002095 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002096 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002097
Jesse Barnes17638cd2011-06-24 12:19:23 -07002098 return 0;
2099}
2100
2101static int ironlake_update_plane(struct drm_crtc *crtc,
2102 struct drm_framebuffer *fb, int x, int y)
2103{
2104 struct drm_device *dev = crtc->dev;
2105 struct drm_i915_private *dev_priv = dev->dev_private;
2106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2107 struct intel_framebuffer *intel_fb;
2108 struct drm_i915_gem_object *obj;
2109 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002110 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002111 u32 dspcntr;
2112 u32 reg;
2113
2114 switch (plane) {
2115 case 0:
2116 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002117 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002118 break;
2119 default:
2120 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2121 return -EINVAL;
2122 }
2123
2124 intel_fb = to_intel_framebuffer(fb);
2125 obj = intel_fb->obj;
2126
2127 reg = DSPCNTR(plane);
2128 dspcntr = I915_READ(reg);
2129 /* Mask out pixel format bits in case we change it */
2130 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002131 switch (fb->pixel_format) {
2132 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002133 dspcntr |= DISPPLANE_8BPP;
2134 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002135 case DRM_FORMAT_RGB565:
2136 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002137 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002138 case DRM_FORMAT_XRGB8888:
2139 case DRM_FORMAT_ARGB8888:
2140 dspcntr |= DISPPLANE_BGRX888;
2141 break;
2142 case DRM_FORMAT_XBGR8888:
2143 case DRM_FORMAT_ABGR8888:
2144 dspcntr |= DISPPLANE_RGBX888;
2145 break;
2146 case DRM_FORMAT_XRGB2101010:
2147 case DRM_FORMAT_ARGB2101010:
2148 dspcntr |= DISPPLANE_BGRX101010;
2149 break;
2150 case DRM_FORMAT_XBGR2101010:
2151 case DRM_FORMAT_ABGR2101010:
2152 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002153 break;
2154 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002155 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002156 return -EINVAL;
2157 }
2158
2159 if (obj->tiling_mode != I915_TILING_NONE)
2160 dspcntr |= DISPPLANE_TILED;
2161 else
2162 dspcntr &= ~DISPPLANE_TILED;
2163
2164 /* must disable */
2165 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2166
2167 I915_WRITE(reg, dspcntr);
2168
Daniel Vettere506a0c2012-07-05 12:17:29 +02002169 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002170 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002171 intel_gen4_compute_offset_xtiled(&x, &y,
2172 fb->bits_per_pixel / 8,
2173 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002174 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002175
Daniel Vettere506a0c2012-07-05 12:17:29 +02002176 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2177 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002178 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002179 I915_MODIFY_DISPBASE(DSPSURF(plane),
2180 obj->gtt_offset + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002181 if (IS_HASWELL(dev)) {
2182 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2183 } else {
2184 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2185 I915_WRITE(DSPLINOFF(plane), linear_offset);
2186 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002187 POSTING_READ(reg);
2188
2189 return 0;
2190}
2191
2192/* Assume fb object is pinned & idle & fenced and just update base pointers */
2193static int
2194intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2195 int x, int y, enum mode_set_atomic state)
2196{
2197 struct drm_device *dev = crtc->dev;
2198 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002199
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002200 if (dev_priv->display.disable_fbc)
2201 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002202 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002203
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002204 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002205}
2206
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002207static int
Chris Wilson14667a42012-04-03 17:58:35 +01002208intel_finish_fb(struct drm_framebuffer *old_fb)
2209{
2210 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2211 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2212 bool was_interruptible = dev_priv->mm.interruptible;
2213 int ret;
2214
2215 wait_event(dev_priv->pending_flip_queue,
2216 atomic_read(&dev_priv->mm.wedged) ||
2217 atomic_read(&obj->pending_flip) == 0);
2218
2219 /* Big Hammer, we also need to ensure that any pending
2220 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2221 * current scanout is retired before unpinning the old
2222 * framebuffer.
2223 *
2224 * This should only fail upon a hung GPU, in which case we
2225 * can safely continue.
2226 */
2227 dev_priv->mm.interruptible = false;
2228 ret = i915_gem_object_finish_gpu(obj);
2229 dev_priv->mm.interruptible = was_interruptible;
2230
2231 return ret;
2232}
2233
Ville Syrjälä198598d2012-10-31 17:50:24 +02002234static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2235{
2236 struct drm_device *dev = crtc->dev;
2237 struct drm_i915_master_private *master_priv;
2238 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2239
2240 if (!dev->primary->master)
2241 return;
2242
2243 master_priv = dev->primary->master->driver_priv;
2244 if (!master_priv->sarea_priv)
2245 return;
2246
2247 switch (intel_crtc->pipe) {
2248 case 0:
2249 master_priv->sarea_priv->pipeA_x = x;
2250 master_priv->sarea_priv->pipeA_y = y;
2251 break;
2252 case 1:
2253 master_priv->sarea_priv->pipeB_x = x;
2254 master_priv->sarea_priv->pipeB_y = y;
2255 break;
2256 default:
2257 break;
2258 }
2259}
2260
Chris Wilson14667a42012-04-03 17:58:35 +01002261static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002262intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002263 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002264{
2265 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002266 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002268 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002269 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002270
2271 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002272 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002273 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002274 return 0;
2275 }
2276
Eugeni Dodonov5826eca2012-05-09 15:37:12 -03002277 if(intel_crtc->plane > dev_priv->num_pipe) {
2278 DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
2279 intel_crtc->plane,
2280 dev_priv->num_pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002281 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002282 }
2283
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002284 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002285 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002286 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002287 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002288 if (ret != 0) {
2289 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002290 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002291 return ret;
2292 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002293
Daniel Vetter94352cf2012-07-05 22:51:56 +02002294 if (crtc->fb)
2295 intel_finish_fb(crtc->fb);
Chris Wilson265db952010-09-20 15:41:01 +01002296
Daniel Vetter94352cf2012-07-05 22:51:56 +02002297 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002298 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002299 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002300 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002301 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002302 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002303 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002304
Daniel Vetter94352cf2012-07-05 22:51:56 +02002305 old_fb = crtc->fb;
2306 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002307 crtc->x = x;
2308 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002309
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002310 if (old_fb) {
2311 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002312 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002313 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002314
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002315 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002316 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002317
Ville Syrjälä198598d2012-10-31 17:50:24 +02002318 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002319
2320 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002321}
2322
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002323static void intel_fdi_normal_train(struct drm_crtc *crtc)
2324{
2325 struct drm_device *dev = crtc->dev;
2326 struct drm_i915_private *dev_priv = dev->dev_private;
2327 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2328 int pipe = intel_crtc->pipe;
2329 u32 reg, temp;
2330
2331 /* enable normal train */
2332 reg = FDI_TX_CTL(pipe);
2333 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002334 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002335 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2336 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002337 } else {
2338 temp &= ~FDI_LINK_TRAIN_NONE;
2339 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002340 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002341 I915_WRITE(reg, temp);
2342
2343 reg = FDI_RX_CTL(pipe);
2344 temp = I915_READ(reg);
2345 if (HAS_PCH_CPT(dev)) {
2346 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2347 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2348 } else {
2349 temp &= ~FDI_LINK_TRAIN_NONE;
2350 temp |= FDI_LINK_TRAIN_NONE;
2351 }
2352 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2353
2354 /* wait one idle pattern time */
2355 POSTING_READ(reg);
2356 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002357
2358 /* IVB wants error correction enabled */
2359 if (IS_IVYBRIDGE(dev))
2360 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2361 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002362}
2363
Jesse Barnes291427f2011-07-29 12:42:37 -07002364static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
2365{
2366 struct drm_i915_private *dev_priv = dev->dev_private;
2367 u32 flags = I915_READ(SOUTH_CHICKEN1);
2368
2369 flags |= FDI_PHASE_SYNC_OVR(pipe);
2370 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2371 flags |= FDI_PHASE_SYNC_EN(pipe);
2372 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2373 POSTING_READ(SOUTH_CHICKEN1);
2374}
2375
Daniel Vetter01a415f2012-10-27 15:58:40 +02002376static void ivb_modeset_global_resources(struct drm_device *dev)
2377{
2378 struct drm_i915_private *dev_priv = dev->dev_private;
2379 struct intel_crtc *pipe_B_crtc =
2380 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2381 struct intel_crtc *pipe_C_crtc =
2382 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2383 uint32_t temp;
2384
2385 /* When everything is off disable fdi C so that we could enable fdi B
2386 * with all lanes. XXX: This misses the case where a pipe is not using
2387 * any pch resources and so doesn't need any fdi lanes. */
2388 if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
2389 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2390 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2391
2392 temp = I915_READ(SOUTH_CHICKEN1);
2393 temp &= ~FDI_BC_BIFURCATION_SELECT;
2394 DRM_DEBUG_KMS("disabling fdi C rx\n");
2395 I915_WRITE(SOUTH_CHICKEN1, temp);
2396 }
2397}
2398
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002399/* The FDI link training functions for ILK/Ibexpeak. */
2400static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2401{
2402 struct drm_device *dev = crtc->dev;
2403 struct drm_i915_private *dev_priv = dev->dev_private;
2404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2405 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002406 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002407 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002408
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002409 /* FDI needs bits from pipe & plane first */
2410 assert_pipe_enabled(dev_priv, pipe);
2411 assert_plane_enabled(dev_priv, plane);
2412
Adam Jacksone1a44742010-06-25 15:32:14 -04002413 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2414 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002415 reg = FDI_RX_IMR(pipe);
2416 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002417 temp &= ~FDI_RX_SYMBOL_LOCK;
2418 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002419 I915_WRITE(reg, temp);
2420 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002421 udelay(150);
2422
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002423 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002424 reg = FDI_TX_CTL(pipe);
2425 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002426 temp &= ~(7 << 19);
2427 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002428 temp &= ~FDI_LINK_TRAIN_NONE;
2429 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002430 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002431
Chris Wilson5eddb702010-09-11 13:48:45 +01002432 reg = FDI_RX_CTL(pipe);
2433 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002434 temp &= ~FDI_LINK_TRAIN_NONE;
2435 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002436 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2437
2438 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002439 udelay(150);
2440
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002441 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002442 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2443 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2444 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002445
Chris Wilson5eddb702010-09-11 13:48:45 +01002446 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002447 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002448 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002449 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2450
2451 if ((temp & FDI_RX_BIT_LOCK)) {
2452 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002453 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002454 break;
2455 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002456 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002457 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002458 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002459
2460 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002461 reg = FDI_TX_CTL(pipe);
2462 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002463 temp &= ~FDI_LINK_TRAIN_NONE;
2464 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002465 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002466
Chris Wilson5eddb702010-09-11 13:48:45 +01002467 reg = FDI_RX_CTL(pipe);
2468 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002469 temp &= ~FDI_LINK_TRAIN_NONE;
2470 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002471 I915_WRITE(reg, temp);
2472
2473 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002474 udelay(150);
2475
Chris Wilson5eddb702010-09-11 13:48:45 +01002476 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002477 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002478 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002479 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2480
2481 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002482 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002483 DRM_DEBUG_KMS("FDI train 2 done.\n");
2484 break;
2485 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002486 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002487 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002488 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002489
2490 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002491
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002492}
2493
Akshay Joshi0206e352011-08-16 15:34:10 -04002494static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002495 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2496 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2497 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2498 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2499};
2500
2501/* The FDI link training functions for SNB/Cougarpoint. */
2502static void gen6_fdi_link_train(struct drm_crtc *crtc)
2503{
2504 struct drm_device *dev = crtc->dev;
2505 struct drm_i915_private *dev_priv = dev->dev_private;
2506 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2507 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002508 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002509
Adam Jacksone1a44742010-06-25 15:32:14 -04002510 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2511 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002512 reg = FDI_RX_IMR(pipe);
2513 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002514 temp &= ~FDI_RX_SYMBOL_LOCK;
2515 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002516 I915_WRITE(reg, temp);
2517
2518 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002519 udelay(150);
2520
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002521 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002522 reg = FDI_TX_CTL(pipe);
2523 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002524 temp &= ~(7 << 19);
2525 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002526 temp &= ~FDI_LINK_TRAIN_NONE;
2527 temp |= FDI_LINK_TRAIN_PATTERN_1;
2528 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2529 /* SNB-B */
2530 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002531 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002532
Daniel Vetterd74cf322012-10-26 10:58:13 +02002533 I915_WRITE(FDI_RX_MISC(pipe),
2534 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2535
Chris Wilson5eddb702010-09-11 13:48:45 +01002536 reg = FDI_RX_CTL(pipe);
2537 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002538 if (HAS_PCH_CPT(dev)) {
2539 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2540 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2541 } else {
2542 temp &= ~FDI_LINK_TRAIN_NONE;
2543 temp |= FDI_LINK_TRAIN_PATTERN_1;
2544 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002545 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2546
2547 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002548 udelay(150);
2549
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002550 cpt_phase_pointer_enable(dev, pipe);
Jesse Barnes291427f2011-07-29 12:42:37 -07002551
Akshay Joshi0206e352011-08-16 15:34:10 -04002552 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002553 reg = FDI_TX_CTL(pipe);
2554 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002555 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2556 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002557 I915_WRITE(reg, temp);
2558
2559 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002560 udelay(500);
2561
Sean Paulfa37d392012-03-02 12:53:39 -05002562 for (retry = 0; retry < 5; retry++) {
2563 reg = FDI_RX_IIR(pipe);
2564 temp = I915_READ(reg);
2565 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2566 if (temp & FDI_RX_BIT_LOCK) {
2567 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2568 DRM_DEBUG_KMS("FDI train 1 done.\n");
2569 break;
2570 }
2571 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002572 }
Sean Paulfa37d392012-03-02 12:53:39 -05002573 if (retry < 5)
2574 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002575 }
2576 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002577 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002578
2579 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002580 reg = FDI_TX_CTL(pipe);
2581 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002582 temp &= ~FDI_LINK_TRAIN_NONE;
2583 temp |= FDI_LINK_TRAIN_PATTERN_2;
2584 if (IS_GEN6(dev)) {
2585 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2586 /* SNB-B */
2587 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2588 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002589 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002590
Chris Wilson5eddb702010-09-11 13:48:45 +01002591 reg = FDI_RX_CTL(pipe);
2592 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002593 if (HAS_PCH_CPT(dev)) {
2594 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2595 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2596 } else {
2597 temp &= ~FDI_LINK_TRAIN_NONE;
2598 temp |= FDI_LINK_TRAIN_PATTERN_2;
2599 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002600 I915_WRITE(reg, temp);
2601
2602 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002603 udelay(150);
2604
Akshay Joshi0206e352011-08-16 15:34:10 -04002605 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002606 reg = FDI_TX_CTL(pipe);
2607 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002608 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2609 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002610 I915_WRITE(reg, temp);
2611
2612 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002613 udelay(500);
2614
Sean Paulfa37d392012-03-02 12:53:39 -05002615 for (retry = 0; retry < 5; retry++) {
2616 reg = FDI_RX_IIR(pipe);
2617 temp = I915_READ(reg);
2618 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2619 if (temp & FDI_RX_SYMBOL_LOCK) {
2620 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2621 DRM_DEBUG_KMS("FDI train 2 done.\n");
2622 break;
2623 }
2624 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002625 }
Sean Paulfa37d392012-03-02 12:53:39 -05002626 if (retry < 5)
2627 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002628 }
2629 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002630 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002631
2632 DRM_DEBUG_KMS("FDI train done.\n");
2633}
2634
Jesse Barnes357555c2011-04-28 15:09:55 -07002635/* Manual link training for Ivy Bridge A0 parts */
2636static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2637{
2638 struct drm_device *dev = crtc->dev;
2639 struct drm_i915_private *dev_priv = dev->dev_private;
2640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2641 int pipe = intel_crtc->pipe;
2642 u32 reg, temp, i;
2643
2644 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2645 for train result */
2646 reg = FDI_RX_IMR(pipe);
2647 temp = I915_READ(reg);
2648 temp &= ~FDI_RX_SYMBOL_LOCK;
2649 temp &= ~FDI_RX_BIT_LOCK;
2650 I915_WRITE(reg, temp);
2651
2652 POSTING_READ(reg);
2653 udelay(150);
2654
Daniel Vetter01a415f2012-10-27 15:58:40 +02002655 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2656 I915_READ(FDI_RX_IIR(pipe)));
2657
Jesse Barnes357555c2011-04-28 15:09:55 -07002658 /* enable CPU FDI TX and PCH FDI RX */
2659 reg = FDI_TX_CTL(pipe);
2660 temp = I915_READ(reg);
2661 temp &= ~(7 << 19);
2662 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2663 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2664 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2665 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2666 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002667 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002668 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2669
Daniel Vetterd74cf322012-10-26 10:58:13 +02002670 I915_WRITE(FDI_RX_MISC(pipe),
2671 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2672
Jesse Barnes357555c2011-04-28 15:09:55 -07002673 reg = FDI_RX_CTL(pipe);
2674 temp = I915_READ(reg);
2675 temp &= ~FDI_LINK_TRAIN_AUTO;
2676 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2677 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002678 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002679 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2680
2681 POSTING_READ(reg);
2682 udelay(150);
2683
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002684 cpt_phase_pointer_enable(dev, pipe);
Jesse Barnes291427f2011-07-29 12:42:37 -07002685
Akshay Joshi0206e352011-08-16 15:34:10 -04002686 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002687 reg = FDI_TX_CTL(pipe);
2688 temp = I915_READ(reg);
2689 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2690 temp |= snb_b_fdi_train_param[i];
2691 I915_WRITE(reg, temp);
2692
2693 POSTING_READ(reg);
2694 udelay(500);
2695
2696 reg = FDI_RX_IIR(pipe);
2697 temp = I915_READ(reg);
2698 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2699
2700 if (temp & FDI_RX_BIT_LOCK ||
2701 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2702 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002703 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002704 break;
2705 }
2706 }
2707 if (i == 4)
2708 DRM_ERROR("FDI train 1 fail!\n");
2709
2710 /* Train 2 */
2711 reg = FDI_TX_CTL(pipe);
2712 temp = I915_READ(reg);
2713 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2714 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2715 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2716 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2717 I915_WRITE(reg, temp);
2718
2719 reg = FDI_RX_CTL(pipe);
2720 temp = I915_READ(reg);
2721 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2722 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2723 I915_WRITE(reg, temp);
2724
2725 POSTING_READ(reg);
2726 udelay(150);
2727
Akshay Joshi0206e352011-08-16 15:34:10 -04002728 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002729 reg = FDI_TX_CTL(pipe);
2730 temp = I915_READ(reg);
2731 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2732 temp |= snb_b_fdi_train_param[i];
2733 I915_WRITE(reg, temp);
2734
2735 POSTING_READ(reg);
2736 udelay(500);
2737
2738 reg = FDI_RX_IIR(pipe);
2739 temp = I915_READ(reg);
2740 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2741
2742 if (temp & FDI_RX_SYMBOL_LOCK) {
2743 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002744 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002745 break;
2746 }
2747 }
2748 if (i == 4)
2749 DRM_ERROR("FDI train 2 fail!\n");
2750
2751 DRM_DEBUG_KMS("FDI train done.\n");
2752}
2753
Daniel Vetter88cefb62012-08-12 19:27:14 +02002754static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002755{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002756 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002757 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002758 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002759 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002760
Jesse Barnesc64e3112010-09-10 11:27:03 -07002761
Jesse Barnes0e23b992010-09-10 11:10:00 -07002762 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002763 reg = FDI_RX_CTL(pipe);
2764 temp = I915_READ(reg);
2765 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002766 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002767 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002768 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2769
2770 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002771 udelay(200);
2772
2773 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002774 temp = I915_READ(reg);
2775 I915_WRITE(reg, temp | FDI_PCDCLK);
2776
2777 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002778 udelay(200);
2779
Paulo Zanoni20749732012-11-23 15:30:38 -02002780 /* Enable CPU FDI TX PLL, always on for Ironlake */
2781 reg = FDI_TX_CTL(pipe);
2782 temp = I915_READ(reg);
2783 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2784 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002785
Paulo Zanoni20749732012-11-23 15:30:38 -02002786 POSTING_READ(reg);
2787 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002788 }
2789}
2790
Daniel Vetter88cefb62012-08-12 19:27:14 +02002791static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2792{
2793 struct drm_device *dev = intel_crtc->base.dev;
2794 struct drm_i915_private *dev_priv = dev->dev_private;
2795 int pipe = intel_crtc->pipe;
2796 u32 reg, temp;
2797
2798 /* Switch from PCDclk to Rawclk */
2799 reg = FDI_RX_CTL(pipe);
2800 temp = I915_READ(reg);
2801 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2802
2803 /* Disable CPU FDI TX PLL */
2804 reg = FDI_TX_CTL(pipe);
2805 temp = I915_READ(reg);
2806 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2807
2808 POSTING_READ(reg);
2809 udelay(100);
2810
2811 reg = FDI_RX_CTL(pipe);
2812 temp = I915_READ(reg);
2813 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2814
2815 /* Wait for the clocks to turn off. */
2816 POSTING_READ(reg);
2817 udelay(100);
2818}
2819
Jesse Barnes291427f2011-07-29 12:42:37 -07002820static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2821{
2822 struct drm_i915_private *dev_priv = dev->dev_private;
2823 u32 flags = I915_READ(SOUTH_CHICKEN1);
2824
2825 flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2826 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2827 flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2828 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2829 POSTING_READ(SOUTH_CHICKEN1);
2830}
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002831static void ironlake_fdi_disable(struct drm_crtc *crtc)
2832{
2833 struct drm_device *dev = crtc->dev;
2834 struct drm_i915_private *dev_priv = dev->dev_private;
2835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2836 int pipe = intel_crtc->pipe;
2837 u32 reg, temp;
2838
2839 /* disable CPU FDI tx and PCH FDI rx */
2840 reg = FDI_TX_CTL(pipe);
2841 temp = I915_READ(reg);
2842 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2843 POSTING_READ(reg);
2844
2845 reg = FDI_RX_CTL(pipe);
2846 temp = I915_READ(reg);
2847 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002848 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002849 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2850
2851 POSTING_READ(reg);
2852 udelay(100);
2853
2854 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002855 if (HAS_PCH_IBX(dev)) {
2856 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes291427f2011-07-29 12:42:37 -07002857 } else if (HAS_PCH_CPT(dev)) {
2858 cpt_phase_pointer_disable(dev, pipe);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002859 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002860
2861 /* still set train pattern 1 */
2862 reg = FDI_TX_CTL(pipe);
2863 temp = I915_READ(reg);
2864 temp &= ~FDI_LINK_TRAIN_NONE;
2865 temp |= FDI_LINK_TRAIN_PATTERN_1;
2866 I915_WRITE(reg, temp);
2867
2868 reg = FDI_RX_CTL(pipe);
2869 temp = I915_READ(reg);
2870 if (HAS_PCH_CPT(dev)) {
2871 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2872 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2873 } else {
2874 temp &= ~FDI_LINK_TRAIN_NONE;
2875 temp |= FDI_LINK_TRAIN_PATTERN_1;
2876 }
2877 /* BPC in FDI rx is consistent with that in PIPECONF */
2878 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002879 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002880 I915_WRITE(reg, temp);
2881
2882 POSTING_READ(reg);
2883 udelay(100);
2884}
2885
Chris Wilson5bb61642012-09-27 21:25:58 +01002886static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2887{
2888 struct drm_device *dev = crtc->dev;
2889 struct drm_i915_private *dev_priv = dev->dev_private;
2890 unsigned long flags;
2891 bool pending;
2892
2893 if (atomic_read(&dev_priv->mm.wedged))
2894 return false;
2895
2896 spin_lock_irqsave(&dev->event_lock, flags);
2897 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2898 spin_unlock_irqrestore(&dev->event_lock, flags);
2899
2900 return pending;
2901}
2902
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002903static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2904{
Chris Wilson0f911282012-04-17 10:05:38 +01002905 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002906 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002907
2908 if (crtc->fb == NULL)
2909 return;
2910
Chris Wilson5bb61642012-09-27 21:25:58 +01002911 wait_event(dev_priv->pending_flip_queue,
2912 !intel_crtc_has_pending_flip(crtc));
2913
Chris Wilson0f911282012-04-17 10:05:38 +01002914 mutex_lock(&dev->struct_mutex);
2915 intel_finish_fb(crtc->fb);
2916 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002917}
2918
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002919static bool ironlake_crtc_driving_pch(struct drm_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08002920{
2921 struct drm_device *dev = crtc->dev;
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002922 struct intel_encoder *intel_encoder;
Jesse Barnes040484a2011-01-03 12:14:26 -08002923
2924 /*
2925 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2926 * must be driven by its own crtc; no sharing is possible.
2927 */
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002928 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002929 switch (intel_encoder->type) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002930 case INTEL_OUTPUT_EDP:
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002931 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnes040484a2011-01-03 12:14:26 -08002932 return false;
2933 continue;
2934 }
2935 }
2936
2937 return true;
2938}
2939
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002940static bool haswell_crtc_driving_pch(struct drm_crtc *crtc)
2941{
2942 return intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG);
2943}
2944
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002945/* Program iCLKIP clock to the desired frequency */
2946static void lpt_program_iclkip(struct drm_crtc *crtc)
2947{
2948 struct drm_device *dev = crtc->dev;
2949 struct drm_i915_private *dev_priv = dev->dev_private;
2950 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2951 u32 temp;
2952
Daniel Vetter09153002012-12-12 14:06:44 +01002953 mutex_lock(&dev_priv->dpio_lock);
2954
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002955 /* It is necessary to ungate the pixclk gate prior to programming
2956 * the divisors, and gate it back when it is done.
2957 */
2958 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2959
2960 /* Disable SSCCTL */
2961 intel_sbi_write(dev_priv, SBI_SSCCTL6,
2962 intel_sbi_read(dev_priv, SBI_SSCCTL6) |
2963 SBI_SSCCTL_DISABLE);
2964
2965 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2966 if (crtc->mode.clock == 20000) {
2967 auxdiv = 1;
2968 divsel = 0x41;
2969 phaseinc = 0x20;
2970 } else {
2971 /* The iCLK virtual clock root frequency is in MHz,
2972 * but the crtc->mode.clock in in KHz. To get the divisors,
2973 * it is necessary to divide one by another, so we
2974 * convert the virtual clock precision to KHz here for higher
2975 * precision.
2976 */
2977 u32 iclk_virtual_root_freq = 172800 * 1000;
2978 u32 iclk_pi_range = 64;
2979 u32 desired_divisor, msb_divisor_value, pi_value;
2980
2981 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2982 msb_divisor_value = desired_divisor / iclk_pi_range;
2983 pi_value = desired_divisor % iclk_pi_range;
2984
2985 auxdiv = 0;
2986 divsel = msb_divisor_value - 2;
2987 phaseinc = pi_value;
2988 }
2989
2990 /* This should not happen with any sane values */
2991 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2992 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2993 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2994 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2995
2996 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2997 crtc->mode.clock,
2998 auxdiv,
2999 divsel,
3000 phasedir,
3001 phaseinc);
3002
3003 /* Program SSCDIVINTPHASE6 */
3004 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6);
3005 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3006 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3007 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3008 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3009 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3010 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
3011
3012 intel_sbi_write(dev_priv,
3013 SBI_SSCDIVINTPHASE6,
3014 temp);
3015
3016 /* Program SSCAUXDIV */
3017 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6);
3018 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3019 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
3020 intel_sbi_write(dev_priv,
3021 SBI_SSCAUXDIV6,
3022 temp);
3023
3024
3025 /* Enable modulator and associated divider */
3026 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6);
3027 temp &= ~SBI_SSCCTL_DISABLE;
3028 intel_sbi_write(dev_priv,
3029 SBI_SSCCTL6,
3030 temp);
3031
3032 /* Wait for initialization time */
3033 udelay(24);
3034
3035 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003036
3037 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003038}
3039
Jesse Barnesf67a5592011-01-05 10:31:48 -08003040/*
3041 * Enable PCH resources required for PCH ports:
3042 * - PCH PLLs
3043 * - FDI training & RX/TX
3044 * - update transcoder timings
3045 * - DP transcoding bits
3046 * - transcoder
3047 */
3048static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003049{
3050 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003051 struct drm_i915_private *dev_priv = dev->dev_private;
3052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3053 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003054 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003055
Chris Wilsone7e164d2012-05-11 09:21:25 +01003056 assert_transcoder_disabled(dev_priv, pipe);
3057
Daniel Vettercd986ab2012-10-26 10:58:12 +02003058 /* Write the TU size bits before fdi link training, so that error
3059 * detection works. */
3060 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3061 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3062
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003063 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003064 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003065
Daniel Vetter572deb32012-10-27 18:46:14 +02003066 /* XXX: pch pll's can be enabled any time before we enable the PCH
3067 * transcoder, and we actually should do this to not upset any PCH
3068 * transcoder that already use the clock when we share it.
3069 *
3070 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
3071 * unconditionally resets the pll - we need that to have the right LVDS
3072 * enable sequence. */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02003073 ironlake_enable_pch_pll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01003074
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003075 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003076 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003077
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003078 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003079 switch (pipe) {
3080 default:
3081 case 0:
3082 temp |= TRANSA_DPLL_ENABLE;
3083 sel = TRANSA_DPLLB_SEL;
3084 break;
3085 case 1:
3086 temp |= TRANSB_DPLL_ENABLE;
3087 sel = TRANSB_DPLLB_SEL;
3088 break;
3089 case 2:
3090 temp |= TRANSC_DPLL_ENABLE;
3091 sel = TRANSC_DPLLB_SEL;
3092 break;
Jesse Barnesd64311a2011-10-12 15:01:33 -07003093 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003094 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3095 temp |= sel;
3096 else
3097 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003098 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003099 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003100
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003101 /* set transcoder timing, panel must allow it */
3102 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003103 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3104 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3105 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3106
3107 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3108 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3109 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003110 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003111
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003112 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003113
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003114 /* For PCH DP, enable TRANS_DP_CTL */
3115 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003116 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3117 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003118 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003119 reg = TRANS_DP_CTL(pipe);
3120 temp = I915_READ(reg);
3121 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003122 TRANS_DP_SYNC_MASK |
3123 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003124 temp |= (TRANS_DP_OUTPUT_ENABLE |
3125 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003126 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003127
3128 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003129 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003130 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003131 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003132
3133 switch (intel_trans_dp_port_sel(crtc)) {
3134 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003135 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003136 break;
3137 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003138 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003139 break;
3140 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003141 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003142 break;
3143 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003144 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003145 }
3146
Chris Wilson5eddb702010-09-11 13:48:45 +01003147 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003148 }
3149
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003150 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003151}
3152
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003153static void lpt_pch_enable(struct drm_crtc *crtc)
3154{
3155 struct drm_device *dev = crtc->dev;
3156 struct drm_i915_private *dev_priv = dev->dev_private;
3157 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003158 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003159
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003160 assert_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003161
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003162 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003163
Paulo Zanoni0540e482012-10-31 18:12:40 -02003164 /* Set transcoder timing. */
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003165 I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
3166 I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
3167 I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003168
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003169 I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
3170 I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
3171 I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
3172 I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003173
Paulo Zanoni937bb612012-10-31 18:12:47 -02003174 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003175}
3176
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003177static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3178{
3179 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3180
3181 if (pll == NULL)
3182 return;
3183
3184 if (pll->refcount == 0) {
3185 WARN(1, "bad PCH PLL refcount\n");
3186 return;
3187 }
3188
3189 --pll->refcount;
3190 intel_crtc->pch_pll = NULL;
3191}
3192
3193static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3194{
3195 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3196 struct intel_pch_pll *pll;
3197 int i;
3198
3199 pll = intel_crtc->pch_pll;
3200 if (pll) {
3201 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3202 intel_crtc->base.base.id, pll->pll_reg);
3203 goto prepare;
3204 }
3205
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003206 if (HAS_PCH_IBX(dev_priv->dev)) {
3207 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3208 i = intel_crtc->pipe;
3209 pll = &dev_priv->pch_plls[i];
3210
3211 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3212 intel_crtc->base.base.id, pll->pll_reg);
3213
3214 goto found;
3215 }
3216
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003217 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3218 pll = &dev_priv->pch_plls[i];
3219
3220 /* Only want to check enabled timings first */
3221 if (pll->refcount == 0)
3222 continue;
3223
3224 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3225 fp == I915_READ(pll->fp0_reg)) {
3226 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3227 intel_crtc->base.base.id,
3228 pll->pll_reg, pll->refcount, pll->active);
3229
3230 goto found;
3231 }
3232 }
3233
3234 /* Ok no matching timings, maybe there's a free one? */
3235 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3236 pll = &dev_priv->pch_plls[i];
3237 if (pll->refcount == 0) {
3238 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3239 intel_crtc->base.base.id, pll->pll_reg);
3240 goto found;
3241 }
3242 }
3243
3244 return NULL;
3245
3246found:
3247 intel_crtc->pch_pll = pll;
3248 pll->refcount++;
3249 DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
3250prepare: /* separate function? */
3251 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003252
Chris Wilsone04c7352012-05-02 20:43:56 +01003253 /* Wait for the clocks to stabilize before rewriting the regs */
3254 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003255 POSTING_READ(pll->pll_reg);
3256 udelay(150);
Chris Wilsone04c7352012-05-02 20:43:56 +01003257
3258 I915_WRITE(pll->fp0_reg, fp);
3259 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003260 pll->on = false;
3261 return pll;
3262}
3263
Jesse Barnesd4270e52011-10-11 10:43:02 -07003264void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3265{
3266 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003267 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003268 u32 temp;
3269
3270 temp = I915_READ(dslreg);
3271 udelay(500);
3272 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003273 if (wait_for(I915_READ(dslreg) != temp, 5))
3274 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3275 }
3276}
3277
Jesse Barnesf67a5592011-01-05 10:31:48 -08003278static void ironlake_crtc_enable(struct drm_crtc *crtc)
3279{
3280 struct drm_device *dev = crtc->dev;
3281 struct drm_i915_private *dev_priv = dev->dev_private;
3282 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003283 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003284 int pipe = intel_crtc->pipe;
3285 int plane = intel_crtc->plane;
3286 u32 temp;
3287 bool is_pch_port;
3288
Daniel Vetter08a48462012-07-02 11:43:47 +02003289 WARN_ON(!crtc->enabled);
3290
Jesse Barnesf67a5592011-01-05 10:31:48 -08003291 if (intel_crtc->active)
3292 return;
3293
3294 intel_crtc->active = true;
3295 intel_update_watermarks(dev);
3296
3297 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3298 temp = I915_READ(PCH_LVDS);
3299 if ((temp & LVDS_PORT_EN) == 0)
3300 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3301 }
3302
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003303 is_pch_port = ironlake_crtc_driving_pch(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003304
Daniel Vetter46b6f812012-09-06 22:08:33 +02003305 if (is_pch_port) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003306 /* Note: FDI PLL enabling _must_ be done before we enable the
3307 * cpu pipes, hence this is separate from all the other fdi/pch
3308 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003309 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003310 } else {
3311 assert_fdi_tx_disabled(dev_priv, pipe);
3312 assert_fdi_rx_disabled(dev_priv, pipe);
3313 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003314
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003315 for_each_encoder_on_crtc(dev, crtc, encoder)
3316 if (encoder->pre_enable)
3317 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003318
3319 /* Enable panel fitting for LVDS */
3320 if (dev_priv->pch_pf_size &&
Jani Nikula547dc042012-11-02 11:24:03 +02003321 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3322 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnesf67a5592011-01-05 10:31:48 -08003323 /* Force use of hard-coded filter coefficients
3324 * as some pre-programmed values are broken,
3325 * e.g. x201.
3326 */
Paulo Zanoni13888d72012-11-20 13:27:41 -02003327 if (IS_IVYBRIDGE(dev))
3328 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3329 PF_PIPE_SEL_IVB(pipe));
3330 else
3331 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003332 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3333 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003334 }
3335
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003336 /*
3337 * On ILK+ LUT must be loaded before the pipe is running but with
3338 * clocks enabled
3339 */
3340 intel_crtc_load_lut(crtc);
3341
Jesse Barnesf67a5592011-01-05 10:31:48 -08003342 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3343 intel_enable_plane(dev_priv, plane, pipe);
3344
3345 if (is_pch_port)
3346 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003347
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003348 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003349 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003350 mutex_unlock(&dev->struct_mutex);
3351
Chris Wilson6b383a72010-09-13 13:54:26 +01003352 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003353
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003354 for_each_encoder_on_crtc(dev, crtc, encoder)
3355 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003356
3357 if (HAS_PCH_CPT(dev))
3358 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003359
3360 /*
3361 * There seems to be a race in PCH platform hw (at least on some
3362 * outputs) where an enabled pipe still completes any pageflip right
3363 * away (as if the pipe is off) instead of waiting for vblank. As soon
3364 * as the first vblank happend, everything works as expected. Hence just
3365 * wait for one vblank before returning to avoid strange things
3366 * happening.
3367 */
3368 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003369}
3370
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003371static void haswell_crtc_enable(struct drm_crtc *crtc)
3372{
3373 struct drm_device *dev = crtc->dev;
3374 struct drm_i915_private *dev_priv = dev->dev_private;
3375 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3376 struct intel_encoder *encoder;
3377 int pipe = intel_crtc->pipe;
3378 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003379 bool is_pch_port;
3380
3381 WARN_ON(!crtc->enabled);
3382
3383 if (intel_crtc->active)
3384 return;
3385
3386 intel_crtc->active = true;
3387 intel_update_watermarks(dev);
3388
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003389 is_pch_port = haswell_crtc_driving_pch(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003390
Paulo Zanoni83616632012-10-23 18:29:54 -02003391 if (is_pch_port)
Paulo Zanoni04945642012-11-01 21:00:59 -02003392 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003393
3394 for_each_encoder_on_crtc(dev, crtc, encoder)
3395 if (encoder->pre_enable)
3396 encoder->pre_enable(encoder);
3397
Paulo Zanoni1f544382012-10-24 11:32:00 -02003398 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003399
Paulo Zanoni1f544382012-10-24 11:32:00 -02003400 /* Enable panel fitting for eDP */
Jani Nikula547dc042012-11-02 11:24:03 +02003401 if (dev_priv->pch_pf_size &&
3402 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003403 /* Force use of hard-coded filter coefficients
3404 * as some pre-programmed values are broken,
3405 * e.g. x201.
3406 */
Paulo Zanoni54075a72012-11-20 13:27:42 -02003407 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3408 PF_PIPE_SEL_IVB(pipe));
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003409 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3410 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3411 }
3412
3413 /*
3414 * On ILK+ LUT must be loaded before the pipe is running but with
3415 * clocks enabled
3416 */
3417 intel_crtc_load_lut(crtc);
3418
Paulo Zanoni1f544382012-10-24 11:32:00 -02003419 intel_ddi_set_pipe_settings(crtc);
3420 intel_ddi_enable_pipe_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003421
3422 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3423 intel_enable_plane(dev_priv, plane, pipe);
3424
3425 if (is_pch_port)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003426 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003427
3428 mutex_lock(&dev->struct_mutex);
3429 intel_update_fbc(dev);
3430 mutex_unlock(&dev->struct_mutex);
3431
3432 intel_crtc_update_cursor(crtc, true);
3433
3434 for_each_encoder_on_crtc(dev, crtc, encoder)
3435 encoder->enable(encoder);
3436
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003437 /*
3438 * There seems to be a race in PCH platform hw (at least on some
3439 * outputs) where an enabled pipe still completes any pageflip right
3440 * away (as if the pipe is off) instead of waiting for vblank. As soon
3441 * as the first vblank happend, everything works as expected. Hence just
3442 * wait for one vblank before returning to avoid strange things
3443 * happening.
3444 */
3445 intel_wait_for_vblank(dev, intel_crtc->pipe);
3446}
3447
Jesse Barnes6be4a602010-09-10 10:26:01 -07003448static void ironlake_crtc_disable(struct drm_crtc *crtc)
3449{
3450 struct drm_device *dev = crtc->dev;
3451 struct drm_i915_private *dev_priv = dev->dev_private;
3452 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003453 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003454 int pipe = intel_crtc->pipe;
3455 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003456 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003457
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003458
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003459 if (!intel_crtc->active)
3460 return;
3461
Daniel Vetterea9d7582012-07-10 10:42:52 +02003462 for_each_encoder_on_crtc(dev, crtc, encoder)
3463 encoder->disable(encoder);
3464
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003465 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003466 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003467 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003468
Jesse Barnesb24e7172011-01-04 15:09:30 -08003469 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003470
Chris Wilson973d04f2011-07-08 12:22:37 +01003471 if (dev_priv->cfb_plane == plane)
3472 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003473
Jesse Barnesb24e7172011-01-04 15:09:30 -08003474 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003475
Jesse Barnes6be4a602010-09-10 10:26:01 -07003476 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003477 I915_WRITE(PF_CTL(pipe), 0);
3478 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003479
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003480 for_each_encoder_on_crtc(dev, crtc, encoder)
3481 if (encoder->post_disable)
3482 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003483
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003485
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003486 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003487
3488 if (HAS_PCH_CPT(dev)) {
3489 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003490 reg = TRANS_DP_CTL(pipe);
3491 temp = I915_READ(reg);
3492 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003493 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003494 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003495
3496 /* disable DPLL_SEL */
3497 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003498 switch (pipe) {
3499 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003500 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003501 break;
3502 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003503 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003504 break;
3505 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003506 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003507 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003508 break;
3509 default:
3510 BUG(); /* wtf */
3511 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003512 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003513 }
3514
3515 /* disable PCH DPLL */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003516 intel_disable_pch_pll(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003517
Daniel Vetter88cefb62012-08-12 19:27:14 +02003518 ironlake_fdi_pll_disable(intel_crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +01003519
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003520 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003521 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003522
3523 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003524 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003525 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003526}
3527
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003528static void haswell_crtc_disable(struct drm_crtc *crtc)
3529{
3530 struct drm_device *dev = crtc->dev;
3531 struct drm_i915_private *dev_priv = dev->dev_private;
3532 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3533 struct intel_encoder *encoder;
3534 int pipe = intel_crtc->pipe;
3535 int plane = intel_crtc->plane;
Paulo Zanoniad80a812012-10-24 16:06:19 -02003536 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni83616632012-10-23 18:29:54 -02003537 bool is_pch_port;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003538
3539 if (!intel_crtc->active)
3540 return;
3541
Paulo Zanoni83616632012-10-23 18:29:54 -02003542 is_pch_port = haswell_crtc_driving_pch(crtc);
3543
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003544 for_each_encoder_on_crtc(dev, crtc, encoder)
3545 encoder->disable(encoder);
3546
3547 intel_crtc_wait_for_pending_flips(crtc);
3548 drm_vblank_off(dev, pipe);
3549 intel_crtc_update_cursor(crtc, false);
3550
3551 intel_disable_plane(dev_priv, plane, pipe);
3552
3553 if (dev_priv->cfb_plane == plane)
3554 intel_disable_fbc(dev);
3555
3556 intel_disable_pipe(dev_priv, pipe);
3557
Paulo Zanoniad80a812012-10-24 16:06:19 -02003558 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003559
3560 /* Disable PF */
3561 I915_WRITE(PF_CTL(pipe), 0);
3562 I915_WRITE(PF_WIN_SZ(pipe), 0);
3563
Paulo Zanoni1f544382012-10-24 11:32:00 -02003564 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003565
3566 for_each_encoder_on_crtc(dev, crtc, encoder)
3567 if (encoder->post_disable)
3568 encoder->post_disable(encoder);
3569
Paulo Zanoni83616632012-10-23 18:29:54 -02003570 if (is_pch_port) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003571 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003572 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003573 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003574
3575 intel_crtc->active = false;
3576 intel_update_watermarks(dev);
3577
3578 mutex_lock(&dev->struct_mutex);
3579 intel_update_fbc(dev);
3580 mutex_unlock(&dev->struct_mutex);
3581}
3582
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003583static void ironlake_crtc_off(struct drm_crtc *crtc)
3584{
3585 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3586 intel_put_pch_pll(intel_crtc);
3587}
3588
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003589static void haswell_crtc_off(struct drm_crtc *crtc)
3590{
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003591 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3592
3593 /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
3594 * start using it. */
Daniel Vetter1a240d42012-11-29 22:18:51 +01003595 intel_crtc->cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003596
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003597 intel_ddi_put_crtc_pll(crtc);
3598}
3599
Daniel Vetter02e792f2009-09-15 22:57:34 +02003600static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3601{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003602 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003603 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003604 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003605
Chris Wilson23f09ce2010-08-12 13:53:37 +01003606 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003607 dev_priv->mm.interruptible = false;
3608 (void) intel_overlay_switch_off(intel_crtc->overlay);
3609 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003610 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003611 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003612
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003613 /* Let userspace switch the overlay on again. In most cases userspace
3614 * has to recompute where to put it anyway.
3615 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003616}
3617
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003618static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003619{
3620 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003621 struct drm_i915_private *dev_priv = dev->dev_private;
3622 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003623 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003624 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003625 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003626
Daniel Vetter08a48462012-07-02 11:43:47 +02003627 WARN_ON(!crtc->enabled);
3628
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003629 if (intel_crtc->active)
3630 return;
3631
3632 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003633 intel_update_watermarks(dev);
3634
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003635 intel_enable_pll(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08003636 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003637 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003638
3639 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003640 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003641
3642 /* Give the overlay scaler a chance to enable if it's on this pipe */
3643 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003644 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003645
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003646 for_each_encoder_on_crtc(dev, crtc, encoder)
3647 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003648}
3649
3650static void i9xx_crtc_disable(struct drm_crtc *crtc)
3651{
3652 struct drm_device *dev = crtc->dev;
3653 struct drm_i915_private *dev_priv = dev->dev_private;
3654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003655 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003656 int pipe = intel_crtc->pipe;
3657 int plane = intel_crtc->plane;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003658
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003659
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003660 if (!intel_crtc->active)
3661 return;
3662
Daniel Vetterea9d7582012-07-10 10:42:52 +02003663 for_each_encoder_on_crtc(dev, crtc, encoder)
3664 encoder->disable(encoder);
3665
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003666 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003667 intel_crtc_wait_for_pending_flips(crtc);
3668 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003669 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003670 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003671
Chris Wilson973d04f2011-07-08 12:22:37 +01003672 if (dev_priv->cfb_plane == plane)
3673 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003674
Jesse Barnesb24e7172011-01-04 15:09:30 -08003675 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003676 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003677 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003678
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003679 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003680 intel_update_fbc(dev);
3681 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003682}
3683
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003684static void i9xx_crtc_off(struct drm_crtc *crtc)
3685{
3686}
3687
Daniel Vetter976f8a22012-07-08 22:34:21 +02003688static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3689 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003690{
3691 struct drm_device *dev = crtc->dev;
3692 struct drm_i915_master_private *master_priv;
3693 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3694 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003695
3696 if (!dev->primary->master)
3697 return;
3698
3699 master_priv = dev->primary->master->driver_priv;
3700 if (!master_priv->sarea_priv)
3701 return;
3702
Jesse Barnes79e53942008-11-07 14:24:08 -08003703 switch (pipe) {
3704 case 0:
3705 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3706 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3707 break;
3708 case 1:
3709 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3710 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3711 break;
3712 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003713 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003714 break;
3715 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003716}
3717
Daniel Vetter976f8a22012-07-08 22:34:21 +02003718/**
3719 * Sets the power management mode of the pipe and plane.
3720 */
3721void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003722{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003723 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003724 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003725 struct intel_encoder *intel_encoder;
3726 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003727
Daniel Vetter976f8a22012-07-08 22:34:21 +02003728 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3729 enable |= intel_encoder->connectors_active;
3730
3731 if (enable)
3732 dev_priv->display.crtc_enable(crtc);
3733 else
3734 dev_priv->display.crtc_disable(crtc);
3735
3736 intel_crtc_update_sarea(crtc, enable);
3737}
3738
3739static void intel_crtc_noop(struct drm_crtc *crtc)
3740{
3741}
3742
3743static void intel_crtc_disable(struct drm_crtc *crtc)
3744{
3745 struct drm_device *dev = crtc->dev;
3746 struct drm_connector *connector;
3747 struct drm_i915_private *dev_priv = dev->dev_private;
3748
3749 /* crtc should still be enabled when we disable it. */
3750 WARN_ON(!crtc->enabled);
3751
3752 dev_priv->display.crtc_disable(crtc);
3753 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003754 dev_priv->display.off(crtc);
3755
Chris Wilson931872f2012-01-16 23:01:13 +00003756 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3757 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003758
3759 if (crtc->fb) {
3760 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003761 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003762 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003763 crtc->fb = NULL;
3764 }
3765
3766 /* Update computed state. */
3767 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3768 if (!connector->encoder || !connector->encoder->crtc)
3769 continue;
3770
3771 if (connector->encoder->crtc != crtc)
3772 continue;
3773
3774 connector->dpms = DRM_MODE_DPMS_OFF;
3775 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003776 }
3777}
3778
Daniel Vettera261b242012-07-26 19:21:47 +02003779void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003780{
Daniel Vettera261b242012-07-26 19:21:47 +02003781 struct drm_crtc *crtc;
3782
3783 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3784 if (crtc->enabled)
3785 intel_crtc_disable(crtc);
3786 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003787}
3788
Daniel Vetter1f703852012-07-11 16:51:39 +02003789void intel_encoder_noop(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003790{
Jesse Barnes79e53942008-11-07 14:24:08 -08003791}
3792
Chris Wilsonea5b2132010-08-04 13:50:23 +01003793void intel_encoder_destroy(struct drm_encoder *encoder)
3794{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003795 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003796
Chris Wilsonea5b2132010-08-04 13:50:23 +01003797 drm_encoder_cleanup(encoder);
3798 kfree(intel_encoder);
3799}
3800
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003801/* Simple dpms helper for encodres with just one connector, no cloning and only
3802 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3803 * state of the entire output pipe. */
3804void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3805{
3806 if (mode == DRM_MODE_DPMS_ON) {
3807 encoder->connectors_active = true;
3808
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003809 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003810 } else {
3811 encoder->connectors_active = false;
3812
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003813 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003814 }
3815}
3816
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003817/* Cross check the actual hw state with our own modeset state tracking (and it's
3818 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003819static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003820{
3821 if (connector->get_hw_state(connector)) {
3822 struct intel_encoder *encoder = connector->encoder;
3823 struct drm_crtc *crtc;
3824 bool encoder_enabled;
3825 enum pipe pipe;
3826
3827 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3828 connector->base.base.id,
3829 drm_get_connector_name(&connector->base));
3830
3831 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3832 "wrong connector dpms state\n");
3833 WARN(connector->base.encoder != &encoder->base,
3834 "active connector not linked to encoder\n");
3835 WARN(!encoder->connectors_active,
3836 "encoder->connectors_active not set\n");
3837
3838 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3839 WARN(!encoder_enabled, "encoder not enabled\n");
3840 if (WARN_ON(!encoder->base.crtc))
3841 return;
3842
3843 crtc = encoder->base.crtc;
3844
3845 WARN(!crtc->enabled, "crtc not enabled\n");
3846 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3847 WARN(pipe != to_intel_crtc(crtc)->pipe,
3848 "encoder active on the wrong pipe\n");
3849 }
3850}
3851
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003852/* Even simpler default implementation, if there's really no special case to
3853 * consider. */
3854void intel_connector_dpms(struct drm_connector *connector, int mode)
3855{
3856 struct intel_encoder *encoder = intel_attached_encoder(connector);
3857
3858 /* All the simple cases only support two dpms states. */
3859 if (mode != DRM_MODE_DPMS_ON)
3860 mode = DRM_MODE_DPMS_OFF;
3861
3862 if (mode == connector->dpms)
3863 return;
3864
3865 connector->dpms = mode;
3866
3867 /* Only need to change hw state when actually enabled */
3868 if (encoder->base.crtc)
3869 intel_encoder_dpms(encoder, mode);
3870 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003871 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003872
Daniel Vetterb9805142012-08-31 17:37:33 +02003873 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003874}
3875
Daniel Vetterf0947c32012-07-02 13:10:34 +02003876/* Simple connector->get_hw_state implementation for encoders that support only
3877 * one connector and no cloning and hence the encoder state determines the state
3878 * of the connector. */
3879bool intel_connector_get_hw_state(struct intel_connector *connector)
3880{
Daniel Vetter24929352012-07-02 20:28:59 +02003881 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003882 struct intel_encoder *encoder = connector->encoder;
3883
3884 return encoder->get_hw_state(encoder, &pipe);
3885}
3886
Jesse Barnes79e53942008-11-07 14:24:08 -08003887static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
Daniel Vetter35313cd2012-07-20 10:30:45 +02003888 const struct drm_display_mode *mode,
Jesse Barnes79e53942008-11-07 14:24:08 -08003889 struct drm_display_mode *adjusted_mode)
3890{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003891 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003892
Eric Anholtbad720f2009-10-22 16:11:14 -07003893 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003894 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003895 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3896 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003897 }
Chris Wilson89749352010-09-12 18:25:19 +01003898
Daniel Vetterf9bef082012-04-15 19:53:19 +02003899 /* All interlaced capable intel hw wants timings in frames. Note though
3900 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3901 * timings, so we need to be careful not to clobber these.*/
3902 if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3903 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003904
Chris Wilson44f46b422012-06-21 13:19:59 +03003905 /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
3906 * with a hsync front porch of 0.
3907 */
3908 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
3909 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
3910 return false;
3911
Jesse Barnes79e53942008-11-07 14:24:08 -08003912 return true;
3913}
3914
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07003915static int valleyview_get_display_clock_speed(struct drm_device *dev)
3916{
3917 return 400000; /* FIXME */
3918}
3919
Jesse Barnese70236a2009-09-21 10:42:27 -07003920static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003921{
Jesse Barnese70236a2009-09-21 10:42:27 -07003922 return 400000;
3923}
Jesse Barnes79e53942008-11-07 14:24:08 -08003924
Jesse Barnese70236a2009-09-21 10:42:27 -07003925static int i915_get_display_clock_speed(struct drm_device *dev)
3926{
3927 return 333000;
3928}
Jesse Barnes79e53942008-11-07 14:24:08 -08003929
Jesse Barnese70236a2009-09-21 10:42:27 -07003930static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3931{
3932 return 200000;
3933}
Jesse Barnes79e53942008-11-07 14:24:08 -08003934
Jesse Barnese70236a2009-09-21 10:42:27 -07003935static int i915gm_get_display_clock_speed(struct drm_device *dev)
3936{
3937 u16 gcfgc = 0;
3938
3939 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3940
3941 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003942 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003943 else {
3944 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3945 case GC_DISPLAY_CLOCK_333_MHZ:
3946 return 333000;
3947 default:
3948 case GC_DISPLAY_CLOCK_190_200_MHZ:
3949 return 190000;
3950 }
3951 }
3952}
Jesse Barnes79e53942008-11-07 14:24:08 -08003953
Jesse Barnese70236a2009-09-21 10:42:27 -07003954static int i865_get_display_clock_speed(struct drm_device *dev)
3955{
3956 return 266000;
3957}
3958
3959static int i855_get_display_clock_speed(struct drm_device *dev)
3960{
3961 u16 hpllcc = 0;
3962 /* Assume that the hardware is in the high speed state. This
3963 * should be the default.
3964 */
3965 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3966 case GC_CLOCK_133_200:
3967 case GC_CLOCK_100_200:
3968 return 200000;
3969 case GC_CLOCK_166_250:
3970 return 250000;
3971 case GC_CLOCK_100_133:
3972 return 133000;
3973 }
3974
3975 /* Shouldn't happen */
3976 return 0;
3977}
3978
3979static int i830_get_display_clock_speed(struct drm_device *dev)
3980{
3981 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08003982}
3983
Zhenyu Wang2c072452009-06-05 15:38:42 +08003984static void
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003985intel_reduce_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003986{
3987 while (*num > 0xffffff || *den > 0xffffff) {
3988 *num >>= 1;
3989 *den >>= 1;
3990 }
3991}
3992
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003993void
3994intel_link_compute_m_n(int bits_per_pixel, int nlanes,
3995 int pixel_clock, int link_clock,
3996 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003997{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003998 m_n->tu = 64;
Chris Wilson22ed1112010-12-04 01:01:29 +00003999 m_n->gmch_m = bits_per_pixel * pixel_clock;
4000 m_n->gmch_n = link_clock * nlanes * 8;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004001 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
Chris Wilson22ed1112010-12-04 01:01:29 +00004002 m_n->link_m = pixel_clock;
4003 m_n->link_n = link_clock;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004004 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004005}
4006
Chris Wilsona7615032011-01-12 17:04:08 +00004007static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4008{
Keith Packard72bbe582011-09-26 16:09:45 -07004009 if (i915_panel_use_ssc >= 0)
4010 return i915_panel_use_ssc != 0;
4011 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004012 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004013}
4014
Jesse Barnes5a354202011-06-24 12:19:22 -07004015/**
4016 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
4017 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004018 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07004019 *
4020 * A pipe may be connected to one or more outputs. Based on the depth of the
4021 * attached framebuffer, choose a good color depth to use on the pipe.
4022 *
4023 * If possible, match the pipe depth to the fb depth. In some cases, this
4024 * isn't ideal, because the connected output supports a lesser or restricted
4025 * set of depths. Resolve that here:
4026 * LVDS typically supports only 6bpc, so clamp down in that case
4027 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
4028 * Displays may support a restricted set as well, check EDID and clamp as
4029 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004030 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07004031 *
4032 * RETURNS:
4033 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
4034 * true if they don't match).
4035 */
4036static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004037 struct drm_framebuffer *fb,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004038 unsigned int *pipe_bpp,
4039 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07004040{
4041 struct drm_device *dev = crtc->dev;
4042 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes5a354202011-06-24 12:19:22 -07004043 struct drm_connector *connector;
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004044 struct intel_encoder *intel_encoder;
Jesse Barnes5a354202011-06-24 12:19:22 -07004045 unsigned int display_bpc = UINT_MAX, bpc;
4046
4047 /* Walk the encoders & connectors on this crtc, get min bpc */
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004048 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004049
4050 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
4051 unsigned int lvds_bpc;
4052
4053 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
4054 LVDS_A3_POWER_UP)
4055 lvds_bpc = 8;
4056 else
4057 lvds_bpc = 6;
4058
4059 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004060 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004061 display_bpc = lvds_bpc;
4062 }
4063 continue;
4064 }
4065
Jesse Barnes5a354202011-06-24 12:19:22 -07004066 /* Not one of the known troublemakers, check the EDID */
4067 list_for_each_entry(connector, &dev->mode_config.connector_list,
4068 head) {
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004069 if (connector->encoder != &intel_encoder->base)
Jesse Barnes5a354202011-06-24 12:19:22 -07004070 continue;
4071
Jesse Barnes62ac41a2011-07-28 12:55:14 -07004072 /* Don't use an invalid EDID bpc value */
4073 if (connector->display_info.bpc &&
4074 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004075 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 -07004076 display_bpc = connector->display_info.bpc;
4077 }
4078 }
4079
4080 /*
4081 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
4082 * through, clamp it down. (Note: >12bpc will be caught below.)
4083 */
4084 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
4085 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04004086 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004087 display_bpc = 12;
4088 } else {
Adam Jackson82820492011-10-10 16:33:34 -04004089 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004090 display_bpc = 8;
4091 }
4092 }
4093 }
4094
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004095 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4096 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
4097 display_bpc = 6;
4098 }
4099
Jesse Barnes5a354202011-06-24 12:19:22 -07004100 /*
4101 * We could just drive the pipe at the highest bpc all the time and
4102 * enable dithering as needed, but that costs bandwidth. So choose
4103 * the minimum value that expresses the full color range of the fb but
4104 * also stays within the max display bpc discovered above.
4105 */
4106
Daniel Vetter94352cf2012-07-05 22:51:56 +02004107 switch (fb->depth) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004108 case 8:
4109 bpc = 8; /* since we go through a colormap */
4110 break;
4111 case 15:
4112 case 16:
4113 bpc = 6; /* min is 18bpp */
4114 break;
4115 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07004116 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07004117 break;
4118 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07004119 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07004120 break;
4121 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07004122 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07004123 break;
4124 default:
4125 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
4126 bpc = min((unsigned int)8, display_bpc);
4127 break;
4128 }
4129
Keith Packard578393c2011-09-05 11:53:21 -07004130 display_bpc = min(display_bpc, bpc);
4131
Adam Jackson82820492011-10-10 16:33:34 -04004132 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
4133 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004134
Keith Packard578393c2011-09-05 11:53:21 -07004135 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07004136
4137 return display_bpc != bpc;
4138}
4139
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004140static int vlv_get_refclk(struct drm_crtc *crtc)
4141{
4142 struct drm_device *dev = crtc->dev;
4143 struct drm_i915_private *dev_priv = dev->dev_private;
4144 int refclk = 27000; /* for DP & HDMI */
4145
4146 return 100000; /* only one validated so far */
4147
4148 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4149 refclk = 96000;
4150 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4151 if (intel_panel_use_ssc(dev_priv))
4152 refclk = 100000;
4153 else
4154 refclk = 96000;
4155 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4156 refclk = 100000;
4157 }
4158
4159 return refclk;
4160}
4161
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004162static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4163{
4164 struct drm_device *dev = crtc->dev;
4165 struct drm_i915_private *dev_priv = dev->dev_private;
4166 int refclk;
4167
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004168 if (IS_VALLEYVIEW(dev)) {
4169 refclk = vlv_get_refclk(crtc);
4170 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004171 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4172 refclk = dev_priv->lvds_ssc_freq * 1000;
4173 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4174 refclk / 1000);
4175 } else if (!IS_GEN2(dev)) {
4176 refclk = 96000;
4177 } else {
4178 refclk = 48000;
4179 }
4180
4181 return refclk;
4182}
4183
4184static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
4185 intel_clock_t *clock)
4186{
4187 /* SDVO TV has fixed PLL values depend on its clock range,
4188 this mirrors vbios setting. */
4189 if (adjusted_mode->clock >= 100000
4190 && adjusted_mode->clock < 140500) {
4191 clock->p1 = 2;
4192 clock->p2 = 10;
4193 clock->n = 3;
4194 clock->m1 = 16;
4195 clock->m2 = 8;
4196 } else if (adjusted_mode->clock >= 140500
4197 && adjusted_mode->clock <= 200000) {
4198 clock->p1 = 1;
4199 clock->p2 = 10;
4200 clock->n = 6;
4201 clock->m1 = 12;
4202 clock->m2 = 8;
4203 }
4204}
4205
Jesse Barnesa7516a02011-12-15 12:30:37 -08004206static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
4207 intel_clock_t *clock,
4208 intel_clock_t *reduced_clock)
4209{
4210 struct drm_device *dev = crtc->dev;
4211 struct drm_i915_private *dev_priv = dev->dev_private;
4212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4213 int pipe = intel_crtc->pipe;
4214 u32 fp, fp2 = 0;
4215
4216 if (IS_PINEVIEW(dev)) {
4217 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
4218 if (reduced_clock)
4219 fp2 = (1 << reduced_clock->n) << 16 |
4220 reduced_clock->m1 << 8 | reduced_clock->m2;
4221 } else {
4222 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4223 if (reduced_clock)
4224 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4225 reduced_clock->m2;
4226 }
4227
4228 I915_WRITE(FP0(pipe), fp);
4229
4230 intel_crtc->lowfreq_avail = false;
4231 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4232 reduced_clock && i915_powersave) {
4233 I915_WRITE(FP1(pipe), fp2);
4234 intel_crtc->lowfreq_avail = true;
4235 } else {
4236 I915_WRITE(FP1(pipe), fp);
4237 }
4238}
4239
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004240static void vlv_update_pll(struct drm_crtc *crtc,
4241 struct drm_display_mode *mode,
4242 struct drm_display_mode *adjusted_mode,
4243 intel_clock_t *clock, intel_clock_t *reduced_clock,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304244 int num_connectors)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004245{
4246 struct drm_device *dev = crtc->dev;
4247 struct drm_i915_private *dev_priv = dev->dev_private;
4248 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4249 int pipe = intel_crtc->pipe;
4250 u32 dpll, mdiv, pdiv;
4251 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304252 bool is_sdvo;
4253 u32 temp;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004254
Daniel Vetter09153002012-12-12 14:06:44 +01004255 mutex_lock(&dev_priv->dpio_lock);
4256
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304257 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4258 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4259
4260 dpll = DPLL_VGA_MODE_DIS;
4261 dpll |= DPLL_EXT_BUFFER_ENABLE_VLV;
4262 dpll |= DPLL_REFA_CLK_ENABLE_VLV;
4263 dpll |= DPLL_INTEGRATED_CLOCK_VLV;
4264
4265 I915_WRITE(DPLL(pipe), dpll);
4266 POSTING_READ(DPLL(pipe));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004267
4268 bestn = clock->n;
4269 bestm1 = clock->m1;
4270 bestm2 = clock->m2;
4271 bestp1 = clock->p1;
4272 bestp2 = clock->p2;
4273
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304274 /*
4275 * In Valleyview PLL and program lane counter registers are exposed
4276 * through DPIO interface
4277 */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004278 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4279 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4280 mdiv |= ((bestn << DPIO_N_SHIFT));
4281 mdiv |= (1 << DPIO_POST_DIV_SHIFT);
4282 mdiv |= (1 << DPIO_K_SHIFT);
4283 mdiv |= DPIO_ENABLE_CALIBRATION;
4284 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4285
4286 intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
4287
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304288 pdiv = (1 << DPIO_REFSEL_OVERRIDE) | (5 << DPIO_PLL_MODESEL_SHIFT) |
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004289 (3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304290 (7 << DPIO_PLL_REFCLK_SEL_SHIFT) | (8 << DPIO_DRIVER_CTL_SHIFT) |
4291 (5 << DPIO_CLK_BIAS_CTL_SHIFT);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004292 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
4293
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304294 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x005f003b);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004295
4296 dpll |= DPLL_VCO_ENABLE;
4297 I915_WRITE(DPLL(pipe), dpll);
4298 POSTING_READ(DPLL(pipe));
4299 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4300 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4301
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304302 intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x620);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004303
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304304 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4305 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4306
4307 I915_WRITE(DPLL(pipe), dpll);
4308
4309 /* Wait for the clocks to stabilize. */
4310 POSTING_READ(DPLL(pipe));
4311 udelay(150);
4312
4313 temp = 0;
4314 if (is_sdvo) {
4315 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004316 if (temp > 1)
4317 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4318 else
4319 temp = 0;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004320 }
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304321 I915_WRITE(DPLL_MD(pipe), temp);
4322 POSTING_READ(DPLL_MD(pipe));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004323
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304324 /* Now program lane control registers */
4325 if(intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)
4326 || intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
4327 {
4328 temp = 0x1000C4;
4329 if(pipe == 1)
4330 temp |= (1 << 21);
4331 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL1, temp);
4332 }
4333 if(intel_pipe_has_type(crtc,INTEL_OUTPUT_EDP))
4334 {
4335 temp = 0x1000C4;
4336 if(pipe == 1)
4337 temp |= (1 << 21);
4338 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL2, temp);
4339 }
Daniel Vetter09153002012-12-12 14:06:44 +01004340
4341 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004342}
4343
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004344static void i9xx_update_pll(struct drm_crtc *crtc,
4345 struct drm_display_mode *mode,
4346 struct drm_display_mode *adjusted_mode,
4347 intel_clock_t *clock, intel_clock_t *reduced_clock,
4348 int num_connectors)
4349{
4350 struct drm_device *dev = crtc->dev;
4351 struct drm_i915_private *dev_priv = dev->dev_private;
4352 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004353 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004354 int pipe = intel_crtc->pipe;
4355 u32 dpll;
4356 bool is_sdvo;
4357
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304358 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4359
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004360 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4361 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4362
4363 dpll = DPLL_VGA_MODE_DIS;
4364
4365 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4366 dpll |= DPLLB_MODE_LVDS;
4367 else
4368 dpll |= DPLLB_MODE_DAC_SERIAL;
4369 if (is_sdvo) {
4370 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4371 if (pixel_multiplier > 1) {
4372 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4373 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4374 }
4375 dpll |= DPLL_DVO_HIGH_SPEED;
4376 }
4377 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4378 dpll |= DPLL_DVO_HIGH_SPEED;
4379
4380 /* compute bitmask from p1 value */
4381 if (IS_PINEVIEW(dev))
4382 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4383 else {
4384 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4385 if (IS_G4X(dev) && reduced_clock)
4386 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4387 }
4388 switch (clock->p2) {
4389 case 5:
4390 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4391 break;
4392 case 7:
4393 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4394 break;
4395 case 10:
4396 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4397 break;
4398 case 14:
4399 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4400 break;
4401 }
4402 if (INTEL_INFO(dev)->gen >= 4)
4403 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4404
4405 if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4406 dpll |= PLL_REF_INPUT_TVCLKINBC;
4407 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4408 /* XXX: just matching BIOS for now */
4409 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4410 dpll |= 3;
4411 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4412 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4413 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4414 else
4415 dpll |= PLL_REF_INPUT_DREFCLK;
4416
4417 dpll |= DPLL_VCO_ENABLE;
4418 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4419 POSTING_READ(DPLL(pipe));
4420 udelay(150);
4421
Daniel Vetterdafd2262012-11-26 17:22:07 +01004422 for_each_encoder_on_crtc(dev, crtc, encoder)
4423 if (encoder->pre_pll_enable)
4424 encoder->pre_pll_enable(encoder);
4425
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004426 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4427 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4428
4429 I915_WRITE(DPLL(pipe), dpll);
4430
4431 /* Wait for the clocks to stabilize. */
4432 POSTING_READ(DPLL(pipe));
4433 udelay(150);
4434
4435 if (INTEL_INFO(dev)->gen >= 4) {
4436 u32 temp = 0;
4437 if (is_sdvo) {
4438 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4439 if (temp > 1)
4440 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4441 else
4442 temp = 0;
4443 }
4444 I915_WRITE(DPLL_MD(pipe), temp);
4445 } else {
4446 /* The pixel multiplier can only be updated once the
4447 * DPLL is enabled and the clocks are stable.
4448 *
4449 * So write it again.
4450 */
4451 I915_WRITE(DPLL(pipe), dpll);
4452 }
4453}
4454
4455static void i8xx_update_pll(struct drm_crtc *crtc,
4456 struct drm_display_mode *adjusted_mode,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304457 intel_clock_t *clock, intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004458 int num_connectors)
4459{
4460 struct drm_device *dev = crtc->dev;
4461 struct drm_i915_private *dev_priv = dev->dev_private;
4462 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004463 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004464 int pipe = intel_crtc->pipe;
4465 u32 dpll;
4466
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304467 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4468
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004469 dpll = DPLL_VGA_MODE_DIS;
4470
4471 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4472 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4473 } else {
4474 if (clock->p1 == 2)
4475 dpll |= PLL_P1_DIVIDE_BY_TWO;
4476 else
4477 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4478 if (clock->p2 == 4)
4479 dpll |= PLL_P2_DIVIDE_BY_4;
4480 }
4481
4482 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4483 /* XXX: just matching BIOS for now */
4484 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4485 dpll |= 3;
4486 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4487 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4488 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4489 else
4490 dpll |= PLL_REF_INPUT_DREFCLK;
4491
4492 dpll |= DPLL_VCO_ENABLE;
4493 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4494 POSTING_READ(DPLL(pipe));
4495 udelay(150);
4496
Daniel Vetterdafd2262012-11-26 17:22:07 +01004497 for_each_encoder_on_crtc(dev, crtc, encoder)
4498 if (encoder->pre_pll_enable)
4499 encoder->pre_pll_enable(encoder);
4500
Daniel Vetter5b5896e2012-09-11 12:37:55 +02004501 I915_WRITE(DPLL(pipe), dpll);
4502
4503 /* Wait for the clocks to stabilize. */
4504 POSTING_READ(DPLL(pipe));
4505 udelay(150);
4506
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004507 /* The pixel multiplier can only be updated once the
4508 * DPLL is enabled and the clocks are stable.
4509 *
4510 * So write it again.
4511 */
4512 I915_WRITE(DPLL(pipe), dpll);
4513}
4514
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004515static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
4516 struct drm_display_mode *mode,
4517 struct drm_display_mode *adjusted_mode)
4518{
4519 struct drm_device *dev = intel_crtc->base.dev;
4520 struct drm_i915_private *dev_priv = dev->dev_private;
4521 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004522 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004523 uint32_t vsyncshift;
4524
4525 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4526 /* the chip adds 2 halflines automatically */
4527 adjusted_mode->crtc_vtotal -= 1;
4528 adjusted_mode->crtc_vblank_end -= 1;
4529 vsyncshift = adjusted_mode->crtc_hsync_start
4530 - adjusted_mode->crtc_htotal / 2;
4531 } else {
4532 vsyncshift = 0;
4533 }
4534
4535 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004536 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004537
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004538 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004539 (adjusted_mode->crtc_hdisplay - 1) |
4540 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004541 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004542 (adjusted_mode->crtc_hblank_start - 1) |
4543 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004544 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004545 (adjusted_mode->crtc_hsync_start - 1) |
4546 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4547
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004548 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004549 (adjusted_mode->crtc_vdisplay - 1) |
4550 ((adjusted_mode->crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004551 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004552 (adjusted_mode->crtc_vblank_start - 1) |
4553 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004554 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004555 (adjusted_mode->crtc_vsync_start - 1) |
4556 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4557
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004558 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4559 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4560 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4561 * bits. */
4562 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4563 (pipe == PIPE_B || pipe == PIPE_C))
4564 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4565
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004566 /* pipesrc controls the size that is scaled from, which should
4567 * always be the user's requested size.
4568 */
4569 I915_WRITE(PIPESRC(pipe),
4570 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4571}
4572
Eric Anholtf564048e2011-03-30 13:01:02 -07004573static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4574 struct drm_display_mode *mode,
4575 struct drm_display_mode *adjusted_mode,
4576 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004577 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004578{
4579 struct drm_device *dev = crtc->dev;
4580 struct drm_i915_private *dev_priv = dev->dev_private;
4581 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4582 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004583 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004584 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004585 intel_clock_t clock, reduced_clock;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004586 u32 dspcntr, pipeconf;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004587 bool ok, has_reduced_clock = false, is_sdvo = false;
4588 bool is_lvds = false, is_tv = false, is_dp = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004589 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004590 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004591 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004592
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004593 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004594 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004595 case INTEL_OUTPUT_LVDS:
4596 is_lvds = true;
4597 break;
4598 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08004599 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08004600 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01004601 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004602 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004603 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004604 case INTEL_OUTPUT_TVOUT:
4605 is_tv = true;
4606 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004607 case INTEL_OUTPUT_DISPLAYPORT:
4608 is_dp = true;
4609 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004610 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004611
Eric Anholtc751ce42010-03-25 11:48:48 -07004612 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004613 }
4614
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004615 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004616
Ma Lingd4906092009-03-18 20:13:27 +08004617 /*
4618 * Returns a set of divisors for the desired target clock with the given
4619 * refclk, or FALSE. The returned values represent the clock equation:
4620 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4621 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004622 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08004623 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4624 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004625 if (!ok) {
4626 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004627 return -EINVAL;
4628 }
4629
4630 /* Ensure that the cursor is valid for the new mode before changing... */
4631 intel_crtc_update_cursor(crtc, true);
4632
4633 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004634 /*
4635 * Ensure we match the reduced clock's P to the target clock.
4636 * If the clocks don't match, we can't switch the display clock
4637 * by using the FP0/FP1. In such case we will disable the LVDS
4638 * downclock feature.
4639 */
Eric Anholtf564048e2011-03-30 13:01:02 -07004640 has_reduced_clock = limit->find_pll(limit, crtc,
4641 dev_priv->lvds_downclock,
4642 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08004643 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004644 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004645 }
4646
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004647 if (is_sdvo && is_tv)
4648 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004649
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004650 if (IS_GEN2(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304651 i8xx_update_pll(crtc, adjusted_mode, &clock,
4652 has_reduced_clock ? &reduced_clock : NULL,
4653 num_connectors);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004654 else if (IS_VALLEYVIEW(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304655 vlv_update_pll(crtc, mode, adjusted_mode, &clock,
4656 has_reduced_clock ? &reduced_clock : NULL,
4657 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004658 else
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004659 i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
4660 has_reduced_clock ? &reduced_clock : NULL,
4661 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004662
4663 /* setup pipeconf */
4664 pipeconf = I915_READ(PIPECONF(pipe));
4665
4666 /* Set up the display plane register */
4667 dspcntr = DISPPLANE_GAMMA_ENABLE;
4668
Eric Anholt929c77f2011-03-30 13:01:04 -07004669 if (pipe == 0)
4670 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4671 else
4672 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07004673
4674 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4675 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4676 * core speed.
4677 *
4678 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4679 * pipe == 0 check?
4680 */
4681 if (mode->clock >
4682 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4683 pipeconf |= PIPECONF_DOUBLE_WIDE;
4684 else
4685 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4686 }
4687
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004688 /* default to 8bpc */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004689 pipeconf &= ~(PIPECONF_BPC_MASK | PIPECONF_DITHER_EN);
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004690 if (is_dp) {
Jani Nikula0c96c652012-09-26 18:43:10 +03004691 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004692 pipeconf |= PIPECONF_6BPC |
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004693 PIPECONF_DITHER_EN |
4694 PIPECONF_DITHER_TYPE_SP;
4695 }
4696 }
4697
Gajanan Bhat19c03922012-09-27 19:13:07 +05304698 if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4699 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004700 pipeconf |= PIPECONF_6BPC |
Gajanan Bhat19c03922012-09-27 19:13:07 +05304701 PIPECONF_ENABLE |
4702 I965_PIPECONF_ACTIVE;
4703 }
4704 }
4705
Eric Anholtf564048e2011-03-30 13:01:02 -07004706 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
4707 drm_mode_debug_printmodeline(mode);
4708
Jesse Barnesa7516a02011-12-15 12:30:37 -08004709 if (HAS_PIPE_CXSR(dev)) {
4710 if (intel_crtc->lowfreq_avail) {
Eric Anholtf564048e2011-03-30 13:01:02 -07004711 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4712 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004713 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07004714 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4715 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4716 }
4717 }
4718
Keith Packard617cf882012-02-08 13:53:38 -08004719 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetterdbb02572012-01-28 14:49:23 +01004720 if (!IS_GEN2(dev) &&
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004721 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Eric Anholtf564048e2011-03-30 13:01:02 -07004722 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004723 else
Keith Packard617cf882012-02-08 13:53:38 -08004724 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01004725
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004726 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Eric Anholtf564048e2011-03-30 13:01:02 -07004727
4728 /* pipesrc and dspsize control the size that is scaled from,
4729 * which should always be the user's requested size.
4730 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004731 I915_WRITE(DSPSIZE(plane),
4732 ((mode->vdisplay - 1) << 16) |
4733 (mode->hdisplay - 1));
4734 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004735
Eric Anholtf564048e2011-03-30 13:01:02 -07004736 I915_WRITE(PIPECONF(pipe), pipeconf);
4737 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07004738 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07004739
4740 intel_wait_for_vblank(dev, pipe);
4741
Eric Anholtf564048e2011-03-30 13:01:02 -07004742 I915_WRITE(DSPCNTR(plane), dspcntr);
4743 POSTING_READ(DSPCNTR(plane));
4744
Daniel Vetter94352cf2012-07-05 22:51:56 +02004745 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004746
4747 intel_update_watermarks(dev);
4748
Eric Anholtf564048e2011-03-30 13:01:02 -07004749 return ret;
4750}
4751
Keith Packard9fb526d2011-09-26 22:24:57 -07004752/*
4753 * Initialize reference clocks when the driver loads
4754 */
4755void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07004756{
4757 struct drm_i915_private *dev_priv = dev->dev_private;
4758 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004759 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004760 u32 temp;
4761 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004762 bool has_cpu_edp = false;
4763 bool has_pch_edp = false;
4764 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07004765 bool has_ck505 = false;
4766 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004767
4768 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07004769 list_for_each_entry(encoder, &mode_config->encoder_list,
4770 base.head) {
4771 switch (encoder->type) {
4772 case INTEL_OUTPUT_LVDS:
4773 has_panel = true;
4774 has_lvds = true;
4775 break;
4776 case INTEL_OUTPUT_EDP:
4777 has_panel = true;
4778 if (intel_encoder_is_pch_edp(&encoder->base))
4779 has_pch_edp = true;
4780 else
4781 has_cpu_edp = true;
4782 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004783 }
4784 }
4785
Keith Packard99eb6a02011-09-26 14:29:12 -07004786 if (HAS_PCH_IBX(dev)) {
4787 has_ck505 = dev_priv->display_clock_mode;
4788 can_ssc = has_ck505;
4789 } else {
4790 has_ck505 = false;
4791 can_ssc = true;
4792 }
4793
4794 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4795 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4796 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004797
4798 /* Ironlake: try to setup display ref clock before DPLL
4799 * enabling. This is only under driver's control after
4800 * PCH B stepping, previous chipset stepping should be
4801 * ignoring this setting.
4802 */
4803 temp = I915_READ(PCH_DREF_CONTROL);
4804 /* Always enable nonspread source */
4805 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004806
Keith Packard99eb6a02011-09-26 14:29:12 -07004807 if (has_ck505)
4808 temp |= DREF_NONSPREAD_CK505_ENABLE;
4809 else
4810 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004811
Keith Packard199e5d72011-09-22 12:01:57 -07004812 if (has_panel) {
4813 temp &= ~DREF_SSC_SOURCE_MASK;
4814 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004815
Keith Packard199e5d72011-09-22 12:01:57 -07004816 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07004817 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004818 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004819 temp |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02004820 } else
4821 temp &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004822
4823 /* Get SSC going before enabling the outputs */
4824 I915_WRITE(PCH_DREF_CONTROL, temp);
4825 POSTING_READ(PCH_DREF_CONTROL);
4826 udelay(200);
4827
Jesse Barnes13d83a62011-08-03 12:59:20 -07004828 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4829
4830 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07004831 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07004832 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004833 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004834 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004835 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07004836 else
4837 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004838 } else
4839 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4840
4841 I915_WRITE(PCH_DREF_CONTROL, temp);
4842 POSTING_READ(PCH_DREF_CONTROL);
4843 udelay(200);
4844 } else {
4845 DRM_DEBUG_KMS("Disabling SSC entirely\n");
4846
4847 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4848
4849 /* Turn off CPU output */
4850 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4851
4852 I915_WRITE(PCH_DREF_CONTROL, temp);
4853 POSTING_READ(PCH_DREF_CONTROL);
4854 udelay(200);
4855
4856 /* Turn off the SSC source */
4857 temp &= ~DREF_SSC_SOURCE_MASK;
4858 temp |= DREF_SSC_SOURCE_DISABLE;
4859
4860 /* Turn off SSC1 */
4861 temp &= ~ DREF_SSC1_ENABLE;
4862
Jesse Barnes13d83a62011-08-03 12:59:20 -07004863 I915_WRITE(PCH_DREF_CONTROL, temp);
4864 POSTING_READ(PCH_DREF_CONTROL);
4865 udelay(200);
4866 }
4867}
4868
Jesse Barnesd9d444c2011-09-02 13:03:05 -07004869static int ironlake_get_refclk(struct drm_crtc *crtc)
4870{
4871 struct drm_device *dev = crtc->dev;
4872 struct drm_i915_private *dev_priv = dev->dev_private;
4873 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07004874 struct intel_encoder *edp_encoder = NULL;
4875 int num_connectors = 0;
4876 bool is_lvds = false;
4877
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004878 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07004879 switch (encoder->type) {
4880 case INTEL_OUTPUT_LVDS:
4881 is_lvds = true;
4882 break;
4883 case INTEL_OUTPUT_EDP:
4884 edp_encoder = encoder;
4885 break;
4886 }
4887 num_connectors++;
4888 }
4889
4890 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4891 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4892 dev_priv->lvds_ssc_freq);
4893 return dev_priv->lvds_ssc_freq * 1000;
4894 }
4895
4896 return 120000;
4897}
4898
Paulo Zanonic8203562012-09-12 10:06:29 -03004899static void ironlake_set_pipeconf(struct drm_crtc *crtc,
4900 struct drm_display_mode *adjusted_mode,
4901 bool dither)
4902{
4903 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4905 int pipe = intel_crtc->pipe;
4906 uint32_t val;
4907
4908 val = I915_READ(PIPECONF(pipe));
4909
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004910 val &= ~PIPECONF_BPC_MASK;
Paulo Zanonic8203562012-09-12 10:06:29 -03004911 switch (intel_crtc->bpp) {
4912 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004913 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03004914 break;
4915 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004916 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03004917 break;
4918 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004919 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03004920 break;
4921 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004922 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03004923 break;
4924 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03004925 /* Case prevented by intel_choose_pipe_bpp_dither. */
4926 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03004927 }
4928
4929 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4930 if (dither)
4931 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4932
4933 val &= ~PIPECONF_INTERLACE_MASK;
4934 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
4935 val |= PIPECONF_INTERLACED_ILK;
4936 else
4937 val |= PIPECONF_PROGRESSIVE;
4938
4939 I915_WRITE(PIPECONF(pipe), val);
4940 POSTING_READ(PIPECONF(pipe));
4941}
4942
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004943static void haswell_set_pipeconf(struct drm_crtc *crtc,
4944 struct drm_display_mode *adjusted_mode,
4945 bool dither)
4946{
4947 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni702e7a52012-10-23 18:29:59 -02004949 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004950 uint32_t val;
4951
Paulo Zanoni702e7a52012-10-23 18:29:59 -02004952 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004953
4954 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
4955 if (dither)
4956 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4957
4958 val &= ~PIPECONF_INTERLACE_MASK_HSW;
4959 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
4960 val |= PIPECONF_INTERLACED_ILK;
4961 else
4962 val |= PIPECONF_PROGRESSIVE;
4963
Paulo Zanoni702e7a52012-10-23 18:29:59 -02004964 I915_WRITE(PIPECONF(cpu_transcoder), val);
4965 POSTING_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03004966}
4967
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03004968static bool ironlake_compute_clocks(struct drm_crtc *crtc,
4969 struct drm_display_mode *adjusted_mode,
4970 intel_clock_t *clock,
4971 bool *has_reduced_clock,
4972 intel_clock_t *reduced_clock)
4973{
4974 struct drm_device *dev = crtc->dev;
4975 struct drm_i915_private *dev_priv = dev->dev_private;
4976 struct intel_encoder *intel_encoder;
4977 int refclk;
4978 const intel_limit_t *limit;
4979 bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
4980
4981 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
4982 switch (intel_encoder->type) {
4983 case INTEL_OUTPUT_LVDS:
4984 is_lvds = true;
4985 break;
4986 case INTEL_OUTPUT_SDVO:
4987 case INTEL_OUTPUT_HDMI:
4988 is_sdvo = true;
4989 if (intel_encoder->needs_tv_clock)
4990 is_tv = true;
4991 break;
4992 case INTEL_OUTPUT_TVOUT:
4993 is_tv = true;
4994 break;
4995 }
4996 }
4997
4998 refclk = ironlake_get_refclk(crtc);
4999
5000 /*
5001 * Returns a set of divisors for the desired target clock with the given
5002 * refclk, or FALSE. The returned values represent the clock equation:
5003 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5004 */
5005 limit = intel_limit(crtc, refclk);
5006 ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5007 clock);
5008 if (!ret)
5009 return false;
5010
5011 if (is_lvds && dev_priv->lvds_downclock_avail) {
5012 /*
5013 * Ensure we match the reduced clock's P to the target clock.
5014 * If the clocks don't match, we can't switch the display clock
5015 * by using the FP0/FP1. In such case we will disable the LVDS
5016 * downclock feature.
5017 */
5018 *has_reduced_clock = limit->find_pll(limit, crtc,
5019 dev_priv->lvds_downclock,
5020 refclk,
5021 clock,
5022 reduced_clock);
5023 }
5024
5025 if (is_sdvo && is_tv)
5026 i9xx_adjust_sdvo_tv_clock(adjusted_mode, clock);
5027
5028 return true;
5029}
5030
Daniel Vetter01a415f2012-10-27 15:58:40 +02005031static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5032{
5033 struct drm_i915_private *dev_priv = dev->dev_private;
5034 uint32_t temp;
5035
5036 temp = I915_READ(SOUTH_CHICKEN1);
5037 if (temp & FDI_BC_BIFURCATION_SELECT)
5038 return;
5039
5040 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5041 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5042
5043 temp |= FDI_BC_BIFURCATION_SELECT;
5044 DRM_DEBUG_KMS("enabling fdi C rx\n");
5045 I915_WRITE(SOUTH_CHICKEN1, temp);
5046 POSTING_READ(SOUTH_CHICKEN1);
5047}
5048
5049static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
5050{
5051 struct drm_device *dev = intel_crtc->base.dev;
5052 struct drm_i915_private *dev_priv = dev->dev_private;
5053 struct intel_crtc *pipe_B_crtc =
5054 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5055
5056 DRM_DEBUG_KMS("checking fdi config on pipe %i, lanes %i\n",
5057 intel_crtc->pipe, intel_crtc->fdi_lanes);
5058 if (intel_crtc->fdi_lanes > 4) {
5059 DRM_DEBUG_KMS("invalid fdi lane config on pipe %i: %i lanes\n",
5060 intel_crtc->pipe, intel_crtc->fdi_lanes);
5061 /* Clamp lanes to avoid programming the hw with bogus values. */
5062 intel_crtc->fdi_lanes = 4;
5063
5064 return false;
5065 }
5066
5067 if (dev_priv->num_pipe == 2)
5068 return true;
5069
5070 switch (intel_crtc->pipe) {
5071 case PIPE_A:
5072 return true;
5073 case PIPE_B:
5074 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5075 intel_crtc->fdi_lanes > 2) {
5076 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5077 intel_crtc->pipe, intel_crtc->fdi_lanes);
5078 /* Clamp lanes to avoid programming the hw with bogus values. */
5079 intel_crtc->fdi_lanes = 2;
5080
5081 return false;
5082 }
5083
5084 if (intel_crtc->fdi_lanes > 2)
5085 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5086 else
5087 cpt_enable_fdi_bc_bifurcation(dev);
5088
5089 return true;
5090 case PIPE_C:
5091 if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
5092 if (intel_crtc->fdi_lanes > 2) {
5093 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5094 intel_crtc->pipe, intel_crtc->fdi_lanes);
5095 /* Clamp lanes to avoid programming the hw with bogus values. */
5096 intel_crtc->fdi_lanes = 2;
5097
5098 return false;
5099 }
5100 } else {
5101 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5102 return false;
5103 }
5104
5105 cpt_enable_fdi_bc_bifurcation(dev);
5106
5107 return true;
5108 default:
5109 BUG();
5110 }
5111}
5112
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005113static void ironlake_set_m_n(struct drm_crtc *crtc,
5114 struct drm_display_mode *mode,
5115 struct drm_display_mode *adjusted_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08005116{
5117 struct drm_device *dev = crtc->dev;
5118 struct drm_i915_private *dev_priv = dev->dev_private;
5119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005120 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005121 struct intel_encoder *intel_encoder, *edp_encoder = NULL;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005122 struct intel_link_m_n m_n = {0};
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005123 int target_clock, pixel_multiplier, lane, link_bw;
5124 bool is_dp = false, is_cpu_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005125
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005126 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5127 switch (intel_encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005128 case INTEL_OUTPUT_DISPLAYPORT:
5129 is_dp = true;
5130 break;
5131 case INTEL_OUTPUT_EDP:
Jesse Barnese3aef172012-04-10 11:58:03 -07005132 is_dp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005133 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnese3aef172012-04-10 11:58:03 -07005134 is_cpu_edp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005135 edp_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005136 break;
5137 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005138 }
5139
Zhenyu Wang2c072452009-06-05 15:38:42 +08005140 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07005141 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5142 lane = 0;
5143 /* CPU eDP doesn't require FDI link, so just set DP M/N
5144 according to current link config */
Jesse Barnese3aef172012-04-10 11:58:03 -07005145 if (is_cpu_edp) {
Jesse Barnese3aef172012-04-10 11:58:03 -07005146 intel_edp_link_config(edp_encoder, &lane, &link_bw);
Eric Anholt8febb292011-03-30 13:01:07 -07005147 } else {
Eric Anholt8febb292011-03-30 13:01:07 -07005148 /* FDI is a binary signal running at ~2.7GHz, encoding
5149 * each output octet as 10 bits. The actual frequency
5150 * is stored as a divider into a 100MHz clock, and the
5151 * mode pixel clock is stored in units of 1KHz.
5152 * Hence the bw of each lane in terms of the mode signal
5153 * is:
5154 */
5155 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005156 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08005157
Daniel Vetter94bf2ce2012-06-04 18:39:19 +02005158 /* [e]DP over FDI requires target mode clock instead of link clock. */
5159 if (edp_encoder)
5160 target_clock = intel_edp_target_clock(edp_encoder, mode);
5161 else if (is_dp)
5162 target_clock = mode->clock;
5163 else
5164 target_clock = adjusted_mode->clock;
5165
Eric Anholt8febb292011-03-30 13:01:07 -07005166 if (!lane) {
5167 /*
5168 * Account for spread spectrum to avoid
5169 * oversubscribing the link. Max center spread
5170 * is 2.5%; use 5% for safety's sake.
5171 */
Jesse Barnes5a354202011-06-24 12:19:22 -07005172 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
Eric Anholt8febb292011-03-30 13:01:07 -07005173 lane = bps / (link_bw * 8) + 1;
5174 }
5175
5176 intel_crtc->fdi_lanes = lane;
5177
5178 if (pixel_multiplier > 1)
5179 link_bw *= pixel_multiplier;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005180 intel_link_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw, &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07005181
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005182 I915_WRITE(PIPE_DATA_M1(cpu_transcoder), TU_SIZE(m_n.tu) | m_n.gmch_m);
5183 I915_WRITE(PIPE_DATA_N1(cpu_transcoder), m_n.gmch_n);
5184 I915_WRITE(PIPE_LINK_M1(cpu_transcoder), m_n.link_m);
5185 I915_WRITE(PIPE_LINK_N1(cpu_transcoder), m_n.link_n);
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005186}
5187
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005188static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5189 struct drm_display_mode *adjusted_mode,
5190 intel_clock_t *clock, u32 fp)
5191{
5192 struct drm_crtc *crtc = &intel_crtc->base;
5193 struct drm_device *dev = crtc->dev;
5194 struct drm_i915_private *dev_priv = dev->dev_private;
5195 struct intel_encoder *intel_encoder;
5196 uint32_t dpll;
5197 int factor, pixel_multiplier, num_connectors = 0;
5198 bool is_lvds = false, is_sdvo = false, is_tv = false;
5199 bool is_dp = false, is_cpu_edp = false;
5200
5201 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5202 switch (intel_encoder->type) {
5203 case INTEL_OUTPUT_LVDS:
5204 is_lvds = true;
5205 break;
5206 case INTEL_OUTPUT_SDVO:
5207 case INTEL_OUTPUT_HDMI:
5208 is_sdvo = true;
5209 if (intel_encoder->needs_tv_clock)
5210 is_tv = true;
5211 break;
5212 case INTEL_OUTPUT_TVOUT:
5213 is_tv = true;
5214 break;
5215 case INTEL_OUTPUT_DISPLAYPORT:
5216 is_dp = true;
5217 break;
5218 case INTEL_OUTPUT_EDP:
5219 is_dp = true;
5220 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
5221 is_cpu_edp = true;
5222 break;
5223 }
5224
5225 num_connectors++;
5226 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005227
Chris Wilsonc1858122010-12-03 21:35:48 +00005228 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005229 factor = 21;
5230 if (is_lvds) {
5231 if ((intel_panel_use_ssc(dev_priv) &&
5232 dev_priv->lvds_ssc_freq == 100) ||
Daniel Vetter1974cad2012-11-26 17:22:09 +01005233 intel_is_dual_link_lvds(dev))
Eric Anholt8febb292011-03-30 13:01:07 -07005234 factor = 25;
5235 } else if (is_sdvo && is_tv)
5236 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005237
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005238 if (clock->m < factor * clock->n)
Eric Anholt8febb292011-03-30 13:01:07 -07005239 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005240
Chris Wilson5eddb702010-09-11 13:48:45 +01005241 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005242
Eric Anholta07d6782011-03-30 13:01:08 -07005243 if (is_lvds)
5244 dpll |= DPLLB_MODE_LVDS;
5245 else
5246 dpll |= DPLLB_MODE_DAC_SERIAL;
5247 if (is_sdvo) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005248 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
Eric Anholta07d6782011-03-30 13:01:08 -07005249 if (pixel_multiplier > 1) {
5250 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08005251 }
Eric Anholta07d6782011-03-30 13:01:08 -07005252 dpll |= DPLL_DVO_HIGH_SPEED;
5253 }
Jesse Barnese3aef172012-04-10 11:58:03 -07005254 if (is_dp && !is_cpu_edp)
Eric Anholta07d6782011-03-30 13:01:08 -07005255 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005256
Eric Anholta07d6782011-03-30 13:01:08 -07005257 /* compute bitmask from p1 value */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005258 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005259 /* also FPA1 */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005260 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005261
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005262 switch (clock->p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005263 case 5:
5264 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5265 break;
5266 case 7:
5267 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5268 break;
5269 case 10:
5270 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5271 break;
5272 case 14:
5273 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5274 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005275 }
5276
5277 if (is_sdvo && is_tv)
5278 dpll |= PLL_REF_INPUT_TVCLKINBC;
5279 else if (is_tv)
5280 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005281 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08005282 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00005283 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005284 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005285 else
5286 dpll |= PLL_REF_INPUT_DREFCLK;
5287
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005288 return dpll;
5289}
5290
Jesse Barnes79e53942008-11-07 14:24:08 -08005291static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5292 struct drm_display_mode *mode,
5293 struct drm_display_mode *adjusted_mode,
5294 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005295 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005296{
5297 struct drm_device *dev = crtc->dev;
5298 struct drm_i915_private *dev_priv = dev->dev_private;
5299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5300 int pipe = intel_crtc->pipe;
5301 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005302 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005303 intel_clock_t clock, reduced_clock;
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005304 u32 dpll, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005305 bool ok, has_reduced_clock = false;
5306 bool is_lvds = false, is_dp = false, is_cpu_edp = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005307 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005308 int ret;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005309 bool dither, fdi_config_ok;
Jesse Barnes79e53942008-11-07 14:24:08 -08005310
5311 for_each_encoder_on_crtc(dev, crtc, encoder) {
5312 switch (encoder->type) {
5313 case INTEL_OUTPUT_LVDS:
5314 is_lvds = true;
5315 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005316 case INTEL_OUTPUT_DISPLAYPORT:
5317 is_dp = true;
5318 break;
5319 case INTEL_OUTPUT_EDP:
5320 is_dp = true;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005321 if (!intel_encoder_is_pch_edp(&encoder->base))
Jesse Barnes79e53942008-11-07 14:24:08 -08005322 is_cpu_edp = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005323 break;
5324 }
5325
5326 num_connectors++;
5327 }
5328
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005329 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5330 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5331
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005332 ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
5333 &has_reduced_clock, &reduced_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005334 if (!ok) {
5335 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5336 return -EINVAL;
5337 }
5338
5339 /* Ensure that the cursor is valid for the new mode before changing... */
5340 intel_crtc_update_cursor(crtc, true);
5341
Jesse Barnes79e53942008-11-07 14:24:08 -08005342 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005343 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5344 adjusted_mode);
Paulo Zanonic8203562012-09-12 10:06:29 -03005345 if (is_lvds && dev_priv->lvds_dither)
5346 dither = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005347
Jesse Barnes79e53942008-11-07 14:24:08 -08005348 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5349 if (has_reduced_clock)
5350 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5351 reduced_clock.m2;
5352
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005353 dpll = ironlake_compute_dpll(intel_crtc, adjusted_mode, &clock, fp);
Jesse Barnes79e53942008-11-07 14:24:08 -08005354
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07005355 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005356 drm_mode_debug_printmodeline(mode);
5357
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005358 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5359 if (!is_cpu_edp) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005360 struct intel_pch_pll *pll;
Chris Wilson5eddb702010-09-11 13:48:45 +01005361
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005362 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5363 if (pll == NULL) {
5364 DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
5365 pipe);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005366 return -EINVAL;
5367 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005368 } else
5369 intel_put_pch_pll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005370
Daniel Vetter2f0c2ad2012-11-29 15:59:35 +01005371 if (is_dp && !is_cpu_edp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005372 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08005373
Daniel Vetterdafd2262012-11-26 17:22:07 +01005374 for_each_encoder_on_crtc(dev, crtc, encoder)
5375 if (encoder->pre_pll_enable)
5376 encoder->pre_pll_enable(encoder);
5377
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005378 if (intel_crtc->pch_pll) {
5379 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005380
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005381 /* Wait for the clocks to stabilize. */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005382 POSTING_READ(intel_crtc->pch_pll->pll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005383 udelay(150);
5384
Eric Anholt8febb292011-03-30 13:01:07 -07005385 /* The pixel multiplier can only be updated once the
5386 * DPLL is enabled and the clocks are stable.
5387 *
5388 * So write it again.
5389 */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005390 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08005391 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005392
Chris Wilson5eddb702010-09-11 13:48:45 +01005393 intel_crtc->lowfreq_avail = false;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005394 if (intel_crtc->pch_pll) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07005395 if (is_lvds && has_reduced_clock && i915_powersave) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005396 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005397 intel_crtc->lowfreq_avail = true;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005398 } else {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005399 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07005400 }
5401 }
5402
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005403 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005404
Daniel Vetter01a415f2012-10-27 15:58:40 +02005405 /* Note, this also computes intel_crtc->fdi_lanes which is used below in
5406 * ironlake_check_fdi_lanes. */
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005407 ironlake_set_m_n(crtc, mode, adjusted_mode);
Chris Wilson5eddb702010-09-11 13:48:45 +01005408
Daniel Vetter01a415f2012-10-27 15:58:40 +02005409 fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005410
Paulo Zanonic8203562012-09-12 10:06:29 -03005411 ironlake_set_pipeconf(crtc, adjusted_mode, dither);
Jesse Barnes79e53942008-11-07 14:24:08 -08005412
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005413 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005414
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005415 /* Set up the display plane register */
5416 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005417 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005418
Daniel Vetter94352cf2012-07-05 22:51:56 +02005419 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005420
5421 intel_update_watermarks(dev);
5422
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03005423 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5424
Daniel Vetter01a415f2012-10-27 15:58:40 +02005425 return fdi_config_ok ? ret : -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005426}
5427
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005428static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5429 struct drm_display_mode *mode,
5430 struct drm_display_mode *adjusted_mode,
5431 int x, int y,
5432 struct drm_framebuffer *fb)
5433{
5434 struct drm_device *dev = crtc->dev;
5435 struct drm_i915_private *dev_priv = dev->dev_private;
5436 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5437 int pipe = intel_crtc->pipe;
5438 int plane = intel_crtc->plane;
5439 int num_connectors = 0;
Daniel Vettered7ef432012-12-06 14:24:21 +01005440 bool is_dp = false, is_cpu_edp = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005441 struct intel_encoder *encoder;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005442 int ret;
5443 bool dither;
5444
5445 for_each_encoder_on_crtc(dev, crtc, encoder) {
5446 switch (encoder->type) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005447 case INTEL_OUTPUT_DISPLAYPORT:
5448 is_dp = true;
5449 break;
5450 case INTEL_OUTPUT_EDP:
5451 is_dp = true;
5452 if (!intel_encoder_is_pch_edp(&encoder->base))
5453 is_cpu_edp = true;
5454 break;
5455 }
5456
5457 num_connectors++;
5458 }
5459
Paulo Zanonia5c961d2012-10-24 15:59:34 -02005460 if (is_cpu_edp)
5461 intel_crtc->cpu_transcoder = TRANSCODER_EDP;
5462 else
5463 intel_crtc->cpu_transcoder = pipe;
5464
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005465 /* We are not sure yet this won't happen. */
5466 WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
5467 INTEL_PCH_TYPE(dev));
5468
5469 WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
5470 num_connectors, pipe_name(pipe));
5471
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005472 WARN_ON(I915_READ(PIPECONF(intel_crtc->cpu_transcoder)) &
Paulo Zanoni1ce42922012-10-05 12:06:01 -03005473 (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
5474
5475 WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
5476
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005477 if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
5478 return -EINVAL;
5479
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005480 /* Ensure that the cursor is valid for the new mode before changing... */
5481 intel_crtc_update_cursor(crtc, true);
5482
5483 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005484 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5485 adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005486
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005487 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
5488 drm_mode_debug_printmodeline(mode);
5489
Daniel Vettered7ef432012-12-06 14:24:21 +01005490 if (is_dp && !is_cpu_edp)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005491 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005492
5493 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005494
5495 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5496
Paulo Zanoni1eb8dfe2012-10-18 12:42:10 -03005497 if (!is_dp || is_cpu_edp)
5498 ironlake_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005499
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005500 haswell_set_pipeconf(crtc, adjusted_mode, dither);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005501
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005502 /* Set up the display plane register */
5503 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5504 POSTING_READ(DSPCNTR(plane));
5505
5506 ret = intel_pipe_set_base(crtc, x, y, fb);
5507
5508 intel_update_watermarks(dev);
5509
5510 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5511
Jesse Barnes79e53942008-11-07 14:24:08 -08005512 return ret;
5513}
5514
Eric Anholtf564048e2011-03-30 13:01:02 -07005515static int intel_crtc_mode_set(struct drm_crtc *crtc,
5516 struct drm_display_mode *mode,
5517 struct drm_display_mode *adjusted_mode,
5518 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005519 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07005520{
5521 struct drm_device *dev = crtc->dev;
5522 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01005523 struct drm_encoder_helper_funcs *encoder_funcs;
5524 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07005525 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5526 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07005527 int ret;
5528
Eric Anholt0b701d22011-03-30 13:01:03 -07005529 drm_vblank_pre_modeset(dev, pipe);
5530
Eric Anholtf564048e2011-03-30 13:01:02 -07005531 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005532 x, y, fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08005533 drm_vblank_post_modeset(dev, pipe);
5534
Daniel Vetter9256aa12012-10-31 19:26:13 +01005535 if (ret != 0)
5536 return ret;
5537
5538 for_each_encoder_on_crtc(dev, crtc, encoder) {
5539 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
5540 encoder->base.base.id,
5541 drm_get_encoder_name(&encoder->base),
5542 mode->base.id, mode->name);
5543 encoder_funcs = encoder->base.helper_private;
5544 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
5545 }
5546
5547 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005548}
5549
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005550static bool intel_eld_uptodate(struct drm_connector *connector,
5551 int reg_eldv, uint32_t bits_eldv,
5552 int reg_elda, uint32_t bits_elda,
5553 int reg_edid)
5554{
5555 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5556 uint8_t *eld = connector->eld;
5557 uint32_t i;
5558
5559 i = I915_READ(reg_eldv);
5560 i &= bits_eldv;
5561
5562 if (!eld[0])
5563 return !i;
5564
5565 if (!i)
5566 return false;
5567
5568 i = I915_READ(reg_elda);
5569 i &= ~bits_elda;
5570 I915_WRITE(reg_elda, i);
5571
5572 for (i = 0; i < eld[2]; i++)
5573 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
5574 return false;
5575
5576 return true;
5577}
5578
Wu Fengguange0dac652011-09-05 14:25:34 +08005579static void g4x_write_eld(struct drm_connector *connector,
5580 struct drm_crtc *crtc)
5581{
5582 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5583 uint8_t *eld = connector->eld;
5584 uint32_t eldv;
5585 uint32_t len;
5586 uint32_t i;
5587
5588 i = I915_READ(G4X_AUD_VID_DID);
5589
5590 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
5591 eldv = G4X_ELDV_DEVCL_DEVBLC;
5592 else
5593 eldv = G4X_ELDV_DEVCTG;
5594
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005595 if (intel_eld_uptodate(connector,
5596 G4X_AUD_CNTL_ST, eldv,
5597 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
5598 G4X_HDMIW_HDMIEDID))
5599 return;
5600
Wu Fengguange0dac652011-09-05 14:25:34 +08005601 i = I915_READ(G4X_AUD_CNTL_ST);
5602 i &= ~(eldv | G4X_ELD_ADDR);
5603 len = (i >> 9) & 0x1f; /* ELD buffer size */
5604 I915_WRITE(G4X_AUD_CNTL_ST, i);
5605
5606 if (!eld[0])
5607 return;
5608
5609 len = min_t(uint8_t, eld[2], len);
5610 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5611 for (i = 0; i < len; i++)
5612 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
5613
5614 i = I915_READ(G4X_AUD_CNTL_ST);
5615 i |= eldv;
5616 I915_WRITE(G4X_AUD_CNTL_ST, i);
5617}
5618
Wang Xingchao83358c852012-08-16 22:43:37 +08005619static void haswell_write_eld(struct drm_connector *connector,
5620 struct drm_crtc *crtc)
5621{
5622 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5623 uint8_t *eld = connector->eld;
5624 struct drm_device *dev = crtc->dev;
5625 uint32_t eldv;
5626 uint32_t i;
5627 int len;
5628 int pipe = to_intel_crtc(crtc)->pipe;
5629 int tmp;
5630
5631 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
5632 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
5633 int aud_config = HSW_AUD_CFG(pipe);
5634 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
5635
5636
5637 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
5638
5639 /* Audio output enable */
5640 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
5641 tmp = I915_READ(aud_cntrl_st2);
5642 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
5643 I915_WRITE(aud_cntrl_st2, tmp);
5644
5645 /* Wait for 1 vertical blank */
5646 intel_wait_for_vblank(dev, pipe);
5647
5648 /* Set ELD valid state */
5649 tmp = I915_READ(aud_cntrl_st2);
5650 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
5651 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
5652 I915_WRITE(aud_cntrl_st2, tmp);
5653 tmp = I915_READ(aud_cntrl_st2);
5654 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
5655
5656 /* Enable HDMI mode */
5657 tmp = I915_READ(aud_config);
5658 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
5659 /* clear N_programing_enable and N_value_index */
5660 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
5661 I915_WRITE(aud_config, tmp);
5662
5663 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5664
5665 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
5666
5667 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5668 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5669 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
5670 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5671 } else
5672 I915_WRITE(aud_config, 0);
5673
5674 if (intel_eld_uptodate(connector,
5675 aud_cntrl_st2, eldv,
5676 aud_cntl_st, IBX_ELD_ADDRESS,
5677 hdmiw_hdmiedid))
5678 return;
5679
5680 i = I915_READ(aud_cntrl_st2);
5681 i &= ~eldv;
5682 I915_WRITE(aud_cntrl_st2, i);
5683
5684 if (!eld[0])
5685 return;
5686
5687 i = I915_READ(aud_cntl_st);
5688 i &= ~IBX_ELD_ADDRESS;
5689 I915_WRITE(aud_cntl_st, i);
5690 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
5691 DRM_DEBUG_DRIVER("port num:%d\n", i);
5692
5693 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5694 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5695 for (i = 0; i < len; i++)
5696 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5697
5698 i = I915_READ(aud_cntrl_st2);
5699 i |= eldv;
5700 I915_WRITE(aud_cntrl_st2, i);
5701
5702}
5703
Wu Fengguange0dac652011-09-05 14:25:34 +08005704static void ironlake_write_eld(struct drm_connector *connector,
5705 struct drm_crtc *crtc)
5706{
5707 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5708 uint8_t *eld = connector->eld;
5709 uint32_t eldv;
5710 uint32_t i;
5711 int len;
5712 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06005713 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08005714 int aud_cntl_st;
5715 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08005716 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08005717
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08005718 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08005719 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
5720 aud_config = IBX_AUD_CFG(pipe);
5721 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005722 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08005723 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08005724 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
5725 aud_config = CPT_AUD_CFG(pipe);
5726 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005727 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08005728 }
5729
Wang Xingchao9b138a82012-08-09 16:52:18 +08005730 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08005731
5732 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08005733 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08005734 if (!i) {
5735 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
5736 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005737 eldv = IBX_ELD_VALIDB;
5738 eldv |= IBX_ELD_VALIDB << 4;
5739 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08005740 } else {
5741 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005742 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08005743 }
5744
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005745 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5746 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5747 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06005748 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5749 } else
5750 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005751
5752 if (intel_eld_uptodate(connector,
5753 aud_cntrl_st2, eldv,
5754 aud_cntl_st, IBX_ELD_ADDRESS,
5755 hdmiw_hdmiedid))
5756 return;
5757
Wu Fengguange0dac652011-09-05 14:25:34 +08005758 i = I915_READ(aud_cntrl_st2);
5759 i &= ~eldv;
5760 I915_WRITE(aud_cntrl_st2, i);
5761
5762 if (!eld[0])
5763 return;
5764
Wu Fengguange0dac652011-09-05 14:25:34 +08005765 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005766 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08005767 I915_WRITE(aud_cntl_st, i);
5768
5769 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5770 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5771 for (i = 0; i < len; i++)
5772 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5773
5774 i = I915_READ(aud_cntrl_st2);
5775 i |= eldv;
5776 I915_WRITE(aud_cntrl_st2, i);
5777}
5778
5779void intel_write_eld(struct drm_encoder *encoder,
5780 struct drm_display_mode *mode)
5781{
5782 struct drm_crtc *crtc = encoder->crtc;
5783 struct drm_connector *connector;
5784 struct drm_device *dev = encoder->dev;
5785 struct drm_i915_private *dev_priv = dev->dev_private;
5786
5787 connector = drm_select_eld(encoder, mode);
5788 if (!connector)
5789 return;
5790
5791 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5792 connector->base.id,
5793 drm_get_connector_name(connector),
5794 connector->encoder->base.id,
5795 drm_get_encoder_name(connector->encoder));
5796
5797 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
5798
5799 if (dev_priv->display.write_eld)
5800 dev_priv->display.write_eld(connector, crtc);
5801}
5802
Jesse Barnes79e53942008-11-07 14:24:08 -08005803/** Loads the palette/gamma unit for the CRTC with the prepared values */
5804void intel_crtc_load_lut(struct drm_crtc *crtc)
5805{
5806 struct drm_device *dev = crtc->dev;
5807 struct drm_i915_private *dev_priv = dev->dev_private;
5808 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005809 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005810 int i;
5811
5812 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00005813 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08005814 return;
5815
Adam Jacksonf2b115e2009-12-03 17:14:42 -05005816 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07005817 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005818 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005819
Jesse Barnes79e53942008-11-07 14:24:08 -08005820 for (i = 0; i < 256; i++) {
5821 I915_WRITE(palreg + 4 * i,
5822 (intel_crtc->lut_r[i] << 16) |
5823 (intel_crtc->lut_g[i] << 8) |
5824 intel_crtc->lut_b[i]);
5825 }
5826}
5827
Chris Wilson560b85b2010-08-07 11:01:38 +01005828static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
5829{
5830 struct drm_device *dev = crtc->dev;
5831 struct drm_i915_private *dev_priv = dev->dev_private;
5832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5833 bool visible = base != 0;
5834 u32 cntl;
5835
5836 if (intel_crtc->cursor_visible == visible)
5837 return;
5838
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005839 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01005840 if (visible) {
5841 /* On these chipsets we can only modify the base whilst
5842 * the cursor is disabled.
5843 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005844 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01005845
5846 cntl &= ~(CURSOR_FORMAT_MASK);
5847 /* XXX width must be 64, stride 256 => 0x00 << 28 */
5848 cntl |= CURSOR_ENABLE |
5849 CURSOR_GAMMA_ENABLE |
5850 CURSOR_FORMAT_ARGB;
5851 } else
5852 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005853 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01005854
5855 intel_crtc->cursor_visible = visible;
5856}
5857
5858static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
5859{
5860 struct drm_device *dev = crtc->dev;
5861 struct drm_i915_private *dev_priv = dev->dev_private;
5862 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5863 int pipe = intel_crtc->pipe;
5864 bool visible = base != 0;
5865
5866 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08005867 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01005868 if (base) {
5869 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
5870 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5871 cntl |= pipe << 28; /* Connect to correct pipe */
5872 } else {
5873 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5874 cntl |= CURSOR_MODE_DISABLE;
5875 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005876 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01005877
5878 intel_crtc->cursor_visible = visible;
5879 }
5880 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005881 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01005882}
5883
Jesse Barnes65a21cd2011-10-12 11:10:21 -07005884static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
5885{
5886 struct drm_device *dev = crtc->dev;
5887 struct drm_i915_private *dev_priv = dev->dev_private;
5888 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5889 int pipe = intel_crtc->pipe;
5890 bool visible = base != 0;
5891
5892 if (intel_crtc->cursor_visible != visible) {
5893 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
5894 if (base) {
5895 cntl &= ~CURSOR_MODE;
5896 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
5897 } else {
5898 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
5899 cntl |= CURSOR_MODE_DISABLE;
5900 }
5901 I915_WRITE(CURCNTR_IVB(pipe), cntl);
5902
5903 intel_crtc->cursor_visible = visible;
5904 }
5905 /* and commit changes on next vblank */
5906 I915_WRITE(CURBASE_IVB(pipe), base);
5907}
5908
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005909/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01005910static void intel_crtc_update_cursor(struct drm_crtc *crtc,
5911 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005912{
5913 struct drm_device *dev = crtc->dev;
5914 struct drm_i915_private *dev_priv = dev->dev_private;
5915 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5916 int pipe = intel_crtc->pipe;
5917 int x = intel_crtc->cursor_x;
5918 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01005919 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005920 bool visible;
5921
5922 pos = 0;
5923
Chris Wilson6b383a72010-09-13 13:54:26 +01005924 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005925 base = intel_crtc->cursor_addr;
5926 if (x > (int) crtc->fb->width)
5927 base = 0;
5928
5929 if (y > (int) crtc->fb->height)
5930 base = 0;
5931 } else
5932 base = 0;
5933
5934 if (x < 0) {
5935 if (x + intel_crtc->cursor_width < 0)
5936 base = 0;
5937
5938 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
5939 x = -x;
5940 }
5941 pos |= x << CURSOR_X_SHIFT;
5942
5943 if (y < 0) {
5944 if (y + intel_crtc->cursor_height < 0)
5945 base = 0;
5946
5947 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
5948 y = -y;
5949 }
5950 pos |= y << CURSOR_Y_SHIFT;
5951
5952 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01005953 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005954 return;
5955
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03005956 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07005957 I915_WRITE(CURPOS_IVB(pipe), pos);
5958 ivb_update_cursor(crtc, base);
5959 } else {
5960 I915_WRITE(CURPOS(pipe), pos);
5961 if (IS_845G(dev) || IS_I865G(dev))
5962 i845_update_cursor(crtc, base);
5963 else
5964 i9xx_update_cursor(crtc, base);
5965 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005966}
5967
Jesse Barnes79e53942008-11-07 14:24:08 -08005968static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00005969 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08005970 uint32_t handle,
5971 uint32_t width, uint32_t height)
5972{
5973 struct drm_device *dev = crtc->dev;
5974 struct drm_i915_private *dev_priv = dev->dev_private;
5975 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00005976 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005977 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005978 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005979
Jesse Barnes79e53942008-11-07 14:24:08 -08005980 /* if we want to turn off the cursor ignore width and height */
5981 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08005982 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005983 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00005984 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10005985 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005986 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08005987 }
5988
5989 /* Currently we only support 64x64 cursors */
5990 if (width != 64 || height != 64) {
5991 DRM_ERROR("we currently only support 64x64 cursors\n");
5992 return -EINVAL;
5993 }
5994
Chris Wilson05394f32010-11-08 19:18:58 +00005995 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00005996 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08005997 return -ENOENT;
5998
Chris Wilson05394f32010-11-08 19:18:58 +00005999 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006000 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006001 ret = -ENOMEM;
6002 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006003 }
6004
Dave Airlie71acb5e2008-12-30 20:31:46 +10006005 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006006 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006007 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00006008 if (obj->tiling_mode) {
6009 DRM_ERROR("cursor cannot be tiled\n");
6010 ret = -EINVAL;
6011 goto fail_locked;
6012 }
6013
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006014 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006015 if (ret) {
6016 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006017 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006018 }
6019
Chris Wilsond9e86c02010-11-10 16:40:20 +00006020 ret = i915_gem_object_put_fence(obj);
6021 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006022 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006023 goto fail_unpin;
6024 }
6025
Chris Wilson05394f32010-11-08 19:18:58 +00006026 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006027 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006028 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006029 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006030 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6031 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006032 if (ret) {
6033 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006034 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006035 }
Chris Wilson05394f32010-11-08 19:18:58 +00006036 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006037 }
6038
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006039 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006040 I915_WRITE(CURSIZE, (height << 12) | width);
6041
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006042 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006043 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006044 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006045 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006046 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6047 } else
6048 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006049 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006050 }
Jesse Barnes80824002009-09-10 15:28:06 -07006051
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006052 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006053
6054 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006055 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006056 intel_crtc->cursor_width = width;
6057 intel_crtc->cursor_height = height;
6058
Chris Wilson6b383a72010-09-13 13:54:26 +01006059 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006060
Jesse Barnes79e53942008-11-07 14:24:08 -08006061 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006062fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006063 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006064fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006065 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006066fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006067 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006068 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006069}
6070
6071static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6072{
Jesse Barnes79e53942008-11-07 14:24:08 -08006073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006074
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006075 intel_crtc->cursor_x = x;
6076 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006077
Chris Wilson6b383a72010-09-13 13:54:26 +01006078 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006079
6080 return 0;
6081}
6082
6083/** Sets the color ramps on behalf of RandR */
6084void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6085 u16 blue, int regno)
6086{
6087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6088
6089 intel_crtc->lut_r[regno] = red >> 8;
6090 intel_crtc->lut_g[regno] = green >> 8;
6091 intel_crtc->lut_b[regno] = blue >> 8;
6092}
6093
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006094void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6095 u16 *blue, int regno)
6096{
6097 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6098
6099 *red = intel_crtc->lut_r[regno] << 8;
6100 *green = intel_crtc->lut_g[regno] << 8;
6101 *blue = intel_crtc->lut_b[regno] << 8;
6102}
6103
Jesse Barnes79e53942008-11-07 14:24:08 -08006104static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006105 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006106{
James Simmons72034252010-08-03 01:33:19 +01006107 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006109
James Simmons72034252010-08-03 01:33:19 +01006110 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006111 intel_crtc->lut_r[i] = red[i] >> 8;
6112 intel_crtc->lut_g[i] = green[i] >> 8;
6113 intel_crtc->lut_b[i] = blue[i] >> 8;
6114 }
6115
6116 intel_crtc_load_lut(crtc);
6117}
6118
6119/**
6120 * Get a pipe with a simple mode set on it for doing load-based monitor
6121 * detection.
6122 *
6123 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07006124 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08006125 *
Eric Anholtc751ce42010-03-25 11:48:48 -07006126 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08006127 * configured for it. In the future, it could choose to temporarily disable
6128 * some outputs to free up a pipe for its use.
6129 *
6130 * \return crtc, or NULL if no pipes are available.
6131 */
6132
6133/* VESA 640x480x72Hz mode to set on the pipe */
6134static struct drm_display_mode load_detect_mode = {
6135 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6136 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6137};
6138
Chris Wilsond2dff872011-04-19 08:36:26 +01006139static struct drm_framebuffer *
6140intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006141 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006142 struct drm_i915_gem_object *obj)
6143{
6144 struct intel_framebuffer *intel_fb;
6145 int ret;
6146
6147 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6148 if (!intel_fb) {
6149 drm_gem_object_unreference_unlocked(&obj->base);
6150 return ERR_PTR(-ENOMEM);
6151 }
6152
6153 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6154 if (ret) {
6155 drm_gem_object_unreference_unlocked(&obj->base);
6156 kfree(intel_fb);
6157 return ERR_PTR(ret);
6158 }
6159
6160 return &intel_fb->base;
6161}
6162
6163static u32
6164intel_framebuffer_pitch_for_width(int width, int bpp)
6165{
6166 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6167 return ALIGN(pitch, 64);
6168}
6169
6170static u32
6171intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6172{
6173 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6174 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6175}
6176
6177static struct drm_framebuffer *
6178intel_framebuffer_create_for_mode(struct drm_device *dev,
6179 struct drm_display_mode *mode,
6180 int depth, int bpp)
6181{
6182 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006183 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006184
6185 obj = i915_gem_alloc_object(dev,
6186 intel_framebuffer_size_for_mode(mode, bpp));
6187 if (obj == NULL)
6188 return ERR_PTR(-ENOMEM);
6189
6190 mode_cmd.width = mode->hdisplay;
6191 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006192 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6193 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006194 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006195
6196 return intel_framebuffer_create(dev, &mode_cmd, obj);
6197}
6198
6199static struct drm_framebuffer *
6200mode_fits_in_fbdev(struct drm_device *dev,
6201 struct drm_display_mode *mode)
6202{
6203 struct drm_i915_private *dev_priv = dev->dev_private;
6204 struct drm_i915_gem_object *obj;
6205 struct drm_framebuffer *fb;
6206
6207 if (dev_priv->fbdev == NULL)
6208 return NULL;
6209
6210 obj = dev_priv->fbdev->ifb.obj;
6211 if (obj == NULL)
6212 return NULL;
6213
6214 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006215 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6216 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006217 return NULL;
6218
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006219 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006220 return NULL;
6221
6222 return fb;
6223}
6224
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006225bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006226 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006227 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006228{
6229 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006230 struct intel_encoder *intel_encoder =
6231 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006232 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006233 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006234 struct drm_crtc *crtc = NULL;
6235 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006236 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006237 int i = -1;
6238
Chris Wilsond2dff872011-04-19 08:36:26 +01006239 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6240 connector->base.id, drm_get_connector_name(connector),
6241 encoder->base.id, drm_get_encoder_name(encoder));
6242
Jesse Barnes79e53942008-11-07 14:24:08 -08006243 /*
6244 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006245 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006246 * - if the connector already has an assigned crtc, use it (but make
6247 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006248 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006249 * - try to find the first unused crtc that can drive this connector,
6250 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006251 */
6252
6253 /* See if we already have a CRTC for this connector */
6254 if (encoder->crtc) {
6255 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006256
Daniel Vetter24218aa2012-08-12 19:27:11 +02006257 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006258 old->load_detect_temp = false;
6259
6260 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006261 if (connector->dpms != DRM_MODE_DPMS_ON)
6262 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006263
Chris Wilson71731882011-04-19 23:10:58 +01006264 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006265 }
6266
6267 /* Find an unused one (if possible) */
6268 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6269 i++;
6270 if (!(encoder->possible_crtcs & (1 << i)))
6271 continue;
6272 if (!possible_crtc->enabled) {
6273 crtc = possible_crtc;
6274 break;
6275 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006276 }
6277
6278 /*
6279 * If we didn't find an unused CRTC, don't use any.
6280 */
6281 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006282 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6283 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006284 }
6285
Daniel Vetterfc303102012-07-09 10:40:58 +02006286 intel_encoder->new_crtc = to_intel_crtc(crtc);
6287 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006288
6289 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006290 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006291 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006292 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006293
Chris Wilson64927112011-04-20 07:25:26 +01006294 if (!mode)
6295 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006296
Chris Wilsond2dff872011-04-19 08:36:26 +01006297 /* We need a framebuffer large enough to accommodate all accesses
6298 * that the plane may generate whilst we perform load detection.
6299 * We can not rely on the fbcon either being present (we get called
6300 * during its initialisation to detect all boot displays, or it may
6301 * not even exist) or that it is large enough to satisfy the
6302 * requested mode.
6303 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006304 fb = mode_fits_in_fbdev(dev, mode);
6305 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006306 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006307 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6308 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006309 } else
6310 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006311 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006312 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006313 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006314 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006315
Daniel Vetter94352cf2012-07-05 22:51:56 +02006316 if (!intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006317 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006318 if (old->release_fb)
6319 old->release_fb->funcs->destroy(old->release_fb);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006320 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006321 }
Chris Wilson71731882011-04-19 23:10:58 +01006322
Jesse Barnes79e53942008-11-07 14:24:08 -08006323 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006324 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006325 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006326}
6327
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006328void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006329 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006330{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006331 struct intel_encoder *intel_encoder =
6332 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006333 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006334
Chris Wilsond2dff872011-04-19 08:36:26 +01006335 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6336 connector->base.id, drm_get_connector_name(connector),
6337 encoder->base.id, drm_get_encoder_name(encoder));
6338
Chris Wilson8261b192011-04-19 23:18:09 +01006339 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006340 struct drm_crtc *crtc = encoder->crtc;
6341
6342 to_intel_connector(connector)->new_encoder = NULL;
6343 intel_encoder->new_crtc = NULL;
6344 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006345
6346 if (old->release_fb)
6347 old->release_fb->funcs->destroy(old->release_fb);
6348
Chris Wilson0622a532011-04-21 09:32:11 +01006349 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006350 }
6351
Eric Anholtc751ce42010-03-25 11:48:48 -07006352 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006353 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6354 connector->funcs->dpms(connector, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006355}
6356
6357/* Returns the clock of the currently programmed mode of the given pipe. */
6358static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6359{
6360 struct drm_i915_private *dev_priv = dev->dev_private;
6361 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6362 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006363 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006364 u32 fp;
6365 intel_clock_t clock;
6366
6367 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006368 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006369 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006370 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006371
6372 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006373 if (IS_PINEVIEW(dev)) {
6374 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6375 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006376 } else {
6377 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6378 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6379 }
6380
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006381 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006382 if (IS_PINEVIEW(dev))
6383 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6384 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006385 else
6386 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006387 DPLL_FPA01_P1_POST_DIV_SHIFT);
6388
6389 switch (dpll & DPLL_MODE_MASK) {
6390 case DPLLB_MODE_DAC_SERIAL:
6391 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6392 5 : 10;
6393 break;
6394 case DPLLB_MODE_LVDS:
6395 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6396 7 : 14;
6397 break;
6398 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006399 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006400 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6401 return 0;
6402 }
6403
6404 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08006405 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006406 } else {
6407 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6408
6409 if (is_lvds) {
6410 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6411 DPLL_FPA01_P1_POST_DIV_SHIFT);
6412 clock.p2 = 14;
6413
6414 if ((dpll & PLL_REF_INPUT_MASK) ==
6415 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6416 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08006417 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006418 } else
Shaohua Li21778322009-02-23 15:19:16 +08006419 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006420 } else {
6421 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6422 clock.p1 = 2;
6423 else {
6424 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6425 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6426 }
6427 if (dpll & PLL_P2_DIVIDE_BY_4)
6428 clock.p2 = 4;
6429 else
6430 clock.p2 = 2;
6431
Shaohua Li21778322009-02-23 15:19:16 +08006432 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006433 }
6434 }
6435
6436 /* XXX: It would be nice to validate the clocks, but we can't reuse
6437 * i830PllIsValid() because it relies on the xf86_config connector
6438 * configuration being accurate, which it isn't necessarily.
6439 */
6440
6441 return clock.dot;
6442}
6443
6444/** Returns the currently programmed mode of the given pipe. */
6445struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6446 struct drm_crtc *crtc)
6447{
Jesse Barnes548f2452011-02-17 10:40:53 -08006448 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006450 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006451 struct drm_display_mode *mode;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006452 int htot = I915_READ(HTOTAL(cpu_transcoder));
6453 int hsync = I915_READ(HSYNC(cpu_transcoder));
6454 int vtot = I915_READ(VTOTAL(cpu_transcoder));
6455 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08006456
6457 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6458 if (!mode)
6459 return NULL;
6460
6461 mode->clock = intel_crtc_clock_get(dev, crtc);
6462 mode->hdisplay = (htot & 0xffff) + 1;
6463 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6464 mode->hsync_start = (hsync & 0xffff) + 1;
6465 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6466 mode->vdisplay = (vtot & 0xffff) + 1;
6467 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6468 mode->vsync_start = (vsync & 0xffff) + 1;
6469 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6470
6471 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006472
6473 return mode;
6474}
6475
Daniel Vetter3dec0092010-08-20 21:40:52 +02006476static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07006477{
6478 struct drm_device *dev = crtc->dev;
6479 drm_i915_private_t *dev_priv = dev->dev_private;
6480 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6481 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006482 int dpll_reg = DPLL(pipe);
6483 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07006484
Eric Anholtbad720f2009-10-22 16:11:14 -07006485 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006486 return;
6487
6488 if (!dev_priv->lvds_downclock_avail)
6489 return;
6490
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006491 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006492 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08006493 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006494
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006495 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006496
6497 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6498 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006499 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006500
Jesse Barnes652c3932009-08-17 13:31:43 -07006501 dpll = I915_READ(dpll_reg);
6502 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08006503 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006504 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006505}
6506
6507static void intel_decrease_pllclock(struct drm_crtc *crtc)
6508{
6509 struct drm_device *dev = crtc->dev;
6510 drm_i915_private_t *dev_priv = dev->dev_private;
6511 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006512
Eric Anholtbad720f2009-10-22 16:11:14 -07006513 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006514 return;
6515
6516 if (!dev_priv->lvds_downclock_avail)
6517 return;
6518
6519 /*
6520 * Since this is called by a timer, we should never get here in
6521 * the manual case.
6522 */
6523 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01006524 int pipe = intel_crtc->pipe;
6525 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02006526 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01006527
Zhao Yakui44d98a62009-10-09 11:39:40 +08006528 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006529
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006530 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006531
Chris Wilson074b5e12012-05-02 12:07:06 +01006532 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006533 dpll |= DISPLAY_RATE_SELECT_FPA1;
6534 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006535 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006536 dpll = I915_READ(dpll_reg);
6537 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08006538 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006539 }
6540
6541}
6542
Chris Wilsonf047e392012-07-21 12:31:41 +01006543void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006544{
Chris Wilsonf047e392012-07-21 12:31:41 +01006545 i915_update_gfx_val(dev->dev_private);
6546}
6547
6548void intel_mark_idle(struct drm_device *dev)
6549{
Chris Wilsonf047e392012-07-21 12:31:41 +01006550}
6551
6552void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
6553{
6554 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07006555 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006556
6557 if (!i915_powersave)
6558 return;
6559
Jesse Barnes652c3932009-08-17 13:31:43 -07006560 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006561 if (!crtc->fb)
6562 continue;
6563
Chris Wilsonf047e392012-07-21 12:31:41 +01006564 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6565 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006566 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006567}
6568
Chris Wilsonf047e392012-07-21 12:31:41 +01006569void intel_mark_fb_idle(struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07006570{
Chris Wilsonf047e392012-07-21 12:31:41 +01006571 struct drm_device *dev = obj->base.dev;
6572 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006573
Chris Wilsonf047e392012-07-21 12:31:41 +01006574 if (!i915_powersave)
Chris Wilsonacb87df2012-05-03 15:47:57 +01006575 return;
6576
Jesse Barnes652c3932009-08-17 13:31:43 -07006577 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6578 if (!crtc->fb)
6579 continue;
6580
Chris Wilsonf047e392012-07-21 12:31:41 +01006581 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6582 intel_decrease_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006583 }
6584}
6585
Jesse Barnes79e53942008-11-07 14:24:08 -08006586static void intel_crtc_destroy(struct drm_crtc *crtc)
6587{
6588 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006589 struct drm_device *dev = crtc->dev;
6590 struct intel_unpin_work *work;
6591 unsigned long flags;
6592
6593 spin_lock_irqsave(&dev->event_lock, flags);
6594 work = intel_crtc->unpin_work;
6595 intel_crtc->unpin_work = NULL;
6596 spin_unlock_irqrestore(&dev->event_lock, flags);
6597
6598 if (work) {
6599 cancel_work_sync(&work->work);
6600 kfree(work);
6601 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006602
6603 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006604
Jesse Barnes79e53942008-11-07 14:24:08 -08006605 kfree(intel_crtc);
6606}
6607
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006608static void intel_unpin_work_fn(struct work_struct *__work)
6609{
6610 struct intel_unpin_work *work =
6611 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006612 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006613
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006614 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01006615 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00006616 drm_gem_object_unreference(&work->pending_flip_obj->base);
6617 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00006618
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006619 intel_update_fbc(dev);
6620 mutex_unlock(&dev->struct_mutex);
6621
6622 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
6623 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
6624
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006625 kfree(work);
6626}
6627
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006628static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01006629 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006630{
6631 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006632 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6633 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00006634 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006635 unsigned long flags;
6636
6637 /* Ignore early vblank irqs */
6638 if (intel_crtc == NULL)
6639 return;
6640
6641 spin_lock_irqsave(&dev->event_lock, flags);
6642 work = intel_crtc->unpin_work;
6643 if (work == NULL || !work->pending) {
6644 spin_unlock_irqrestore(&dev->event_lock, flags);
6645 return;
6646 }
6647
6648 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006649
Rob Clark45a066e2012-10-08 14:50:40 -05006650 if (work->event)
6651 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006652
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01006653 drm_vblank_put(dev, intel_crtc->pipe);
6654
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006655 spin_unlock_irqrestore(&dev->event_lock, flags);
6656
Chris Wilson05394f32010-11-08 19:18:58 +00006657 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00006658
Chris Wilson5bb61642012-09-27 21:25:58 +01006659 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006660
6661 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07006662
6663 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006664}
6665
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006666void intel_finish_page_flip(struct drm_device *dev, int pipe)
6667{
6668 drm_i915_private_t *dev_priv = dev->dev_private;
6669 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
6670
Mario Kleiner49b14a52010-12-09 07:00:07 +01006671 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006672}
6673
6674void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
6675{
6676 drm_i915_private_t *dev_priv = dev->dev_private;
6677 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
6678
Mario Kleiner49b14a52010-12-09 07:00:07 +01006679 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006680}
6681
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006682void intel_prepare_page_flip(struct drm_device *dev, int plane)
6683{
6684 drm_i915_private_t *dev_priv = dev->dev_private;
6685 struct intel_crtc *intel_crtc =
6686 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
6687 unsigned long flags;
6688
6689 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08006690 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01006691 if ((++intel_crtc->unpin_work->pending) > 1)
6692 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08006693 } else {
6694 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
6695 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006696 spin_unlock_irqrestore(&dev->event_lock, flags);
6697}
6698
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006699static int intel_gen2_queue_flip(struct drm_device *dev,
6700 struct drm_crtc *crtc,
6701 struct drm_framebuffer *fb,
6702 struct drm_i915_gem_object *obj)
6703{
6704 struct drm_i915_private *dev_priv = dev->dev_private;
6705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006706 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006707 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006708 int ret;
6709
Daniel Vetter6d90c952012-04-26 23:28:05 +02006710 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006711 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006712 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006713
Daniel Vetter6d90c952012-04-26 23:28:05 +02006714 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006715 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006716 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006717
6718 /* Can't queue multiple flips, so wait for the previous
6719 * one to finish before executing the next.
6720 */
6721 if (intel_crtc->plane)
6722 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6723 else
6724 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006725 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6726 intel_ring_emit(ring, MI_NOOP);
6727 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6728 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6729 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02006730 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006731 intel_ring_emit(ring, 0); /* aux display base address, unused */
6732 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006733 return 0;
6734
6735err_unpin:
6736 intel_unpin_fb_obj(obj);
6737err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006738 return ret;
6739}
6740
6741static int intel_gen3_queue_flip(struct drm_device *dev,
6742 struct drm_crtc *crtc,
6743 struct drm_framebuffer *fb,
6744 struct drm_i915_gem_object *obj)
6745{
6746 struct drm_i915_private *dev_priv = dev->dev_private;
6747 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006748 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006749 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006750 int ret;
6751
Daniel Vetter6d90c952012-04-26 23:28:05 +02006752 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006753 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006754 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006755
Daniel Vetter6d90c952012-04-26 23:28:05 +02006756 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006757 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006758 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006759
6760 if (intel_crtc->plane)
6761 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6762 else
6763 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006764 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6765 intel_ring_emit(ring, MI_NOOP);
6766 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
6767 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6768 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02006769 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006770 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006771
Daniel Vetter6d90c952012-04-26 23:28:05 +02006772 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006773 return 0;
6774
6775err_unpin:
6776 intel_unpin_fb_obj(obj);
6777err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006778 return ret;
6779}
6780
6781static int intel_gen4_queue_flip(struct drm_device *dev,
6782 struct drm_crtc *crtc,
6783 struct drm_framebuffer *fb,
6784 struct drm_i915_gem_object *obj)
6785{
6786 struct drm_i915_private *dev_priv = dev->dev_private;
6787 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6788 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006789 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006790 int ret;
6791
Daniel Vetter6d90c952012-04-26 23:28:05 +02006792 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006793 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006794 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006795
Daniel Vetter6d90c952012-04-26 23:28:05 +02006796 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006797 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006798 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006799
6800 /* i965+ uses the linear or tiled offsets from the
6801 * Display Registers (which do not change across a page-flip)
6802 * so we need only reprogram the base address.
6803 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02006804 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6805 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6806 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02006807 intel_ring_emit(ring,
6808 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
6809 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006810
6811 /* XXX Enabling the panel-fitter across page-flip is so far
6812 * untested on non-native modes, so ignore it for now.
6813 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
6814 */
6815 pf = 0;
6816 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006817 intel_ring_emit(ring, pf | pipesrc);
6818 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006819 return 0;
6820
6821err_unpin:
6822 intel_unpin_fb_obj(obj);
6823err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006824 return ret;
6825}
6826
6827static int intel_gen6_queue_flip(struct drm_device *dev,
6828 struct drm_crtc *crtc,
6829 struct drm_framebuffer *fb,
6830 struct drm_i915_gem_object *obj)
6831{
6832 struct drm_i915_private *dev_priv = dev->dev_private;
6833 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006834 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006835 uint32_t pf, pipesrc;
6836 int ret;
6837
Daniel Vetter6d90c952012-04-26 23:28:05 +02006838 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006839 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006840 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006841
Daniel Vetter6d90c952012-04-26 23:28:05 +02006842 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006843 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006844 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006845
Daniel Vetter6d90c952012-04-26 23:28:05 +02006846 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6847 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6848 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Daniel Vetterc2c75132012-07-05 12:17:30 +02006849 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006850
Chris Wilson99d9acd2012-04-17 20:37:00 +01006851 /* Contrary to the suggestions in the documentation,
6852 * "Enable Panel Fitter" does not seem to be required when page
6853 * flipping with a non-native mode, and worse causes a normal
6854 * modeset to fail.
6855 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
6856 */
6857 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006858 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006859 intel_ring_emit(ring, pf | pipesrc);
6860 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006861 return 0;
6862
6863err_unpin:
6864 intel_unpin_fb_obj(obj);
6865err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006866 return ret;
6867}
6868
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006869/*
6870 * On gen7 we currently use the blit ring because (in early silicon at least)
6871 * the render ring doesn't give us interrpts for page flip completion, which
6872 * means clients will hang after the first flip is queued. Fortunately the
6873 * blit ring generates interrupts properly, so use it instead.
6874 */
6875static int intel_gen7_queue_flip(struct drm_device *dev,
6876 struct drm_crtc *crtc,
6877 struct drm_framebuffer *fb,
6878 struct drm_i915_gem_object *obj)
6879{
6880 struct drm_i915_private *dev_priv = dev->dev_private;
6881 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6882 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006883 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006884 int ret;
6885
6886 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6887 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006888 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006889
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006890 switch(intel_crtc->plane) {
6891 case PLANE_A:
6892 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
6893 break;
6894 case PLANE_B:
6895 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
6896 break;
6897 case PLANE_C:
6898 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
6899 break;
6900 default:
6901 WARN_ONCE(1, "unknown plane in flip command\n");
6902 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03006903 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006904 }
6905
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006906 ret = intel_ring_begin(ring, 4);
6907 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006908 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006909
Daniel Vettercb05d8d2012-05-23 14:02:00 +02006910 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006911 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Daniel Vetterc2c75132012-07-05 12:17:30 +02006912 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006913 intel_ring_emit(ring, (MI_NOOP));
6914 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006915 return 0;
6916
6917err_unpin:
6918 intel_unpin_fb_obj(obj);
6919err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006920 return ret;
6921}
6922
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006923static int intel_default_queue_flip(struct drm_device *dev,
6924 struct drm_crtc *crtc,
6925 struct drm_framebuffer *fb,
6926 struct drm_i915_gem_object *obj)
6927{
6928 return -ENODEV;
6929}
6930
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006931static int intel_crtc_page_flip(struct drm_crtc *crtc,
6932 struct drm_framebuffer *fb,
6933 struct drm_pending_vblank_event *event)
6934{
6935 struct drm_device *dev = crtc->dev;
6936 struct drm_i915_private *dev_priv = dev->dev_private;
6937 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00006938 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006939 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6940 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006941 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01006942 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006943
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03006944 /* Can't change pixel format via MI display flips. */
6945 if (fb->pixel_format != crtc->fb->pixel_format)
6946 return -EINVAL;
6947
6948 /*
6949 * TILEOFF/LINOFF registers can't be changed via MI display flips.
6950 * Note that pitch changes could also affect these register.
6951 */
6952 if (INTEL_INFO(dev)->gen > 3 &&
6953 (fb->offsets[0] != crtc->fb->offsets[0] ||
6954 fb->pitches[0] != crtc->fb->pitches[0]))
6955 return -EINVAL;
6956
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006957 work = kzalloc(sizeof *work, GFP_KERNEL);
6958 if (work == NULL)
6959 return -ENOMEM;
6960
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006961 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006962 work->crtc = crtc;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006963 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08006964 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006965 INIT_WORK(&work->work, intel_unpin_work_fn);
6966
Jesse Barnes7317c75e62011-08-29 09:45:28 -07006967 ret = drm_vblank_get(dev, intel_crtc->pipe);
6968 if (ret)
6969 goto free_work;
6970
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006971 /* We borrow the event spin lock for protecting unpin_work */
6972 spin_lock_irqsave(&dev->event_lock, flags);
6973 if (intel_crtc->unpin_work) {
6974 spin_unlock_irqrestore(&dev->event_lock, flags);
6975 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07006976 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01006977
6978 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006979 return -EBUSY;
6980 }
6981 intel_crtc->unpin_work = work;
6982 spin_unlock_irqrestore(&dev->event_lock, flags);
6983
6984 intel_fb = to_intel_framebuffer(fb);
6985 obj = intel_fb->obj;
6986
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006987 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
6988 flush_workqueue(dev_priv->wq);
6989
Chris Wilson79158102012-05-23 11:13:58 +01006990 ret = i915_mutex_lock_interruptible(dev);
6991 if (ret)
6992 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006993
Jesse Barnes75dfca82010-02-10 15:09:44 -08006994 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00006995 drm_gem_object_reference(&work->old_fb_obj->base);
6996 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006997
6998 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01006999
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007000 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007001
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007002 work->enable_stall_check = true;
7003
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007004 atomic_inc(&intel_crtc->unpin_work_count);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007005
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007006 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7007 if (ret)
7008 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007009
Chris Wilson7782de32011-07-08 12:22:41 +01007010 intel_disable_fbc(dev);
Chris Wilsonf047e392012-07-21 12:31:41 +01007011 intel_mark_fb_busy(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007012 mutex_unlock(&dev->struct_mutex);
7013
Jesse Barnese5510fa2010-07-01 16:48:37 -07007014 trace_i915_flip_request(intel_crtc->plane, obj);
7015
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007016 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007017
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007018cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007019 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson05394f32010-11-08 19:18:58 +00007020 drm_gem_object_unreference(&work->old_fb_obj->base);
7021 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007022 mutex_unlock(&dev->struct_mutex);
7023
Chris Wilson79158102012-05-23 11:13:58 +01007024cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007025 spin_lock_irqsave(&dev->event_lock, flags);
7026 intel_crtc->unpin_work = NULL;
7027 spin_unlock_irqrestore(&dev->event_lock, flags);
7028
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007029 drm_vblank_put(dev, intel_crtc->pipe);
7030free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007031 kfree(work);
7032
7033 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007034}
7035
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007036static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007037 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7038 .load_lut = intel_crtc_load_lut,
Daniel Vetter976f8a22012-07-08 22:34:21 +02007039 .disable = intel_crtc_noop,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007040};
7041
Daniel Vetter6ed0f792012-07-08 19:41:43 +02007042bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
7043{
7044 struct intel_encoder *other_encoder;
7045 struct drm_crtc *crtc = &encoder->new_crtc->base;
7046
7047 if (WARN_ON(!crtc))
7048 return false;
7049
7050 list_for_each_entry(other_encoder,
7051 &crtc->dev->mode_config.encoder_list,
7052 base.head) {
7053
7054 if (&other_encoder->new_crtc->base != crtc ||
7055 encoder == other_encoder)
7056 continue;
7057 else
7058 return true;
7059 }
7060
7061 return false;
7062}
7063
Daniel Vetter50f56112012-07-02 09:35:43 +02007064static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7065 struct drm_crtc *crtc)
7066{
7067 struct drm_device *dev;
7068 struct drm_crtc *tmp;
7069 int crtc_mask = 1;
7070
7071 WARN(!crtc, "checking null crtc?\n");
7072
7073 dev = crtc->dev;
7074
7075 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7076 if (tmp == crtc)
7077 break;
7078 crtc_mask <<= 1;
7079 }
7080
7081 if (encoder->possible_crtcs & crtc_mask)
7082 return true;
7083 return false;
7084}
7085
Daniel Vetter9a935852012-07-05 22:34:27 +02007086/**
7087 * intel_modeset_update_staged_output_state
7088 *
7089 * Updates the staged output configuration state, e.g. after we've read out the
7090 * current hw state.
7091 */
7092static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7093{
7094 struct intel_encoder *encoder;
7095 struct intel_connector *connector;
7096
7097 list_for_each_entry(connector, &dev->mode_config.connector_list,
7098 base.head) {
7099 connector->new_encoder =
7100 to_intel_encoder(connector->base.encoder);
7101 }
7102
7103 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7104 base.head) {
7105 encoder->new_crtc =
7106 to_intel_crtc(encoder->base.crtc);
7107 }
7108}
7109
7110/**
7111 * intel_modeset_commit_output_state
7112 *
7113 * This function copies the stage display pipe configuration to the real one.
7114 */
7115static void intel_modeset_commit_output_state(struct drm_device *dev)
7116{
7117 struct intel_encoder *encoder;
7118 struct intel_connector *connector;
7119
7120 list_for_each_entry(connector, &dev->mode_config.connector_list,
7121 base.head) {
7122 connector->base.encoder = &connector->new_encoder->base;
7123 }
7124
7125 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7126 base.head) {
7127 encoder->base.crtc = &encoder->new_crtc->base;
7128 }
7129}
7130
Daniel Vetter7758a112012-07-08 19:40:39 +02007131static struct drm_display_mode *
7132intel_modeset_adjusted_mode(struct drm_crtc *crtc,
7133 struct drm_display_mode *mode)
7134{
7135 struct drm_device *dev = crtc->dev;
7136 struct drm_display_mode *adjusted_mode;
7137 struct drm_encoder_helper_funcs *encoder_funcs;
7138 struct intel_encoder *encoder;
7139
7140 adjusted_mode = drm_mode_duplicate(dev, mode);
7141 if (!adjusted_mode)
7142 return ERR_PTR(-ENOMEM);
7143
7144 /* Pass our mode to the connectors and the CRTC to give them a chance to
7145 * adjust it according to limitations or connector properties, and also
7146 * a chance to reject the mode entirely.
7147 */
7148 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7149 base.head) {
7150
7151 if (&encoder->new_crtc->base != crtc)
7152 continue;
7153 encoder_funcs = encoder->base.helper_private;
7154 if (!(encoder_funcs->mode_fixup(&encoder->base, mode,
7155 adjusted_mode))) {
7156 DRM_DEBUG_KMS("Encoder fixup failed\n");
7157 goto fail;
7158 }
7159 }
7160
7161 if (!(intel_crtc_mode_fixup(crtc, mode, adjusted_mode))) {
7162 DRM_DEBUG_KMS("CRTC fixup failed\n");
7163 goto fail;
7164 }
7165 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
7166
7167 return adjusted_mode;
7168fail:
7169 drm_mode_destroy(dev, adjusted_mode);
7170 return ERR_PTR(-EINVAL);
7171}
7172
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007173/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7174 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7175static void
7176intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7177 unsigned *prepare_pipes, unsigned *disable_pipes)
7178{
7179 struct intel_crtc *intel_crtc;
7180 struct drm_device *dev = crtc->dev;
7181 struct intel_encoder *encoder;
7182 struct intel_connector *connector;
7183 struct drm_crtc *tmp_crtc;
7184
7185 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7186
7187 /* Check which crtcs have changed outputs connected to them, these need
7188 * to be part of the prepare_pipes mask. We don't (yet) support global
7189 * modeset across multiple crtcs, so modeset_pipes will only have one
7190 * bit set at most. */
7191 list_for_each_entry(connector, &dev->mode_config.connector_list,
7192 base.head) {
7193 if (connector->base.encoder == &connector->new_encoder->base)
7194 continue;
7195
7196 if (connector->base.encoder) {
7197 tmp_crtc = connector->base.encoder->crtc;
7198
7199 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7200 }
7201
7202 if (connector->new_encoder)
7203 *prepare_pipes |=
7204 1 << connector->new_encoder->new_crtc->pipe;
7205 }
7206
7207 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7208 base.head) {
7209 if (encoder->base.crtc == &encoder->new_crtc->base)
7210 continue;
7211
7212 if (encoder->base.crtc) {
7213 tmp_crtc = encoder->base.crtc;
7214
7215 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7216 }
7217
7218 if (encoder->new_crtc)
7219 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7220 }
7221
7222 /* Check for any pipes that will be fully disabled ... */
7223 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7224 base.head) {
7225 bool used = false;
7226
7227 /* Don't try to disable disabled crtcs. */
7228 if (!intel_crtc->base.enabled)
7229 continue;
7230
7231 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7232 base.head) {
7233 if (encoder->new_crtc == intel_crtc)
7234 used = true;
7235 }
7236
7237 if (!used)
7238 *disable_pipes |= 1 << intel_crtc->pipe;
7239 }
7240
7241
7242 /* set_mode is also used to update properties on life display pipes. */
7243 intel_crtc = to_intel_crtc(crtc);
7244 if (crtc->enabled)
7245 *prepare_pipes |= 1 << intel_crtc->pipe;
7246
7247 /* We only support modeset on one single crtc, hence we need to do that
7248 * only for the passed in crtc iff we change anything else than just
7249 * disable crtcs.
7250 *
7251 * This is actually not true, to be fully compatible with the old crtc
7252 * helper we automatically disable _any_ output (i.e. doesn't need to be
7253 * connected to the crtc we're modesetting on) if it's disconnected.
7254 * Which is a rather nutty api (since changed the output configuration
7255 * without userspace's explicit request can lead to confusion), but
7256 * alas. Hence we currently need to modeset on all pipes we prepare. */
7257 if (*prepare_pipes)
7258 *modeset_pipes = *prepare_pipes;
7259
7260 /* ... and mask these out. */
7261 *modeset_pipes &= ~(*disable_pipes);
7262 *prepare_pipes &= ~(*disable_pipes);
7263}
7264
Daniel Vetterea9d7582012-07-10 10:42:52 +02007265static bool intel_crtc_in_use(struct drm_crtc *crtc)
7266{
7267 struct drm_encoder *encoder;
7268 struct drm_device *dev = crtc->dev;
7269
7270 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
7271 if (encoder->crtc == crtc)
7272 return true;
7273
7274 return false;
7275}
7276
7277static void
7278intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
7279{
7280 struct intel_encoder *intel_encoder;
7281 struct intel_crtc *intel_crtc;
7282 struct drm_connector *connector;
7283
7284 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
7285 base.head) {
7286 if (!intel_encoder->base.crtc)
7287 continue;
7288
7289 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
7290
7291 if (prepare_pipes & (1 << intel_crtc->pipe))
7292 intel_encoder->connectors_active = false;
7293 }
7294
7295 intel_modeset_commit_output_state(dev);
7296
7297 /* Update computed state. */
7298 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7299 base.head) {
7300 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
7301 }
7302
7303 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7304 if (!connector->encoder || !connector->encoder->crtc)
7305 continue;
7306
7307 intel_crtc = to_intel_crtc(connector->encoder->crtc);
7308
7309 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02007310 struct drm_property *dpms_property =
7311 dev->mode_config.dpms_property;
7312
Daniel Vetterea9d7582012-07-10 10:42:52 +02007313 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05007314 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02007315 dpms_property,
7316 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02007317
7318 intel_encoder = to_intel_encoder(connector->encoder);
7319 intel_encoder->connectors_active = true;
7320 }
7321 }
7322
7323}
7324
Daniel Vetter25c5b262012-07-08 22:08:04 +02007325#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
7326 list_for_each_entry((intel_crtc), \
7327 &(dev)->mode_config.crtc_list, \
7328 base.head) \
7329 if (mask & (1 <<(intel_crtc)->pipe)) \
7330
Daniel Vetterb9805142012-08-31 17:37:33 +02007331void
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007332intel_modeset_check_state(struct drm_device *dev)
7333{
7334 struct intel_crtc *crtc;
7335 struct intel_encoder *encoder;
7336 struct intel_connector *connector;
7337
7338 list_for_each_entry(connector, &dev->mode_config.connector_list,
7339 base.head) {
7340 /* This also checks the encoder/connector hw state with the
7341 * ->get_hw_state callbacks. */
7342 intel_connector_check_state(connector);
7343
7344 WARN(&connector->new_encoder->base != connector->base.encoder,
7345 "connector's staged encoder doesn't match current encoder\n");
7346 }
7347
7348 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7349 base.head) {
7350 bool enabled = false;
7351 bool active = false;
7352 enum pipe pipe, tracked_pipe;
7353
7354 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
7355 encoder->base.base.id,
7356 drm_get_encoder_name(&encoder->base));
7357
7358 WARN(&encoder->new_crtc->base != encoder->base.crtc,
7359 "encoder's stage crtc doesn't match current crtc\n");
7360 WARN(encoder->connectors_active && !encoder->base.crtc,
7361 "encoder's active_connectors set, but no crtc\n");
7362
7363 list_for_each_entry(connector, &dev->mode_config.connector_list,
7364 base.head) {
7365 if (connector->base.encoder != &encoder->base)
7366 continue;
7367 enabled = true;
7368 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
7369 active = true;
7370 }
7371 WARN(!!encoder->base.crtc != enabled,
7372 "encoder's enabled state mismatch "
7373 "(expected %i, found %i)\n",
7374 !!encoder->base.crtc, enabled);
7375 WARN(active && !encoder->base.crtc,
7376 "active encoder with no crtc\n");
7377
7378 WARN(encoder->connectors_active != active,
7379 "encoder's computed active state doesn't match tracked active state "
7380 "(expected %i, found %i)\n", active, encoder->connectors_active);
7381
7382 active = encoder->get_hw_state(encoder, &pipe);
7383 WARN(active != encoder->connectors_active,
7384 "encoder's hw state doesn't match sw tracking "
7385 "(expected %i, found %i)\n",
7386 encoder->connectors_active, active);
7387
7388 if (!encoder->base.crtc)
7389 continue;
7390
7391 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
7392 WARN(active && pipe != tracked_pipe,
7393 "active encoder's pipe doesn't match"
7394 "(expected %i, found %i)\n",
7395 tracked_pipe, pipe);
7396
7397 }
7398
7399 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
7400 base.head) {
7401 bool enabled = false;
7402 bool active = false;
7403
7404 DRM_DEBUG_KMS("[CRTC:%d]\n",
7405 crtc->base.base.id);
7406
7407 WARN(crtc->active && !crtc->base.enabled,
7408 "active crtc, but not enabled in sw tracking\n");
7409
7410 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7411 base.head) {
7412 if (encoder->base.crtc != &crtc->base)
7413 continue;
7414 enabled = true;
7415 if (encoder->connectors_active)
7416 active = true;
7417 }
7418 WARN(active != crtc->active,
7419 "crtc's computed active state doesn't match tracked active state "
7420 "(expected %i, found %i)\n", active, crtc->active);
7421 WARN(enabled != crtc->base.enabled,
7422 "crtc's computed enabled state doesn't match tracked enabled state "
7423 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
7424
7425 assert_pipe(dev->dev_private, crtc->pipe, crtc->active);
7426 }
7427}
7428
Daniel Vettera6778b32012-07-02 09:56:42 +02007429bool intel_set_mode(struct drm_crtc *crtc,
7430 struct drm_display_mode *mode,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007431 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02007432{
7433 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02007434 drm_i915_private_t *dev_priv = dev->dev_private;
Tim Gardner3ac18232012-12-07 07:54:26 -07007435 struct drm_display_mode *adjusted_mode, *saved_mode, *saved_hwmode;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007436 struct intel_crtc *intel_crtc;
7437 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Daniel Vettera6778b32012-07-02 09:56:42 +02007438 bool ret = true;
7439
Tim Gardner3ac18232012-12-07 07:54:26 -07007440 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
7441 if (!saved_mode) {
7442 DRM_ERROR("i915: Could not allocate saved display mode.\n");
7443 return false;
7444 }
7445 saved_hwmode = saved_mode + 1;
7446
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007447 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02007448 &prepare_pipes, &disable_pipes);
7449
7450 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7451 modeset_pipes, prepare_pipes, disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007452
Daniel Vetter976f8a22012-07-08 22:34:21 +02007453 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
7454 intel_crtc_disable(&intel_crtc->base);
7455
Tim Gardner3ac18232012-12-07 07:54:26 -07007456 *saved_hwmode = crtc->hwmode;
7457 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007458
Daniel Vetter25c5b262012-07-08 22:08:04 +02007459 /* Hack: Because we don't (yet) support global modeset on multiple
7460 * crtcs, we don't keep track of the new mode for more than one crtc.
7461 * Hence simply check whether any bit is set in modeset_pipes in all the
7462 * pieces of code that are not yet converted to deal with mutliple crtcs
7463 * changing their mode at the same time. */
7464 adjusted_mode = NULL;
7465 if (modeset_pipes) {
7466 adjusted_mode = intel_modeset_adjusted_mode(crtc, mode);
7467 if (IS_ERR(adjusted_mode)) {
Tim Gardner3ac18232012-12-07 07:54:26 -07007468 ret = false;
7469 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007470 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007471 }
7472
Daniel Vetterea9d7582012-07-10 10:42:52 +02007473 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
7474 if (intel_crtc->base.enabled)
7475 dev_priv->display.crtc_disable(&intel_crtc->base);
7476 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007477
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02007478 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
7479 * to set it here already despite that we pass it down the callchain.
7480 */
7481 if (modeset_pipes)
Daniel Vetter25c5b262012-07-08 22:08:04 +02007482 crtc->mode = *mode;
Daniel Vetter7758a112012-07-08 19:40:39 +02007483
Daniel Vetterea9d7582012-07-10 10:42:52 +02007484 /* Only after disabling all output pipelines that will be changed can we
7485 * update the the output configuration. */
7486 intel_modeset_update_state(dev, prepare_pipes);
7487
Daniel Vetter47fab732012-10-26 10:58:18 +02007488 if (dev_priv->display.modeset_global_resources)
7489 dev_priv->display.modeset_global_resources(dev);
7490
Daniel Vettera6778b32012-07-02 09:56:42 +02007491 /* Set up the DPLL and any encoders state that needs to adjust or depend
7492 * on the DPLL.
7493 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007494 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
7495 ret = !intel_crtc_mode_set(&intel_crtc->base,
7496 mode, adjusted_mode,
7497 x, y, fb);
7498 if (!ret)
7499 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02007500 }
7501
7502 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007503 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
7504 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02007505
Daniel Vetter25c5b262012-07-08 22:08:04 +02007506 if (modeset_pipes) {
7507 /* Store real post-adjustment hardware mode. */
7508 crtc->hwmode = *adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007509
Daniel Vetter25c5b262012-07-08 22:08:04 +02007510 /* Calculate and store various constants which
7511 * are later needed by vblank and swap-completion
7512 * timestamping. They are derived from true hwmode.
7513 */
7514 drm_calc_timestamping_constants(crtc);
7515 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007516
7517 /* FIXME: add subpixel order */
7518done:
7519 drm_mode_destroy(dev, adjusted_mode);
Daniel Vetter25c5b262012-07-08 22:08:04 +02007520 if (!ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07007521 crtc->hwmode = *saved_hwmode;
7522 crtc->mode = *saved_mode;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007523 } else {
7524 intel_modeset_check_state(dev);
Daniel Vettera6778b32012-07-02 09:56:42 +02007525 }
7526
Tim Gardner3ac18232012-12-07 07:54:26 -07007527out:
7528 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02007529 return ret;
7530}
7531
Daniel Vetter25c5b262012-07-08 22:08:04 +02007532#undef for_each_intel_crtc_masked
7533
Daniel Vetterd9e55602012-07-04 22:16:09 +02007534static void intel_set_config_free(struct intel_set_config *config)
7535{
7536 if (!config)
7537 return;
7538
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007539 kfree(config->save_connector_encoders);
7540 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02007541 kfree(config);
7542}
7543
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007544static int intel_set_config_save_state(struct drm_device *dev,
7545 struct intel_set_config *config)
7546{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007547 struct drm_encoder *encoder;
7548 struct drm_connector *connector;
7549 int count;
7550
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007551 config->save_encoder_crtcs =
7552 kcalloc(dev->mode_config.num_encoder,
7553 sizeof(struct drm_crtc *), GFP_KERNEL);
7554 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007555 return -ENOMEM;
7556
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007557 config->save_connector_encoders =
7558 kcalloc(dev->mode_config.num_connector,
7559 sizeof(struct drm_encoder *), GFP_KERNEL);
7560 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007561 return -ENOMEM;
7562
7563 /* Copy data. Note that driver private data is not affected.
7564 * Should anything bad happen only the expected state is
7565 * restored, not the drivers personal bookkeeping.
7566 */
7567 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007568 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007569 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007570 }
7571
7572 count = 0;
7573 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007574 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007575 }
7576
7577 return 0;
7578}
7579
7580static void intel_set_config_restore_state(struct drm_device *dev,
7581 struct intel_set_config *config)
7582{
Daniel Vetter9a935852012-07-05 22:34:27 +02007583 struct intel_encoder *encoder;
7584 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007585 int count;
7586
7587 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007588 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7589 encoder->new_crtc =
7590 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007591 }
7592
7593 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007594 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
7595 connector->new_encoder =
7596 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007597 }
7598}
7599
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007600static void
7601intel_set_config_compute_mode_changes(struct drm_mode_set *set,
7602 struct intel_set_config *config)
7603{
7604
7605 /* We should be able to check here if the fb has the same properties
7606 * and then just flip_or_move it */
7607 if (set->crtc->fb != set->fb) {
7608 /* If we have no fb then treat it as a full mode set */
7609 if (set->crtc->fb == NULL) {
7610 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
7611 config->mode_changed = true;
7612 } else if (set->fb == NULL) {
7613 config->mode_changed = true;
7614 } else if (set->fb->depth != set->crtc->fb->depth) {
7615 config->mode_changed = true;
7616 } else if (set->fb->bits_per_pixel !=
7617 set->crtc->fb->bits_per_pixel) {
7618 config->mode_changed = true;
7619 } else
7620 config->fb_changed = true;
7621 }
7622
Daniel Vetter835c5872012-07-10 18:11:08 +02007623 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007624 config->fb_changed = true;
7625
7626 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
7627 DRM_DEBUG_KMS("modes are different, full mode set\n");
7628 drm_mode_debug_printmodeline(&set->crtc->mode);
7629 drm_mode_debug_printmodeline(set->mode);
7630 config->mode_changed = true;
7631 }
7632}
7633
Daniel Vetter2e431052012-07-04 22:42:15 +02007634static int
Daniel Vetter9a935852012-07-05 22:34:27 +02007635intel_modeset_stage_output_state(struct drm_device *dev,
7636 struct drm_mode_set *set,
7637 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02007638{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007639 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02007640 struct intel_connector *connector;
7641 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02007642 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02007643
Daniel Vetter9a935852012-07-05 22:34:27 +02007644 /* The upper layers ensure that we either disabl a crtc or have a list
7645 * of connectors. For paranoia, double-check this. */
7646 WARN_ON(!set->fb && (set->num_connectors != 0));
7647 WARN_ON(set->fb && (set->num_connectors == 0));
7648
Daniel Vetter50f56112012-07-02 09:35:43 +02007649 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007650 list_for_each_entry(connector, &dev->mode_config.connector_list,
7651 base.head) {
7652 /* Otherwise traverse passed in connector list and get encoders
7653 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007654 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02007655 if (set->connectors[ro] == &connector->base) {
7656 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02007657 break;
7658 }
7659 }
7660
Daniel Vetter9a935852012-07-05 22:34:27 +02007661 /* If we disable the crtc, disable all its connectors. Also, if
7662 * the connector is on the changing crtc but not on the new
7663 * connector list, disable it. */
7664 if ((!set->fb || ro == set->num_connectors) &&
7665 connector->base.encoder &&
7666 connector->base.encoder->crtc == set->crtc) {
7667 connector->new_encoder = NULL;
7668
7669 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
7670 connector->base.base.id,
7671 drm_get_connector_name(&connector->base));
7672 }
7673
7674
7675 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007676 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007677 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02007678 }
Daniel Vetter50f56112012-07-02 09:35:43 +02007679
Daniel Vetter9a935852012-07-05 22:34:27 +02007680 /* Disable all disconnected encoders. */
7681 if (connector->base.status == connector_status_disconnected)
7682 connector->new_encoder = NULL;
7683 }
7684 /* connector->new_encoder is now updated for all connectors. */
7685
7686 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007687 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007688 list_for_each_entry(connector, &dev->mode_config.connector_list,
7689 base.head) {
7690 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02007691 continue;
7692
Daniel Vetter9a935852012-07-05 22:34:27 +02007693 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02007694
7695 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02007696 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02007697 new_crtc = set->crtc;
7698 }
7699
7700 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02007701 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
7702 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007703 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02007704 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007705 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
7706
7707 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
7708 connector->base.base.id,
7709 drm_get_connector_name(&connector->base),
7710 new_crtc->base.id);
7711 }
7712
7713 /* Check for any encoders that needs to be disabled. */
7714 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7715 base.head) {
7716 list_for_each_entry(connector,
7717 &dev->mode_config.connector_list,
7718 base.head) {
7719 if (connector->new_encoder == encoder) {
7720 WARN_ON(!connector->new_encoder->new_crtc);
7721
7722 goto next_encoder;
7723 }
7724 }
7725 encoder->new_crtc = NULL;
7726next_encoder:
7727 /* Only now check for crtc changes so we don't miss encoders
7728 * that will be disabled. */
7729 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007730 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007731 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02007732 }
7733 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007734 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007735
Daniel Vetter2e431052012-07-04 22:42:15 +02007736 return 0;
7737}
7738
7739static int intel_crtc_set_config(struct drm_mode_set *set)
7740{
7741 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02007742 struct drm_mode_set save_set;
7743 struct intel_set_config *config;
7744 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02007745
Daniel Vetter8d3e3752012-07-05 16:09:09 +02007746 BUG_ON(!set);
7747 BUG_ON(!set->crtc);
7748 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02007749
7750 if (!set->mode)
7751 set->fb = NULL;
7752
Daniel Vetter431e50f2012-07-10 17:53:42 +02007753 /* The fb helper likes to play gross jokes with ->mode_set_config.
7754 * Unfortunately the crtc helper doesn't do much at all for this case,
7755 * so we have to cope with this madness until the fb helper is fixed up. */
7756 if (set->fb && set->num_connectors == 0)
7757 return 0;
7758
Daniel Vetter2e431052012-07-04 22:42:15 +02007759 if (set->fb) {
7760 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
7761 set->crtc->base.id, set->fb->base.id,
7762 (int)set->num_connectors, set->x, set->y);
7763 } else {
7764 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02007765 }
7766
7767 dev = set->crtc->dev;
7768
7769 ret = -ENOMEM;
7770 config = kzalloc(sizeof(*config), GFP_KERNEL);
7771 if (!config)
7772 goto out_config;
7773
7774 ret = intel_set_config_save_state(dev, config);
7775 if (ret)
7776 goto out_config;
7777
7778 save_set.crtc = set->crtc;
7779 save_set.mode = &set->crtc->mode;
7780 save_set.x = set->crtc->x;
7781 save_set.y = set->crtc->y;
7782 save_set.fb = set->crtc->fb;
7783
7784 /* Compute whether we need a full modeset, only an fb base update or no
7785 * change at all. In the future we might also check whether only the
7786 * mode changed, e.g. for LVDS where we only change the panel fitter in
7787 * such cases. */
7788 intel_set_config_compute_mode_changes(set, config);
7789
Daniel Vetter9a935852012-07-05 22:34:27 +02007790 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02007791 if (ret)
7792 goto fail;
7793
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007794 if (config->mode_changed) {
Daniel Vetter87f1faa2012-07-05 23:36:17 +02007795 if (set->mode) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007796 DRM_DEBUG_KMS("attempting to set mode from"
7797 " userspace\n");
7798 drm_mode_debug_printmodeline(set->mode);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02007799 }
7800
7801 if (!intel_set_mode(set->crtc, set->mode,
7802 set->x, set->y, set->fb)) {
7803 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
7804 set->crtc->base.id);
7805 ret = -EINVAL;
7806 goto fail;
7807 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007808 } else if (config->fb_changed) {
Daniel Vetter4f660f42012-07-02 09:47:37 +02007809 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007810 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02007811 }
7812
Daniel Vetterd9e55602012-07-04 22:16:09 +02007813 intel_set_config_free(config);
7814
Daniel Vetter50f56112012-07-02 09:35:43 +02007815 return 0;
7816
7817fail:
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007818 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02007819
7820 /* Try to restore the config */
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007821 if (config->mode_changed &&
Daniel Vettera6778b32012-07-02 09:56:42 +02007822 !intel_set_mode(save_set.crtc, save_set.mode,
7823 save_set.x, save_set.y, save_set.fb))
Daniel Vetter50f56112012-07-02 09:35:43 +02007824 DRM_ERROR("failed to restore config after modeset failure\n");
7825
Daniel Vetterd9e55602012-07-04 22:16:09 +02007826out_config:
7827 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02007828 return ret;
7829}
7830
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007831static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007832 .cursor_set = intel_crtc_cursor_set,
7833 .cursor_move = intel_crtc_cursor_move,
7834 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02007835 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007836 .destroy = intel_crtc_destroy,
7837 .page_flip = intel_crtc_page_flip,
7838};
7839
Paulo Zanoni79f689a2012-10-05 12:05:52 -03007840static void intel_cpu_pll_init(struct drm_device *dev)
7841{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02007842 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03007843 intel_ddi_pll_init(dev);
7844}
7845
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007846static void intel_pch_pll_init(struct drm_device *dev)
7847{
7848 drm_i915_private_t *dev_priv = dev->dev_private;
7849 int i;
7850
7851 if (dev_priv->num_pch_pll == 0) {
7852 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
7853 return;
7854 }
7855
7856 for (i = 0; i < dev_priv->num_pch_pll; i++) {
7857 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
7858 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
7859 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
7860 }
7861}
7862
Hannes Ederb358d0a2008-12-18 21:18:47 +01007863static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08007864{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007865 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007866 struct intel_crtc *intel_crtc;
7867 int i;
7868
7869 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
7870 if (intel_crtc == NULL)
7871 return;
7872
7873 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
7874
7875 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08007876 for (i = 0; i < 256; i++) {
7877 intel_crtc->lut_r[i] = i;
7878 intel_crtc->lut_g[i] = i;
7879 intel_crtc->lut_b[i] = i;
7880 }
7881
Jesse Barnes80824002009-09-10 15:28:06 -07007882 /* Swap pipes & planes for FBC on pre-965 */
7883 intel_crtc->pipe = pipe;
7884 intel_crtc->plane = pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02007885 intel_crtc->cpu_transcoder = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01007886 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007887 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01007888 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07007889 }
7890
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007891 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
7892 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
7893 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
7894 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
7895
Jesse Barnes5a354202011-06-24 12:19:22 -07007896 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07007897
Jesse Barnes79e53942008-11-07 14:24:08 -08007898 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08007899}
7900
Carl Worth08d7b3d2009-04-29 14:43:54 -07007901int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00007902 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07007903{
Carl Worth08d7b3d2009-04-29 14:43:54 -07007904 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02007905 struct drm_mode_object *drmmode_obj;
7906 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007907
Daniel Vetter1cff8f62012-04-24 09:55:08 +02007908 if (!drm_core_check_feature(dev, DRIVER_MODESET))
7909 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007910
Daniel Vetterc05422d2009-08-11 16:05:30 +02007911 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
7912 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07007913
Daniel Vetterc05422d2009-08-11 16:05:30 +02007914 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07007915 DRM_ERROR("no such CRTC id\n");
7916 return -EINVAL;
7917 }
7918
Daniel Vetterc05422d2009-08-11 16:05:30 +02007919 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
7920 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007921
Daniel Vetterc05422d2009-08-11 16:05:30 +02007922 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007923}
7924
Daniel Vetter66a92782012-07-12 20:08:18 +02007925static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08007926{
Daniel Vetter66a92782012-07-12 20:08:18 +02007927 struct drm_device *dev = encoder->base.dev;
7928 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007929 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007930 int entry = 0;
7931
Daniel Vetter66a92782012-07-12 20:08:18 +02007932 list_for_each_entry(source_encoder,
7933 &dev->mode_config.encoder_list, base.head) {
7934
7935 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08007936 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02007937
7938 /* Intel hw has only one MUX where enocoders could be cloned. */
7939 if (encoder->cloneable && source_encoder->cloneable)
7940 index_mask |= (1 << entry);
7941
Jesse Barnes79e53942008-11-07 14:24:08 -08007942 entry++;
7943 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01007944
Jesse Barnes79e53942008-11-07 14:24:08 -08007945 return index_mask;
7946}
7947
Chris Wilson4d302442010-12-14 19:21:29 +00007948static bool has_edp_a(struct drm_device *dev)
7949{
7950 struct drm_i915_private *dev_priv = dev->dev_private;
7951
7952 if (!IS_MOBILE(dev))
7953 return false;
7954
7955 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
7956 return false;
7957
7958 if (IS_GEN5(dev) &&
7959 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
7960 return false;
7961
7962 return true;
7963}
7964
Jesse Barnes79e53942008-11-07 14:24:08 -08007965static void intel_setup_outputs(struct drm_device *dev)
7966{
Eric Anholt725e30a2009-01-22 13:01:02 -08007967 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007968 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007969 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00007970 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08007971
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00007972 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00007973 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
7974 /* disable the panel fitter on everything but LVDS */
7975 I915_WRITE(PFIT_CONTROL, 0);
7976 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007977
Paulo Zanoniaffa9352012-11-23 15:30:39 -02007978 if (!(HAS_DDI(dev) && (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02007979 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007980
Paulo Zanoniaffa9352012-11-23 15:30:39 -02007981 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03007982 int found;
7983
7984 /* Haswell uses DDI functions to detect digital outputs */
7985 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
7986 /* DDI A only supports eDP */
7987 if (found)
7988 intel_ddi_init(dev, PORT_A);
7989
7990 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
7991 * register */
7992 found = I915_READ(SFUSE_STRAP);
7993
7994 if (found & SFUSE_STRAP_DDIB_DETECTED)
7995 intel_ddi_init(dev, PORT_B);
7996 if (found & SFUSE_STRAP_DDIC_DETECTED)
7997 intel_ddi_init(dev, PORT_C);
7998 if (found & SFUSE_STRAP_DDID_DETECTED)
7999 intel_ddi_init(dev, PORT_D);
8000 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008001 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02008002 dpd_is_edp = intel_dpd_is_edp(dev);
8003
8004 if (has_edp_a(dev))
8005 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008006
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008007 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08008008 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01008009 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008010 if (!found)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008011 intel_hdmi_init(dev, HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008012 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008013 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008014 }
8015
8016 if (I915_READ(HDMIC) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008017 intel_hdmi_init(dev, HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008018
Jesse Barnesb708a1d2012-06-11 14:39:56 -04008019 if (!dpd_is_edp && I915_READ(HDMID) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008020 intel_hdmi_init(dev, HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008021
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008022 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008023 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008024
Daniel Vetter270b3042012-10-27 15:52:05 +02008025 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008026 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008027 } else if (IS_VALLEYVIEW(dev)) {
8028 int found;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008029
Gajanan Bhat19c03922012-09-27 19:13:07 +05308030 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
8031 if (I915_READ(DP_C) & DP_DETECTED)
8032 intel_dp_init(dev, DP_C, PORT_C);
8033
Jesse Barnes4a87d652012-06-15 11:55:16 -07008034 if (I915_READ(SDVOB) & PORT_DETECTED) {
8035 /* SDVOB multiplex with HDMIB */
8036 found = intel_sdvo_init(dev, SDVOB, true);
8037 if (!found)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008038 intel_hdmi_init(dev, SDVOB, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008039 if (!found && (I915_READ(DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008040 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008041 }
8042
8043 if (I915_READ(SDVOC) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008044 intel_hdmi_init(dev, SDVOC, PORT_C);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008045
Zhenyu Wang103a1962009-11-27 11:44:36 +08008046 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008047 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008048
Eric Anholt725e30a2009-01-22 13:01:02 -08008049 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008050 DRM_DEBUG_KMS("probing SDVOB\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008051 found = intel_sdvo_init(dev, SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008052 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8053 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008054 intel_hdmi_init(dev, SDVOB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008055 }
Ma Ling27185ae2009-08-24 13:50:23 +08008056
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008057 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8058 DRM_DEBUG_KMS("probing DP_B\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008059 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008060 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008061 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008062
8063 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008064
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008065 if (I915_READ(SDVOB) & SDVO_DETECTED) {
8066 DRM_DEBUG_KMS("probing SDVOC\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008067 found = intel_sdvo_init(dev, SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008068 }
Ma Ling27185ae2009-08-24 13:50:23 +08008069
8070 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
8071
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008072 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8073 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008074 intel_hdmi_init(dev, SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008075 }
8076 if (SUPPORTS_INTEGRATED_DP(dev)) {
8077 DRM_DEBUG_KMS("probing DP_C\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008078 intel_dp_init(dev, DP_C, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008079 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008080 }
Ma Ling27185ae2009-08-24 13:50:23 +08008081
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008082 if (SUPPORTS_INTEGRATED_DP(dev) &&
8083 (I915_READ(DP_D) & DP_DETECTED)) {
8084 DRM_DEBUG_KMS("probing DP_D\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008085 intel_dp_init(dev, DP_D, PORT_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008086 }
Eric Anholtbad720f2009-10-22 16:11:14 -07008087 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008088 intel_dvo_init(dev);
8089
Zhenyu Wang103a1962009-11-27 11:44:36 +08008090 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008091 intel_tv_init(dev);
8092
Chris Wilson4ef69c72010-09-09 15:14:28 +01008093 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8094 encoder->base.possible_crtcs = encoder->crtc_mask;
8095 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02008096 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08008097 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008098
Paulo Zanoni40579ab2012-07-03 15:57:33 -03008099 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Keith Packard9fb526d2011-09-26 22:24:57 -07008100 ironlake_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02008101
8102 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008103}
8104
8105static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8106{
8107 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008108
8109 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008110 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008111
8112 kfree(intel_fb);
8113}
8114
8115static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008116 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008117 unsigned int *handle)
8118{
8119 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008120 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008121
Chris Wilson05394f32010-11-08 19:18:58 +00008122 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008123}
8124
8125static const struct drm_framebuffer_funcs intel_fb_funcs = {
8126 .destroy = intel_user_framebuffer_destroy,
8127 .create_handle = intel_user_framebuffer_create_handle,
8128};
8129
Dave Airlie38651672010-03-30 05:34:13 +00008130int intel_framebuffer_init(struct drm_device *dev,
8131 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008132 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008133 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008134{
Jesse Barnes79e53942008-11-07 14:24:08 -08008135 int ret;
8136
Chris Wilson05394f32010-11-08 19:18:58 +00008137 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01008138 return -EINVAL;
8139
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008140 if (mode_cmd->pitches[0] & 63)
Chris Wilson57cd6502010-08-08 12:34:44 +01008141 return -EINVAL;
8142
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008143 /* FIXME <= Gen4 stride limits are bit unclear */
8144 if (mode_cmd->pitches[0] > 32768)
8145 return -EINVAL;
8146
8147 if (obj->tiling_mode != I915_TILING_NONE &&
8148 mode_cmd->pitches[0] != obj->stride)
8149 return -EINVAL;
8150
Ville Syrjälä57779d02012-10-31 17:50:14 +02008151 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008152 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02008153 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008154 case DRM_FORMAT_RGB565:
8155 case DRM_FORMAT_XRGB8888:
8156 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008157 break;
8158 case DRM_FORMAT_XRGB1555:
8159 case DRM_FORMAT_ARGB1555:
8160 if (INTEL_INFO(dev)->gen > 3)
8161 return -EINVAL;
8162 break;
8163 case DRM_FORMAT_XBGR8888:
8164 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008165 case DRM_FORMAT_XRGB2101010:
8166 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008167 case DRM_FORMAT_XBGR2101010:
8168 case DRM_FORMAT_ABGR2101010:
8169 if (INTEL_INFO(dev)->gen < 4)
8170 return -EINVAL;
Jesse Barnesb5626742011-06-24 12:19:27 -07008171 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02008172 case DRM_FORMAT_YUYV:
8173 case DRM_FORMAT_UYVY:
8174 case DRM_FORMAT_YVYU:
8175 case DRM_FORMAT_VYUY:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008176 if (INTEL_INFO(dev)->gen < 6)
8177 return -EINVAL;
Chris Wilson57cd6502010-08-08 12:34:44 +01008178 break;
8179 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008180 DRM_DEBUG_KMS("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01008181 return -EINVAL;
8182 }
8183
Ville Syrjälä90f9a332012-10-31 17:50:19 +02008184 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
8185 if (mode_cmd->offsets[0] != 0)
8186 return -EINVAL;
8187
Jesse Barnes79e53942008-11-07 14:24:08 -08008188 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8189 if (ret) {
8190 DRM_ERROR("framebuffer init failed %d\n", ret);
8191 return ret;
8192 }
8193
8194 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08008195 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008196 return 0;
8197}
8198
Jesse Barnes79e53942008-11-07 14:24:08 -08008199static struct drm_framebuffer *
8200intel_user_framebuffer_create(struct drm_device *dev,
8201 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008202 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08008203{
Chris Wilson05394f32010-11-08 19:18:58 +00008204 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008205
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008206 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8207 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00008208 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01008209 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08008210
Chris Wilsond2dff872011-04-19 08:36:26 +01008211 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08008212}
8213
Jesse Barnes79e53942008-11-07 14:24:08 -08008214static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08008215 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00008216 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08008217};
8218
Jesse Barnese70236a2009-09-21 10:42:27 -07008219/* Set up chip specific display functions */
8220static void intel_init_display(struct drm_device *dev)
8221{
8222 struct drm_i915_private *dev_priv = dev->dev_private;
8223
8224 /* We always want a DPMS function */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008225 if (HAS_DDI(dev)) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008226 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02008227 dev_priv->display.crtc_enable = haswell_crtc_enable;
8228 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008229 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008230 dev_priv->display.update_plane = ironlake_update_plane;
8231 } else if (HAS_PCH_SPLIT(dev)) {
Eric Anholtf564048e2011-03-30 13:01:02 -07008232 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008233 dev_priv->display.crtc_enable = ironlake_crtc_enable;
8234 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008235 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008236 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008237 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07008238 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008239 dev_priv->display.crtc_enable = i9xx_crtc_enable;
8240 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008241 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008242 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008243 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008244
Jesse Barnese70236a2009-09-21 10:42:27 -07008245 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07008246 if (IS_VALLEYVIEW(dev))
8247 dev_priv->display.get_display_clock_speed =
8248 valleyview_get_display_clock_speed;
8249 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07008250 dev_priv->display.get_display_clock_speed =
8251 i945_get_display_clock_speed;
8252 else if (IS_I915G(dev))
8253 dev_priv->display.get_display_clock_speed =
8254 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008255 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008256 dev_priv->display.get_display_clock_speed =
8257 i9xx_misc_get_display_clock_speed;
8258 else if (IS_I915GM(dev))
8259 dev_priv->display.get_display_clock_speed =
8260 i915gm_get_display_clock_speed;
8261 else if (IS_I865G(dev))
8262 dev_priv->display.get_display_clock_speed =
8263 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02008264 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008265 dev_priv->display.get_display_clock_speed =
8266 i855_get_display_clock_speed;
8267 else /* 852, 830 */
8268 dev_priv->display.get_display_clock_speed =
8269 i830_get_display_clock_speed;
8270
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008271 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01008272 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008273 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008274 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08008275 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008276 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008277 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07008278 } else if (IS_IVYBRIDGE(dev)) {
8279 /* FIXME: detect B0+ stepping and use auto training */
8280 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008281 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02008282 dev_priv->display.modeset_global_resources =
8283 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03008284 } else if (IS_HASWELL(dev)) {
8285 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08008286 dev_priv->display.write_eld = haswell_write_eld;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02008287 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07008288 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08008289 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07008290 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008291
8292 /* Default just returns -ENODEV to indicate unsupported */
8293 dev_priv->display.queue_flip = intel_default_queue_flip;
8294
8295 switch (INTEL_INFO(dev)->gen) {
8296 case 2:
8297 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8298 break;
8299
8300 case 3:
8301 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8302 break;
8303
8304 case 4:
8305 case 5:
8306 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8307 break;
8308
8309 case 6:
8310 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8311 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008312 case 7:
8313 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8314 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008315 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008316}
8317
Jesse Barnesb690e962010-07-19 13:53:12 -07008318/*
8319 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8320 * resume, or other times. This quirk makes sure that's the case for
8321 * affected systems.
8322 */
Akshay Joshi0206e352011-08-16 15:34:10 -04008323static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07008324{
8325 struct drm_i915_private *dev_priv = dev->dev_private;
8326
8327 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008328 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008329}
8330
Keith Packard435793d2011-07-12 14:56:22 -07008331/*
8332 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8333 */
8334static void quirk_ssc_force_disable(struct drm_device *dev)
8335{
8336 struct drm_i915_private *dev_priv = dev->dev_private;
8337 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008338 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07008339}
8340
Carsten Emde4dca20e2012-03-15 15:56:26 +01008341/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01008342 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
8343 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01008344 */
8345static void quirk_invert_brightness(struct drm_device *dev)
8346{
8347 struct drm_i915_private *dev_priv = dev->dev_private;
8348 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008349 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008350}
8351
8352struct intel_quirk {
8353 int device;
8354 int subsystem_vendor;
8355 int subsystem_device;
8356 void (*hook)(struct drm_device *dev);
8357};
8358
Egbert Eich5f85f1762012-10-14 15:46:38 +02008359/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
8360struct intel_dmi_quirk {
8361 void (*hook)(struct drm_device *dev);
8362 const struct dmi_system_id (*dmi_id_list)[];
8363};
8364
8365static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
8366{
8367 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
8368 return 1;
8369}
8370
8371static const struct intel_dmi_quirk intel_dmi_quirks[] = {
8372 {
8373 .dmi_id_list = &(const struct dmi_system_id[]) {
8374 {
8375 .callback = intel_dmi_reverse_brightness,
8376 .ident = "NCR Corporation",
8377 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
8378 DMI_MATCH(DMI_PRODUCT_NAME, ""),
8379 },
8380 },
8381 { } /* terminating entry */
8382 },
8383 .hook = quirk_invert_brightness,
8384 },
8385};
8386
Ben Widawskyc43b5632012-04-16 14:07:40 -07008387static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07008388 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04008389 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07008390
Jesse Barnesb690e962010-07-19 13:53:12 -07008391 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
8392 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
8393
Jesse Barnesb690e962010-07-19 13:53:12 -07008394 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
8395 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
8396
Daniel Vetterccd0d362012-10-10 23:13:59 +02008397 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07008398 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02008399 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07008400
8401 /* Lenovo U160 cannot use SSC on LVDS */
8402 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02008403
8404 /* Sony Vaio Y cannot use SSC on LVDS */
8405 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01008406
8407 /* Acer Aspire 5734Z must invert backlight brightness */
8408 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07008409};
8410
8411static void intel_init_quirks(struct drm_device *dev)
8412{
8413 struct pci_dev *d = dev->pdev;
8414 int i;
8415
8416 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
8417 struct intel_quirk *q = &intel_quirks[i];
8418
8419 if (d->device == q->device &&
8420 (d->subsystem_vendor == q->subsystem_vendor ||
8421 q->subsystem_vendor == PCI_ANY_ID) &&
8422 (d->subsystem_device == q->subsystem_device ||
8423 q->subsystem_device == PCI_ANY_ID))
8424 q->hook(dev);
8425 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02008426 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
8427 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
8428 intel_dmi_quirks[i].hook(dev);
8429 }
Jesse Barnesb690e962010-07-19 13:53:12 -07008430}
8431
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008432/* Disable the VGA plane that we never use */
8433static void i915_disable_vga(struct drm_device *dev)
8434{
8435 struct drm_i915_private *dev_priv = dev->dev_private;
8436 u8 sr1;
8437 u32 vga_reg;
8438
8439 if (HAS_PCH_SPLIT(dev))
8440 vga_reg = CPU_VGACNTRL;
8441 else
8442 vga_reg = VGACNTRL;
8443
8444 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07008445 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008446 sr1 = inb(VGA_SR_DATA);
8447 outb(sr1 | 1<<5, VGA_SR_DATA);
8448 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
8449 udelay(300);
8450
8451 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
8452 POSTING_READ(vga_reg);
8453}
8454
Daniel Vetterf8175862012-04-10 15:50:11 +02008455void intel_modeset_init_hw(struct drm_device *dev)
8456{
Eugeni Dodonov0232e922012-07-06 15:42:36 -03008457 /* We attempt to init the necessary power wells early in the initialization
8458 * time, so the subsystems that expect power to be enabled can work.
8459 */
8460 intel_init_power_wells(dev);
8461
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03008462 intel_prepare_ddi(dev);
8463
Daniel Vetterf8175862012-04-10 15:50:11 +02008464 intel_init_clock_gating(dev);
8465
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008466 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008467 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008468 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02008469}
8470
Jesse Barnes79e53942008-11-07 14:24:08 -08008471void intel_modeset_init(struct drm_device *dev)
8472{
Jesse Barnes652c3932009-08-17 13:31:43 -07008473 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008474 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008475
8476 drm_mode_config_init(dev);
8477
8478 dev->mode_config.min_width = 0;
8479 dev->mode_config.min_height = 0;
8480
Dave Airlie019d96c2011-09-29 16:20:42 +01008481 dev->mode_config.preferred_depth = 24;
8482 dev->mode_config.prefer_shadow = 1;
8483
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02008484 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08008485
Jesse Barnesb690e962010-07-19 13:53:12 -07008486 intel_init_quirks(dev);
8487
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008488 intel_init_pm(dev);
8489
Jesse Barnese70236a2009-09-21 10:42:27 -07008490 intel_init_display(dev);
8491
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008492 if (IS_GEN2(dev)) {
8493 dev->mode_config.max_width = 2048;
8494 dev->mode_config.max_height = 2048;
8495 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07008496 dev->mode_config.max_width = 4096;
8497 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08008498 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008499 dev->mode_config.max_width = 8192;
8500 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08008501 }
Daniel Vetterdd2757f2012-06-07 15:55:57 +02008502 dev->mode_config.fb_base = dev_priv->mm.gtt_base_addr;
Jesse Barnes79e53942008-11-07 14:24:08 -08008503
Zhao Yakui28c97732009-10-09 11:39:41 +08008504 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10008505 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08008506
Dave Airliea3524f12010-06-06 18:59:41 +10008507 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008508 intel_crtc_init(dev, i);
Jesse Barnes00c2064b2012-01-13 15:48:39 -08008509 ret = intel_plane_init(dev, i);
8510 if (ret)
8511 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08008512 }
8513
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008514 intel_cpu_pll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008515 intel_pch_pll_init(dev);
8516
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008517 /* Just disable it once at startup */
8518 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008519 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00008520
8521 /* Just in case the BIOS is doing something questionable. */
8522 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01008523}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008524
Daniel Vetter24929352012-07-02 20:28:59 +02008525static void
8526intel_connector_break_all_links(struct intel_connector *connector)
8527{
8528 connector->base.dpms = DRM_MODE_DPMS_OFF;
8529 connector->base.encoder = NULL;
8530 connector->encoder->connectors_active = false;
8531 connector->encoder->base.crtc = NULL;
8532}
8533
Daniel Vetter7fad7982012-07-04 17:51:47 +02008534static void intel_enable_pipe_a(struct drm_device *dev)
8535{
8536 struct intel_connector *connector;
8537 struct drm_connector *crt = NULL;
8538 struct intel_load_detect_pipe load_detect_temp;
8539
8540 /* We can't just switch on the pipe A, we need to set things up with a
8541 * proper mode and output configuration. As a gross hack, enable pipe A
8542 * by enabling the load detect pipe once. */
8543 list_for_each_entry(connector,
8544 &dev->mode_config.connector_list,
8545 base.head) {
8546 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
8547 crt = &connector->base;
8548 break;
8549 }
8550 }
8551
8552 if (!crt)
8553 return;
8554
8555 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
8556 intel_release_load_detect_pipe(crt, &load_detect_temp);
8557
8558
8559}
8560
Daniel Vetterfa555832012-10-10 23:14:00 +02008561static bool
8562intel_check_plane_mapping(struct intel_crtc *crtc)
8563{
8564 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8565 u32 reg, val;
8566
8567 if (dev_priv->num_pipe == 1)
8568 return true;
8569
8570 reg = DSPCNTR(!crtc->plane);
8571 val = I915_READ(reg);
8572
8573 if ((val & DISPLAY_PLANE_ENABLE) &&
8574 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
8575 return false;
8576
8577 return true;
8578}
8579
Daniel Vetter24929352012-07-02 20:28:59 +02008580static void intel_sanitize_crtc(struct intel_crtc *crtc)
8581{
8582 struct drm_device *dev = crtc->base.dev;
8583 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02008584 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02008585
Daniel Vetter24929352012-07-02 20:28:59 +02008586 /* Clear any frame start delays used for debugging left by the BIOS */
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008587 reg = PIPECONF(crtc->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02008588 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
8589
8590 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02008591 * disable the crtc (and hence change the state) if it is wrong. Note
8592 * that gen4+ has a fixed plane -> pipe mapping. */
8593 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02008594 struct intel_connector *connector;
8595 bool plane;
8596
Daniel Vetter24929352012-07-02 20:28:59 +02008597 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
8598 crtc->base.base.id);
8599
8600 /* Pipe has the wrong plane attached and the plane is active.
8601 * Temporarily change the plane mapping and disable everything
8602 * ... */
8603 plane = crtc->plane;
8604 crtc->plane = !plane;
8605 dev_priv->display.crtc_disable(&crtc->base);
8606 crtc->plane = plane;
8607
8608 /* ... and break all links. */
8609 list_for_each_entry(connector, &dev->mode_config.connector_list,
8610 base.head) {
8611 if (connector->encoder->base.crtc != &crtc->base)
8612 continue;
8613
8614 intel_connector_break_all_links(connector);
8615 }
8616
8617 WARN_ON(crtc->active);
8618 crtc->base.enabled = false;
8619 }
Daniel Vetter24929352012-07-02 20:28:59 +02008620
Daniel Vetter7fad7982012-07-04 17:51:47 +02008621 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
8622 crtc->pipe == PIPE_A && !crtc->active) {
8623 /* BIOS forgot to enable pipe A, this mostly happens after
8624 * resume. Force-enable the pipe to fix this, the update_dpms
8625 * call below we restore the pipe to the right state, but leave
8626 * the required bits on. */
8627 intel_enable_pipe_a(dev);
8628 }
8629
Daniel Vetter24929352012-07-02 20:28:59 +02008630 /* Adjust the state of the output pipe according to whether we
8631 * have active connectors/encoders. */
8632 intel_crtc_update_dpms(&crtc->base);
8633
8634 if (crtc->active != crtc->base.enabled) {
8635 struct intel_encoder *encoder;
8636
8637 /* This can happen either due to bugs in the get_hw_state
8638 * functions or because the pipe is force-enabled due to the
8639 * pipe A quirk. */
8640 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
8641 crtc->base.base.id,
8642 crtc->base.enabled ? "enabled" : "disabled",
8643 crtc->active ? "enabled" : "disabled");
8644
8645 crtc->base.enabled = crtc->active;
8646
8647 /* Because we only establish the connector -> encoder ->
8648 * crtc links if something is active, this means the
8649 * crtc is now deactivated. Break the links. connector
8650 * -> encoder links are only establish when things are
8651 * actually up, hence no need to break them. */
8652 WARN_ON(crtc->active);
8653
8654 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
8655 WARN_ON(encoder->connectors_active);
8656 encoder->base.crtc = NULL;
8657 }
8658 }
8659}
8660
8661static void intel_sanitize_encoder(struct intel_encoder *encoder)
8662{
8663 struct intel_connector *connector;
8664 struct drm_device *dev = encoder->base.dev;
8665
8666 /* We need to check both for a crtc link (meaning that the
8667 * encoder is active and trying to read from a pipe) and the
8668 * pipe itself being active. */
8669 bool has_active_crtc = encoder->base.crtc &&
8670 to_intel_crtc(encoder->base.crtc)->active;
8671
8672 if (encoder->connectors_active && !has_active_crtc) {
8673 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
8674 encoder->base.base.id,
8675 drm_get_encoder_name(&encoder->base));
8676
8677 /* Connector is active, but has no active pipe. This is
8678 * fallout from our resume register restoring. Disable
8679 * the encoder manually again. */
8680 if (encoder->base.crtc) {
8681 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
8682 encoder->base.base.id,
8683 drm_get_encoder_name(&encoder->base));
8684 encoder->disable(encoder);
8685 }
8686
8687 /* Inconsistent output/port/pipe state happens presumably due to
8688 * a bug in one of the get_hw_state functions. Or someplace else
8689 * in our code, like the register restore mess on resume. Clamp
8690 * things to off as a safer default. */
8691 list_for_each_entry(connector,
8692 &dev->mode_config.connector_list,
8693 base.head) {
8694 if (connector->encoder != encoder)
8695 continue;
8696
8697 intel_connector_break_all_links(connector);
8698 }
8699 }
8700 /* Enabled encoders without active connectors will be fixed in
8701 * the crtc fixup. */
8702}
8703
8704/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
8705 * and i915 state tracking structures. */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01008706void intel_modeset_setup_hw_state(struct drm_device *dev,
8707 bool force_restore)
Daniel Vetter24929352012-07-02 20:28:59 +02008708{
8709 struct drm_i915_private *dev_priv = dev->dev_private;
8710 enum pipe pipe;
8711 u32 tmp;
8712 struct intel_crtc *crtc;
8713 struct intel_encoder *encoder;
8714 struct intel_connector *connector;
8715
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008716 if (HAS_DDI(dev)) {
Paulo Zanonie28d54c2012-10-24 16:09:25 -02008717 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8718
8719 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8720 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8721 case TRANS_DDI_EDP_INPUT_A_ON:
8722 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8723 pipe = PIPE_A;
8724 break;
8725 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8726 pipe = PIPE_B;
8727 break;
8728 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8729 pipe = PIPE_C;
8730 break;
8731 }
8732
8733 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8734 crtc->cpu_transcoder = TRANSCODER_EDP;
8735
8736 DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
8737 pipe_name(pipe));
8738 }
8739 }
8740
Daniel Vetter24929352012-07-02 20:28:59 +02008741 for_each_pipe(pipe) {
8742 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8743
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008744 tmp = I915_READ(PIPECONF(crtc->cpu_transcoder));
Daniel Vetter24929352012-07-02 20:28:59 +02008745 if (tmp & PIPECONF_ENABLE)
8746 crtc->active = true;
8747 else
8748 crtc->active = false;
8749
8750 crtc->base.enabled = crtc->active;
8751
8752 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
8753 crtc->base.base.id,
8754 crtc->active ? "enabled" : "disabled");
8755 }
8756
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008757 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008758 intel_ddi_setup_hw_pll_state(dev);
8759
Daniel Vetter24929352012-07-02 20:28:59 +02008760 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8761 base.head) {
8762 pipe = 0;
8763
8764 if (encoder->get_hw_state(encoder, &pipe)) {
8765 encoder->base.crtc =
8766 dev_priv->pipe_to_crtc_mapping[pipe];
8767 } else {
8768 encoder->base.crtc = NULL;
8769 }
8770
8771 encoder->connectors_active = false;
8772 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
8773 encoder->base.base.id,
8774 drm_get_encoder_name(&encoder->base),
8775 encoder->base.crtc ? "enabled" : "disabled",
8776 pipe);
8777 }
8778
8779 list_for_each_entry(connector, &dev->mode_config.connector_list,
8780 base.head) {
8781 if (connector->get_hw_state(connector)) {
8782 connector->base.dpms = DRM_MODE_DPMS_ON;
8783 connector->encoder->connectors_active = true;
8784 connector->base.encoder = &connector->encoder->base;
8785 } else {
8786 connector->base.dpms = DRM_MODE_DPMS_OFF;
8787 connector->base.encoder = NULL;
8788 }
8789 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
8790 connector->base.base.id,
8791 drm_get_connector_name(&connector->base),
8792 connector->base.encoder ? "enabled" : "disabled");
8793 }
8794
8795 /* HW state is read out, now we need to sanitize this mess. */
8796 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8797 base.head) {
8798 intel_sanitize_encoder(encoder);
8799 }
8800
8801 for_each_pipe(pipe) {
8802 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8803 intel_sanitize_crtc(crtc);
8804 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008805
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01008806 if (force_restore) {
8807 for_each_pipe(pipe) {
8808 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8809 intel_set_mode(&crtc->base, &crtc->base.mode,
8810 crtc->base.x, crtc->base.y, crtc->base.fb);
8811 }
8812 } else {
8813 intel_modeset_update_staged_output_state(dev);
8814 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008815
8816 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +02008817
8818 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01008819}
8820
8821void intel_modeset_gem_init(struct drm_device *dev)
8822{
Chris Wilson1833b132012-05-09 11:56:28 +01008823 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02008824
8825 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +02008826
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01008827 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08008828}
8829
8830void intel_modeset_cleanup(struct drm_device *dev)
8831{
Jesse Barnes652c3932009-08-17 13:31:43 -07008832 struct drm_i915_private *dev_priv = dev->dev_private;
8833 struct drm_crtc *crtc;
8834 struct intel_crtc *intel_crtc;
8835
Keith Packardf87ea762010-10-03 19:36:26 -07008836 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07008837 mutex_lock(&dev->struct_mutex);
8838
Jesse Barnes723bfd72010-10-07 16:01:13 -07008839 intel_unregister_dsm_handler();
8840
8841
Jesse Barnes652c3932009-08-17 13:31:43 -07008842 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8843 /* Skip inactive CRTCs */
8844 if (!crtc->fb)
8845 continue;
8846
8847 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02008848 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008849 }
8850
Chris Wilson973d04f2011-07-08 12:22:37 +01008851 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07008852
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008853 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00008854
Daniel Vetter930ebb42012-06-29 23:32:16 +02008855 ironlake_teardown_rc6(dev);
8856
Jesse Barnes57f350b2012-03-28 13:39:25 -07008857 if (IS_VALLEYVIEW(dev))
8858 vlv_init_dpio(dev);
8859
Kristian Høgsberg69341a52009-11-11 12:19:17 -05008860 mutex_unlock(&dev->struct_mutex);
8861
Daniel Vetter6c0d93502010-08-20 18:26:46 +02008862 /* Disable the irq before mode object teardown, for the irq might
8863 * enqueue unpin/hotplug work. */
8864 drm_irq_uninstall(dev);
8865 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02008866 cancel_work_sync(&dev_priv->rps.work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02008867
Chris Wilson1630fe72011-07-08 12:22:42 +01008868 /* flush any delayed tasks or pending work */
8869 flush_scheduled_work();
8870
Jesse Barnes79e53942008-11-07 14:24:08 -08008871 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +01008872
8873 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008874}
8875
Dave Airlie28d52042009-09-21 14:33:58 +10008876/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08008877 * Return which encoder is currently attached for connector.
8878 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01008879struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08008880{
Chris Wilsondf0e9242010-09-09 16:20:55 +01008881 return &intel_attached_encoder(connector)->base;
8882}
Jesse Barnes79e53942008-11-07 14:24:08 -08008883
Chris Wilsondf0e9242010-09-09 16:20:55 +01008884void intel_connector_attach_encoder(struct intel_connector *connector,
8885 struct intel_encoder *encoder)
8886{
8887 connector->encoder = encoder;
8888 drm_mode_connector_attach_encoder(&connector->base,
8889 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008890}
Dave Airlie28d52042009-09-21 14:33:58 +10008891
8892/*
8893 * set vga decode state - true == enable VGA decode
8894 */
8895int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
8896{
8897 struct drm_i915_private *dev_priv = dev->dev_private;
8898 u16 gmch_ctrl;
8899
8900 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
8901 if (state)
8902 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
8903 else
8904 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
8905 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
8906 return 0;
8907}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008908
8909#ifdef CONFIG_DEBUG_FS
8910#include <linux/seq_file.h>
8911
8912struct intel_display_error_state {
8913 struct intel_cursor_error_state {
8914 u32 control;
8915 u32 position;
8916 u32 base;
8917 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +01008918 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008919
8920 struct intel_pipe_error_state {
8921 u32 conf;
8922 u32 source;
8923
8924 u32 htotal;
8925 u32 hblank;
8926 u32 hsync;
8927 u32 vtotal;
8928 u32 vblank;
8929 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +01008930 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008931
8932 struct intel_plane_error_state {
8933 u32 control;
8934 u32 stride;
8935 u32 size;
8936 u32 pos;
8937 u32 addr;
8938 u32 surface;
8939 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +01008940 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008941};
8942
8943struct intel_display_error_state *
8944intel_display_capture_error_state(struct drm_device *dev)
8945{
Akshay Joshi0206e352011-08-16 15:34:10 -04008946 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008947 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008948 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008949 int i;
8950
8951 error = kmalloc(sizeof(*error), GFP_ATOMIC);
8952 if (error == NULL)
8953 return NULL;
8954
Damien Lespiau52331302012-08-15 19:23:25 +01008955 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008956 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
8957
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008958 error->cursor[i].control = I915_READ(CURCNTR(i));
8959 error->cursor[i].position = I915_READ(CURPOS(i));
8960 error->cursor[i].base = I915_READ(CURBASE(i));
8961
8962 error->plane[i].control = I915_READ(DSPCNTR(i));
8963 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
8964 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04008965 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008966 error->plane[i].addr = I915_READ(DSPADDR(i));
8967 if (INTEL_INFO(dev)->gen >= 4) {
8968 error->plane[i].surface = I915_READ(DSPSURF(i));
8969 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
8970 }
8971
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008972 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008973 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008974 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
8975 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
8976 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
8977 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
8978 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
8979 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008980 }
8981
8982 return error;
8983}
8984
8985void
8986intel_display_print_error_state(struct seq_file *m,
8987 struct drm_device *dev,
8988 struct intel_display_error_state *error)
8989{
Damien Lespiau52331302012-08-15 19:23:25 +01008990 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008991 int i;
8992
Damien Lespiau52331302012-08-15 19:23:25 +01008993 seq_printf(m, "Num Pipes: %d\n", dev_priv->num_pipe);
8994 for_each_pipe(i) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00008995 seq_printf(m, "Pipe [%d]:\n", i);
8996 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
8997 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
8998 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
8999 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9000 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9001 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9002 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9003 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9004
9005 seq_printf(m, "Plane [%d]:\n", i);
9006 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9007 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9008 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
9009 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9010 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9011 if (INTEL_INFO(dev)->gen >= 4) {
9012 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9013 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9014 }
9015
9016 seq_printf(m, "Cursor [%d]:\n", i);
9017 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9018 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9019 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9020 }
9021}
9022#endif