blob: 44f9d8f73a205c92268f94a387a9e2364280824f [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
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001492intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value,
1493 enum intel_sbi_destination destination)
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001494{
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001495 u32 tmp;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001496
Daniel Vetter09153002012-12-12 14:06:44 +01001497 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001498
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001499 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001500 100)) {
1501 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001502 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001503 }
1504
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001505 I915_WRITE(SBI_ADDR, (reg << 16));
1506 I915_WRITE(SBI_DATA, value);
1507
1508 if (destination == SBI_ICLK)
1509 tmp = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRWR;
1510 else
1511 tmp = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IOWR;
1512 I915_WRITE(SBI_CTL_STAT, SBI_BUSY | tmp);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001513
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001514 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001515 100)) {
1516 DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001517 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001518 }
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001519}
1520
1521static u32
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001522intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg,
1523 enum intel_sbi_destination destination)
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001524{
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001525 u32 value = 0;
Daniel Vetter09153002012-12-12 14:06:44 +01001526 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001527
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001528 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001529 100)) {
1530 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001531 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001532 }
1533
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001534 I915_WRITE(SBI_ADDR, (reg << 16));
1535
1536 if (destination == SBI_ICLK)
1537 value = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRRD;
1538 else
1539 value = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IORD;
1540 I915_WRITE(SBI_CTL_STAT, value | SBI_BUSY);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001541
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001542 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001543 100)) {
1544 DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001545 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001546 }
1547
Daniel Vetter09153002012-12-12 14:06:44 +01001548 return I915_READ(SBI_DATA);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001549}
1550
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001551/**
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001552 * ironlake_enable_pch_pll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001553 * @dev_priv: i915 private structure
1554 * @pipe: pipe PLL to enable
1555 *
1556 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1557 * drives the transcoder clock.
1558 */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001559static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001560{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001561 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Chris Wilson48da64a2012-05-13 20:16:12 +01001562 struct intel_pch_pll *pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001563 int reg;
1564 u32 val;
1565
Chris Wilson48da64a2012-05-13 20:16:12 +01001566 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001567 BUG_ON(dev_priv->info->gen < 5);
Chris Wilson48da64a2012-05-13 20:16:12 +01001568 pll = intel_crtc->pch_pll;
1569 if (pll == NULL)
1570 return;
1571
1572 if (WARN_ON(pll->refcount == 0))
1573 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001574
1575 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1576 pll->pll_reg, pll->active, pll->on,
1577 intel_crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001578
1579 /* PCH refclock must be enabled first */
1580 assert_pch_refclk_enabled(dev_priv);
1581
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001582 if (pll->active++ && pll->on) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001583 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001584 return;
1585 }
1586
1587 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1588
1589 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001590 val = I915_READ(reg);
1591 val |= DPLL_VCO_ENABLE;
1592 I915_WRITE(reg, val);
1593 POSTING_READ(reg);
1594 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001595
1596 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001597}
1598
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001599static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001600{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001601 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1602 struct intel_pch_pll *pll = intel_crtc->pch_pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001603 int reg;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001604 u32 val;
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001605
Jesse Barnes92f25842011-01-04 15:09:34 -08001606 /* PCH only available on ILK+ */
1607 BUG_ON(dev_priv->info->gen < 5);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001608 if (pll == NULL)
1609 return;
1610
Chris Wilson48da64a2012-05-13 20:16:12 +01001611 if (WARN_ON(pll->refcount == 0))
1612 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001613
1614 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1615 pll->pll_reg, pll->active, pll->on,
1616 intel_crtc->base.base.id);
1617
Chris Wilson48da64a2012-05-13 20:16:12 +01001618 if (WARN_ON(pll->active == 0)) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001619 assert_pch_pll_disabled(dev_priv, pll, NULL);
Chris Wilson48da64a2012-05-13 20:16:12 +01001620 return;
1621 }
1622
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001623 if (--pll->active) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001624 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001625 return;
1626 }
1627
1628 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
Jesse Barnes92f25842011-01-04 15:09:34 -08001629
1630 /* Make sure transcoder isn't still depending on us */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001631 assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001632
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001633 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001634 val = I915_READ(reg);
1635 val &= ~DPLL_VCO_ENABLE;
1636 I915_WRITE(reg, val);
1637 POSTING_READ(reg);
1638 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001639
1640 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001641}
1642
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001643static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1644 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001645{
Daniel Vetter23670b322012-11-01 09:15:30 +01001646 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001647 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetter23670b322012-11-01 09:15:30 +01001648 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001649
1650 /* PCH only available on ILK+ */
1651 BUG_ON(dev_priv->info->gen < 5);
1652
1653 /* Make sure PCH DPLL is enabled */
Chris Wilson92b27b02012-05-20 18:10:50 +01001654 assert_pch_pll_enabled(dev_priv,
1655 to_intel_crtc(crtc)->pch_pll,
1656 to_intel_crtc(crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001657
1658 /* FDI must be feeding us bits for PCH ports */
1659 assert_fdi_tx_enabled(dev_priv, pipe);
1660 assert_fdi_rx_enabled(dev_priv, pipe);
1661
Daniel Vetter23670b322012-11-01 09:15:30 +01001662 if (HAS_PCH_CPT(dev)) {
1663 /* Workaround: Set the timing override bit before enabling the
1664 * pch transcoder. */
1665 reg = TRANS_CHICKEN2(pipe);
1666 val = I915_READ(reg);
1667 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1668 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001669 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001670
Jesse Barnes040484a2011-01-03 12:14:26 -08001671 reg = TRANSCONF(pipe);
1672 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001673 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001674
1675 if (HAS_PCH_IBX(dev_priv->dev)) {
1676 /*
1677 * make the BPC in transcoder be consistent with
1678 * that in pipeconf reg.
1679 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001680 val &= ~PIPECONF_BPC_MASK;
1681 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001682 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001683
1684 val &= ~TRANS_INTERLACE_MASK;
1685 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001686 if (HAS_PCH_IBX(dev_priv->dev) &&
1687 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1688 val |= TRANS_LEGACY_INTERLACED_ILK;
1689 else
1690 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001691 else
1692 val |= TRANS_PROGRESSIVE;
1693
Jesse Barnes040484a2011-01-03 12:14:26 -08001694 I915_WRITE(reg, val | TRANS_ENABLE);
1695 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1696 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1697}
1698
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001699static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001700 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001701{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001702 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001703
1704 /* PCH only available on ILK+ */
1705 BUG_ON(dev_priv->info->gen < 5);
1706
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001707 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001708 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001709 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001710
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001711 /* Workaround: set timing override bit. */
1712 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001713 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001714 I915_WRITE(_TRANSA_CHICKEN2, val);
1715
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001716 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001717 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001718
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001719 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1720 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001721 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001722 else
1723 val |= TRANS_PROGRESSIVE;
1724
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001725 I915_WRITE(TRANSCONF(TRANSCODER_A), val);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001726 if (wait_for(I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE, 100))
1727 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001728}
1729
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001730static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1731 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001732{
Daniel Vetter23670b322012-11-01 09:15:30 +01001733 struct drm_device *dev = dev_priv->dev;
1734 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001735
1736 /* FDI relies on the transcoder */
1737 assert_fdi_tx_disabled(dev_priv, pipe);
1738 assert_fdi_rx_disabled(dev_priv, pipe);
1739
Jesse Barnes291906f2011-02-02 12:28:03 -08001740 /* Ports must be off as well */
1741 assert_pch_ports_disabled(dev_priv, pipe);
1742
Jesse Barnes040484a2011-01-03 12:14:26 -08001743 reg = TRANSCONF(pipe);
1744 val = I915_READ(reg);
1745 val &= ~TRANS_ENABLE;
1746 I915_WRITE(reg, val);
1747 /* wait for PCH transcoder off, transcoder state */
1748 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes4c9c18c2011-10-13 09:46:32 -07001749 DRM_ERROR("failed to disable transcoder %d\n", pipe);
Daniel Vetter23670b322012-11-01 09:15:30 +01001750
1751 if (!HAS_PCH_IBX(dev)) {
1752 /* Workaround: Clear the timing override chicken bit again. */
1753 reg = TRANS_CHICKEN2(pipe);
1754 val = I915_READ(reg);
1755 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1756 I915_WRITE(reg, val);
1757 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001758}
1759
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001760static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001761{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001762 u32 val;
1763
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001764 val = I915_READ(_TRANSACONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001765 val &= ~TRANS_ENABLE;
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001766 I915_WRITE(_TRANSACONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001767 /* wait for PCH transcoder off, transcoder state */
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001768 if (wait_for((I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE) == 0, 50))
1769 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001770
1771 /* Workaround: clear timing override bit. */
1772 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001773 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001774 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001775}
1776
1777/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001778 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001779 * @dev_priv: i915 private structure
1780 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001781 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001782 *
1783 * Enable @pipe, making sure that various hardware specific requirements
1784 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1785 *
1786 * @pipe should be %PIPE_A or %PIPE_B.
1787 *
1788 * Will wait until the pipe is actually running (i.e. first vblank) before
1789 * returning.
1790 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001791static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1792 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001793{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001794 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1795 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001796 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001797 int reg;
1798 u32 val;
1799
Paulo Zanoni681e5812012-12-06 11:12:38 -02001800 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001801 pch_transcoder = TRANSCODER_A;
1802 else
1803 pch_transcoder = pipe;
1804
Jesse Barnesb24e7172011-01-04 15:09:30 -08001805 /*
1806 * A pipe without a PLL won't actually be able to drive bits from
1807 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1808 * need the check.
1809 */
1810 if (!HAS_PCH_SPLIT(dev_priv->dev))
1811 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001812 else {
1813 if (pch_port) {
1814 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001815 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001816 assert_fdi_tx_pll_enabled(dev_priv,
1817 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001818 }
1819 /* FIXME: assert CPU port conditions for SNB+ */
1820 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001821
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001822 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001823 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001824 if (val & PIPECONF_ENABLE)
1825 return;
1826
1827 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001828 intel_wait_for_vblank(dev_priv->dev, pipe);
1829}
1830
1831/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001832 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001833 * @dev_priv: i915 private structure
1834 * @pipe: pipe to disable
1835 *
1836 * Disable @pipe, making sure that various hardware specific requirements
1837 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1838 *
1839 * @pipe should be %PIPE_A or %PIPE_B.
1840 *
1841 * Will wait until the pipe has shut down before returning.
1842 */
1843static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1844 enum pipe pipe)
1845{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001846 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1847 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001848 int reg;
1849 u32 val;
1850
1851 /*
1852 * Make sure planes won't keep trying to pump pixels to us,
1853 * or we might hang the display.
1854 */
1855 assert_planes_disabled(dev_priv, pipe);
1856
1857 /* Don't disable pipe A or pipe A PLLs if needed */
1858 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1859 return;
1860
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001861 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001862 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001863 if ((val & PIPECONF_ENABLE) == 0)
1864 return;
1865
1866 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001867 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1868}
1869
Keith Packardd74362c2011-07-28 14:47:14 -07001870/*
1871 * Plane regs are double buffered, going from enabled->disabled needs a
1872 * trigger in order to latch. The display address reg provides this.
1873 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001874void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001875 enum plane plane)
1876{
Damien Lespiau14f86142012-10-29 15:24:49 +00001877 if (dev_priv->info->gen >= 4)
1878 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1879 else
1880 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001881}
1882
Jesse Barnesb24e7172011-01-04 15:09:30 -08001883/**
1884 * intel_enable_plane - enable a display plane on a given pipe
1885 * @dev_priv: i915 private structure
1886 * @plane: plane to enable
1887 * @pipe: pipe being fed
1888 *
1889 * Enable @plane on @pipe, making sure that @pipe is running first.
1890 */
1891static void intel_enable_plane(struct drm_i915_private *dev_priv,
1892 enum plane plane, enum pipe pipe)
1893{
1894 int reg;
1895 u32 val;
1896
1897 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1898 assert_pipe_enabled(dev_priv, pipe);
1899
1900 reg = DSPCNTR(plane);
1901 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001902 if (val & DISPLAY_PLANE_ENABLE)
1903 return;
1904
1905 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001906 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001907 intel_wait_for_vblank(dev_priv->dev, pipe);
1908}
1909
Jesse Barnesb24e7172011-01-04 15:09:30 -08001910/**
1911 * intel_disable_plane - disable a display plane
1912 * @dev_priv: i915 private structure
1913 * @plane: plane to disable
1914 * @pipe: pipe consuming the data
1915 *
1916 * Disable @plane; should be an independent operation.
1917 */
1918static void intel_disable_plane(struct drm_i915_private *dev_priv,
1919 enum plane plane, enum pipe pipe)
1920{
1921 int reg;
1922 u32 val;
1923
1924 reg = DSPCNTR(plane);
1925 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001926 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1927 return;
1928
1929 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001930 intel_flush_display_plane(dev_priv, plane);
1931 intel_wait_for_vblank(dev_priv->dev, pipe);
1932}
1933
Chris Wilson127bd2a2010-07-23 23:32:05 +01001934int
Chris Wilson48b956c2010-09-14 12:50:34 +01001935intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001936 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001937 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001938{
Chris Wilsonce453d82011-02-21 14:43:56 +00001939 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001940 u32 alignment;
1941 int ret;
1942
Chris Wilson05394f32010-11-08 19:18:58 +00001943 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001944 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001945 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1946 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001947 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001948 alignment = 4 * 1024;
1949 else
1950 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001951 break;
1952 case I915_TILING_X:
1953 /* pin() will align the object as required by fence */
1954 alignment = 0;
1955 break;
1956 case I915_TILING_Y:
1957 /* FIXME: Is this true? */
1958 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1959 return -EINVAL;
1960 default:
1961 BUG();
1962 }
1963
Chris Wilsonce453d82011-02-21 14:43:56 +00001964 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001965 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001966 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001967 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001968
1969 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1970 * fence, whereas 965+ only requires a fence if using
1971 * framebuffer compression. For simplicity, we always install
1972 * a fence as the cost is not that onerous.
1973 */
Chris Wilson06d98132012-04-17 15:31:24 +01001974 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001975 if (ret)
1976 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001977
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001978 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001979
Chris Wilsonce453d82011-02-21 14:43:56 +00001980 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001981 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001982
1983err_unpin:
1984 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001985err_interruptible:
1986 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001987 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001988}
1989
Chris Wilson1690e1e2011-12-14 13:57:08 +01001990void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1991{
1992 i915_gem_object_unpin_fence(obj);
1993 i915_gem_object_unpin(obj);
1994}
1995
Daniel Vetterc2c75132012-07-05 12:17:30 +02001996/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1997 * is assumed to be a power-of-two. */
Damien Lespiau5a35e992012-10-26 18:20:12 +01001998unsigned long intel_gen4_compute_offset_xtiled(int *x, int *y,
1999 unsigned int bpp,
2000 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002001{
2002 int tile_rows, tiles;
2003
2004 tile_rows = *y / 8;
2005 *y %= 8;
2006 tiles = *x / (512/bpp);
2007 *x %= 512/bpp;
2008
2009 return tile_rows * pitch * 8 + tiles * 4096;
2010}
2011
Jesse Barnes17638cd2011-06-24 12:19:23 -07002012static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2013 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002014{
2015 struct drm_device *dev = crtc->dev;
2016 struct drm_i915_private *dev_priv = dev->dev_private;
2017 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2018 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002019 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002020 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002021 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002022 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002023 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002024
2025 switch (plane) {
2026 case 0:
2027 case 1:
2028 break;
2029 default:
2030 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2031 return -EINVAL;
2032 }
2033
2034 intel_fb = to_intel_framebuffer(fb);
2035 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002036
Chris Wilson5eddb702010-09-11 13:48:45 +01002037 reg = DSPCNTR(plane);
2038 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002039 /* Mask out pixel format bits in case we change it */
2040 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002041 switch (fb->pixel_format) {
2042 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002043 dspcntr |= DISPPLANE_8BPP;
2044 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002045 case DRM_FORMAT_XRGB1555:
2046 case DRM_FORMAT_ARGB1555:
2047 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002048 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002049 case DRM_FORMAT_RGB565:
2050 dspcntr |= DISPPLANE_BGRX565;
2051 break;
2052 case DRM_FORMAT_XRGB8888:
2053 case DRM_FORMAT_ARGB8888:
2054 dspcntr |= DISPPLANE_BGRX888;
2055 break;
2056 case DRM_FORMAT_XBGR8888:
2057 case DRM_FORMAT_ABGR8888:
2058 dspcntr |= DISPPLANE_RGBX888;
2059 break;
2060 case DRM_FORMAT_XRGB2101010:
2061 case DRM_FORMAT_ARGB2101010:
2062 dspcntr |= DISPPLANE_BGRX101010;
2063 break;
2064 case DRM_FORMAT_XBGR2101010:
2065 case DRM_FORMAT_ABGR2101010:
2066 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002067 break;
2068 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002069 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes81255562010-08-02 12:07:50 -07002070 return -EINVAL;
2071 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002072
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002073 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002074 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002075 dspcntr |= DISPPLANE_TILED;
2076 else
2077 dspcntr &= ~DISPPLANE_TILED;
2078 }
2079
Chris Wilson5eddb702010-09-11 13:48:45 +01002080 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002081
Daniel Vettere506a0c2012-07-05 12:17:29 +02002082 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002083
Daniel Vetterc2c75132012-07-05 12:17:30 +02002084 if (INTEL_INFO(dev)->gen >= 4) {
2085 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002086 intel_gen4_compute_offset_xtiled(&x, &y,
2087 fb->bits_per_pixel / 8,
2088 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002089 linear_offset -= intel_crtc->dspaddr_offset;
2090 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002091 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002092 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002093
2094 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2095 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002096 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002097 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002098 I915_MODIFY_DISPBASE(DSPSURF(plane),
2099 obj->gtt_offset + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002100 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002101 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002102 } else
Daniel Vettere506a0c2012-07-05 12:17:29 +02002103 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002104 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002105
Jesse Barnes17638cd2011-06-24 12:19:23 -07002106 return 0;
2107}
2108
2109static int ironlake_update_plane(struct drm_crtc *crtc,
2110 struct drm_framebuffer *fb, int x, int y)
2111{
2112 struct drm_device *dev = crtc->dev;
2113 struct drm_i915_private *dev_priv = dev->dev_private;
2114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2115 struct intel_framebuffer *intel_fb;
2116 struct drm_i915_gem_object *obj;
2117 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002118 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002119 u32 dspcntr;
2120 u32 reg;
2121
2122 switch (plane) {
2123 case 0:
2124 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002125 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002126 break;
2127 default:
2128 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2129 return -EINVAL;
2130 }
2131
2132 intel_fb = to_intel_framebuffer(fb);
2133 obj = intel_fb->obj;
2134
2135 reg = DSPCNTR(plane);
2136 dspcntr = I915_READ(reg);
2137 /* Mask out pixel format bits in case we change it */
2138 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002139 switch (fb->pixel_format) {
2140 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002141 dspcntr |= DISPPLANE_8BPP;
2142 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002143 case DRM_FORMAT_RGB565:
2144 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002145 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002146 case DRM_FORMAT_XRGB8888:
2147 case DRM_FORMAT_ARGB8888:
2148 dspcntr |= DISPPLANE_BGRX888;
2149 break;
2150 case DRM_FORMAT_XBGR8888:
2151 case DRM_FORMAT_ABGR8888:
2152 dspcntr |= DISPPLANE_RGBX888;
2153 break;
2154 case DRM_FORMAT_XRGB2101010:
2155 case DRM_FORMAT_ARGB2101010:
2156 dspcntr |= DISPPLANE_BGRX101010;
2157 break;
2158 case DRM_FORMAT_XBGR2101010:
2159 case DRM_FORMAT_ABGR2101010:
2160 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002161 break;
2162 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002163 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002164 return -EINVAL;
2165 }
2166
2167 if (obj->tiling_mode != I915_TILING_NONE)
2168 dspcntr |= DISPPLANE_TILED;
2169 else
2170 dspcntr &= ~DISPPLANE_TILED;
2171
2172 /* must disable */
2173 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2174
2175 I915_WRITE(reg, dspcntr);
2176
Daniel Vettere506a0c2012-07-05 12:17:29 +02002177 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002178 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002179 intel_gen4_compute_offset_xtiled(&x, &y,
2180 fb->bits_per_pixel / 8,
2181 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002182 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002183
Daniel Vettere506a0c2012-07-05 12:17:29 +02002184 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2185 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002186 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002187 I915_MODIFY_DISPBASE(DSPSURF(plane),
2188 obj->gtt_offset + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002189 if (IS_HASWELL(dev)) {
2190 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2191 } else {
2192 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2193 I915_WRITE(DSPLINOFF(plane), linear_offset);
2194 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002195 POSTING_READ(reg);
2196
2197 return 0;
2198}
2199
2200/* Assume fb object is pinned & idle & fenced and just update base pointers */
2201static int
2202intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2203 int x, int y, enum mode_set_atomic state)
2204{
2205 struct drm_device *dev = crtc->dev;
2206 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002207
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002208 if (dev_priv->display.disable_fbc)
2209 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002210 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002211
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002212 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002213}
2214
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002215static int
Chris Wilson14667a42012-04-03 17:58:35 +01002216intel_finish_fb(struct drm_framebuffer *old_fb)
2217{
2218 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2219 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2220 bool was_interruptible = dev_priv->mm.interruptible;
2221 int ret;
2222
Daniel Vetter2c10d572012-12-20 21:24:07 +01002223 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2224
Chris Wilson14667a42012-04-03 17:58:35 +01002225 wait_event(dev_priv->pending_flip_queue,
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002226 i915_reset_in_progress(&dev_priv->gpu_error) ||
Chris Wilson14667a42012-04-03 17:58:35 +01002227 atomic_read(&obj->pending_flip) == 0);
2228
2229 /* Big Hammer, we also need to ensure that any pending
2230 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2231 * current scanout is retired before unpinning the old
2232 * framebuffer.
2233 *
2234 * This should only fail upon a hung GPU, in which case we
2235 * can safely continue.
2236 */
2237 dev_priv->mm.interruptible = false;
2238 ret = i915_gem_object_finish_gpu(obj);
2239 dev_priv->mm.interruptible = was_interruptible;
2240
2241 return ret;
2242}
2243
Ville Syrjälä198598d2012-10-31 17:50:24 +02002244static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2245{
2246 struct drm_device *dev = crtc->dev;
2247 struct drm_i915_master_private *master_priv;
2248 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2249
2250 if (!dev->primary->master)
2251 return;
2252
2253 master_priv = dev->primary->master->driver_priv;
2254 if (!master_priv->sarea_priv)
2255 return;
2256
2257 switch (intel_crtc->pipe) {
2258 case 0:
2259 master_priv->sarea_priv->pipeA_x = x;
2260 master_priv->sarea_priv->pipeA_y = y;
2261 break;
2262 case 1:
2263 master_priv->sarea_priv->pipeB_x = x;
2264 master_priv->sarea_priv->pipeB_y = y;
2265 break;
2266 default:
2267 break;
2268 }
2269}
2270
Chris Wilson14667a42012-04-03 17:58:35 +01002271static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002272intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002273 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002274{
2275 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002276 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002278 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002279 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002280
2281 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002282 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002283 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002284 return 0;
2285 }
2286
Eugeni Dodonov5826eca2012-05-09 15:37:12 -03002287 if(intel_crtc->plane > dev_priv->num_pipe) {
2288 DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
2289 intel_crtc->plane,
2290 dev_priv->num_pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002291 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002292 }
2293
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002294 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002295 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002296 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002297 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002298 if (ret != 0) {
2299 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002300 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002301 return ret;
2302 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002303
Daniel Vetter94352cf2012-07-05 22:51:56 +02002304 if (crtc->fb)
2305 intel_finish_fb(crtc->fb);
Chris Wilson265db952010-09-20 15:41:01 +01002306
Daniel Vetter94352cf2012-07-05 22:51:56 +02002307 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002308 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002309 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002310 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002311 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002312 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002313 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002314
Daniel Vetter94352cf2012-07-05 22:51:56 +02002315 old_fb = crtc->fb;
2316 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002317 crtc->x = x;
2318 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002319
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002320 if (old_fb) {
2321 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002322 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002323 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002324
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002325 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002326 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002327
Ville Syrjälä198598d2012-10-31 17:50:24 +02002328 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002329
2330 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002331}
2332
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002333static void intel_fdi_normal_train(struct drm_crtc *crtc)
2334{
2335 struct drm_device *dev = crtc->dev;
2336 struct drm_i915_private *dev_priv = dev->dev_private;
2337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2338 int pipe = intel_crtc->pipe;
2339 u32 reg, temp;
2340
2341 /* enable normal train */
2342 reg = FDI_TX_CTL(pipe);
2343 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002344 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002345 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2346 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002347 } else {
2348 temp &= ~FDI_LINK_TRAIN_NONE;
2349 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002350 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002351 I915_WRITE(reg, temp);
2352
2353 reg = FDI_RX_CTL(pipe);
2354 temp = I915_READ(reg);
2355 if (HAS_PCH_CPT(dev)) {
2356 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2357 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2358 } else {
2359 temp &= ~FDI_LINK_TRAIN_NONE;
2360 temp |= FDI_LINK_TRAIN_NONE;
2361 }
2362 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2363
2364 /* wait one idle pattern time */
2365 POSTING_READ(reg);
2366 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002367
2368 /* IVB wants error correction enabled */
2369 if (IS_IVYBRIDGE(dev))
2370 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2371 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002372}
2373
Daniel Vetter01a415f2012-10-27 15:58:40 +02002374static void ivb_modeset_global_resources(struct drm_device *dev)
2375{
2376 struct drm_i915_private *dev_priv = dev->dev_private;
2377 struct intel_crtc *pipe_B_crtc =
2378 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2379 struct intel_crtc *pipe_C_crtc =
2380 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2381 uint32_t temp;
2382
2383 /* When everything is off disable fdi C so that we could enable fdi B
2384 * with all lanes. XXX: This misses the case where a pipe is not using
2385 * any pch resources and so doesn't need any fdi lanes. */
2386 if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
2387 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2388 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2389
2390 temp = I915_READ(SOUTH_CHICKEN1);
2391 temp &= ~FDI_BC_BIFURCATION_SELECT;
2392 DRM_DEBUG_KMS("disabling fdi C rx\n");
2393 I915_WRITE(SOUTH_CHICKEN1, temp);
2394 }
2395}
2396
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002397/* The FDI link training functions for ILK/Ibexpeak. */
2398static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2399{
2400 struct drm_device *dev = crtc->dev;
2401 struct drm_i915_private *dev_priv = dev->dev_private;
2402 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2403 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002404 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002405 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002406
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002407 /* FDI needs bits from pipe & plane first */
2408 assert_pipe_enabled(dev_priv, pipe);
2409 assert_plane_enabled(dev_priv, plane);
2410
Adam Jacksone1a44742010-06-25 15:32:14 -04002411 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2412 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002413 reg = FDI_RX_IMR(pipe);
2414 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002415 temp &= ~FDI_RX_SYMBOL_LOCK;
2416 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002417 I915_WRITE(reg, temp);
2418 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002419 udelay(150);
2420
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002421 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002422 reg = FDI_TX_CTL(pipe);
2423 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002424 temp &= ~(7 << 19);
2425 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002426 temp &= ~FDI_LINK_TRAIN_NONE;
2427 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002428 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002429
Chris Wilson5eddb702010-09-11 13:48:45 +01002430 reg = FDI_RX_CTL(pipe);
2431 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002432 temp &= ~FDI_LINK_TRAIN_NONE;
2433 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002434 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2435
2436 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002437 udelay(150);
2438
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002439 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002440 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2441 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2442 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002443
Chris Wilson5eddb702010-09-11 13:48:45 +01002444 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002445 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002446 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002447 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2448
2449 if ((temp & FDI_RX_BIT_LOCK)) {
2450 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002451 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002452 break;
2453 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002454 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002455 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002456 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002457
2458 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002459 reg = FDI_TX_CTL(pipe);
2460 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002461 temp &= ~FDI_LINK_TRAIN_NONE;
2462 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002463 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002464
Chris Wilson5eddb702010-09-11 13:48:45 +01002465 reg = FDI_RX_CTL(pipe);
2466 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002467 temp &= ~FDI_LINK_TRAIN_NONE;
2468 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002469 I915_WRITE(reg, temp);
2470
2471 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002472 udelay(150);
2473
Chris Wilson5eddb702010-09-11 13:48:45 +01002474 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002475 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002476 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002477 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2478
2479 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002480 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002481 DRM_DEBUG_KMS("FDI train 2 done.\n");
2482 break;
2483 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002484 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002485 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002486 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002487
2488 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002489
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002490}
2491
Akshay Joshi0206e352011-08-16 15:34:10 -04002492static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002493 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2494 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2495 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2496 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2497};
2498
2499/* The FDI link training functions for SNB/Cougarpoint. */
2500static void gen6_fdi_link_train(struct drm_crtc *crtc)
2501{
2502 struct drm_device *dev = crtc->dev;
2503 struct drm_i915_private *dev_priv = dev->dev_private;
2504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2505 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002506 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002507
Adam Jacksone1a44742010-06-25 15:32:14 -04002508 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2509 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002510 reg = FDI_RX_IMR(pipe);
2511 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002512 temp &= ~FDI_RX_SYMBOL_LOCK;
2513 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002514 I915_WRITE(reg, temp);
2515
2516 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002517 udelay(150);
2518
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002519 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002520 reg = FDI_TX_CTL(pipe);
2521 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002522 temp &= ~(7 << 19);
2523 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002524 temp &= ~FDI_LINK_TRAIN_NONE;
2525 temp |= FDI_LINK_TRAIN_PATTERN_1;
2526 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2527 /* SNB-B */
2528 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002529 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002530
Daniel Vetterd74cf322012-10-26 10:58:13 +02002531 I915_WRITE(FDI_RX_MISC(pipe),
2532 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2533
Chris Wilson5eddb702010-09-11 13:48:45 +01002534 reg = FDI_RX_CTL(pipe);
2535 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002536 if (HAS_PCH_CPT(dev)) {
2537 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2538 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2539 } else {
2540 temp &= ~FDI_LINK_TRAIN_NONE;
2541 temp |= FDI_LINK_TRAIN_PATTERN_1;
2542 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002543 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2544
2545 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002546 udelay(150);
2547
Akshay Joshi0206e352011-08-16 15:34:10 -04002548 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002549 reg = FDI_TX_CTL(pipe);
2550 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002551 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2552 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002553 I915_WRITE(reg, temp);
2554
2555 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002556 udelay(500);
2557
Sean Paulfa37d392012-03-02 12:53:39 -05002558 for (retry = 0; retry < 5; retry++) {
2559 reg = FDI_RX_IIR(pipe);
2560 temp = I915_READ(reg);
2561 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2562 if (temp & FDI_RX_BIT_LOCK) {
2563 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2564 DRM_DEBUG_KMS("FDI train 1 done.\n");
2565 break;
2566 }
2567 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002568 }
Sean Paulfa37d392012-03-02 12:53:39 -05002569 if (retry < 5)
2570 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002571 }
2572 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002573 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002574
2575 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002576 reg = FDI_TX_CTL(pipe);
2577 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002578 temp &= ~FDI_LINK_TRAIN_NONE;
2579 temp |= FDI_LINK_TRAIN_PATTERN_2;
2580 if (IS_GEN6(dev)) {
2581 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2582 /* SNB-B */
2583 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2584 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002585 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002586
Chris Wilson5eddb702010-09-11 13:48:45 +01002587 reg = FDI_RX_CTL(pipe);
2588 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002589 if (HAS_PCH_CPT(dev)) {
2590 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2591 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2592 } else {
2593 temp &= ~FDI_LINK_TRAIN_NONE;
2594 temp |= FDI_LINK_TRAIN_PATTERN_2;
2595 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002596 I915_WRITE(reg, temp);
2597
2598 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002599 udelay(150);
2600
Akshay Joshi0206e352011-08-16 15:34:10 -04002601 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002602 reg = FDI_TX_CTL(pipe);
2603 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002604 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2605 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002606 I915_WRITE(reg, temp);
2607
2608 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002609 udelay(500);
2610
Sean Paulfa37d392012-03-02 12:53:39 -05002611 for (retry = 0; retry < 5; retry++) {
2612 reg = FDI_RX_IIR(pipe);
2613 temp = I915_READ(reg);
2614 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2615 if (temp & FDI_RX_SYMBOL_LOCK) {
2616 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2617 DRM_DEBUG_KMS("FDI train 2 done.\n");
2618 break;
2619 }
2620 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002621 }
Sean Paulfa37d392012-03-02 12:53:39 -05002622 if (retry < 5)
2623 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002624 }
2625 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002626 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002627
2628 DRM_DEBUG_KMS("FDI train done.\n");
2629}
2630
Jesse Barnes357555c2011-04-28 15:09:55 -07002631/* Manual link training for Ivy Bridge A0 parts */
2632static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2633{
2634 struct drm_device *dev = crtc->dev;
2635 struct drm_i915_private *dev_priv = dev->dev_private;
2636 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2637 int pipe = intel_crtc->pipe;
2638 u32 reg, temp, i;
2639
2640 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2641 for train result */
2642 reg = FDI_RX_IMR(pipe);
2643 temp = I915_READ(reg);
2644 temp &= ~FDI_RX_SYMBOL_LOCK;
2645 temp &= ~FDI_RX_BIT_LOCK;
2646 I915_WRITE(reg, temp);
2647
2648 POSTING_READ(reg);
2649 udelay(150);
2650
Daniel Vetter01a415f2012-10-27 15:58:40 +02002651 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2652 I915_READ(FDI_RX_IIR(pipe)));
2653
Jesse Barnes357555c2011-04-28 15:09:55 -07002654 /* enable CPU FDI TX and PCH FDI RX */
2655 reg = FDI_TX_CTL(pipe);
2656 temp = I915_READ(reg);
2657 temp &= ~(7 << 19);
2658 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2659 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2660 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2661 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2662 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002663 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002664 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2665
Daniel Vetterd74cf322012-10-26 10:58:13 +02002666 I915_WRITE(FDI_RX_MISC(pipe),
2667 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2668
Jesse Barnes357555c2011-04-28 15:09:55 -07002669 reg = FDI_RX_CTL(pipe);
2670 temp = I915_READ(reg);
2671 temp &= ~FDI_LINK_TRAIN_AUTO;
2672 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2673 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002674 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002675 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2676
2677 POSTING_READ(reg);
2678 udelay(150);
2679
Akshay Joshi0206e352011-08-16 15:34:10 -04002680 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002681 reg = FDI_TX_CTL(pipe);
2682 temp = I915_READ(reg);
2683 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2684 temp |= snb_b_fdi_train_param[i];
2685 I915_WRITE(reg, temp);
2686
2687 POSTING_READ(reg);
2688 udelay(500);
2689
2690 reg = FDI_RX_IIR(pipe);
2691 temp = I915_READ(reg);
2692 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2693
2694 if (temp & FDI_RX_BIT_LOCK ||
2695 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2696 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002697 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002698 break;
2699 }
2700 }
2701 if (i == 4)
2702 DRM_ERROR("FDI train 1 fail!\n");
2703
2704 /* Train 2 */
2705 reg = FDI_TX_CTL(pipe);
2706 temp = I915_READ(reg);
2707 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2708 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2709 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2710 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2711 I915_WRITE(reg, temp);
2712
2713 reg = FDI_RX_CTL(pipe);
2714 temp = I915_READ(reg);
2715 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2716 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2717 I915_WRITE(reg, temp);
2718
2719 POSTING_READ(reg);
2720 udelay(150);
2721
Akshay Joshi0206e352011-08-16 15:34:10 -04002722 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002723 reg = FDI_TX_CTL(pipe);
2724 temp = I915_READ(reg);
2725 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2726 temp |= snb_b_fdi_train_param[i];
2727 I915_WRITE(reg, temp);
2728
2729 POSTING_READ(reg);
2730 udelay(500);
2731
2732 reg = FDI_RX_IIR(pipe);
2733 temp = I915_READ(reg);
2734 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2735
2736 if (temp & FDI_RX_SYMBOL_LOCK) {
2737 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002738 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002739 break;
2740 }
2741 }
2742 if (i == 4)
2743 DRM_ERROR("FDI train 2 fail!\n");
2744
2745 DRM_DEBUG_KMS("FDI train done.\n");
2746}
2747
Daniel Vetter88cefb62012-08-12 19:27:14 +02002748static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002749{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002750 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002751 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002752 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002753 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002754
Jesse Barnesc64e3112010-09-10 11:27:03 -07002755
Jesse Barnes0e23b992010-09-10 11:10:00 -07002756 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002757 reg = FDI_RX_CTL(pipe);
2758 temp = I915_READ(reg);
2759 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002760 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002761 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002762 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2763
2764 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002765 udelay(200);
2766
2767 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002768 temp = I915_READ(reg);
2769 I915_WRITE(reg, temp | FDI_PCDCLK);
2770
2771 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002772 udelay(200);
2773
Paulo Zanoni20749732012-11-23 15:30:38 -02002774 /* Enable CPU FDI TX PLL, always on for Ironlake */
2775 reg = FDI_TX_CTL(pipe);
2776 temp = I915_READ(reg);
2777 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2778 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002779
Paulo Zanoni20749732012-11-23 15:30:38 -02002780 POSTING_READ(reg);
2781 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002782 }
2783}
2784
Daniel Vetter88cefb62012-08-12 19:27:14 +02002785static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2786{
2787 struct drm_device *dev = intel_crtc->base.dev;
2788 struct drm_i915_private *dev_priv = dev->dev_private;
2789 int pipe = intel_crtc->pipe;
2790 u32 reg, temp;
2791
2792 /* Switch from PCDclk to Rawclk */
2793 reg = FDI_RX_CTL(pipe);
2794 temp = I915_READ(reg);
2795 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2796
2797 /* Disable CPU FDI TX PLL */
2798 reg = FDI_TX_CTL(pipe);
2799 temp = I915_READ(reg);
2800 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2801
2802 POSTING_READ(reg);
2803 udelay(100);
2804
2805 reg = FDI_RX_CTL(pipe);
2806 temp = I915_READ(reg);
2807 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2808
2809 /* Wait for the clocks to turn off. */
2810 POSTING_READ(reg);
2811 udelay(100);
2812}
2813
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002814static void ironlake_fdi_disable(struct drm_crtc *crtc)
2815{
2816 struct drm_device *dev = crtc->dev;
2817 struct drm_i915_private *dev_priv = dev->dev_private;
2818 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2819 int pipe = intel_crtc->pipe;
2820 u32 reg, temp;
2821
2822 /* disable CPU FDI tx and PCH FDI rx */
2823 reg = FDI_TX_CTL(pipe);
2824 temp = I915_READ(reg);
2825 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2826 POSTING_READ(reg);
2827
2828 reg = FDI_RX_CTL(pipe);
2829 temp = I915_READ(reg);
2830 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002831 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002832 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2833
2834 POSTING_READ(reg);
2835 udelay(100);
2836
2837 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002838 if (HAS_PCH_IBX(dev)) {
2839 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002840 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002841
2842 /* still set train pattern 1 */
2843 reg = FDI_TX_CTL(pipe);
2844 temp = I915_READ(reg);
2845 temp &= ~FDI_LINK_TRAIN_NONE;
2846 temp |= FDI_LINK_TRAIN_PATTERN_1;
2847 I915_WRITE(reg, temp);
2848
2849 reg = FDI_RX_CTL(pipe);
2850 temp = I915_READ(reg);
2851 if (HAS_PCH_CPT(dev)) {
2852 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2853 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2854 } else {
2855 temp &= ~FDI_LINK_TRAIN_NONE;
2856 temp |= FDI_LINK_TRAIN_PATTERN_1;
2857 }
2858 /* BPC in FDI rx is consistent with that in PIPECONF */
2859 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002860 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002861 I915_WRITE(reg, temp);
2862
2863 POSTING_READ(reg);
2864 udelay(100);
2865}
2866
Chris Wilson5bb61642012-09-27 21:25:58 +01002867static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2868{
2869 struct drm_device *dev = crtc->dev;
2870 struct drm_i915_private *dev_priv = dev->dev_private;
2871 unsigned long flags;
2872 bool pending;
2873
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002874 if (i915_reset_in_progress(&dev_priv->gpu_error))
Chris Wilson5bb61642012-09-27 21:25:58 +01002875 return false;
2876
2877 spin_lock_irqsave(&dev->event_lock, flags);
2878 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2879 spin_unlock_irqrestore(&dev->event_lock, flags);
2880
2881 return pending;
2882}
2883
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002884static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2885{
Chris Wilson0f911282012-04-17 10:05:38 +01002886 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002887 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002888
2889 if (crtc->fb == NULL)
2890 return;
2891
Daniel Vetter2c10d572012-12-20 21:24:07 +01002892 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2893
Chris Wilson5bb61642012-09-27 21:25:58 +01002894 wait_event(dev_priv->pending_flip_queue,
2895 !intel_crtc_has_pending_flip(crtc));
2896
Chris Wilson0f911282012-04-17 10:05:38 +01002897 mutex_lock(&dev->struct_mutex);
2898 intel_finish_fb(crtc->fb);
2899 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002900}
2901
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002902static bool ironlake_crtc_driving_pch(struct drm_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08002903{
2904 struct drm_device *dev = crtc->dev;
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002905 struct intel_encoder *intel_encoder;
Jesse Barnes040484a2011-01-03 12:14:26 -08002906
2907 /*
2908 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2909 * must be driven by its own crtc; no sharing is possible.
2910 */
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002911 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002912 switch (intel_encoder->type) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002913 case INTEL_OUTPUT_EDP:
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002914 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnes040484a2011-01-03 12:14:26 -08002915 return false;
2916 continue;
2917 }
2918 }
2919
2920 return true;
2921}
2922
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002923static bool haswell_crtc_driving_pch(struct drm_crtc *crtc)
2924{
2925 return intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG);
2926}
2927
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002928/* Program iCLKIP clock to the desired frequency */
2929static void lpt_program_iclkip(struct drm_crtc *crtc)
2930{
2931 struct drm_device *dev = crtc->dev;
2932 struct drm_i915_private *dev_priv = dev->dev_private;
2933 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2934 u32 temp;
2935
Daniel Vetter09153002012-12-12 14:06:44 +01002936 mutex_lock(&dev_priv->dpio_lock);
2937
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002938 /* It is necessary to ungate the pixclk gate prior to programming
2939 * the divisors, and gate it back when it is done.
2940 */
2941 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2942
2943 /* Disable SSCCTL */
2944 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002945 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2946 SBI_SSCCTL_DISABLE,
2947 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002948
2949 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2950 if (crtc->mode.clock == 20000) {
2951 auxdiv = 1;
2952 divsel = 0x41;
2953 phaseinc = 0x20;
2954 } else {
2955 /* The iCLK virtual clock root frequency is in MHz,
2956 * but the crtc->mode.clock in in KHz. To get the divisors,
2957 * it is necessary to divide one by another, so we
2958 * convert the virtual clock precision to KHz here for higher
2959 * precision.
2960 */
2961 u32 iclk_virtual_root_freq = 172800 * 1000;
2962 u32 iclk_pi_range = 64;
2963 u32 desired_divisor, msb_divisor_value, pi_value;
2964
2965 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2966 msb_divisor_value = desired_divisor / iclk_pi_range;
2967 pi_value = desired_divisor % iclk_pi_range;
2968
2969 auxdiv = 0;
2970 divsel = msb_divisor_value - 2;
2971 phaseinc = pi_value;
2972 }
2973
2974 /* This should not happen with any sane values */
2975 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2976 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2977 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2978 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2979
2980 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2981 crtc->mode.clock,
2982 auxdiv,
2983 divsel,
2984 phasedir,
2985 phaseinc);
2986
2987 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002988 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002989 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2990 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2991 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2992 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2993 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2994 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002995 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002996
2997 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002998 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002999 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3000 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003001 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003002
3003 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003004 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003005 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003006 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003007
3008 /* Wait for initialization time */
3009 udelay(24);
3010
3011 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003012
3013 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003014}
3015
Jesse Barnesf67a5592011-01-05 10:31:48 -08003016/*
3017 * Enable PCH resources required for PCH ports:
3018 * - PCH PLLs
3019 * - FDI training & RX/TX
3020 * - update transcoder timings
3021 * - DP transcoding bits
3022 * - transcoder
3023 */
3024static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003025{
3026 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003027 struct drm_i915_private *dev_priv = dev->dev_private;
3028 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3029 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003030 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003031
Chris Wilsone7e164d2012-05-11 09:21:25 +01003032 assert_transcoder_disabled(dev_priv, pipe);
3033
Daniel Vettercd986ab2012-10-26 10:58:12 +02003034 /* Write the TU size bits before fdi link training, so that error
3035 * detection works. */
3036 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3037 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3038
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003039 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003040 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003041
Daniel Vetter572deb32012-10-27 18:46:14 +02003042 /* XXX: pch pll's can be enabled any time before we enable the PCH
3043 * transcoder, and we actually should do this to not upset any PCH
3044 * transcoder that already use the clock when we share it.
3045 *
3046 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
3047 * unconditionally resets the pll - we need that to have the right LVDS
3048 * enable sequence. */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02003049 ironlake_enable_pch_pll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01003050
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003051 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003052 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003053
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003054 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003055 switch (pipe) {
3056 default:
3057 case 0:
3058 temp |= TRANSA_DPLL_ENABLE;
3059 sel = TRANSA_DPLLB_SEL;
3060 break;
3061 case 1:
3062 temp |= TRANSB_DPLL_ENABLE;
3063 sel = TRANSB_DPLLB_SEL;
3064 break;
3065 case 2:
3066 temp |= TRANSC_DPLL_ENABLE;
3067 sel = TRANSC_DPLLB_SEL;
3068 break;
Jesse Barnesd64311a2011-10-12 15:01:33 -07003069 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003070 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3071 temp |= sel;
3072 else
3073 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003074 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003075 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003076
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003077 /* set transcoder timing, panel must allow it */
3078 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003079 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3080 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3081 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3082
3083 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3084 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3085 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003086 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003087
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003088 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003089
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003090 /* For PCH DP, enable TRANS_DP_CTL */
3091 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003092 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3093 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003094 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003095 reg = TRANS_DP_CTL(pipe);
3096 temp = I915_READ(reg);
3097 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003098 TRANS_DP_SYNC_MASK |
3099 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003100 temp |= (TRANS_DP_OUTPUT_ENABLE |
3101 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003102 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003103
3104 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003105 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003106 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003107 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003108
3109 switch (intel_trans_dp_port_sel(crtc)) {
3110 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003111 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003112 break;
3113 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003114 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003115 break;
3116 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003117 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003118 break;
3119 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003120 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003121 }
3122
Chris Wilson5eddb702010-09-11 13:48:45 +01003123 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003124 }
3125
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003126 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003127}
3128
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003129static void lpt_pch_enable(struct drm_crtc *crtc)
3130{
3131 struct drm_device *dev = crtc->dev;
3132 struct drm_i915_private *dev_priv = dev->dev_private;
3133 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003134 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003135
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003136 assert_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003137
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003138 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003139
Paulo Zanoni0540e482012-10-31 18:12:40 -02003140 /* Set transcoder timing. */
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003141 I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
3142 I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
3143 I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003144
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003145 I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
3146 I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
3147 I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
3148 I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003149
Paulo Zanoni937bb612012-10-31 18:12:47 -02003150 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003151}
3152
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003153static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3154{
3155 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3156
3157 if (pll == NULL)
3158 return;
3159
3160 if (pll->refcount == 0) {
3161 WARN(1, "bad PCH PLL refcount\n");
3162 return;
3163 }
3164
3165 --pll->refcount;
3166 intel_crtc->pch_pll = NULL;
3167}
3168
3169static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3170{
3171 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3172 struct intel_pch_pll *pll;
3173 int i;
3174
3175 pll = intel_crtc->pch_pll;
3176 if (pll) {
3177 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3178 intel_crtc->base.base.id, pll->pll_reg);
3179 goto prepare;
3180 }
3181
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003182 if (HAS_PCH_IBX(dev_priv->dev)) {
3183 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3184 i = intel_crtc->pipe;
3185 pll = &dev_priv->pch_plls[i];
3186
3187 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3188 intel_crtc->base.base.id, pll->pll_reg);
3189
3190 goto found;
3191 }
3192
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003193 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3194 pll = &dev_priv->pch_plls[i];
3195
3196 /* Only want to check enabled timings first */
3197 if (pll->refcount == 0)
3198 continue;
3199
3200 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3201 fp == I915_READ(pll->fp0_reg)) {
3202 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3203 intel_crtc->base.base.id,
3204 pll->pll_reg, pll->refcount, pll->active);
3205
3206 goto found;
3207 }
3208 }
3209
3210 /* Ok no matching timings, maybe there's a free one? */
3211 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3212 pll = &dev_priv->pch_plls[i];
3213 if (pll->refcount == 0) {
3214 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3215 intel_crtc->base.base.id, pll->pll_reg);
3216 goto found;
3217 }
3218 }
3219
3220 return NULL;
3221
3222found:
3223 intel_crtc->pch_pll = pll;
3224 pll->refcount++;
3225 DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
3226prepare: /* separate function? */
3227 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003228
Chris Wilsone04c7352012-05-02 20:43:56 +01003229 /* Wait for the clocks to stabilize before rewriting the regs */
3230 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003231 POSTING_READ(pll->pll_reg);
3232 udelay(150);
Chris Wilsone04c7352012-05-02 20:43:56 +01003233
3234 I915_WRITE(pll->fp0_reg, fp);
3235 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003236 pll->on = false;
3237 return pll;
3238}
3239
Jesse Barnesd4270e52011-10-11 10:43:02 -07003240void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3241{
3242 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003243 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003244 u32 temp;
3245
3246 temp = I915_READ(dslreg);
3247 udelay(500);
3248 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003249 if (wait_for(I915_READ(dslreg) != temp, 5))
3250 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3251 }
3252}
3253
Jesse Barnesf67a5592011-01-05 10:31:48 -08003254static void ironlake_crtc_enable(struct drm_crtc *crtc)
3255{
3256 struct drm_device *dev = crtc->dev;
3257 struct drm_i915_private *dev_priv = dev->dev_private;
3258 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003259 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003260 int pipe = intel_crtc->pipe;
3261 int plane = intel_crtc->plane;
3262 u32 temp;
3263 bool is_pch_port;
3264
Daniel Vetter08a48462012-07-02 11:43:47 +02003265 WARN_ON(!crtc->enabled);
3266
Jesse Barnesf67a5592011-01-05 10:31:48 -08003267 if (intel_crtc->active)
3268 return;
3269
3270 intel_crtc->active = true;
3271 intel_update_watermarks(dev);
3272
3273 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3274 temp = I915_READ(PCH_LVDS);
3275 if ((temp & LVDS_PORT_EN) == 0)
3276 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3277 }
3278
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003279 is_pch_port = ironlake_crtc_driving_pch(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003280
Daniel Vetter46b6f812012-09-06 22:08:33 +02003281 if (is_pch_port) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003282 /* Note: FDI PLL enabling _must_ be done before we enable the
3283 * cpu pipes, hence this is separate from all the other fdi/pch
3284 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003285 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003286 } else {
3287 assert_fdi_tx_disabled(dev_priv, pipe);
3288 assert_fdi_rx_disabled(dev_priv, pipe);
3289 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003290
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003291 for_each_encoder_on_crtc(dev, crtc, encoder)
3292 if (encoder->pre_enable)
3293 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003294
3295 /* Enable panel fitting for LVDS */
3296 if (dev_priv->pch_pf_size &&
Jani Nikula547dc042012-11-02 11:24:03 +02003297 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3298 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnesf67a5592011-01-05 10:31:48 -08003299 /* Force use of hard-coded filter coefficients
3300 * as some pre-programmed values are broken,
3301 * e.g. x201.
3302 */
Paulo Zanoni13888d72012-11-20 13:27:41 -02003303 if (IS_IVYBRIDGE(dev))
3304 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3305 PF_PIPE_SEL_IVB(pipe));
3306 else
3307 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003308 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3309 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003310 }
3311
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003312 /*
3313 * On ILK+ LUT must be loaded before the pipe is running but with
3314 * clocks enabled
3315 */
3316 intel_crtc_load_lut(crtc);
3317
Jesse Barnesf67a5592011-01-05 10:31:48 -08003318 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3319 intel_enable_plane(dev_priv, plane, pipe);
3320
3321 if (is_pch_port)
3322 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003323
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003324 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003325 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003326 mutex_unlock(&dev->struct_mutex);
3327
Chris Wilson6b383a72010-09-13 13:54:26 +01003328 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003329
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003330 for_each_encoder_on_crtc(dev, crtc, encoder)
3331 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003332
3333 if (HAS_PCH_CPT(dev))
3334 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003335
3336 /*
3337 * There seems to be a race in PCH platform hw (at least on some
3338 * outputs) where an enabled pipe still completes any pageflip right
3339 * away (as if the pipe is off) instead of waiting for vblank. As soon
3340 * as the first vblank happend, everything works as expected. Hence just
3341 * wait for one vblank before returning to avoid strange things
3342 * happening.
3343 */
3344 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003345}
3346
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003347static void haswell_crtc_enable(struct drm_crtc *crtc)
3348{
3349 struct drm_device *dev = crtc->dev;
3350 struct drm_i915_private *dev_priv = dev->dev_private;
3351 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3352 struct intel_encoder *encoder;
3353 int pipe = intel_crtc->pipe;
3354 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003355 bool is_pch_port;
3356
3357 WARN_ON(!crtc->enabled);
3358
3359 if (intel_crtc->active)
3360 return;
3361
3362 intel_crtc->active = true;
3363 intel_update_watermarks(dev);
3364
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003365 is_pch_port = haswell_crtc_driving_pch(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003366
Paulo Zanoni83616632012-10-23 18:29:54 -02003367 if (is_pch_port)
Paulo Zanoni04945642012-11-01 21:00:59 -02003368 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003369
3370 for_each_encoder_on_crtc(dev, crtc, encoder)
3371 if (encoder->pre_enable)
3372 encoder->pre_enable(encoder);
3373
Paulo Zanoni1f544382012-10-24 11:32:00 -02003374 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003375
Paulo Zanoni1f544382012-10-24 11:32:00 -02003376 /* Enable panel fitting for eDP */
Jani Nikula547dc042012-11-02 11:24:03 +02003377 if (dev_priv->pch_pf_size &&
3378 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003379 /* Force use of hard-coded filter coefficients
3380 * as some pre-programmed values are broken,
3381 * e.g. x201.
3382 */
Paulo Zanoni54075a72012-11-20 13:27:42 -02003383 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3384 PF_PIPE_SEL_IVB(pipe));
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003385 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3386 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3387 }
3388
3389 /*
3390 * On ILK+ LUT must be loaded before the pipe is running but with
3391 * clocks enabled
3392 */
3393 intel_crtc_load_lut(crtc);
3394
Paulo Zanoni1f544382012-10-24 11:32:00 -02003395 intel_ddi_set_pipe_settings(crtc);
3396 intel_ddi_enable_pipe_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003397
3398 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3399 intel_enable_plane(dev_priv, plane, pipe);
3400
3401 if (is_pch_port)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003402 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003403
3404 mutex_lock(&dev->struct_mutex);
3405 intel_update_fbc(dev);
3406 mutex_unlock(&dev->struct_mutex);
3407
3408 intel_crtc_update_cursor(crtc, true);
3409
3410 for_each_encoder_on_crtc(dev, crtc, encoder)
3411 encoder->enable(encoder);
3412
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003413 /*
3414 * There seems to be a race in PCH platform hw (at least on some
3415 * outputs) where an enabled pipe still completes any pageflip right
3416 * away (as if the pipe is off) instead of waiting for vblank. As soon
3417 * as the first vblank happend, everything works as expected. Hence just
3418 * wait for one vblank before returning to avoid strange things
3419 * happening.
3420 */
3421 intel_wait_for_vblank(dev, intel_crtc->pipe);
3422}
3423
Jesse Barnes6be4a602010-09-10 10:26:01 -07003424static void ironlake_crtc_disable(struct drm_crtc *crtc)
3425{
3426 struct drm_device *dev = crtc->dev;
3427 struct drm_i915_private *dev_priv = dev->dev_private;
3428 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003429 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003430 int pipe = intel_crtc->pipe;
3431 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003432 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003433
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003434
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003435 if (!intel_crtc->active)
3436 return;
3437
Daniel Vetterea9d7582012-07-10 10:42:52 +02003438 for_each_encoder_on_crtc(dev, crtc, encoder)
3439 encoder->disable(encoder);
3440
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003441 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003442 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003443 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003444
Jesse Barnesb24e7172011-01-04 15:09:30 -08003445 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003446
Chris Wilson973d04f2011-07-08 12:22:37 +01003447 if (dev_priv->cfb_plane == plane)
3448 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003449
Jesse Barnesb24e7172011-01-04 15:09:30 -08003450 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003451
Jesse Barnes6be4a602010-09-10 10:26:01 -07003452 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003453 I915_WRITE(PF_CTL(pipe), 0);
3454 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003455
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003456 for_each_encoder_on_crtc(dev, crtc, encoder)
3457 if (encoder->post_disable)
3458 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003459
Chris Wilson5eddb702010-09-11 13:48:45 +01003460 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003461
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003462 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003463
3464 if (HAS_PCH_CPT(dev)) {
3465 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 reg = TRANS_DP_CTL(pipe);
3467 temp = I915_READ(reg);
3468 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003469 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003471
3472 /* disable DPLL_SEL */
3473 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003474 switch (pipe) {
3475 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003476 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003477 break;
3478 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003479 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003480 break;
3481 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003482 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003483 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003484 break;
3485 default:
3486 BUG(); /* wtf */
3487 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003488 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003489 }
3490
3491 /* disable PCH DPLL */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003492 intel_disable_pch_pll(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003493
Daniel Vetter88cefb62012-08-12 19:27:14 +02003494 ironlake_fdi_pll_disable(intel_crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +01003495
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003496 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003497 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003498
3499 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003500 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003501 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003502}
3503
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003504static void haswell_crtc_disable(struct drm_crtc *crtc)
3505{
3506 struct drm_device *dev = crtc->dev;
3507 struct drm_i915_private *dev_priv = dev->dev_private;
3508 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3509 struct intel_encoder *encoder;
3510 int pipe = intel_crtc->pipe;
3511 int plane = intel_crtc->plane;
Paulo Zanoniad80a812012-10-24 16:06:19 -02003512 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni83616632012-10-23 18:29:54 -02003513 bool is_pch_port;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003514
3515 if (!intel_crtc->active)
3516 return;
3517
Paulo Zanoni83616632012-10-23 18:29:54 -02003518 is_pch_port = haswell_crtc_driving_pch(crtc);
3519
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003520 for_each_encoder_on_crtc(dev, crtc, encoder)
3521 encoder->disable(encoder);
3522
3523 intel_crtc_wait_for_pending_flips(crtc);
3524 drm_vblank_off(dev, pipe);
3525 intel_crtc_update_cursor(crtc, false);
3526
3527 intel_disable_plane(dev_priv, plane, pipe);
3528
3529 if (dev_priv->cfb_plane == plane)
3530 intel_disable_fbc(dev);
3531
3532 intel_disable_pipe(dev_priv, pipe);
3533
Paulo Zanoniad80a812012-10-24 16:06:19 -02003534 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003535
3536 /* Disable PF */
3537 I915_WRITE(PF_CTL(pipe), 0);
3538 I915_WRITE(PF_WIN_SZ(pipe), 0);
3539
Paulo Zanoni1f544382012-10-24 11:32:00 -02003540 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003541
3542 for_each_encoder_on_crtc(dev, crtc, encoder)
3543 if (encoder->post_disable)
3544 encoder->post_disable(encoder);
3545
Paulo Zanoni83616632012-10-23 18:29:54 -02003546 if (is_pch_port) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003547 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003548 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003549 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003550
3551 intel_crtc->active = false;
3552 intel_update_watermarks(dev);
3553
3554 mutex_lock(&dev->struct_mutex);
3555 intel_update_fbc(dev);
3556 mutex_unlock(&dev->struct_mutex);
3557}
3558
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003559static void ironlake_crtc_off(struct drm_crtc *crtc)
3560{
3561 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3562 intel_put_pch_pll(intel_crtc);
3563}
3564
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003565static void haswell_crtc_off(struct drm_crtc *crtc)
3566{
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003567 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3568
3569 /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
3570 * start using it. */
Daniel Vetter1a240d42012-11-29 22:18:51 +01003571 intel_crtc->cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003572
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003573 intel_ddi_put_crtc_pll(crtc);
3574}
3575
Daniel Vetter02e792f2009-09-15 22:57:34 +02003576static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3577{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003578 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003579 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003580 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003581
Chris Wilson23f09ce2010-08-12 13:53:37 +01003582 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003583 dev_priv->mm.interruptible = false;
3584 (void) intel_overlay_switch_off(intel_crtc->overlay);
3585 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003586 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003587 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003588
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003589 /* Let userspace switch the overlay on again. In most cases userspace
3590 * has to recompute where to put it anyway.
3591 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003592}
3593
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003594static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003595{
3596 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003597 struct drm_i915_private *dev_priv = dev->dev_private;
3598 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003599 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003600 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003601 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003602
Daniel Vetter08a48462012-07-02 11:43:47 +02003603 WARN_ON(!crtc->enabled);
3604
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003605 if (intel_crtc->active)
3606 return;
3607
3608 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003609 intel_update_watermarks(dev);
3610
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003611 intel_enable_pll(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08003612 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003613 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003614
3615 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003616 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003617
3618 /* Give the overlay scaler a chance to enable if it's on this pipe */
3619 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003620 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003621
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003622 for_each_encoder_on_crtc(dev, crtc, encoder)
3623 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003624}
3625
3626static void i9xx_crtc_disable(struct drm_crtc *crtc)
3627{
3628 struct drm_device *dev = crtc->dev;
3629 struct drm_i915_private *dev_priv = dev->dev_private;
3630 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003631 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003632 int pipe = intel_crtc->pipe;
3633 int plane = intel_crtc->plane;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003634
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003635
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003636 if (!intel_crtc->active)
3637 return;
3638
Daniel Vetterea9d7582012-07-10 10:42:52 +02003639 for_each_encoder_on_crtc(dev, crtc, encoder)
3640 encoder->disable(encoder);
3641
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003642 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003643 intel_crtc_wait_for_pending_flips(crtc);
3644 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003645 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003646 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003647
Chris Wilson973d04f2011-07-08 12:22:37 +01003648 if (dev_priv->cfb_plane == plane)
3649 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003650
Jesse Barnesb24e7172011-01-04 15:09:30 -08003651 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003652 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003653 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003654
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003655 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003656 intel_update_fbc(dev);
3657 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003658}
3659
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003660static void i9xx_crtc_off(struct drm_crtc *crtc)
3661{
3662}
3663
Daniel Vetter976f8a22012-07-08 22:34:21 +02003664static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3665 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003666{
3667 struct drm_device *dev = crtc->dev;
3668 struct drm_i915_master_private *master_priv;
3669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3670 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003671
3672 if (!dev->primary->master)
3673 return;
3674
3675 master_priv = dev->primary->master->driver_priv;
3676 if (!master_priv->sarea_priv)
3677 return;
3678
Jesse Barnes79e53942008-11-07 14:24:08 -08003679 switch (pipe) {
3680 case 0:
3681 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3682 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3683 break;
3684 case 1:
3685 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3686 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3687 break;
3688 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003689 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003690 break;
3691 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003692}
3693
Daniel Vetter976f8a22012-07-08 22:34:21 +02003694/**
3695 * Sets the power management mode of the pipe and plane.
3696 */
3697void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003698{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003699 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003700 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003701 struct intel_encoder *intel_encoder;
3702 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003703
Daniel Vetter976f8a22012-07-08 22:34:21 +02003704 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3705 enable |= intel_encoder->connectors_active;
3706
3707 if (enable)
3708 dev_priv->display.crtc_enable(crtc);
3709 else
3710 dev_priv->display.crtc_disable(crtc);
3711
3712 intel_crtc_update_sarea(crtc, enable);
3713}
3714
3715static void intel_crtc_noop(struct drm_crtc *crtc)
3716{
3717}
3718
3719static void intel_crtc_disable(struct drm_crtc *crtc)
3720{
3721 struct drm_device *dev = crtc->dev;
3722 struct drm_connector *connector;
3723 struct drm_i915_private *dev_priv = dev->dev_private;
3724
3725 /* crtc should still be enabled when we disable it. */
3726 WARN_ON(!crtc->enabled);
3727
3728 dev_priv->display.crtc_disable(crtc);
3729 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003730 dev_priv->display.off(crtc);
3731
Chris Wilson931872f2012-01-16 23:01:13 +00003732 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3733 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003734
3735 if (crtc->fb) {
3736 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003737 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003738 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003739 crtc->fb = NULL;
3740 }
3741
3742 /* Update computed state. */
3743 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3744 if (!connector->encoder || !connector->encoder->crtc)
3745 continue;
3746
3747 if (connector->encoder->crtc != crtc)
3748 continue;
3749
3750 connector->dpms = DRM_MODE_DPMS_OFF;
3751 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003752 }
3753}
3754
Daniel Vettera261b242012-07-26 19:21:47 +02003755void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003756{
Daniel Vettera261b242012-07-26 19:21:47 +02003757 struct drm_crtc *crtc;
3758
3759 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3760 if (crtc->enabled)
3761 intel_crtc_disable(crtc);
3762 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003763}
3764
Daniel Vetter1f703852012-07-11 16:51:39 +02003765void intel_encoder_noop(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003766{
Jesse Barnes79e53942008-11-07 14:24:08 -08003767}
3768
Chris Wilsonea5b2132010-08-04 13:50:23 +01003769void intel_encoder_destroy(struct drm_encoder *encoder)
3770{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003771 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003772
Chris Wilsonea5b2132010-08-04 13:50:23 +01003773 drm_encoder_cleanup(encoder);
3774 kfree(intel_encoder);
3775}
3776
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003777/* Simple dpms helper for encodres with just one connector, no cloning and only
3778 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3779 * state of the entire output pipe. */
3780void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3781{
3782 if (mode == DRM_MODE_DPMS_ON) {
3783 encoder->connectors_active = true;
3784
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003785 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003786 } else {
3787 encoder->connectors_active = false;
3788
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003789 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003790 }
3791}
3792
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003793/* Cross check the actual hw state with our own modeset state tracking (and it's
3794 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003795static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003796{
3797 if (connector->get_hw_state(connector)) {
3798 struct intel_encoder *encoder = connector->encoder;
3799 struct drm_crtc *crtc;
3800 bool encoder_enabled;
3801 enum pipe pipe;
3802
3803 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3804 connector->base.base.id,
3805 drm_get_connector_name(&connector->base));
3806
3807 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3808 "wrong connector dpms state\n");
3809 WARN(connector->base.encoder != &encoder->base,
3810 "active connector not linked to encoder\n");
3811 WARN(!encoder->connectors_active,
3812 "encoder->connectors_active not set\n");
3813
3814 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3815 WARN(!encoder_enabled, "encoder not enabled\n");
3816 if (WARN_ON(!encoder->base.crtc))
3817 return;
3818
3819 crtc = encoder->base.crtc;
3820
3821 WARN(!crtc->enabled, "crtc not enabled\n");
3822 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3823 WARN(pipe != to_intel_crtc(crtc)->pipe,
3824 "encoder active on the wrong pipe\n");
3825 }
3826}
3827
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003828/* Even simpler default implementation, if there's really no special case to
3829 * consider. */
3830void intel_connector_dpms(struct drm_connector *connector, int mode)
3831{
3832 struct intel_encoder *encoder = intel_attached_encoder(connector);
3833
3834 /* All the simple cases only support two dpms states. */
3835 if (mode != DRM_MODE_DPMS_ON)
3836 mode = DRM_MODE_DPMS_OFF;
3837
3838 if (mode == connector->dpms)
3839 return;
3840
3841 connector->dpms = mode;
3842
3843 /* Only need to change hw state when actually enabled */
3844 if (encoder->base.crtc)
3845 intel_encoder_dpms(encoder, mode);
3846 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003847 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003848
Daniel Vetterb9805142012-08-31 17:37:33 +02003849 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003850}
3851
Daniel Vetterf0947c32012-07-02 13:10:34 +02003852/* Simple connector->get_hw_state implementation for encoders that support only
3853 * one connector and no cloning and hence the encoder state determines the state
3854 * of the connector. */
3855bool intel_connector_get_hw_state(struct intel_connector *connector)
3856{
Daniel Vetter24929352012-07-02 20:28:59 +02003857 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003858 struct intel_encoder *encoder = connector->encoder;
3859
3860 return encoder->get_hw_state(encoder, &pipe);
3861}
3862
Jesse Barnes79e53942008-11-07 14:24:08 -08003863static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
Daniel Vetter35313cd2012-07-20 10:30:45 +02003864 const struct drm_display_mode *mode,
Jesse Barnes79e53942008-11-07 14:24:08 -08003865 struct drm_display_mode *adjusted_mode)
3866{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003867 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003868
Eric Anholtbad720f2009-10-22 16:11:14 -07003869 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003870 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003871 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3872 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003873 }
Chris Wilson89749352010-09-12 18:25:19 +01003874
Daniel Vetterf9bef082012-04-15 19:53:19 +02003875 /* All interlaced capable intel hw wants timings in frames. Note though
3876 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3877 * timings, so we need to be careful not to clobber these.*/
3878 if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3879 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003880
Chris Wilson44f46b422012-06-21 13:19:59 +03003881 /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
3882 * with a hsync front porch of 0.
3883 */
3884 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
3885 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
3886 return false;
3887
Jesse Barnes79e53942008-11-07 14:24:08 -08003888 return true;
3889}
3890
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07003891static int valleyview_get_display_clock_speed(struct drm_device *dev)
3892{
3893 return 400000; /* FIXME */
3894}
3895
Jesse Barnese70236a2009-09-21 10:42:27 -07003896static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003897{
Jesse Barnese70236a2009-09-21 10:42:27 -07003898 return 400000;
3899}
Jesse Barnes79e53942008-11-07 14:24:08 -08003900
Jesse Barnese70236a2009-09-21 10:42:27 -07003901static int i915_get_display_clock_speed(struct drm_device *dev)
3902{
3903 return 333000;
3904}
Jesse Barnes79e53942008-11-07 14:24:08 -08003905
Jesse Barnese70236a2009-09-21 10:42:27 -07003906static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3907{
3908 return 200000;
3909}
Jesse Barnes79e53942008-11-07 14:24:08 -08003910
Jesse Barnese70236a2009-09-21 10:42:27 -07003911static int i915gm_get_display_clock_speed(struct drm_device *dev)
3912{
3913 u16 gcfgc = 0;
3914
3915 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3916
3917 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003918 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003919 else {
3920 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3921 case GC_DISPLAY_CLOCK_333_MHZ:
3922 return 333000;
3923 default:
3924 case GC_DISPLAY_CLOCK_190_200_MHZ:
3925 return 190000;
3926 }
3927 }
3928}
Jesse Barnes79e53942008-11-07 14:24:08 -08003929
Jesse Barnese70236a2009-09-21 10:42:27 -07003930static int i865_get_display_clock_speed(struct drm_device *dev)
3931{
3932 return 266000;
3933}
3934
3935static int i855_get_display_clock_speed(struct drm_device *dev)
3936{
3937 u16 hpllcc = 0;
3938 /* Assume that the hardware is in the high speed state. This
3939 * should be the default.
3940 */
3941 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3942 case GC_CLOCK_133_200:
3943 case GC_CLOCK_100_200:
3944 return 200000;
3945 case GC_CLOCK_166_250:
3946 return 250000;
3947 case GC_CLOCK_100_133:
3948 return 133000;
3949 }
3950
3951 /* Shouldn't happen */
3952 return 0;
3953}
3954
3955static int i830_get_display_clock_speed(struct drm_device *dev)
3956{
3957 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08003958}
3959
Zhenyu Wang2c072452009-06-05 15:38:42 +08003960static void
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003961intel_reduce_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003962{
3963 while (*num > 0xffffff || *den > 0xffffff) {
3964 *num >>= 1;
3965 *den >>= 1;
3966 }
3967}
3968
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003969void
3970intel_link_compute_m_n(int bits_per_pixel, int nlanes,
3971 int pixel_clock, int link_clock,
3972 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003973{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003974 m_n->tu = 64;
Chris Wilson22ed1112010-12-04 01:01:29 +00003975 m_n->gmch_m = bits_per_pixel * pixel_clock;
3976 m_n->gmch_n = link_clock * nlanes * 8;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003977 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
Chris Wilson22ed1112010-12-04 01:01:29 +00003978 m_n->link_m = pixel_clock;
3979 m_n->link_n = link_clock;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003980 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08003981}
3982
Chris Wilsona7615032011-01-12 17:04:08 +00003983static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
3984{
Keith Packard72bbe582011-09-26 16:09:45 -07003985 if (i915_panel_use_ssc >= 0)
3986 return i915_panel_use_ssc != 0;
3987 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07003988 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00003989}
3990
Jesse Barnes5a354202011-06-24 12:19:22 -07003991/**
3992 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
3993 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08003994 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07003995 *
3996 * A pipe may be connected to one or more outputs. Based on the depth of the
3997 * attached framebuffer, choose a good color depth to use on the pipe.
3998 *
3999 * If possible, match the pipe depth to the fb depth. In some cases, this
4000 * isn't ideal, because the connected output supports a lesser or restricted
4001 * set of depths. Resolve that here:
4002 * LVDS typically supports only 6bpc, so clamp down in that case
4003 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
4004 * Displays may support a restricted set as well, check EDID and clamp as
4005 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004006 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07004007 *
4008 * RETURNS:
4009 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
4010 * true if they don't match).
4011 */
4012static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004013 struct drm_framebuffer *fb,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004014 unsigned int *pipe_bpp,
4015 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07004016{
4017 struct drm_device *dev = crtc->dev;
4018 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes5a354202011-06-24 12:19:22 -07004019 struct drm_connector *connector;
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004020 struct intel_encoder *intel_encoder;
Jesse Barnes5a354202011-06-24 12:19:22 -07004021 unsigned int display_bpc = UINT_MAX, bpc;
4022
4023 /* Walk the encoders & connectors on this crtc, get min bpc */
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004024 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004025
4026 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
4027 unsigned int lvds_bpc;
4028
4029 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
4030 LVDS_A3_POWER_UP)
4031 lvds_bpc = 8;
4032 else
4033 lvds_bpc = 6;
4034
4035 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004036 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004037 display_bpc = lvds_bpc;
4038 }
4039 continue;
4040 }
4041
Jesse Barnes5a354202011-06-24 12:19:22 -07004042 /* Not one of the known troublemakers, check the EDID */
4043 list_for_each_entry(connector, &dev->mode_config.connector_list,
4044 head) {
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004045 if (connector->encoder != &intel_encoder->base)
Jesse Barnes5a354202011-06-24 12:19:22 -07004046 continue;
4047
Jesse Barnes62ac41a2011-07-28 12:55:14 -07004048 /* Don't use an invalid EDID bpc value */
4049 if (connector->display_info.bpc &&
4050 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004051 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 -07004052 display_bpc = connector->display_info.bpc;
4053 }
4054 }
4055
Jani Nikula2f4f6492012-11-12 14:33:44 +02004056 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
4057 /* Use VBT settings if we have an eDP panel */
4058 unsigned int edp_bpc = dev_priv->edp.bpp / 3;
4059
Jani Nikula9a30a612012-11-12 14:33:45 +02004060 if (edp_bpc && edp_bpc < display_bpc) {
Jani Nikula2f4f6492012-11-12 14:33:44 +02004061 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
4062 display_bpc = edp_bpc;
4063 }
4064 continue;
4065 }
4066
Jesse Barnes5a354202011-06-24 12:19:22 -07004067 /*
4068 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
4069 * through, clamp it down. (Note: >12bpc will be caught below.)
4070 */
4071 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
4072 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04004073 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004074 display_bpc = 12;
4075 } else {
Adam Jackson82820492011-10-10 16:33:34 -04004076 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004077 display_bpc = 8;
4078 }
4079 }
4080 }
4081
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004082 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4083 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
4084 display_bpc = 6;
4085 }
4086
Jesse Barnes5a354202011-06-24 12:19:22 -07004087 /*
4088 * We could just drive the pipe at the highest bpc all the time and
4089 * enable dithering as needed, but that costs bandwidth. So choose
4090 * the minimum value that expresses the full color range of the fb but
4091 * also stays within the max display bpc discovered above.
4092 */
4093
Daniel Vetter94352cf2012-07-05 22:51:56 +02004094 switch (fb->depth) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004095 case 8:
4096 bpc = 8; /* since we go through a colormap */
4097 break;
4098 case 15:
4099 case 16:
4100 bpc = 6; /* min is 18bpp */
4101 break;
4102 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07004103 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07004104 break;
4105 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07004106 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07004107 break;
4108 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07004109 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07004110 break;
4111 default:
4112 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
4113 bpc = min((unsigned int)8, display_bpc);
4114 break;
4115 }
4116
Keith Packard578393c2011-09-05 11:53:21 -07004117 display_bpc = min(display_bpc, bpc);
4118
Adam Jackson82820492011-10-10 16:33:34 -04004119 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
4120 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004121
Keith Packard578393c2011-09-05 11:53:21 -07004122 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07004123
4124 return display_bpc != bpc;
4125}
4126
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004127static int vlv_get_refclk(struct drm_crtc *crtc)
4128{
4129 struct drm_device *dev = crtc->dev;
4130 struct drm_i915_private *dev_priv = dev->dev_private;
4131 int refclk = 27000; /* for DP & HDMI */
4132
4133 return 100000; /* only one validated so far */
4134
4135 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4136 refclk = 96000;
4137 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4138 if (intel_panel_use_ssc(dev_priv))
4139 refclk = 100000;
4140 else
4141 refclk = 96000;
4142 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4143 refclk = 100000;
4144 }
4145
4146 return refclk;
4147}
4148
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004149static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4150{
4151 struct drm_device *dev = crtc->dev;
4152 struct drm_i915_private *dev_priv = dev->dev_private;
4153 int refclk;
4154
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004155 if (IS_VALLEYVIEW(dev)) {
4156 refclk = vlv_get_refclk(crtc);
4157 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004158 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4159 refclk = dev_priv->lvds_ssc_freq * 1000;
4160 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4161 refclk / 1000);
4162 } else if (!IS_GEN2(dev)) {
4163 refclk = 96000;
4164 } else {
4165 refclk = 48000;
4166 }
4167
4168 return refclk;
4169}
4170
4171static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
4172 intel_clock_t *clock)
4173{
4174 /* SDVO TV has fixed PLL values depend on its clock range,
4175 this mirrors vbios setting. */
4176 if (adjusted_mode->clock >= 100000
4177 && adjusted_mode->clock < 140500) {
4178 clock->p1 = 2;
4179 clock->p2 = 10;
4180 clock->n = 3;
4181 clock->m1 = 16;
4182 clock->m2 = 8;
4183 } else if (adjusted_mode->clock >= 140500
4184 && adjusted_mode->clock <= 200000) {
4185 clock->p1 = 1;
4186 clock->p2 = 10;
4187 clock->n = 6;
4188 clock->m1 = 12;
4189 clock->m2 = 8;
4190 }
4191}
4192
Jesse Barnesa7516a02011-12-15 12:30:37 -08004193static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
4194 intel_clock_t *clock,
4195 intel_clock_t *reduced_clock)
4196{
4197 struct drm_device *dev = crtc->dev;
4198 struct drm_i915_private *dev_priv = dev->dev_private;
4199 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4200 int pipe = intel_crtc->pipe;
4201 u32 fp, fp2 = 0;
4202
4203 if (IS_PINEVIEW(dev)) {
4204 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
4205 if (reduced_clock)
4206 fp2 = (1 << reduced_clock->n) << 16 |
4207 reduced_clock->m1 << 8 | reduced_clock->m2;
4208 } else {
4209 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4210 if (reduced_clock)
4211 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4212 reduced_clock->m2;
4213 }
4214
4215 I915_WRITE(FP0(pipe), fp);
4216
4217 intel_crtc->lowfreq_avail = false;
4218 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4219 reduced_clock && i915_powersave) {
4220 I915_WRITE(FP1(pipe), fp2);
4221 intel_crtc->lowfreq_avail = true;
4222 } else {
4223 I915_WRITE(FP1(pipe), fp);
4224 }
4225}
4226
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004227static void vlv_update_pll(struct drm_crtc *crtc,
4228 struct drm_display_mode *mode,
4229 struct drm_display_mode *adjusted_mode,
4230 intel_clock_t *clock, intel_clock_t *reduced_clock,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304231 int num_connectors)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004232{
4233 struct drm_device *dev = crtc->dev;
4234 struct drm_i915_private *dev_priv = dev->dev_private;
4235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4236 int pipe = intel_crtc->pipe;
4237 u32 dpll, mdiv, pdiv;
4238 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304239 bool is_sdvo;
4240 u32 temp;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004241
Daniel Vetter09153002012-12-12 14:06:44 +01004242 mutex_lock(&dev_priv->dpio_lock);
4243
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304244 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4245 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4246
4247 dpll = DPLL_VGA_MODE_DIS;
4248 dpll |= DPLL_EXT_BUFFER_ENABLE_VLV;
4249 dpll |= DPLL_REFA_CLK_ENABLE_VLV;
4250 dpll |= DPLL_INTEGRATED_CLOCK_VLV;
4251
4252 I915_WRITE(DPLL(pipe), dpll);
4253 POSTING_READ(DPLL(pipe));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004254
4255 bestn = clock->n;
4256 bestm1 = clock->m1;
4257 bestm2 = clock->m2;
4258 bestp1 = clock->p1;
4259 bestp2 = clock->p2;
4260
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304261 /*
4262 * In Valleyview PLL and program lane counter registers are exposed
4263 * through DPIO interface
4264 */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004265 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4266 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4267 mdiv |= ((bestn << DPIO_N_SHIFT));
4268 mdiv |= (1 << DPIO_POST_DIV_SHIFT);
4269 mdiv |= (1 << DPIO_K_SHIFT);
4270 mdiv |= DPIO_ENABLE_CALIBRATION;
4271 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4272
4273 intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
4274
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304275 pdiv = (1 << DPIO_REFSEL_OVERRIDE) | (5 << DPIO_PLL_MODESEL_SHIFT) |
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004276 (3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304277 (7 << DPIO_PLL_REFCLK_SEL_SHIFT) | (8 << DPIO_DRIVER_CTL_SHIFT) |
4278 (5 << DPIO_CLK_BIAS_CTL_SHIFT);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004279 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
4280
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304281 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x005f003b);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004282
4283 dpll |= DPLL_VCO_ENABLE;
4284 I915_WRITE(DPLL(pipe), dpll);
4285 POSTING_READ(DPLL(pipe));
4286 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4287 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4288
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304289 intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x620);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004290
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304291 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4292 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4293
4294 I915_WRITE(DPLL(pipe), dpll);
4295
4296 /* Wait for the clocks to stabilize. */
4297 POSTING_READ(DPLL(pipe));
4298 udelay(150);
4299
4300 temp = 0;
4301 if (is_sdvo) {
4302 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004303 if (temp > 1)
4304 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4305 else
4306 temp = 0;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004307 }
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304308 I915_WRITE(DPLL_MD(pipe), temp);
4309 POSTING_READ(DPLL_MD(pipe));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004310
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304311 /* Now program lane control registers */
4312 if(intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)
4313 || intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
4314 {
4315 temp = 0x1000C4;
4316 if(pipe == 1)
4317 temp |= (1 << 21);
4318 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL1, temp);
4319 }
4320 if(intel_pipe_has_type(crtc,INTEL_OUTPUT_EDP))
4321 {
4322 temp = 0x1000C4;
4323 if(pipe == 1)
4324 temp |= (1 << 21);
4325 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL2, temp);
4326 }
Daniel Vetter09153002012-12-12 14:06:44 +01004327
4328 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004329}
4330
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004331static void i9xx_update_pll(struct drm_crtc *crtc,
4332 struct drm_display_mode *mode,
4333 struct drm_display_mode *adjusted_mode,
4334 intel_clock_t *clock, intel_clock_t *reduced_clock,
4335 int num_connectors)
4336{
4337 struct drm_device *dev = crtc->dev;
4338 struct drm_i915_private *dev_priv = dev->dev_private;
4339 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004340 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004341 int pipe = intel_crtc->pipe;
4342 u32 dpll;
4343 bool is_sdvo;
4344
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304345 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4346
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004347 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4348 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4349
4350 dpll = DPLL_VGA_MODE_DIS;
4351
4352 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4353 dpll |= DPLLB_MODE_LVDS;
4354 else
4355 dpll |= DPLLB_MODE_DAC_SERIAL;
4356 if (is_sdvo) {
4357 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4358 if (pixel_multiplier > 1) {
4359 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4360 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4361 }
4362 dpll |= DPLL_DVO_HIGH_SPEED;
4363 }
4364 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4365 dpll |= DPLL_DVO_HIGH_SPEED;
4366
4367 /* compute bitmask from p1 value */
4368 if (IS_PINEVIEW(dev))
4369 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4370 else {
4371 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4372 if (IS_G4X(dev) && reduced_clock)
4373 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4374 }
4375 switch (clock->p2) {
4376 case 5:
4377 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4378 break;
4379 case 7:
4380 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4381 break;
4382 case 10:
4383 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4384 break;
4385 case 14:
4386 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4387 break;
4388 }
4389 if (INTEL_INFO(dev)->gen >= 4)
4390 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4391
4392 if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4393 dpll |= PLL_REF_INPUT_TVCLKINBC;
4394 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4395 /* XXX: just matching BIOS for now */
4396 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4397 dpll |= 3;
4398 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4399 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4400 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4401 else
4402 dpll |= PLL_REF_INPUT_DREFCLK;
4403
4404 dpll |= DPLL_VCO_ENABLE;
4405 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4406 POSTING_READ(DPLL(pipe));
4407 udelay(150);
4408
Daniel Vetterdafd2262012-11-26 17:22:07 +01004409 for_each_encoder_on_crtc(dev, crtc, encoder)
4410 if (encoder->pre_pll_enable)
4411 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004412
4413 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4414 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4415
4416 I915_WRITE(DPLL(pipe), dpll);
4417
4418 /* Wait for the clocks to stabilize. */
4419 POSTING_READ(DPLL(pipe));
4420 udelay(150);
4421
4422 if (INTEL_INFO(dev)->gen >= 4) {
4423 u32 temp = 0;
4424 if (is_sdvo) {
4425 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4426 if (temp > 1)
4427 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4428 else
4429 temp = 0;
4430 }
4431 I915_WRITE(DPLL_MD(pipe), temp);
4432 } else {
4433 /* The pixel multiplier can only be updated once the
4434 * DPLL is enabled and the clocks are stable.
4435 *
4436 * So write it again.
4437 */
4438 I915_WRITE(DPLL(pipe), dpll);
4439 }
4440}
4441
4442static void i8xx_update_pll(struct drm_crtc *crtc,
4443 struct drm_display_mode *adjusted_mode,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304444 intel_clock_t *clock, intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004445 int num_connectors)
4446{
4447 struct drm_device *dev = crtc->dev;
4448 struct drm_i915_private *dev_priv = dev->dev_private;
4449 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004450 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004451 int pipe = intel_crtc->pipe;
4452 u32 dpll;
4453
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304454 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4455
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004456 dpll = DPLL_VGA_MODE_DIS;
4457
4458 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4459 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4460 } else {
4461 if (clock->p1 == 2)
4462 dpll |= PLL_P1_DIVIDE_BY_TWO;
4463 else
4464 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4465 if (clock->p2 == 4)
4466 dpll |= PLL_P2_DIVIDE_BY_4;
4467 }
4468
4469 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4470 /* XXX: just matching BIOS for now */
4471 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4472 dpll |= 3;
4473 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4474 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4475 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4476 else
4477 dpll |= PLL_REF_INPUT_DREFCLK;
4478
4479 dpll |= DPLL_VCO_ENABLE;
4480 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4481 POSTING_READ(DPLL(pipe));
4482 udelay(150);
4483
Daniel Vetterdafd2262012-11-26 17:22:07 +01004484 for_each_encoder_on_crtc(dev, crtc, encoder)
4485 if (encoder->pre_pll_enable)
4486 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004487
Daniel Vetter5b5896e2012-09-11 12:37:55 +02004488 I915_WRITE(DPLL(pipe), dpll);
4489
4490 /* Wait for the clocks to stabilize. */
4491 POSTING_READ(DPLL(pipe));
4492 udelay(150);
4493
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004494 /* The pixel multiplier can only be updated once the
4495 * DPLL is enabled and the clocks are stable.
4496 *
4497 * So write it again.
4498 */
4499 I915_WRITE(DPLL(pipe), dpll);
4500}
4501
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004502static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
4503 struct drm_display_mode *mode,
4504 struct drm_display_mode *adjusted_mode)
4505{
4506 struct drm_device *dev = intel_crtc->base.dev;
4507 struct drm_i915_private *dev_priv = dev->dev_private;
4508 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004509 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004510 uint32_t vsyncshift;
4511
4512 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4513 /* the chip adds 2 halflines automatically */
4514 adjusted_mode->crtc_vtotal -= 1;
4515 adjusted_mode->crtc_vblank_end -= 1;
4516 vsyncshift = adjusted_mode->crtc_hsync_start
4517 - adjusted_mode->crtc_htotal / 2;
4518 } else {
4519 vsyncshift = 0;
4520 }
4521
4522 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004523 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004524
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004525 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004526 (adjusted_mode->crtc_hdisplay - 1) |
4527 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004528 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004529 (adjusted_mode->crtc_hblank_start - 1) |
4530 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004531 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004532 (adjusted_mode->crtc_hsync_start - 1) |
4533 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4534
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004535 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004536 (adjusted_mode->crtc_vdisplay - 1) |
4537 ((adjusted_mode->crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004538 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004539 (adjusted_mode->crtc_vblank_start - 1) |
4540 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004541 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004542 (adjusted_mode->crtc_vsync_start - 1) |
4543 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4544
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004545 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4546 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4547 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4548 * bits. */
4549 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4550 (pipe == PIPE_B || pipe == PIPE_C))
4551 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4552
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004553 /* pipesrc controls the size that is scaled from, which should
4554 * always be the user's requested size.
4555 */
4556 I915_WRITE(PIPESRC(pipe),
4557 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4558}
4559
Eric Anholtf564048e2011-03-30 13:01:02 -07004560static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4561 struct drm_display_mode *mode,
4562 struct drm_display_mode *adjusted_mode,
4563 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004564 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004565{
4566 struct drm_device *dev = crtc->dev;
4567 struct drm_i915_private *dev_priv = dev->dev_private;
4568 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4569 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004570 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004571 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004572 intel_clock_t clock, reduced_clock;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004573 u32 dspcntr, pipeconf;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004574 bool ok, has_reduced_clock = false, is_sdvo = false;
4575 bool is_lvds = false, is_tv = false, is_dp = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004576 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004577 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004578 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004579
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004580 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004581 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004582 case INTEL_OUTPUT_LVDS:
4583 is_lvds = true;
4584 break;
4585 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08004586 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08004587 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01004588 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004589 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004590 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004591 case INTEL_OUTPUT_TVOUT:
4592 is_tv = true;
4593 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004594 case INTEL_OUTPUT_DISPLAYPORT:
4595 is_dp = true;
4596 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004597 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004598
Eric Anholtc751ce42010-03-25 11:48:48 -07004599 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004600 }
4601
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004602 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004603
Ma Lingd4906092009-03-18 20:13:27 +08004604 /*
4605 * Returns a set of divisors for the desired target clock with the given
4606 * refclk, or FALSE. The returned values represent the clock equation:
4607 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4608 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004609 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08004610 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4611 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004612 if (!ok) {
4613 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004614 return -EINVAL;
4615 }
4616
4617 /* Ensure that the cursor is valid for the new mode before changing... */
4618 intel_crtc_update_cursor(crtc, true);
4619
4620 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004621 /*
4622 * Ensure we match the reduced clock's P to the target clock.
4623 * If the clocks don't match, we can't switch the display clock
4624 * by using the FP0/FP1. In such case we will disable the LVDS
4625 * downclock feature.
4626 */
Eric Anholtf564048e2011-03-30 13:01:02 -07004627 has_reduced_clock = limit->find_pll(limit, crtc,
4628 dev_priv->lvds_downclock,
4629 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08004630 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004631 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004632 }
4633
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004634 if (is_sdvo && is_tv)
4635 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004636
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004637 if (IS_GEN2(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304638 i8xx_update_pll(crtc, adjusted_mode, &clock,
4639 has_reduced_clock ? &reduced_clock : NULL,
4640 num_connectors);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004641 else if (IS_VALLEYVIEW(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304642 vlv_update_pll(crtc, mode, adjusted_mode, &clock,
4643 has_reduced_clock ? &reduced_clock : NULL,
4644 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004645 else
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004646 i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
4647 has_reduced_clock ? &reduced_clock : NULL,
4648 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004649
4650 /* setup pipeconf */
4651 pipeconf = I915_READ(PIPECONF(pipe));
4652
4653 /* Set up the display plane register */
4654 dspcntr = DISPPLANE_GAMMA_ENABLE;
4655
Eric Anholt929c77f2011-03-30 13:01:04 -07004656 if (pipe == 0)
4657 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4658 else
4659 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07004660
4661 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4662 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4663 * core speed.
4664 *
4665 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4666 * pipe == 0 check?
4667 */
4668 if (mode->clock >
4669 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4670 pipeconf |= PIPECONF_DOUBLE_WIDE;
4671 else
4672 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4673 }
4674
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004675 /* default to 8bpc */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004676 pipeconf &= ~(PIPECONF_BPC_MASK | PIPECONF_DITHER_EN);
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004677 if (is_dp) {
Jani Nikula0c96c652012-09-26 18:43:10 +03004678 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004679 pipeconf |= PIPECONF_6BPC |
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004680 PIPECONF_DITHER_EN |
4681 PIPECONF_DITHER_TYPE_SP;
4682 }
4683 }
4684
Gajanan Bhat19c03922012-09-27 19:13:07 +05304685 if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4686 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004687 pipeconf |= PIPECONF_6BPC |
Gajanan Bhat19c03922012-09-27 19:13:07 +05304688 PIPECONF_ENABLE |
4689 I965_PIPECONF_ACTIVE;
4690 }
4691 }
4692
Eric Anholtf564048e2011-03-30 13:01:02 -07004693 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
4694 drm_mode_debug_printmodeline(mode);
4695
Jesse Barnesa7516a02011-12-15 12:30:37 -08004696 if (HAS_PIPE_CXSR(dev)) {
4697 if (intel_crtc->lowfreq_avail) {
Eric Anholtf564048e2011-03-30 13:01:02 -07004698 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4699 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004700 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07004701 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4702 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4703 }
4704 }
4705
Keith Packard617cf882012-02-08 13:53:38 -08004706 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetterdbb02572012-01-28 14:49:23 +01004707 if (!IS_GEN2(dev) &&
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004708 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Eric Anholtf564048e2011-03-30 13:01:02 -07004709 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004710 else
Keith Packard617cf882012-02-08 13:53:38 -08004711 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01004712
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004713 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Eric Anholtf564048e2011-03-30 13:01:02 -07004714
4715 /* pipesrc and dspsize control the size that is scaled from,
4716 * which should always be the user's requested size.
4717 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004718 I915_WRITE(DSPSIZE(plane),
4719 ((mode->vdisplay - 1) << 16) |
4720 (mode->hdisplay - 1));
4721 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004722
Eric Anholtf564048e2011-03-30 13:01:02 -07004723 I915_WRITE(PIPECONF(pipe), pipeconf);
4724 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07004725 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07004726
4727 intel_wait_for_vblank(dev, pipe);
4728
Eric Anholtf564048e2011-03-30 13:01:02 -07004729 I915_WRITE(DSPCNTR(plane), dspcntr);
4730 POSTING_READ(DSPCNTR(plane));
4731
Daniel Vetter94352cf2012-07-05 22:51:56 +02004732 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004733
4734 intel_update_watermarks(dev);
4735
Eric Anholtf564048e2011-03-30 13:01:02 -07004736 return ret;
4737}
4738
Paulo Zanonidde86e22012-12-01 12:04:25 -02004739static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07004740{
4741 struct drm_i915_private *dev_priv = dev->dev_private;
4742 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004743 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004744 u32 temp;
4745 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004746 bool has_cpu_edp = false;
4747 bool has_pch_edp = false;
4748 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07004749 bool has_ck505 = false;
4750 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004751
4752 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07004753 list_for_each_entry(encoder, &mode_config->encoder_list,
4754 base.head) {
4755 switch (encoder->type) {
4756 case INTEL_OUTPUT_LVDS:
4757 has_panel = true;
4758 has_lvds = true;
4759 break;
4760 case INTEL_OUTPUT_EDP:
4761 has_panel = true;
4762 if (intel_encoder_is_pch_edp(&encoder->base))
4763 has_pch_edp = true;
4764 else
4765 has_cpu_edp = true;
4766 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004767 }
4768 }
4769
Keith Packard99eb6a02011-09-26 14:29:12 -07004770 if (HAS_PCH_IBX(dev)) {
4771 has_ck505 = dev_priv->display_clock_mode;
4772 can_ssc = has_ck505;
4773 } else {
4774 has_ck505 = false;
4775 can_ssc = true;
4776 }
4777
4778 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4779 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4780 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004781
4782 /* Ironlake: try to setup display ref clock before DPLL
4783 * enabling. This is only under driver's control after
4784 * PCH B stepping, previous chipset stepping should be
4785 * ignoring this setting.
4786 */
4787 temp = I915_READ(PCH_DREF_CONTROL);
4788 /* Always enable nonspread source */
4789 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004790
Keith Packard99eb6a02011-09-26 14:29:12 -07004791 if (has_ck505)
4792 temp |= DREF_NONSPREAD_CK505_ENABLE;
4793 else
4794 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004795
Keith Packard199e5d72011-09-22 12:01:57 -07004796 if (has_panel) {
4797 temp &= ~DREF_SSC_SOURCE_MASK;
4798 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004799
Keith Packard199e5d72011-09-22 12:01:57 -07004800 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07004801 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004802 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004803 temp |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02004804 } else
4805 temp &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004806
4807 /* Get SSC going before enabling the outputs */
4808 I915_WRITE(PCH_DREF_CONTROL, temp);
4809 POSTING_READ(PCH_DREF_CONTROL);
4810 udelay(200);
4811
Jesse Barnes13d83a62011-08-03 12:59:20 -07004812 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4813
4814 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07004815 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07004816 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004817 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004818 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004819 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07004820 else
4821 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004822 } else
4823 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4824
4825 I915_WRITE(PCH_DREF_CONTROL, temp);
4826 POSTING_READ(PCH_DREF_CONTROL);
4827 udelay(200);
4828 } else {
4829 DRM_DEBUG_KMS("Disabling SSC entirely\n");
4830
4831 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4832
4833 /* Turn off CPU output */
4834 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4835
4836 I915_WRITE(PCH_DREF_CONTROL, temp);
4837 POSTING_READ(PCH_DREF_CONTROL);
4838 udelay(200);
4839
4840 /* Turn off the SSC source */
4841 temp &= ~DREF_SSC_SOURCE_MASK;
4842 temp |= DREF_SSC_SOURCE_DISABLE;
4843
4844 /* Turn off SSC1 */
4845 temp &= ~ DREF_SSC1_ENABLE;
4846
Jesse Barnes13d83a62011-08-03 12:59:20 -07004847 I915_WRITE(PCH_DREF_CONTROL, temp);
4848 POSTING_READ(PCH_DREF_CONTROL);
4849 udelay(200);
4850 }
4851}
4852
Paulo Zanonidde86e22012-12-01 12:04:25 -02004853/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
4854static void lpt_init_pch_refclk(struct drm_device *dev)
4855{
4856 struct drm_i915_private *dev_priv = dev->dev_private;
4857 struct drm_mode_config *mode_config = &dev->mode_config;
4858 struct intel_encoder *encoder;
4859 bool has_vga = false;
4860 bool is_sdv = false;
4861 u32 tmp;
4862
4863 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
4864 switch (encoder->type) {
4865 case INTEL_OUTPUT_ANALOG:
4866 has_vga = true;
4867 break;
4868 }
4869 }
4870
4871 if (!has_vga)
4872 return;
4873
4874 /* XXX: Rip out SDV support once Haswell ships for real. */
4875 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
4876 is_sdv = true;
4877
4878 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4879 tmp &= ~SBI_SSCCTL_DISABLE;
4880 tmp |= SBI_SSCCTL_PATHALT;
4881 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4882
4883 udelay(24);
4884
4885 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4886 tmp &= ~SBI_SSCCTL_PATHALT;
4887 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4888
4889 if (!is_sdv) {
4890 tmp = I915_READ(SOUTH_CHICKEN2);
4891 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
4892 I915_WRITE(SOUTH_CHICKEN2, tmp);
4893
4894 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
4895 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
4896 DRM_ERROR("FDI mPHY reset assert timeout\n");
4897
4898 tmp = I915_READ(SOUTH_CHICKEN2);
4899 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
4900 I915_WRITE(SOUTH_CHICKEN2, tmp);
4901
4902 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
4903 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
4904 100))
4905 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
4906 }
4907
4908 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
4909 tmp &= ~(0xFF << 24);
4910 tmp |= (0x12 << 24);
4911 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
4912
4913 if (!is_sdv) {
4914 tmp = intel_sbi_read(dev_priv, 0x808C, SBI_MPHY);
4915 tmp &= ~(0x3 << 6);
4916 tmp |= (1 << 6) | (1 << 0);
4917 intel_sbi_write(dev_priv, 0x808C, tmp, SBI_MPHY);
4918 }
4919
4920 if (is_sdv) {
4921 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
4922 tmp |= 0x7FFF;
4923 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
4924 }
4925
4926 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
4927 tmp |= (1 << 11);
4928 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
4929
4930 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
4931 tmp |= (1 << 11);
4932 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
4933
4934 if (is_sdv) {
4935 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
4936 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
4937 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
4938
4939 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
4940 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
4941 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
4942
4943 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
4944 tmp |= (0x3F << 8);
4945 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
4946
4947 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
4948 tmp |= (0x3F << 8);
4949 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
4950 }
4951
4952 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
4953 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
4954 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
4955
4956 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
4957 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
4958 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
4959
4960 if (!is_sdv) {
4961 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
4962 tmp &= ~(7 << 13);
4963 tmp |= (5 << 13);
4964 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
4965
4966 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
4967 tmp &= ~(7 << 13);
4968 tmp |= (5 << 13);
4969 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
4970 }
4971
4972 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
4973 tmp &= ~0xFF;
4974 tmp |= 0x1C;
4975 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
4976
4977 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
4978 tmp &= ~0xFF;
4979 tmp |= 0x1C;
4980 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
4981
4982 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
4983 tmp &= ~(0xFF << 16);
4984 tmp |= (0x1C << 16);
4985 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
4986
4987 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
4988 tmp &= ~(0xFF << 16);
4989 tmp |= (0x1C << 16);
4990 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
4991
4992 if (!is_sdv) {
4993 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
4994 tmp |= (1 << 27);
4995 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
4996
4997 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
4998 tmp |= (1 << 27);
4999 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5000
5001 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5002 tmp &= ~(0xF << 28);
5003 tmp |= (4 << 28);
5004 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5005
5006 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5007 tmp &= ~(0xF << 28);
5008 tmp |= (4 << 28);
5009 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5010 }
5011
5012 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5013 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5014 tmp |= SBI_DBUFF0_ENABLE;
5015 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
5016}
5017
5018/*
5019 * Initialize reference clocks when the driver loads
5020 */
5021void intel_init_pch_refclk(struct drm_device *dev)
5022{
5023 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5024 ironlake_init_pch_refclk(dev);
5025 else if (HAS_PCH_LPT(dev))
5026 lpt_init_pch_refclk(dev);
5027}
5028
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005029static int ironlake_get_refclk(struct drm_crtc *crtc)
5030{
5031 struct drm_device *dev = crtc->dev;
5032 struct drm_i915_private *dev_priv = dev->dev_private;
5033 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005034 struct intel_encoder *edp_encoder = NULL;
5035 int num_connectors = 0;
5036 bool is_lvds = false;
5037
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005038 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005039 switch (encoder->type) {
5040 case INTEL_OUTPUT_LVDS:
5041 is_lvds = true;
5042 break;
5043 case INTEL_OUTPUT_EDP:
5044 edp_encoder = encoder;
5045 break;
5046 }
5047 num_connectors++;
5048 }
5049
5050 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5051 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5052 dev_priv->lvds_ssc_freq);
5053 return dev_priv->lvds_ssc_freq * 1000;
5054 }
5055
5056 return 120000;
5057}
5058
Paulo Zanonic8203562012-09-12 10:06:29 -03005059static void ironlake_set_pipeconf(struct drm_crtc *crtc,
5060 struct drm_display_mode *adjusted_mode,
5061 bool dither)
5062{
5063 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5064 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5065 int pipe = intel_crtc->pipe;
5066 uint32_t val;
5067
5068 val = I915_READ(PIPECONF(pipe));
5069
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005070 val &= ~PIPECONF_BPC_MASK;
Paulo Zanonic8203562012-09-12 10:06:29 -03005071 switch (intel_crtc->bpp) {
5072 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005073 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005074 break;
5075 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005076 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005077 break;
5078 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005079 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005080 break;
5081 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005082 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005083 break;
5084 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005085 /* Case prevented by intel_choose_pipe_bpp_dither. */
5086 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005087 }
5088
5089 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5090 if (dither)
5091 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5092
5093 val &= ~PIPECONF_INTERLACE_MASK;
5094 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5095 val |= PIPECONF_INTERLACED_ILK;
5096 else
5097 val |= PIPECONF_PROGRESSIVE;
5098
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005099 if (adjusted_mode->private_flags & INTEL_MODE_LIMITED_COLOR_RANGE)
5100 val |= PIPECONF_COLOR_RANGE_SELECT;
5101 else
5102 val &= ~PIPECONF_COLOR_RANGE_SELECT;
5103
Paulo Zanonic8203562012-09-12 10:06:29 -03005104 I915_WRITE(PIPECONF(pipe), val);
5105 POSTING_READ(PIPECONF(pipe));
5106}
5107
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005108static void haswell_set_pipeconf(struct drm_crtc *crtc,
5109 struct drm_display_mode *adjusted_mode,
5110 bool dither)
5111{
5112 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005114 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005115 uint32_t val;
5116
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005117 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005118
5119 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5120 if (dither)
5121 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5122
5123 val &= ~PIPECONF_INTERLACE_MASK_HSW;
5124 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5125 val |= PIPECONF_INTERLACED_ILK;
5126 else
5127 val |= PIPECONF_PROGRESSIVE;
5128
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005129 I915_WRITE(PIPECONF(cpu_transcoder), val);
5130 POSTING_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005131}
5132
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005133static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5134 struct drm_display_mode *adjusted_mode,
5135 intel_clock_t *clock,
5136 bool *has_reduced_clock,
5137 intel_clock_t *reduced_clock)
5138{
5139 struct drm_device *dev = crtc->dev;
5140 struct drm_i915_private *dev_priv = dev->dev_private;
5141 struct intel_encoder *intel_encoder;
5142 int refclk;
5143 const intel_limit_t *limit;
5144 bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
5145
5146 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5147 switch (intel_encoder->type) {
5148 case INTEL_OUTPUT_LVDS:
5149 is_lvds = true;
5150 break;
5151 case INTEL_OUTPUT_SDVO:
5152 case INTEL_OUTPUT_HDMI:
5153 is_sdvo = true;
5154 if (intel_encoder->needs_tv_clock)
5155 is_tv = true;
5156 break;
5157 case INTEL_OUTPUT_TVOUT:
5158 is_tv = true;
5159 break;
5160 }
5161 }
5162
5163 refclk = ironlake_get_refclk(crtc);
5164
5165 /*
5166 * Returns a set of divisors for the desired target clock with the given
5167 * refclk, or FALSE. The returned values represent the clock equation:
5168 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5169 */
5170 limit = intel_limit(crtc, refclk);
5171 ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5172 clock);
5173 if (!ret)
5174 return false;
5175
5176 if (is_lvds && dev_priv->lvds_downclock_avail) {
5177 /*
5178 * Ensure we match the reduced clock's P to the target clock.
5179 * If the clocks don't match, we can't switch the display clock
5180 * by using the FP0/FP1. In such case we will disable the LVDS
5181 * downclock feature.
5182 */
5183 *has_reduced_clock = limit->find_pll(limit, crtc,
5184 dev_priv->lvds_downclock,
5185 refclk,
5186 clock,
5187 reduced_clock);
5188 }
5189
5190 if (is_sdvo && is_tv)
5191 i9xx_adjust_sdvo_tv_clock(adjusted_mode, clock);
5192
5193 return true;
5194}
5195
Daniel Vetter01a415f2012-10-27 15:58:40 +02005196static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5197{
5198 struct drm_i915_private *dev_priv = dev->dev_private;
5199 uint32_t temp;
5200
5201 temp = I915_READ(SOUTH_CHICKEN1);
5202 if (temp & FDI_BC_BIFURCATION_SELECT)
5203 return;
5204
5205 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5206 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5207
5208 temp |= FDI_BC_BIFURCATION_SELECT;
5209 DRM_DEBUG_KMS("enabling fdi C rx\n");
5210 I915_WRITE(SOUTH_CHICKEN1, temp);
5211 POSTING_READ(SOUTH_CHICKEN1);
5212}
5213
5214static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
5215{
5216 struct drm_device *dev = intel_crtc->base.dev;
5217 struct drm_i915_private *dev_priv = dev->dev_private;
5218 struct intel_crtc *pipe_B_crtc =
5219 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5220
5221 DRM_DEBUG_KMS("checking fdi config on pipe %i, lanes %i\n",
5222 intel_crtc->pipe, intel_crtc->fdi_lanes);
5223 if (intel_crtc->fdi_lanes > 4) {
5224 DRM_DEBUG_KMS("invalid fdi lane config on pipe %i: %i lanes\n",
5225 intel_crtc->pipe, intel_crtc->fdi_lanes);
5226 /* Clamp lanes to avoid programming the hw with bogus values. */
5227 intel_crtc->fdi_lanes = 4;
5228
5229 return false;
5230 }
5231
5232 if (dev_priv->num_pipe == 2)
5233 return true;
5234
5235 switch (intel_crtc->pipe) {
5236 case PIPE_A:
5237 return true;
5238 case PIPE_B:
5239 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5240 intel_crtc->fdi_lanes > 2) {
5241 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5242 intel_crtc->pipe, intel_crtc->fdi_lanes);
5243 /* Clamp lanes to avoid programming the hw with bogus values. */
5244 intel_crtc->fdi_lanes = 2;
5245
5246 return false;
5247 }
5248
5249 if (intel_crtc->fdi_lanes > 2)
5250 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5251 else
5252 cpt_enable_fdi_bc_bifurcation(dev);
5253
5254 return true;
5255 case PIPE_C:
5256 if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
5257 if (intel_crtc->fdi_lanes > 2) {
5258 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5259 intel_crtc->pipe, intel_crtc->fdi_lanes);
5260 /* Clamp lanes to avoid programming the hw with bogus values. */
5261 intel_crtc->fdi_lanes = 2;
5262
5263 return false;
5264 }
5265 } else {
5266 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5267 return false;
5268 }
5269
5270 cpt_enable_fdi_bc_bifurcation(dev);
5271
5272 return true;
5273 default:
5274 BUG();
5275 }
5276}
5277
Paulo Zanonid4b19312012-11-29 11:29:32 -02005278int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5279{
5280 /*
5281 * Account for spread spectrum to avoid
5282 * oversubscribing the link. Max center spread
5283 * is 2.5%; use 5% for safety's sake.
5284 */
5285 u32 bps = target_clock * bpp * 21 / 20;
5286 return bps / (link_bw * 8) + 1;
5287}
5288
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005289static void ironlake_set_m_n(struct drm_crtc *crtc,
5290 struct drm_display_mode *mode,
5291 struct drm_display_mode *adjusted_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08005292{
5293 struct drm_device *dev = crtc->dev;
5294 struct drm_i915_private *dev_priv = dev->dev_private;
5295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005296 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005297 struct intel_encoder *intel_encoder, *edp_encoder = NULL;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005298 struct intel_link_m_n m_n = {0};
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005299 int target_clock, pixel_multiplier, lane, link_bw;
5300 bool is_dp = false, is_cpu_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005301
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005302 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5303 switch (intel_encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005304 case INTEL_OUTPUT_DISPLAYPORT:
5305 is_dp = true;
5306 break;
5307 case INTEL_OUTPUT_EDP:
Jesse Barnese3aef172012-04-10 11:58:03 -07005308 is_dp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005309 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnese3aef172012-04-10 11:58:03 -07005310 is_cpu_edp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005311 edp_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005312 break;
5313 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005314 }
5315
Zhenyu Wang2c072452009-06-05 15:38:42 +08005316 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07005317 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5318 lane = 0;
5319 /* CPU eDP doesn't require FDI link, so just set DP M/N
5320 according to current link config */
Jesse Barnese3aef172012-04-10 11:58:03 -07005321 if (is_cpu_edp) {
Jesse Barnese3aef172012-04-10 11:58:03 -07005322 intel_edp_link_config(edp_encoder, &lane, &link_bw);
Eric Anholt8febb292011-03-30 13:01:07 -07005323 } else {
Eric Anholt8febb292011-03-30 13:01:07 -07005324 /* FDI is a binary signal running at ~2.7GHz, encoding
5325 * each output octet as 10 bits. The actual frequency
5326 * is stored as a divider into a 100MHz clock, and the
5327 * mode pixel clock is stored in units of 1KHz.
5328 * Hence the bw of each lane in terms of the mode signal
5329 * is:
5330 */
5331 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005332 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08005333
Daniel Vetter94bf2ce2012-06-04 18:39:19 +02005334 /* [e]DP over FDI requires target mode clock instead of link clock. */
5335 if (edp_encoder)
5336 target_clock = intel_edp_target_clock(edp_encoder, mode);
5337 else if (is_dp)
5338 target_clock = mode->clock;
5339 else
5340 target_clock = adjusted_mode->clock;
5341
Paulo Zanonid4b19312012-11-29 11:29:32 -02005342 if (!lane)
5343 lane = ironlake_get_lanes_required(target_clock, link_bw,
5344 intel_crtc->bpp);
Eric Anholt8febb292011-03-30 13:01:07 -07005345
5346 intel_crtc->fdi_lanes = lane;
5347
5348 if (pixel_multiplier > 1)
5349 link_bw *= pixel_multiplier;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005350 intel_link_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw, &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07005351
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005352 I915_WRITE(PIPE_DATA_M1(cpu_transcoder), TU_SIZE(m_n.tu) | m_n.gmch_m);
5353 I915_WRITE(PIPE_DATA_N1(cpu_transcoder), m_n.gmch_n);
5354 I915_WRITE(PIPE_LINK_M1(cpu_transcoder), m_n.link_m);
5355 I915_WRITE(PIPE_LINK_N1(cpu_transcoder), m_n.link_n);
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005356}
5357
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005358static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5359 struct drm_display_mode *adjusted_mode,
5360 intel_clock_t *clock, u32 fp)
5361{
5362 struct drm_crtc *crtc = &intel_crtc->base;
5363 struct drm_device *dev = crtc->dev;
5364 struct drm_i915_private *dev_priv = dev->dev_private;
5365 struct intel_encoder *intel_encoder;
5366 uint32_t dpll;
5367 int factor, pixel_multiplier, num_connectors = 0;
5368 bool is_lvds = false, is_sdvo = false, is_tv = false;
5369 bool is_dp = false, is_cpu_edp = false;
5370
5371 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5372 switch (intel_encoder->type) {
5373 case INTEL_OUTPUT_LVDS:
5374 is_lvds = true;
5375 break;
5376 case INTEL_OUTPUT_SDVO:
5377 case INTEL_OUTPUT_HDMI:
5378 is_sdvo = true;
5379 if (intel_encoder->needs_tv_clock)
5380 is_tv = true;
5381 break;
5382 case INTEL_OUTPUT_TVOUT:
5383 is_tv = true;
5384 break;
5385 case INTEL_OUTPUT_DISPLAYPORT:
5386 is_dp = true;
5387 break;
5388 case INTEL_OUTPUT_EDP:
5389 is_dp = true;
5390 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
5391 is_cpu_edp = true;
5392 break;
5393 }
5394
5395 num_connectors++;
5396 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005397
Chris Wilsonc1858122010-12-03 21:35:48 +00005398 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005399 factor = 21;
5400 if (is_lvds) {
5401 if ((intel_panel_use_ssc(dev_priv) &&
5402 dev_priv->lvds_ssc_freq == 100) ||
Daniel Vetter1974cad2012-11-26 17:22:09 +01005403 intel_is_dual_link_lvds(dev))
Eric Anholt8febb292011-03-30 13:01:07 -07005404 factor = 25;
5405 } else if (is_sdvo && is_tv)
5406 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005407
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005408 if (clock->m < factor * clock->n)
Eric Anholt8febb292011-03-30 13:01:07 -07005409 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005410
Chris Wilson5eddb702010-09-11 13:48:45 +01005411 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005412
Eric Anholta07d6782011-03-30 13:01:08 -07005413 if (is_lvds)
5414 dpll |= DPLLB_MODE_LVDS;
5415 else
5416 dpll |= DPLLB_MODE_DAC_SERIAL;
5417 if (is_sdvo) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005418 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
Eric Anholta07d6782011-03-30 13:01:08 -07005419 if (pixel_multiplier > 1) {
5420 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08005421 }
Eric Anholta07d6782011-03-30 13:01:08 -07005422 dpll |= DPLL_DVO_HIGH_SPEED;
5423 }
Jesse Barnese3aef172012-04-10 11:58:03 -07005424 if (is_dp && !is_cpu_edp)
Eric Anholta07d6782011-03-30 13:01:08 -07005425 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005426
Eric Anholta07d6782011-03-30 13:01:08 -07005427 /* compute bitmask from p1 value */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005428 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005429 /* also FPA1 */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005430 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005431
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005432 switch (clock->p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005433 case 5:
5434 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5435 break;
5436 case 7:
5437 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5438 break;
5439 case 10:
5440 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5441 break;
5442 case 14:
5443 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5444 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005445 }
5446
5447 if (is_sdvo && is_tv)
5448 dpll |= PLL_REF_INPUT_TVCLKINBC;
5449 else if (is_tv)
5450 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005451 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08005452 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00005453 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005454 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005455 else
5456 dpll |= PLL_REF_INPUT_DREFCLK;
5457
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005458 return dpll;
5459}
5460
Jesse Barnes79e53942008-11-07 14:24:08 -08005461static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5462 struct drm_display_mode *mode,
5463 struct drm_display_mode *adjusted_mode,
5464 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005465 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005466{
5467 struct drm_device *dev = crtc->dev;
5468 struct drm_i915_private *dev_priv = dev->dev_private;
5469 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5470 int pipe = intel_crtc->pipe;
5471 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005472 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005473 intel_clock_t clock, reduced_clock;
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005474 u32 dpll, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005475 bool ok, has_reduced_clock = false;
5476 bool is_lvds = false, is_dp = false, is_cpu_edp = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005477 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005478 int ret;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005479 bool dither, fdi_config_ok;
Jesse Barnes79e53942008-11-07 14:24:08 -08005480
5481 for_each_encoder_on_crtc(dev, crtc, encoder) {
5482 switch (encoder->type) {
5483 case INTEL_OUTPUT_LVDS:
5484 is_lvds = true;
5485 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005486 case INTEL_OUTPUT_DISPLAYPORT:
5487 is_dp = true;
5488 break;
5489 case INTEL_OUTPUT_EDP:
5490 is_dp = true;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005491 if (!intel_encoder_is_pch_edp(&encoder->base))
Jesse Barnes79e53942008-11-07 14:24:08 -08005492 is_cpu_edp = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005493 break;
5494 }
5495
5496 num_connectors++;
5497 }
5498
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005499 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5500 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5501
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005502 ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
5503 &has_reduced_clock, &reduced_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005504 if (!ok) {
5505 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5506 return -EINVAL;
5507 }
5508
5509 /* Ensure that the cursor is valid for the new mode before changing... */
5510 intel_crtc_update_cursor(crtc, true);
5511
Jesse Barnes79e53942008-11-07 14:24:08 -08005512 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005513 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5514 adjusted_mode);
Paulo Zanonic8203562012-09-12 10:06:29 -03005515 if (is_lvds && dev_priv->lvds_dither)
5516 dither = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005517
Jesse Barnes79e53942008-11-07 14:24:08 -08005518 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5519 if (has_reduced_clock)
5520 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5521 reduced_clock.m2;
5522
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005523 dpll = ironlake_compute_dpll(intel_crtc, adjusted_mode, &clock, fp);
Jesse Barnes79e53942008-11-07 14:24:08 -08005524
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07005525 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005526 drm_mode_debug_printmodeline(mode);
5527
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005528 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5529 if (!is_cpu_edp) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005530 struct intel_pch_pll *pll;
Chris Wilson5eddb702010-09-11 13:48:45 +01005531
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005532 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5533 if (pll == NULL) {
5534 DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
5535 pipe);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005536 return -EINVAL;
5537 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005538 } else
5539 intel_put_pch_pll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005540
Daniel Vetter2f0c2ad2012-11-29 15:59:35 +01005541 if (is_dp && !is_cpu_edp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005542 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08005543
Daniel Vetterdafd2262012-11-26 17:22:07 +01005544 for_each_encoder_on_crtc(dev, crtc, encoder)
5545 if (encoder->pre_pll_enable)
5546 encoder->pre_pll_enable(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08005547
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005548 if (intel_crtc->pch_pll) {
5549 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005550
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005551 /* Wait for the clocks to stabilize. */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005552 POSTING_READ(intel_crtc->pch_pll->pll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005553 udelay(150);
5554
Eric Anholt8febb292011-03-30 13:01:07 -07005555 /* The pixel multiplier can only be updated once the
5556 * DPLL is enabled and the clocks are stable.
5557 *
5558 * So write it again.
5559 */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005560 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08005561 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005562
Chris Wilson5eddb702010-09-11 13:48:45 +01005563 intel_crtc->lowfreq_avail = false;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005564 if (intel_crtc->pch_pll) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07005565 if (is_lvds && has_reduced_clock && i915_powersave) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005566 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005567 intel_crtc->lowfreq_avail = true;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005568 } else {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005569 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07005570 }
5571 }
5572
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005573 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005574
Daniel Vetter01a415f2012-10-27 15:58:40 +02005575 /* Note, this also computes intel_crtc->fdi_lanes which is used below in
5576 * ironlake_check_fdi_lanes. */
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005577 ironlake_set_m_n(crtc, mode, adjusted_mode);
Chris Wilson5eddb702010-09-11 13:48:45 +01005578
Daniel Vetter01a415f2012-10-27 15:58:40 +02005579 fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005580
Paulo Zanonic8203562012-09-12 10:06:29 -03005581 ironlake_set_pipeconf(crtc, adjusted_mode, dither);
Jesse Barnes79e53942008-11-07 14:24:08 -08005582
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005583 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005584
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005585 /* Set up the display plane register */
5586 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005587 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005588
Daniel Vetter94352cf2012-07-05 22:51:56 +02005589 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005590
5591 intel_update_watermarks(dev);
5592
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03005593 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5594
Daniel Vetter01a415f2012-10-27 15:58:40 +02005595 return fdi_config_ok ? ret : -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005596}
5597
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005598static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5599 struct drm_display_mode *mode,
5600 struct drm_display_mode *adjusted_mode,
5601 int x, int y,
5602 struct drm_framebuffer *fb)
5603{
5604 struct drm_device *dev = crtc->dev;
5605 struct drm_i915_private *dev_priv = dev->dev_private;
5606 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5607 int pipe = intel_crtc->pipe;
5608 int plane = intel_crtc->plane;
5609 int num_connectors = 0;
Daniel Vettered7ef432012-12-06 14:24:21 +01005610 bool is_dp = false, is_cpu_edp = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005611 struct intel_encoder *encoder;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005612 int ret;
5613 bool dither;
5614
5615 for_each_encoder_on_crtc(dev, crtc, encoder) {
5616 switch (encoder->type) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005617 case INTEL_OUTPUT_DISPLAYPORT:
5618 is_dp = true;
5619 break;
5620 case INTEL_OUTPUT_EDP:
5621 is_dp = true;
5622 if (!intel_encoder_is_pch_edp(&encoder->base))
5623 is_cpu_edp = true;
5624 break;
5625 }
5626
5627 num_connectors++;
5628 }
5629
Paulo Zanonia5c961d2012-10-24 15:59:34 -02005630 if (is_cpu_edp)
5631 intel_crtc->cpu_transcoder = TRANSCODER_EDP;
5632 else
5633 intel_crtc->cpu_transcoder = pipe;
5634
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005635 /* We are not sure yet this won't happen. */
5636 WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
5637 INTEL_PCH_TYPE(dev));
5638
5639 WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
5640 num_connectors, pipe_name(pipe));
5641
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005642 WARN_ON(I915_READ(PIPECONF(intel_crtc->cpu_transcoder)) &
Paulo Zanoni1ce42922012-10-05 12:06:01 -03005643 (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
5644
5645 WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
5646
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005647 if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
5648 return -EINVAL;
5649
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005650 /* Ensure that the cursor is valid for the new mode before changing... */
5651 intel_crtc_update_cursor(crtc, true);
5652
5653 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005654 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5655 adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005656
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005657 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
5658 drm_mode_debug_printmodeline(mode);
5659
Daniel Vettered7ef432012-12-06 14:24:21 +01005660 if (is_dp && !is_cpu_edp)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005661 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005662
5663 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005664
5665 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5666
Paulo Zanoni1eb8dfe2012-10-18 12:42:10 -03005667 if (!is_dp || is_cpu_edp)
5668 ironlake_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005669
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005670 haswell_set_pipeconf(crtc, adjusted_mode, dither);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005671
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005672 /* Set up the display plane register */
5673 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5674 POSTING_READ(DSPCNTR(plane));
5675
5676 ret = intel_pipe_set_base(crtc, x, y, fb);
5677
5678 intel_update_watermarks(dev);
5679
5680 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5681
Jesse Barnes79e53942008-11-07 14:24:08 -08005682 return ret;
5683}
5684
Eric Anholtf564048e2011-03-30 13:01:02 -07005685static int intel_crtc_mode_set(struct drm_crtc *crtc,
5686 struct drm_display_mode *mode,
5687 struct drm_display_mode *adjusted_mode,
5688 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005689 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07005690{
5691 struct drm_device *dev = crtc->dev;
5692 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01005693 struct drm_encoder_helper_funcs *encoder_funcs;
5694 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07005695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5696 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07005697 int ret;
5698
Eric Anholt0b701d22011-03-30 13:01:03 -07005699 drm_vblank_pre_modeset(dev, pipe);
5700
Eric Anholtf564048e2011-03-30 13:01:02 -07005701 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005702 x, y, fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08005703 drm_vblank_post_modeset(dev, pipe);
5704
Daniel Vetter9256aa12012-10-31 19:26:13 +01005705 if (ret != 0)
5706 return ret;
5707
5708 for_each_encoder_on_crtc(dev, crtc, encoder) {
5709 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
5710 encoder->base.base.id,
5711 drm_get_encoder_name(&encoder->base),
5712 mode->base.id, mode->name);
5713 encoder_funcs = encoder->base.helper_private;
5714 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
5715 }
5716
5717 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005718}
5719
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005720static bool intel_eld_uptodate(struct drm_connector *connector,
5721 int reg_eldv, uint32_t bits_eldv,
5722 int reg_elda, uint32_t bits_elda,
5723 int reg_edid)
5724{
5725 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5726 uint8_t *eld = connector->eld;
5727 uint32_t i;
5728
5729 i = I915_READ(reg_eldv);
5730 i &= bits_eldv;
5731
5732 if (!eld[0])
5733 return !i;
5734
5735 if (!i)
5736 return false;
5737
5738 i = I915_READ(reg_elda);
5739 i &= ~bits_elda;
5740 I915_WRITE(reg_elda, i);
5741
5742 for (i = 0; i < eld[2]; i++)
5743 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
5744 return false;
5745
5746 return true;
5747}
5748
Wu Fengguange0dac652011-09-05 14:25:34 +08005749static void g4x_write_eld(struct drm_connector *connector,
5750 struct drm_crtc *crtc)
5751{
5752 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5753 uint8_t *eld = connector->eld;
5754 uint32_t eldv;
5755 uint32_t len;
5756 uint32_t i;
5757
5758 i = I915_READ(G4X_AUD_VID_DID);
5759
5760 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
5761 eldv = G4X_ELDV_DEVCL_DEVBLC;
5762 else
5763 eldv = G4X_ELDV_DEVCTG;
5764
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005765 if (intel_eld_uptodate(connector,
5766 G4X_AUD_CNTL_ST, eldv,
5767 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
5768 G4X_HDMIW_HDMIEDID))
5769 return;
5770
Wu Fengguange0dac652011-09-05 14:25:34 +08005771 i = I915_READ(G4X_AUD_CNTL_ST);
5772 i &= ~(eldv | G4X_ELD_ADDR);
5773 len = (i >> 9) & 0x1f; /* ELD buffer size */
5774 I915_WRITE(G4X_AUD_CNTL_ST, i);
5775
5776 if (!eld[0])
5777 return;
5778
5779 len = min_t(uint8_t, eld[2], len);
5780 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5781 for (i = 0; i < len; i++)
5782 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
5783
5784 i = I915_READ(G4X_AUD_CNTL_ST);
5785 i |= eldv;
5786 I915_WRITE(G4X_AUD_CNTL_ST, i);
5787}
5788
Wang Xingchao83358c852012-08-16 22:43:37 +08005789static void haswell_write_eld(struct drm_connector *connector,
5790 struct drm_crtc *crtc)
5791{
5792 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5793 uint8_t *eld = connector->eld;
5794 struct drm_device *dev = crtc->dev;
5795 uint32_t eldv;
5796 uint32_t i;
5797 int len;
5798 int pipe = to_intel_crtc(crtc)->pipe;
5799 int tmp;
5800
5801 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
5802 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
5803 int aud_config = HSW_AUD_CFG(pipe);
5804 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
5805
5806
5807 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
5808
5809 /* Audio output enable */
5810 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
5811 tmp = I915_READ(aud_cntrl_st2);
5812 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
5813 I915_WRITE(aud_cntrl_st2, tmp);
5814
5815 /* Wait for 1 vertical blank */
5816 intel_wait_for_vblank(dev, pipe);
5817
5818 /* Set ELD valid state */
5819 tmp = I915_READ(aud_cntrl_st2);
5820 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
5821 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
5822 I915_WRITE(aud_cntrl_st2, tmp);
5823 tmp = I915_READ(aud_cntrl_st2);
5824 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
5825
5826 /* Enable HDMI mode */
5827 tmp = I915_READ(aud_config);
5828 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
5829 /* clear N_programing_enable and N_value_index */
5830 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
5831 I915_WRITE(aud_config, tmp);
5832
5833 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5834
5835 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
5836
5837 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5838 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5839 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
5840 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5841 } else
5842 I915_WRITE(aud_config, 0);
5843
5844 if (intel_eld_uptodate(connector,
5845 aud_cntrl_st2, eldv,
5846 aud_cntl_st, IBX_ELD_ADDRESS,
5847 hdmiw_hdmiedid))
5848 return;
5849
5850 i = I915_READ(aud_cntrl_st2);
5851 i &= ~eldv;
5852 I915_WRITE(aud_cntrl_st2, i);
5853
5854 if (!eld[0])
5855 return;
5856
5857 i = I915_READ(aud_cntl_st);
5858 i &= ~IBX_ELD_ADDRESS;
5859 I915_WRITE(aud_cntl_st, i);
5860 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
5861 DRM_DEBUG_DRIVER("port num:%d\n", i);
5862
5863 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5864 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5865 for (i = 0; i < len; i++)
5866 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5867
5868 i = I915_READ(aud_cntrl_st2);
5869 i |= eldv;
5870 I915_WRITE(aud_cntrl_st2, i);
5871
5872}
5873
Wu Fengguange0dac652011-09-05 14:25:34 +08005874static void ironlake_write_eld(struct drm_connector *connector,
5875 struct drm_crtc *crtc)
5876{
5877 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5878 uint8_t *eld = connector->eld;
5879 uint32_t eldv;
5880 uint32_t i;
5881 int len;
5882 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06005883 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08005884 int aud_cntl_st;
5885 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08005886 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08005887
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08005888 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08005889 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
5890 aud_config = IBX_AUD_CFG(pipe);
5891 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005892 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08005893 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08005894 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
5895 aud_config = CPT_AUD_CFG(pipe);
5896 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005897 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08005898 }
5899
Wang Xingchao9b138a82012-08-09 16:52:18 +08005900 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08005901
5902 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08005903 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08005904 if (!i) {
5905 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
5906 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005907 eldv = IBX_ELD_VALIDB;
5908 eldv |= IBX_ELD_VALIDB << 4;
5909 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08005910 } else {
5911 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005912 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08005913 }
5914
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005915 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5916 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5917 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06005918 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5919 } else
5920 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005921
5922 if (intel_eld_uptodate(connector,
5923 aud_cntrl_st2, eldv,
5924 aud_cntl_st, IBX_ELD_ADDRESS,
5925 hdmiw_hdmiedid))
5926 return;
5927
Wu Fengguange0dac652011-09-05 14:25:34 +08005928 i = I915_READ(aud_cntrl_st2);
5929 i &= ~eldv;
5930 I915_WRITE(aud_cntrl_st2, i);
5931
5932 if (!eld[0])
5933 return;
5934
Wu Fengguange0dac652011-09-05 14:25:34 +08005935 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005936 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08005937 I915_WRITE(aud_cntl_st, i);
5938
5939 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5940 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5941 for (i = 0; i < len; i++)
5942 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5943
5944 i = I915_READ(aud_cntrl_st2);
5945 i |= eldv;
5946 I915_WRITE(aud_cntrl_st2, i);
5947}
5948
5949void intel_write_eld(struct drm_encoder *encoder,
5950 struct drm_display_mode *mode)
5951{
5952 struct drm_crtc *crtc = encoder->crtc;
5953 struct drm_connector *connector;
5954 struct drm_device *dev = encoder->dev;
5955 struct drm_i915_private *dev_priv = dev->dev_private;
5956
5957 connector = drm_select_eld(encoder, mode);
5958 if (!connector)
5959 return;
5960
5961 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5962 connector->base.id,
5963 drm_get_connector_name(connector),
5964 connector->encoder->base.id,
5965 drm_get_encoder_name(connector->encoder));
5966
5967 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
5968
5969 if (dev_priv->display.write_eld)
5970 dev_priv->display.write_eld(connector, crtc);
5971}
5972
Jesse Barnes79e53942008-11-07 14:24:08 -08005973/** Loads the palette/gamma unit for the CRTC with the prepared values */
5974void intel_crtc_load_lut(struct drm_crtc *crtc)
5975{
5976 struct drm_device *dev = crtc->dev;
5977 struct drm_i915_private *dev_priv = dev->dev_private;
5978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005979 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005980 int i;
5981
5982 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00005983 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08005984 return;
5985
Adam Jacksonf2b115e2009-12-03 17:14:42 -05005986 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07005987 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005988 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005989
Jesse Barnes79e53942008-11-07 14:24:08 -08005990 for (i = 0; i < 256; i++) {
5991 I915_WRITE(palreg + 4 * i,
5992 (intel_crtc->lut_r[i] << 16) |
5993 (intel_crtc->lut_g[i] << 8) |
5994 intel_crtc->lut_b[i]);
5995 }
5996}
5997
Chris Wilson560b85b2010-08-07 11:01:38 +01005998static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
5999{
6000 struct drm_device *dev = crtc->dev;
6001 struct drm_i915_private *dev_priv = dev->dev_private;
6002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6003 bool visible = base != 0;
6004 u32 cntl;
6005
6006 if (intel_crtc->cursor_visible == visible)
6007 return;
6008
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006009 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006010 if (visible) {
6011 /* On these chipsets we can only modify the base whilst
6012 * the cursor is disabled.
6013 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006014 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006015
6016 cntl &= ~(CURSOR_FORMAT_MASK);
6017 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6018 cntl |= CURSOR_ENABLE |
6019 CURSOR_GAMMA_ENABLE |
6020 CURSOR_FORMAT_ARGB;
6021 } else
6022 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006023 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006024
6025 intel_crtc->cursor_visible = visible;
6026}
6027
6028static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6029{
6030 struct drm_device *dev = crtc->dev;
6031 struct drm_i915_private *dev_priv = dev->dev_private;
6032 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6033 int pipe = intel_crtc->pipe;
6034 bool visible = base != 0;
6035
6036 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006037 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006038 if (base) {
6039 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6040 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6041 cntl |= pipe << 28; /* Connect to correct pipe */
6042 } else {
6043 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6044 cntl |= CURSOR_MODE_DISABLE;
6045 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006046 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006047
6048 intel_crtc->cursor_visible = visible;
6049 }
6050 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006051 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006052}
6053
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006054static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6055{
6056 struct drm_device *dev = crtc->dev;
6057 struct drm_i915_private *dev_priv = dev->dev_private;
6058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6059 int pipe = intel_crtc->pipe;
6060 bool visible = base != 0;
6061
6062 if (intel_crtc->cursor_visible != visible) {
6063 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6064 if (base) {
6065 cntl &= ~CURSOR_MODE;
6066 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6067 } else {
6068 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6069 cntl |= CURSOR_MODE_DISABLE;
6070 }
6071 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6072
6073 intel_crtc->cursor_visible = visible;
6074 }
6075 /* and commit changes on next vblank */
6076 I915_WRITE(CURBASE_IVB(pipe), base);
6077}
6078
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006079/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006080static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6081 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006082{
6083 struct drm_device *dev = crtc->dev;
6084 struct drm_i915_private *dev_priv = dev->dev_private;
6085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6086 int pipe = intel_crtc->pipe;
6087 int x = intel_crtc->cursor_x;
6088 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006089 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006090 bool visible;
6091
6092 pos = 0;
6093
Chris Wilson6b383a72010-09-13 13:54:26 +01006094 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006095 base = intel_crtc->cursor_addr;
6096 if (x > (int) crtc->fb->width)
6097 base = 0;
6098
6099 if (y > (int) crtc->fb->height)
6100 base = 0;
6101 } else
6102 base = 0;
6103
6104 if (x < 0) {
6105 if (x + intel_crtc->cursor_width < 0)
6106 base = 0;
6107
6108 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6109 x = -x;
6110 }
6111 pos |= x << CURSOR_X_SHIFT;
6112
6113 if (y < 0) {
6114 if (y + intel_crtc->cursor_height < 0)
6115 base = 0;
6116
6117 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6118 y = -y;
6119 }
6120 pos |= y << CURSOR_Y_SHIFT;
6121
6122 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006123 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006124 return;
6125
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006126 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006127 I915_WRITE(CURPOS_IVB(pipe), pos);
6128 ivb_update_cursor(crtc, base);
6129 } else {
6130 I915_WRITE(CURPOS(pipe), pos);
6131 if (IS_845G(dev) || IS_I865G(dev))
6132 i845_update_cursor(crtc, base);
6133 else
6134 i9xx_update_cursor(crtc, base);
6135 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006136}
6137
Jesse Barnes79e53942008-11-07 14:24:08 -08006138static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006139 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006140 uint32_t handle,
6141 uint32_t width, uint32_t height)
6142{
6143 struct drm_device *dev = crtc->dev;
6144 struct drm_i915_private *dev_priv = dev->dev_private;
6145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006146 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006147 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006148 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006149
Jesse Barnes79e53942008-11-07 14:24:08 -08006150 /* if we want to turn off the cursor ignore width and height */
6151 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006152 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006153 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006154 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006155 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006156 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006157 }
6158
6159 /* Currently we only support 64x64 cursors */
6160 if (width != 64 || height != 64) {
6161 DRM_ERROR("we currently only support 64x64 cursors\n");
6162 return -EINVAL;
6163 }
6164
Chris Wilson05394f32010-11-08 19:18:58 +00006165 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006166 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006167 return -ENOENT;
6168
Chris Wilson05394f32010-11-08 19:18:58 +00006169 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006170 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006171 ret = -ENOMEM;
6172 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006173 }
6174
Dave Airlie71acb5e2008-12-30 20:31:46 +10006175 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006176 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006177 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00006178 if (obj->tiling_mode) {
6179 DRM_ERROR("cursor cannot be tiled\n");
6180 ret = -EINVAL;
6181 goto fail_locked;
6182 }
6183
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006184 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006185 if (ret) {
6186 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006187 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006188 }
6189
Chris Wilsond9e86c02010-11-10 16:40:20 +00006190 ret = i915_gem_object_put_fence(obj);
6191 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006192 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006193 goto fail_unpin;
6194 }
6195
Chris Wilson05394f32010-11-08 19:18:58 +00006196 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006197 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006198 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006199 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006200 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6201 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006202 if (ret) {
6203 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006204 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006205 }
Chris Wilson05394f32010-11-08 19:18:58 +00006206 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006207 }
6208
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006209 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006210 I915_WRITE(CURSIZE, (height << 12) | width);
6211
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006212 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006213 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006214 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006215 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006216 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6217 } else
6218 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006219 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006220 }
Jesse Barnes80824002009-09-10 15:28:06 -07006221
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006222 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006223
6224 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006225 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006226 intel_crtc->cursor_width = width;
6227 intel_crtc->cursor_height = height;
6228
Chris Wilson6b383a72010-09-13 13:54:26 +01006229 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006230
Jesse Barnes79e53942008-11-07 14:24:08 -08006231 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006232fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006233 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006234fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006235 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006236fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006237 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006238 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006239}
6240
6241static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6242{
Jesse Barnes79e53942008-11-07 14:24:08 -08006243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006244
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006245 intel_crtc->cursor_x = x;
6246 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006247
Chris Wilson6b383a72010-09-13 13:54:26 +01006248 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006249
6250 return 0;
6251}
6252
6253/** Sets the color ramps on behalf of RandR */
6254void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6255 u16 blue, int regno)
6256{
6257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6258
6259 intel_crtc->lut_r[regno] = red >> 8;
6260 intel_crtc->lut_g[regno] = green >> 8;
6261 intel_crtc->lut_b[regno] = blue >> 8;
6262}
6263
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006264void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6265 u16 *blue, int regno)
6266{
6267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6268
6269 *red = intel_crtc->lut_r[regno] << 8;
6270 *green = intel_crtc->lut_g[regno] << 8;
6271 *blue = intel_crtc->lut_b[regno] << 8;
6272}
6273
Jesse Barnes79e53942008-11-07 14:24:08 -08006274static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006275 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006276{
James Simmons72034252010-08-03 01:33:19 +01006277 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006278 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006279
James Simmons72034252010-08-03 01:33:19 +01006280 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006281 intel_crtc->lut_r[i] = red[i] >> 8;
6282 intel_crtc->lut_g[i] = green[i] >> 8;
6283 intel_crtc->lut_b[i] = blue[i] >> 8;
6284 }
6285
6286 intel_crtc_load_lut(crtc);
6287}
6288
6289/**
6290 * Get a pipe with a simple mode set on it for doing load-based monitor
6291 * detection.
6292 *
6293 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07006294 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08006295 *
Eric Anholtc751ce42010-03-25 11:48:48 -07006296 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08006297 * configured for it. In the future, it could choose to temporarily disable
6298 * some outputs to free up a pipe for its use.
6299 *
6300 * \return crtc, or NULL if no pipes are available.
6301 */
6302
6303/* VESA 640x480x72Hz mode to set on the pipe */
6304static struct drm_display_mode load_detect_mode = {
6305 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6306 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6307};
6308
Chris Wilsond2dff872011-04-19 08:36:26 +01006309static struct drm_framebuffer *
6310intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006311 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006312 struct drm_i915_gem_object *obj)
6313{
6314 struct intel_framebuffer *intel_fb;
6315 int ret;
6316
6317 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6318 if (!intel_fb) {
6319 drm_gem_object_unreference_unlocked(&obj->base);
6320 return ERR_PTR(-ENOMEM);
6321 }
6322
6323 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6324 if (ret) {
6325 drm_gem_object_unreference_unlocked(&obj->base);
6326 kfree(intel_fb);
6327 return ERR_PTR(ret);
6328 }
6329
6330 return &intel_fb->base;
6331}
6332
6333static u32
6334intel_framebuffer_pitch_for_width(int width, int bpp)
6335{
6336 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6337 return ALIGN(pitch, 64);
6338}
6339
6340static u32
6341intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6342{
6343 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6344 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6345}
6346
6347static struct drm_framebuffer *
6348intel_framebuffer_create_for_mode(struct drm_device *dev,
6349 struct drm_display_mode *mode,
6350 int depth, int bpp)
6351{
6352 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006353 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006354
6355 obj = i915_gem_alloc_object(dev,
6356 intel_framebuffer_size_for_mode(mode, bpp));
6357 if (obj == NULL)
6358 return ERR_PTR(-ENOMEM);
6359
6360 mode_cmd.width = mode->hdisplay;
6361 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006362 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6363 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006364 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006365
6366 return intel_framebuffer_create(dev, &mode_cmd, obj);
6367}
6368
6369static struct drm_framebuffer *
6370mode_fits_in_fbdev(struct drm_device *dev,
6371 struct drm_display_mode *mode)
6372{
6373 struct drm_i915_private *dev_priv = dev->dev_private;
6374 struct drm_i915_gem_object *obj;
6375 struct drm_framebuffer *fb;
6376
6377 if (dev_priv->fbdev == NULL)
6378 return NULL;
6379
6380 obj = dev_priv->fbdev->ifb.obj;
6381 if (obj == NULL)
6382 return NULL;
6383
6384 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006385 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6386 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006387 return NULL;
6388
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006389 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006390 return NULL;
6391
6392 return fb;
6393}
6394
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006395bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006396 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006397 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006398{
6399 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006400 struct intel_encoder *intel_encoder =
6401 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006402 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006403 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006404 struct drm_crtc *crtc = NULL;
6405 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006406 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006407 int i = -1;
6408
Chris Wilsond2dff872011-04-19 08:36:26 +01006409 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6410 connector->base.id, drm_get_connector_name(connector),
6411 encoder->base.id, drm_get_encoder_name(encoder));
6412
Jesse Barnes79e53942008-11-07 14:24:08 -08006413 /*
6414 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006415 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006416 * - if the connector already has an assigned crtc, use it (but make
6417 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006418 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006419 * - try to find the first unused crtc that can drive this connector,
6420 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006421 */
6422
6423 /* See if we already have a CRTC for this connector */
6424 if (encoder->crtc) {
6425 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006426
Daniel Vetter24218aa2012-08-12 19:27:11 +02006427 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006428 old->load_detect_temp = false;
6429
6430 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006431 if (connector->dpms != DRM_MODE_DPMS_ON)
6432 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006433
Chris Wilson71731882011-04-19 23:10:58 +01006434 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006435 }
6436
6437 /* Find an unused one (if possible) */
6438 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6439 i++;
6440 if (!(encoder->possible_crtcs & (1 << i)))
6441 continue;
6442 if (!possible_crtc->enabled) {
6443 crtc = possible_crtc;
6444 break;
6445 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006446 }
6447
6448 /*
6449 * If we didn't find an unused CRTC, don't use any.
6450 */
6451 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006452 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6453 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006454 }
6455
Daniel Vetterfc303102012-07-09 10:40:58 +02006456 intel_encoder->new_crtc = to_intel_crtc(crtc);
6457 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006458
6459 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006460 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006461 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006462 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006463
Chris Wilson64927112011-04-20 07:25:26 +01006464 if (!mode)
6465 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006466
Chris Wilsond2dff872011-04-19 08:36:26 +01006467 /* We need a framebuffer large enough to accommodate all accesses
6468 * that the plane may generate whilst we perform load detection.
6469 * We can not rely on the fbcon either being present (we get called
6470 * during its initialisation to detect all boot displays, or it may
6471 * not even exist) or that it is large enough to satisfy the
6472 * requested mode.
6473 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006474 fb = mode_fits_in_fbdev(dev, mode);
6475 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006476 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006477 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6478 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006479 } else
6480 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006481 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006482 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006483 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006484 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006485
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006486 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006487 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006488 if (old->release_fb)
6489 old->release_fb->funcs->destroy(old->release_fb);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006490 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006491 }
Chris Wilson71731882011-04-19 23:10:58 +01006492
Jesse Barnes79e53942008-11-07 14:24:08 -08006493 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006494 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006495 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006496}
6497
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006498void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006499 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006500{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006501 struct intel_encoder *intel_encoder =
6502 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006503 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006504
Chris Wilsond2dff872011-04-19 08:36:26 +01006505 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6506 connector->base.id, drm_get_connector_name(connector),
6507 encoder->base.id, drm_get_encoder_name(encoder));
6508
Chris Wilson8261b192011-04-19 23:18:09 +01006509 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006510 struct drm_crtc *crtc = encoder->crtc;
6511
6512 to_intel_connector(connector)->new_encoder = NULL;
6513 intel_encoder->new_crtc = NULL;
6514 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006515
6516 if (old->release_fb)
6517 old->release_fb->funcs->destroy(old->release_fb);
6518
Chris Wilson0622a532011-04-21 09:32:11 +01006519 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006520 }
6521
Eric Anholtc751ce42010-03-25 11:48:48 -07006522 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006523 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6524 connector->funcs->dpms(connector, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006525}
6526
6527/* Returns the clock of the currently programmed mode of the given pipe. */
6528static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6529{
6530 struct drm_i915_private *dev_priv = dev->dev_private;
6531 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6532 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006533 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006534 u32 fp;
6535 intel_clock_t clock;
6536
6537 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006538 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006539 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006540 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006541
6542 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006543 if (IS_PINEVIEW(dev)) {
6544 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6545 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006546 } else {
6547 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6548 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6549 }
6550
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006551 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006552 if (IS_PINEVIEW(dev))
6553 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6554 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006555 else
6556 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006557 DPLL_FPA01_P1_POST_DIV_SHIFT);
6558
6559 switch (dpll & DPLL_MODE_MASK) {
6560 case DPLLB_MODE_DAC_SERIAL:
6561 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6562 5 : 10;
6563 break;
6564 case DPLLB_MODE_LVDS:
6565 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6566 7 : 14;
6567 break;
6568 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006569 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006570 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6571 return 0;
6572 }
6573
6574 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08006575 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006576 } else {
6577 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6578
6579 if (is_lvds) {
6580 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6581 DPLL_FPA01_P1_POST_DIV_SHIFT);
6582 clock.p2 = 14;
6583
6584 if ((dpll & PLL_REF_INPUT_MASK) ==
6585 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6586 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08006587 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006588 } else
Shaohua Li21778322009-02-23 15:19:16 +08006589 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006590 } else {
6591 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6592 clock.p1 = 2;
6593 else {
6594 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6595 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6596 }
6597 if (dpll & PLL_P2_DIVIDE_BY_4)
6598 clock.p2 = 4;
6599 else
6600 clock.p2 = 2;
6601
Shaohua Li21778322009-02-23 15:19:16 +08006602 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006603 }
6604 }
6605
6606 /* XXX: It would be nice to validate the clocks, but we can't reuse
6607 * i830PllIsValid() because it relies on the xf86_config connector
6608 * configuration being accurate, which it isn't necessarily.
6609 */
6610
6611 return clock.dot;
6612}
6613
6614/** Returns the currently programmed mode of the given pipe. */
6615struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6616 struct drm_crtc *crtc)
6617{
Jesse Barnes548f2452011-02-17 10:40:53 -08006618 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006619 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006620 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006621 struct drm_display_mode *mode;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006622 int htot = I915_READ(HTOTAL(cpu_transcoder));
6623 int hsync = I915_READ(HSYNC(cpu_transcoder));
6624 int vtot = I915_READ(VTOTAL(cpu_transcoder));
6625 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08006626
6627 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6628 if (!mode)
6629 return NULL;
6630
6631 mode->clock = intel_crtc_clock_get(dev, crtc);
6632 mode->hdisplay = (htot & 0xffff) + 1;
6633 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6634 mode->hsync_start = (hsync & 0xffff) + 1;
6635 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6636 mode->vdisplay = (vtot & 0xffff) + 1;
6637 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6638 mode->vsync_start = (vsync & 0xffff) + 1;
6639 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6640
6641 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006642
6643 return mode;
6644}
6645
Daniel Vetter3dec0092010-08-20 21:40:52 +02006646static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07006647{
6648 struct drm_device *dev = crtc->dev;
6649 drm_i915_private_t *dev_priv = dev->dev_private;
6650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6651 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006652 int dpll_reg = DPLL(pipe);
6653 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07006654
Eric Anholtbad720f2009-10-22 16:11:14 -07006655 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006656 return;
6657
6658 if (!dev_priv->lvds_downclock_avail)
6659 return;
6660
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006661 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006662 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08006663 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006664
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006665 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006666
6667 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6668 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006669 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006670
Jesse Barnes652c3932009-08-17 13:31:43 -07006671 dpll = I915_READ(dpll_reg);
6672 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08006673 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006674 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006675}
6676
6677static void intel_decrease_pllclock(struct drm_crtc *crtc)
6678{
6679 struct drm_device *dev = crtc->dev;
6680 drm_i915_private_t *dev_priv = dev->dev_private;
6681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006682
Eric Anholtbad720f2009-10-22 16:11:14 -07006683 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006684 return;
6685
6686 if (!dev_priv->lvds_downclock_avail)
6687 return;
6688
6689 /*
6690 * Since this is called by a timer, we should never get here in
6691 * the manual case.
6692 */
6693 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01006694 int pipe = intel_crtc->pipe;
6695 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02006696 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01006697
Zhao Yakui44d98a62009-10-09 11:39:40 +08006698 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006699
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006700 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006701
Chris Wilson074b5e12012-05-02 12:07:06 +01006702 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006703 dpll |= DISPLAY_RATE_SELECT_FPA1;
6704 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006705 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006706 dpll = I915_READ(dpll_reg);
6707 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08006708 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006709 }
6710
6711}
6712
Chris Wilsonf047e392012-07-21 12:31:41 +01006713void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006714{
Chris Wilsonf047e392012-07-21 12:31:41 +01006715 i915_update_gfx_val(dev->dev_private);
6716}
6717
6718void intel_mark_idle(struct drm_device *dev)
6719{
Chris Wilsonf047e392012-07-21 12:31:41 +01006720}
6721
6722void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
6723{
6724 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07006725 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006726
6727 if (!i915_powersave)
6728 return;
6729
Jesse Barnes652c3932009-08-17 13:31:43 -07006730 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006731 if (!crtc->fb)
6732 continue;
6733
Chris Wilsonf047e392012-07-21 12:31:41 +01006734 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6735 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006736 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006737}
6738
Chris Wilsonf047e392012-07-21 12:31:41 +01006739void intel_mark_fb_idle(struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07006740{
Chris Wilsonf047e392012-07-21 12:31:41 +01006741 struct drm_device *dev = obj->base.dev;
6742 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006743
Chris Wilsonf047e392012-07-21 12:31:41 +01006744 if (!i915_powersave)
Chris Wilsonacb87df2012-05-03 15:47:57 +01006745 return;
6746
Jesse Barnes652c3932009-08-17 13:31:43 -07006747 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6748 if (!crtc->fb)
6749 continue;
6750
Chris Wilsonf047e392012-07-21 12:31:41 +01006751 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6752 intel_decrease_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006753 }
6754}
6755
Jesse Barnes79e53942008-11-07 14:24:08 -08006756static void intel_crtc_destroy(struct drm_crtc *crtc)
6757{
6758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006759 struct drm_device *dev = crtc->dev;
6760 struct intel_unpin_work *work;
6761 unsigned long flags;
6762
6763 spin_lock_irqsave(&dev->event_lock, flags);
6764 work = intel_crtc->unpin_work;
6765 intel_crtc->unpin_work = NULL;
6766 spin_unlock_irqrestore(&dev->event_lock, flags);
6767
6768 if (work) {
6769 cancel_work_sync(&work->work);
6770 kfree(work);
6771 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006772
6773 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006774
Jesse Barnes79e53942008-11-07 14:24:08 -08006775 kfree(intel_crtc);
6776}
6777
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006778static void intel_unpin_work_fn(struct work_struct *__work)
6779{
6780 struct intel_unpin_work *work =
6781 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006782 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006783
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006784 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01006785 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00006786 drm_gem_object_unreference(&work->pending_flip_obj->base);
6787 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00006788
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006789 intel_update_fbc(dev);
6790 mutex_unlock(&dev->struct_mutex);
6791
6792 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
6793 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
6794
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006795 kfree(work);
6796}
6797
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006798static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01006799 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006800{
6801 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006802 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6803 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00006804 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006805 unsigned long flags;
6806
6807 /* Ignore early vblank irqs */
6808 if (intel_crtc == NULL)
6809 return;
6810
6811 spin_lock_irqsave(&dev->event_lock, flags);
6812 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00006813
6814 /* Ensure we don't miss a work->pending update ... */
6815 smp_rmb();
6816
6817 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006818 spin_unlock_irqrestore(&dev->event_lock, flags);
6819 return;
6820 }
6821
Chris Wilsone7d841c2012-12-03 11:36:30 +00006822 /* and that the unpin work is consistent wrt ->pending. */
6823 smp_rmb();
6824
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006825 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006826
Rob Clark45a066e2012-10-08 14:50:40 -05006827 if (work->event)
6828 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006829
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01006830 drm_vblank_put(dev, intel_crtc->pipe);
6831
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006832 spin_unlock_irqrestore(&dev->event_lock, flags);
6833
Chris Wilson05394f32010-11-08 19:18:58 +00006834 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00006835
Daniel Vetter2c10d572012-12-20 21:24:07 +01006836 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006837
6838 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07006839
6840 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006841}
6842
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006843void intel_finish_page_flip(struct drm_device *dev, int pipe)
6844{
6845 drm_i915_private_t *dev_priv = dev->dev_private;
6846 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
6847
Mario Kleiner49b14a52010-12-09 07:00:07 +01006848 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006849}
6850
6851void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
6852{
6853 drm_i915_private_t *dev_priv = dev->dev_private;
6854 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
6855
Mario Kleiner49b14a52010-12-09 07:00:07 +01006856 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006857}
6858
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006859void intel_prepare_page_flip(struct drm_device *dev, int plane)
6860{
6861 drm_i915_private_t *dev_priv = dev->dev_private;
6862 struct intel_crtc *intel_crtc =
6863 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
6864 unsigned long flags;
6865
Chris Wilsone7d841c2012-12-03 11:36:30 +00006866 /* NB: An MMIO update of the plane base pointer will also
6867 * generate a page-flip completion irq, i.e. every modeset
6868 * is also accompanied by a spurious intel_prepare_page_flip().
6869 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006870 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00006871 if (intel_crtc->unpin_work)
6872 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006873 spin_unlock_irqrestore(&dev->event_lock, flags);
6874}
6875
Chris Wilsone7d841c2012-12-03 11:36:30 +00006876inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
6877{
6878 /* Ensure that the work item is consistent when activating it ... */
6879 smp_wmb();
6880 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
6881 /* and that it is marked active as soon as the irq could fire. */
6882 smp_wmb();
6883}
6884
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006885static int intel_gen2_queue_flip(struct drm_device *dev,
6886 struct drm_crtc *crtc,
6887 struct drm_framebuffer *fb,
6888 struct drm_i915_gem_object *obj)
6889{
6890 struct drm_i915_private *dev_priv = dev->dev_private;
6891 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006892 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006893 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006894 int ret;
6895
Daniel Vetter6d90c952012-04-26 23:28:05 +02006896 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006897 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006898 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006899
Daniel Vetter6d90c952012-04-26 23:28:05 +02006900 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006901 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006902 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006903
6904 /* Can't queue multiple flips, so wait for the previous
6905 * one to finish before executing the next.
6906 */
6907 if (intel_crtc->plane)
6908 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6909 else
6910 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006911 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6912 intel_ring_emit(ring, MI_NOOP);
6913 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6914 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6915 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02006916 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006917 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00006918
6919 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006920 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006921 return 0;
6922
6923err_unpin:
6924 intel_unpin_fb_obj(obj);
6925err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006926 return ret;
6927}
6928
6929static int intel_gen3_queue_flip(struct drm_device *dev,
6930 struct drm_crtc *crtc,
6931 struct drm_framebuffer *fb,
6932 struct drm_i915_gem_object *obj)
6933{
6934 struct drm_i915_private *dev_priv = dev->dev_private;
6935 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006936 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006937 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006938 int ret;
6939
Daniel Vetter6d90c952012-04-26 23:28:05 +02006940 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006941 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006942 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006943
Daniel Vetter6d90c952012-04-26 23:28:05 +02006944 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006945 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006946 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006947
6948 if (intel_crtc->plane)
6949 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6950 else
6951 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006952 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6953 intel_ring_emit(ring, MI_NOOP);
6954 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
6955 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6956 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02006957 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006958 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006959
Chris Wilsone7d841c2012-12-03 11:36:30 +00006960 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006961 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006962 return 0;
6963
6964err_unpin:
6965 intel_unpin_fb_obj(obj);
6966err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006967 return ret;
6968}
6969
6970static int intel_gen4_queue_flip(struct drm_device *dev,
6971 struct drm_crtc *crtc,
6972 struct drm_framebuffer *fb,
6973 struct drm_i915_gem_object *obj)
6974{
6975 struct drm_i915_private *dev_priv = dev->dev_private;
6976 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6977 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006978 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006979 int ret;
6980
Daniel Vetter6d90c952012-04-26 23:28:05 +02006981 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006982 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006983 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006984
Daniel Vetter6d90c952012-04-26 23:28:05 +02006985 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006986 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006987 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006988
6989 /* i965+ uses the linear or tiled offsets from the
6990 * Display Registers (which do not change across a page-flip)
6991 * so we need only reprogram the base address.
6992 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02006993 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6994 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6995 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02006996 intel_ring_emit(ring,
6997 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
6998 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006999
7000 /* XXX Enabling the panel-fitter across page-flip is so far
7001 * untested on non-native modes, so ignore it for now.
7002 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7003 */
7004 pf = 0;
7005 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007006 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007007
7008 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007009 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007010 return 0;
7011
7012err_unpin:
7013 intel_unpin_fb_obj(obj);
7014err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007015 return ret;
7016}
7017
7018static int intel_gen6_queue_flip(struct drm_device *dev,
7019 struct drm_crtc *crtc,
7020 struct drm_framebuffer *fb,
7021 struct drm_i915_gem_object *obj)
7022{
7023 struct drm_i915_private *dev_priv = dev->dev_private;
7024 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007025 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007026 uint32_t pf, pipesrc;
7027 int ret;
7028
Daniel Vetter6d90c952012-04-26 23:28:05 +02007029 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007030 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007031 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007032
Daniel Vetter6d90c952012-04-26 23:28:05 +02007033 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007034 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007035 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007036
Daniel Vetter6d90c952012-04-26 23:28:05 +02007037 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7038 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7039 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007040 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007041
Chris Wilson99d9acd2012-04-17 20:37:00 +01007042 /* Contrary to the suggestions in the documentation,
7043 * "Enable Panel Fitter" does not seem to be required when page
7044 * flipping with a non-native mode, and worse causes a normal
7045 * modeset to fail.
7046 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7047 */
7048 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007049 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007050 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007051
7052 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007053 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007054 return 0;
7055
7056err_unpin:
7057 intel_unpin_fb_obj(obj);
7058err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007059 return ret;
7060}
7061
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007062/*
7063 * On gen7 we currently use the blit ring because (in early silicon at least)
7064 * the render ring doesn't give us interrpts for page flip completion, which
7065 * means clients will hang after the first flip is queued. Fortunately the
7066 * blit ring generates interrupts properly, so use it instead.
7067 */
7068static int intel_gen7_queue_flip(struct drm_device *dev,
7069 struct drm_crtc *crtc,
7070 struct drm_framebuffer *fb,
7071 struct drm_i915_gem_object *obj)
7072{
7073 struct drm_i915_private *dev_priv = dev->dev_private;
7074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7075 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007076 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007077 int ret;
7078
7079 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7080 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007081 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007082
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007083 switch(intel_crtc->plane) {
7084 case PLANE_A:
7085 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7086 break;
7087 case PLANE_B:
7088 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7089 break;
7090 case PLANE_C:
7091 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7092 break;
7093 default:
7094 WARN_ONCE(1, "unknown plane in flip command\n");
7095 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007096 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007097 }
7098
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007099 ret = intel_ring_begin(ring, 4);
7100 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007101 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007102
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007103 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007104 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Daniel Vetterc2c75132012-07-05 12:17:30 +02007105 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007106 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007107
7108 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007109 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007110 return 0;
7111
7112err_unpin:
7113 intel_unpin_fb_obj(obj);
7114err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007115 return ret;
7116}
7117
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007118static int intel_default_queue_flip(struct drm_device *dev,
7119 struct drm_crtc *crtc,
7120 struct drm_framebuffer *fb,
7121 struct drm_i915_gem_object *obj)
7122{
7123 return -ENODEV;
7124}
7125
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007126static int intel_crtc_page_flip(struct drm_crtc *crtc,
7127 struct drm_framebuffer *fb,
7128 struct drm_pending_vblank_event *event)
7129{
7130 struct drm_device *dev = crtc->dev;
7131 struct drm_i915_private *dev_priv = dev->dev_private;
7132 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007133 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7135 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007136 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007137 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007138
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007139 /* Can't change pixel format via MI display flips. */
7140 if (fb->pixel_format != crtc->fb->pixel_format)
7141 return -EINVAL;
7142
7143 /*
7144 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7145 * Note that pitch changes could also affect these register.
7146 */
7147 if (INTEL_INFO(dev)->gen > 3 &&
7148 (fb->offsets[0] != crtc->fb->offsets[0] ||
7149 fb->pitches[0] != crtc->fb->pitches[0]))
7150 return -EINVAL;
7151
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007152 work = kzalloc(sizeof *work, GFP_KERNEL);
7153 if (work == NULL)
7154 return -ENOMEM;
7155
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007156 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007157 work->crtc = crtc;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007158 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08007159 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007160 INIT_WORK(&work->work, intel_unpin_work_fn);
7161
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007162 ret = drm_vblank_get(dev, intel_crtc->pipe);
7163 if (ret)
7164 goto free_work;
7165
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007166 /* We borrow the event spin lock for protecting unpin_work */
7167 spin_lock_irqsave(&dev->event_lock, flags);
7168 if (intel_crtc->unpin_work) {
7169 spin_unlock_irqrestore(&dev->event_lock, flags);
7170 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007171 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007172
7173 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007174 return -EBUSY;
7175 }
7176 intel_crtc->unpin_work = work;
7177 spin_unlock_irqrestore(&dev->event_lock, flags);
7178
7179 intel_fb = to_intel_framebuffer(fb);
7180 obj = intel_fb->obj;
7181
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007182 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7183 flush_workqueue(dev_priv->wq);
7184
Chris Wilson79158102012-05-23 11:13:58 +01007185 ret = i915_mutex_lock_interruptible(dev);
7186 if (ret)
7187 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007188
Jesse Barnes75dfca82010-02-10 15:09:44 -08007189 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007190 drm_gem_object_reference(&work->old_fb_obj->base);
7191 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007192
7193 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007194
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007195 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007196
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007197 work->enable_stall_check = true;
7198
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007199 atomic_inc(&intel_crtc->unpin_work_count);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007200
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007201 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7202 if (ret)
7203 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007204
Chris Wilson7782de32011-07-08 12:22:41 +01007205 intel_disable_fbc(dev);
Chris Wilsonf047e392012-07-21 12:31:41 +01007206 intel_mark_fb_busy(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007207 mutex_unlock(&dev->struct_mutex);
7208
Jesse Barnese5510fa2010-07-01 16:48:37 -07007209 trace_i915_flip_request(intel_crtc->plane, obj);
7210
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007211 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007212
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007213cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007214 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson05394f32010-11-08 19:18:58 +00007215 drm_gem_object_unreference(&work->old_fb_obj->base);
7216 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007217 mutex_unlock(&dev->struct_mutex);
7218
Chris Wilson79158102012-05-23 11:13:58 +01007219cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007220 spin_lock_irqsave(&dev->event_lock, flags);
7221 intel_crtc->unpin_work = NULL;
7222 spin_unlock_irqrestore(&dev->event_lock, flags);
7223
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007224 drm_vblank_put(dev, intel_crtc->pipe);
7225free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007226 kfree(work);
7227
7228 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007229}
7230
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007231static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007232 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7233 .load_lut = intel_crtc_load_lut,
Daniel Vetter976f8a22012-07-08 22:34:21 +02007234 .disable = intel_crtc_noop,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007235};
7236
Daniel Vetter6ed0f792012-07-08 19:41:43 +02007237bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
7238{
7239 struct intel_encoder *other_encoder;
7240 struct drm_crtc *crtc = &encoder->new_crtc->base;
7241
7242 if (WARN_ON(!crtc))
7243 return false;
7244
7245 list_for_each_entry(other_encoder,
7246 &crtc->dev->mode_config.encoder_list,
7247 base.head) {
7248
7249 if (&other_encoder->new_crtc->base != crtc ||
7250 encoder == other_encoder)
7251 continue;
7252 else
7253 return true;
7254 }
7255
7256 return false;
7257}
7258
Daniel Vetter50f56112012-07-02 09:35:43 +02007259static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7260 struct drm_crtc *crtc)
7261{
7262 struct drm_device *dev;
7263 struct drm_crtc *tmp;
7264 int crtc_mask = 1;
7265
7266 WARN(!crtc, "checking null crtc?\n");
7267
7268 dev = crtc->dev;
7269
7270 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7271 if (tmp == crtc)
7272 break;
7273 crtc_mask <<= 1;
7274 }
7275
7276 if (encoder->possible_crtcs & crtc_mask)
7277 return true;
7278 return false;
7279}
7280
Daniel Vetter9a935852012-07-05 22:34:27 +02007281/**
7282 * intel_modeset_update_staged_output_state
7283 *
7284 * Updates the staged output configuration state, e.g. after we've read out the
7285 * current hw state.
7286 */
7287static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7288{
7289 struct intel_encoder *encoder;
7290 struct intel_connector *connector;
7291
7292 list_for_each_entry(connector, &dev->mode_config.connector_list,
7293 base.head) {
7294 connector->new_encoder =
7295 to_intel_encoder(connector->base.encoder);
7296 }
7297
7298 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7299 base.head) {
7300 encoder->new_crtc =
7301 to_intel_crtc(encoder->base.crtc);
7302 }
7303}
7304
7305/**
7306 * intel_modeset_commit_output_state
7307 *
7308 * This function copies the stage display pipe configuration to the real one.
7309 */
7310static void intel_modeset_commit_output_state(struct drm_device *dev)
7311{
7312 struct intel_encoder *encoder;
7313 struct intel_connector *connector;
7314
7315 list_for_each_entry(connector, &dev->mode_config.connector_list,
7316 base.head) {
7317 connector->base.encoder = &connector->new_encoder->base;
7318 }
7319
7320 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7321 base.head) {
7322 encoder->base.crtc = &encoder->new_crtc->base;
7323 }
7324}
7325
Daniel Vetter7758a112012-07-08 19:40:39 +02007326static struct drm_display_mode *
7327intel_modeset_adjusted_mode(struct drm_crtc *crtc,
7328 struct drm_display_mode *mode)
7329{
7330 struct drm_device *dev = crtc->dev;
7331 struct drm_display_mode *adjusted_mode;
7332 struct drm_encoder_helper_funcs *encoder_funcs;
7333 struct intel_encoder *encoder;
7334
7335 adjusted_mode = drm_mode_duplicate(dev, mode);
7336 if (!adjusted_mode)
7337 return ERR_PTR(-ENOMEM);
7338
7339 /* Pass our mode to the connectors and the CRTC to give them a chance to
7340 * adjust it according to limitations or connector properties, and also
7341 * a chance to reject the mode entirely.
7342 */
7343 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7344 base.head) {
7345
7346 if (&encoder->new_crtc->base != crtc)
7347 continue;
7348 encoder_funcs = encoder->base.helper_private;
7349 if (!(encoder_funcs->mode_fixup(&encoder->base, mode,
7350 adjusted_mode))) {
7351 DRM_DEBUG_KMS("Encoder fixup failed\n");
7352 goto fail;
7353 }
7354 }
7355
7356 if (!(intel_crtc_mode_fixup(crtc, mode, adjusted_mode))) {
7357 DRM_DEBUG_KMS("CRTC fixup failed\n");
7358 goto fail;
7359 }
7360 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
7361
7362 return adjusted_mode;
7363fail:
7364 drm_mode_destroy(dev, adjusted_mode);
7365 return ERR_PTR(-EINVAL);
7366}
7367
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007368/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7369 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7370static void
7371intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7372 unsigned *prepare_pipes, unsigned *disable_pipes)
7373{
7374 struct intel_crtc *intel_crtc;
7375 struct drm_device *dev = crtc->dev;
7376 struct intel_encoder *encoder;
7377 struct intel_connector *connector;
7378 struct drm_crtc *tmp_crtc;
7379
7380 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7381
7382 /* Check which crtcs have changed outputs connected to them, these need
7383 * to be part of the prepare_pipes mask. We don't (yet) support global
7384 * modeset across multiple crtcs, so modeset_pipes will only have one
7385 * bit set at most. */
7386 list_for_each_entry(connector, &dev->mode_config.connector_list,
7387 base.head) {
7388 if (connector->base.encoder == &connector->new_encoder->base)
7389 continue;
7390
7391 if (connector->base.encoder) {
7392 tmp_crtc = connector->base.encoder->crtc;
7393
7394 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7395 }
7396
7397 if (connector->new_encoder)
7398 *prepare_pipes |=
7399 1 << connector->new_encoder->new_crtc->pipe;
7400 }
7401
7402 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7403 base.head) {
7404 if (encoder->base.crtc == &encoder->new_crtc->base)
7405 continue;
7406
7407 if (encoder->base.crtc) {
7408 tmp_crtc = encoder->base.crtc;
7409
7410 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7411 }
7412
7413 if (encoder->new_crtc)
7414 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7415 }
7416
7417 /* Check for any pipes that will be fully disabled ... */
7418 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7419 base.head) {
7420 bool used = false;
7421
7422 /* Don't try to disable disabled crtcs. */
7423 if (!intel_crtc->base.enabled)
7424 continue;
7425
7426 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7427 base.head) {
7428 if (encoder->new_crtc == intel_crtc)
7429 used = true;
7430 }
7431
7432 if (!used)
7433 *disable_pipes |= 1 << intel_crtc->pipe;
7434 }
7435
7436
7437 /* set_mode is also used to update properties on life display pipes. */
7438 intel_crtc = to_intel_crtc(crtc);
7439 if (crtc->enabled)
7440 *prepare_pipes |= 1 << intel_crtc->pipe;
7441
7442 /* We only support modeset on one single crtc, hence we need to do that
7443 * only for the passed in crtc iff we change anything else than just
7444 * disable crtcs.
7445 *
7446 * This is actually not true, to be fully compatible with the old crtc
7447 * helper we automatically disable _any_ output (i.e. doesn't need to be
7448 * connected to the crtc we're modesetting on) if it's disconnected.
7449 * Which is a rather nutty api (since changed the output configuration
7450 * without userspace's explicit request can lead to confusion), but
7451 * alas. Hence we currently need to modeset on all pipes we prepare. */
7452 if (*prepare_pipes)
7453 *modeset_pipes = *prepare_pipes;
7454
7455 /* ... and mask these out. */
7456 *modeset_pipes &= ~(*disable_pipes);
7457 *prepare_pipes &= ~(*disable_pipes);
7458}
7459
Daniel Vetterea9d7582012-07-10 10:42:52 +02007460static bool intel_crtc_in_use(struct drm_crtc *crtc)
7461{
7462 struct drm_encoder *encoder;
7463 struct drm_device *dev = crtc->dev;
7464
7465 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
7466 if (encoder->crtc == crtc)
7467 return true;
7468
7469 return false;
7470}
7471
7472static void
7473intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
7474{
7475 struct intel_encoder *intel_encoder;
7476 struct intel_crtc *intel_crtc;
7477 struct drm_connector *connector;
7478
7479 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
7480 base.head) {
7481 if (!intel_encoder->base.crtc)
7482 continue;
7483
7484 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
7485
7486 if (prepare_pipes & (1 << intel_crtc->pipe))
7487 intel_encoder->connectors_active = false;
7488 }
7489
7490 intel_modeset_commit_output_state(dev);
7491
7492 /* Update computed state. */
7493 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7494 base.head) {
7495 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
7496 }
7497
7498 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7499 if (!connector->encoder || !connector->encoder->crtc)
7500 continue;
7501
7502 intel_crtc = to_intel_crtc(connector->encoder->crtc);
7503
7504 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02007505 struct drm_property *dpms_property =
7506 dev->mode_config.dpms_property;
7507
Daniel Vetterea9d7582012-07-10 10:42:52 +02007508 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05007509 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02007510 dpms_property,
7511 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02007512
7513 intel_encoder = to_intel_encoder(connector->encoder);
7514 intel_encoder->connectors_active = true;
7515 }
7516 }
7517
7518}
7519
Daniel Vetter25c5b262012-07-08 22:08:04 +02007520#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
7521 list_for_each_entry((intel_crtc), \
7522 &(dev)->mode_config.crtc_list, \
7523 base.head) \
7524 if (mask & (1 <<(intel_crtc)->pipe)) \
7525
Daniel Vetterb9805142012-08-31 17:37:33 +02007526void
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007527intel_modeset_check_state(struct drm_device *dev)
7528{
7529 struct intel_crtc *crtc;
7530 struct intel_encoder *encoder;
7531 struct intel_connector *connector;
7532
7533 list_for_each_entry(connector, &dev->mode_config.connector_list,
7534 base.head) {
7535 /* This also checks the encoder/connector hw state with the
7536 * ->get_hw_state callbacks. */
7537 intel_connector_check_state(connector);
7538
7539 WARN(&connector->new_encoder->base != connector->base.encoder,
7540 "connector's staged encoder doesn't match current encoder\n");
7541 }
7542
7543 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7544 base.head) {
7545 bool enabled = false;
7546 bool active = false;
7547 enum pipe pipe, tracked_pipe;
7548
7549 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
7550 encoder->base.base.id,
7551 drm_get_encoder_name(&encoder->base));
7552
7553 WARN(&encoder->new_crtc->base != encoder->base.crtc,
7554 "encoder's stage crtc doesn't match current crtc\n");
7555 WARN(encoder->connectors_active && !encoder->base.crtc,
7556 "encoder's active_connectors set, but no crtc\n");
7557
7558 list_for_each_entry(connector, &dev->mode_config.connector_list,
7559 base.head) {
7560 if (connector->base.encoder != &encoder->base)
7561 continue;
7562 enabled = true;
7563 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
7564 active = true;
7565 }
7566 WARN(!!encoder->base.crtc != enabled,
7567 "encoder's enabled state mismatch "
7568 "(expected %i, found %i)\n",
7569 !!encoder->base.crtc, enabled);
7570 WARN(active && !encoder->base.crtc,
7571 "active encoder with no crtc\n");
7572
7573 WARN(encoder->connectors_active != active,
7574 "encoder's computed active state doesn't match tracked active state "
7575 "(expected %i, found %i)\n", active, encoder->connectors_active);
7576
7577 active = encoder->get_hw_state(encoder, &pipe);
7578 WARN(active != encoder->connectors_active,
7579 "encoder's hw state doesn't match sw tracking "
7580 "(expected %i, found %i)\n",
7581 encoder->connectors_active, active);
7582
7583 if (!encoder->base.crtc)
7584 continue;
7585
7586 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
7587 WARN(active && pipe != tracked_pipe,
7588 "active encoder's pipe doesn't match"
7589 "(expected %i, found %i)\n",
7590 tracked_pipe, pipe);
7591
7592 }
7593
7594 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
7595 base.head) {
7596 bool enabled = false;
7597 bool active = false;
7598
7599 DRM_DEBUG_KMS("[CRTC:%d]\n",
7600 crtc->base.base.id);
7601
7602 WARN(crtc->active && !crtc->base.enabled,
7603 "active crtc, but not enabled in sw tracking\n");
7604
7605 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7606 base.head) {
7607 if (encoder->base.crtc != &crtc->base)
7608 continue;
7609 enabled = true;
7610 if (encoder->connectors_active)
7611 active = true;
7612 }
7613 WARN(active != crtc->active,
7614 "crtc's computed active state doesn't match tracked active state "
7615 "(expected %i, found %i)\n", active, crtc->active);
7616 WARN(enabled != crtc->base.enabled,
7617 "crtc's computed enabled state doesn't match tracked enabled state "
7618 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
7619
7620 assert_pipe(dev->dev_private, crtc->pipe, crtc->active);
7621 }
7622}
7623
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007624int intel_set_mode(struct drm_crtc *crtc,
7625 struct drm_display_mode *mode,
7626 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02007627{
7628 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02007629 drm_i915_private_t *dev_priv = dev->dev_private;
Tim Gardner3ac18232012-12-07 07:54:26 -07007630 struct drm_display_mode *adjusted_mode, *saved_mode, *saved_hwmode;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007631 struct intel_crtc *intel_crtc;
7632 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007633 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02007634
Tim Gardner3ac18232012-12-07 07:54:26 -07007635 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007636 if (!saved_mode)
7637 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07007638 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02007639
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007640 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02007641 &prepare_pipes, &disable_pipes);
7642
7643 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7644 modeset_pipes, prepare_pipes, disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007645
Daniel Vetter976f8a22012-07-08 22:34:21 +02007646 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
7647 intel_crtc_disable(&intel_crtc->base);
7648
Tim Gardner3ac18232012-12-07 07:54:26 -07007649 *saved_hwmode = crtc->hwmode;
7650 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007651
Daniel Vetter25c5b262012-07-08 22:08:04 +02007652 /* Hack: Because we don't (yet) support global modeset on multiple
7653 * crtcs, we don't keep track of the new mode for more than one crtc.
7654 * Hence simply check whether any bit is set in modeset_pipes in all the
7655 * pieces of code that are not yet converted to deal with mutliple crtcs
7656 * changing their mode at the same time. */
7657 adjusted_mode = NULL;
7658 if (modeset_pipes) {
7659 adjusted_mode = intel_modeset_adjusted_mode(crtc, mode);
7660 if (IS_ERR(adjusted_mode)) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007661 ret = PTR_ERR(adjusted_mode);
Tim Gardner3ac18232012-12-07 07:54:26 -07007662 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007663 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007664 }
7665
Daniel Vetterea9d7582012-07-10 10:42:52 +02007666 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
7667 if (intel_crtc->base.enabled)
7668 dev_priv->display.crtc_disable(&intel_crtc->base);
7669 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007670
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02007671 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
7672 * to set it here already despite that we pass it down the callchain.
7673 */
7674 if (modeset_pipes)
Daniel Vetter25c5b262012-07-08 22:08:04 +02007675 crtc->mode = *mode;
Daniel Vetter7758a112012-07-08 19:40:39 +02007676
Daniel Vetterea9d7582012-07-10 10:42:52 +02007677 /* Only after disabling all output pipelines that will be changed can we
7678 * update the the output configuration. */
7679 intel_modeset_update_state(dev, prepare_pipes);
7680
Daniel Vetter47fab732012-10-26 10:58:18 +02007681 if (dev_priv->display.modeset_global_resources)
7682 dev_priv->display.modeset_global_resources(dev);
7683
Daniel Vettera6778b32012-07-02 09:56:42 +02007684 /* Set up the DPLL and any encoders state that needs to adjust or depend
7685 * on the DPLL.
7686 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007687 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007688 ret = intel_crtc_mode_set(&intel_crtc->base,
7689 mode, adjusted_mode,
7690 x, y, fb);
7691 if (ret)
7692 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02007693 }
7694
7695 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007696 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
7697 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02007698
Daniel Vetter25c5b262012-07-08 22:08:04 +02007699 if (modeset_pipes) {
7700 /* Store real post-adjustment hardware mode. */
7701 crtc->hwmode = *adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007702
Daniel Vetter25c5b262012-07-08 22:08:04 +02007703 /* Calculate and store various constants which
7704 * are later needed by vblank and swap-completion
7705 * timestamping. They are derived from true hwmode.
7706 */
7707 drm_calc_timestamping_constants(crtc);
7708 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007709
7710 /* FIXME: add subpixel order */
7711done:
7712 drm_mode_destroy(dev, adjusted_mode);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007713 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07007714 crtc->hwmode = *saved_hwmode;
7715 crtc->mode = *saved_mode;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007716 } else {
7717 intel_modeset_check_state(dev);
Daniel Vettera6778b32012-07-02 09:56:42 +02007718 }
7719
Tim Gardner3ac18232012-12-07 07:54:26 -07007720out:
7721 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02007722 return ret;
7723}
7724
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007725void intel_crtc_restore_mode(struct drm_crtc *crtc)
7726{
7727 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
7728}
7729
Daniel Vetter25c5b262012-07-08 22:08:04 +02007730#undef for_each_intel_crtc_masked
7731
Daniel Vetterd9e55602012-07-04 22:16:09 +02007732static void intel_set_config_free(struct intel_set_config *config)
7733{
7734 if (!config)
7735 return;
7736
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007737 kfree(config->save_connector_encoders);
7738 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02007739 kfree(config);
7740}
7741
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007742static int intel_set_config_save_state(struct drm_device *dev,
7743 struct intel_set_config *config)
7744{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007745 struct drm_encoder *encoder;
7746 struct drm_connector *connector;
7747 int count;
7748
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007749 config->save_encoder_crtcs =
7750 kcalloc(dev->mode_config.num_encoder,
7751 sizeof(struct drm_crtc *), GFP_KERNEL);
7752 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007753 return -ENOMEM;
7754
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007755 config->save_connector_encoders =
7756 kcalloc(dev->mode_config.num_connector,
7757 sizeof(struct drm_encoder *), GFP_KERNEL);
7758 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007759 return -ENOMEM;
7760
7761 /* Copy data. Note that driver private data is not affected.
7762 * Should anything bad happen only the expected state is
7763 * restored, not the drivers personal bookkeeping.
7764 */
7765 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007766 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007767 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007768 }
7769
7770 count = 0;
7771 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007772 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007773 }
7774
7775 return 0;
7776}
7777
7778static void intel_set_config_restore_state(struct drm_device *dev,
7779 struct intel_set_config *config)
7780{
Daniel Vetter9a935852012-07-05 22:34:27 +02007781 struct intel_encoder *encoder;
7782 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007783 int count;
7784
7785 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007786 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7787 encoder->new_crtc =
7788 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007789 }
7790
7791 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007792 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
7793 connector->new_encoder =
7794 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007795 }
7796}
7797
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007798static void
7799intel_set_config_compute_mode_changes(struct drm_mode_set *set,
7800 struct intel_set_config *config)
7801{
7802
7803 /* We should be able to check here if the fb has the same properties
7804 * and then just flip_or_move it */
7805 if (set->crtc->fb != set->fb) {
7806 /* If we have no fb then treat it as a full mode set */
7807 if (set->crtc->fb == NULL) {
7808 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
7809 config->mode_changed = true;
7810 } else if (set->fb == NULL) {
7811 config->mode_changed = true;
7812 } else if (set->fb->depth != set->crtc->fb->depth) {
7813 config->mode_changed = true;
7814 } else if (set->fb->bits_per_pixel !=
7815 set->crtc->fb->bits_per_pixel) {
7816 config->mode_changed = true;
7817 } else
7818 config->fb_changed = true;
7819 }
7820
Daniel Vetter835c5872012-07-10 18:11:08 +02007821 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007822 config->fb_changed = true;
7823
7824 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
7825 DRM_DEBUG_KMS("modes are different, full mode set\n");
7826 drm_mode_debug_printmodeline(&set->crtc->mode);
7827 drm_mode_debug_printmodeline(set->mode);
7828 config->mode_changed = true;
7829 }
7830}
7831
Daniel Vetter2e431052012-07-04 22:42:15 +02007832static int
Daniel Vetter9a935852012-07-05 22:34:27 +02007833intel_modeset_stage_output_state(struct drm_device *dev,
7834 struct drm_mode_set *set,
7835 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02007836{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007837 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02007838 struct intel_connector *connector;
7839 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02007840 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02007841
Daniel Vetter9a935852012-07-05 22:34:27 +02007842 /* The upper layers ensure that we either disabl a crtc or have a list
7843 * of connectors. For paranoia, double-check this. */
7844 WARN_ON(!set->fb && (set->num_connectors != 0));
7845 WARN_ON(set->fb && (set->num_connectors == 0));
7846
Daniel Vetter50f56112012-07-02 09:35:43 +02007847 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007848 list_for_each_entry(connector, &dev->mode_config.connector_list,
7849 base.head) {
7850 /* Otherwise traverse passed in connector list and get encoders
7851 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007852 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02007853 if (set->connectors[ro] == &connector->base) {
7854 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02007855 break;
7856 }
7857 }
7858
Daniel Vetter9a935852012-07-05 22:34:27 +02007859 /* If we disable the crtc, disable all its connectors. Also, if
7860 * the connector is on the changing crtc but not on the new
7861 * connector list, disable it. */
7862 if ((!set->fb || ro == set->num_connectors) &&
7863 connector->base.encoder &&
7864 connector->base.encoder->crtc == set->crtc) {
7865 connector->new_encoder = NULL;
7866
7867 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
7868 connector->base.base.id,
7869 drm_get_connector_name(&connector->base));
7870 }
7871
7872
7873 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007874 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007875 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02007876 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007877 }
7878 /* connector->new_encoder is now updated for all connectors. */
7879
7880 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007881 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007882 list_for_each_entry(connector, &dev->mode_config.connector_list,
7883 base.head) {
7884 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02007885 continue;
7886
Daniel Vetter9a935852012-07-05 22:34:27 +02007887 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02007888
7889 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02007890 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02007891 new_crtc = set->crtc;
7892 }
7893
7894 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02007895 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
7896 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007897 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02007898 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007899 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
7900
7901 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
7902 connector->base.base.id,
7903 drm_get_connector_name(&connector->base),
7904 new_crtc->base.id);
7905 }
7906
7907 /* Check for any encoders that needs to be disabled. */
7908 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7909 base.head) {
7910 list_for_each_entry(connector,
7911 &dev->mode_config.connector_list,
7912 base.head) {
7913 if (connector->new_encoder == encoder) {
7914 WARN_ON(!connector->new_encoder->new_crtc);
7915
7916 goto next_encoder;
7917 }
7918 }
7919 encoder->new_crtc = NULL;
7920next_encoder:
7921 /* Only now check for crtc changes so we don't miss encoders
7922 * that will be disabled. */
7923 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007924 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007925 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02007926 }
7927 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007928 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007929
Daniel Vetter2e431052012-07-04 22:42:15 +02007930 return 0;
7931}
7932
7933static int intel_crtc_set_config(struct drm_mode_set *set)
7934{
7935 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02007936 struct drm_mode_set save_set;
7937 struct intel_set_config *config;
7938 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02007939
Daniel Vetter8d3e3752012-07-05 16:09:09 +02007940 BUG_ON(!set);
7941 BUG_ON(!set->crtc);
7942 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02007943
7944 if (!set->mode)
7945 set->fb = NULL;
7946
Daniel Vetter431e50f2012-07-10 17:53:42 +02007947 /* The fb helper likes to play gross jokes with ->mode_set_config.
7948 * Unfortunately the crtc helper doesn't do much at all for this case,
7949 * so we have to cope with this madness until the fb helper is fixed up. */
7950 if (set->fb && set->num_connectors == 0)
7951 return 0;
7952
Daniel Vetter2e431052012-07-04 22:42:15 +02007953 if (set->fb) {
7954 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
7955 set->crtc->base.id, set->fb->base.id,
7956 (int)set->num_connectors, set->x, set->y);
7957 } else {
7958 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02007959 }
7960
7961 dev = set->crtc->dev;
7962
7963 ret = -ENOMEM;
7964 config = kzalloc(sizeof(*config), GFP_KERNEL);
7965 if (!config)
7966 goto out_config;
7967
7968 ret = intel_set_config_save_state(dev, config);
7969 if (ret)
7970 goto out_config;
7971
7972 save_set.crtc = set->crtc;
7973 save_set.mode = &set->crtc->mode;
7974 save_set.x = set->crtc->x;
7975 save_set.y = set->crtc->y;
7976 save_set.fb = set->crtc->fb;
7977
7978 /* Compute whether we need a full modeset, only an fb base update or no
7979 * change at all. In the future we might also check whether only the
7980 * mode changed, e.g. for LVDS where we only change the panel fitter in
7981 * such cases. */
7982 intel_set_config_compute_mode_changes(set, config);
7983
Daniel Vetter9a935852012-07-05 22:34:27 +02007984 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02007985 if (ret)
7986 goto fail;
7987
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007988 if (config->mode_changed) {
Daniel Vetter87f1faa2012-07-05 23:36:17 +02007989 if (set->mode) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007990 DRM_DEBUG_KMS("attempting to set mode from"
7991 " userspace\n");
7992 drm_mode_debug_printmodeline(set->mode);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02007993 }
7994
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007995 ret = intel_set_mode(set->crtc, set->mode,
7996 set->x, set->y, set->fb);
7997 if (ret) {
7998 DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
7999 set->crtc->base.id, ret);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008000 goto fail;
8001 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008002 } else if (config->fb_changed) {
Daniel Vetter4f660f42012-07-02 09:47:37 +02008003 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008004 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008005 }
8006
Daniel Vetterd9e55602012-07-04 22:16:09 +02008007 intel_set_config_free(config);
8008
Daniel Vetter50f56112012-07-02 09:35:43 +02008009 return 0;
8010
8011fail:
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008012 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008013
8014 /* Try to restore the config */
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008015 if (config->mode_changed &&
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008016 intel_set_mode(save_set.crtc, save_set.mode,
8017 save_set.x, save_set.y, save_set.fb))
Daniel Vetter50f56112012-07-02 09:35:43 +02008018 DRM_ERROR("failed to restore config after modeset failure\n");
8019
Daniel Vetterd9e55602012-07-04 22:16:09 +02008020out_config:
8021 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008022 return ret;
8023}
8024
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008025static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008026 .cursor_set = intel_crtc_cursor_set,
8027 .cursor_move = intel_crtc_cursor_move,
8028 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008029 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008030 .destroy = intel_crtc_destroy,
8031 .page_flip = intel_crtc_page_flip,
8032};
8033
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008034static void intel_cpu_pll_init(struct drm_device *dev)
8035{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008036 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008037 intel_ddi_pll_init(dev);
8038}
8039
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008040static void intel_pch_pll_init(struct drm_device *dev)
8041{
8042 drm_i915_private_t *dev_priv = dev->dev_private;
8043 int i;
8044
8045 if (dev_priv->num_pch_pll == 0) {
8046 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
8047 return;
8048 }
8049
8050 for (i = 0; i < dev_priv->num_pch_pll; i++) {
8051 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
8052 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
8053 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
8054 }
8055}
8056
Hannes Ederb358d0a2008-12-18 21:18:47 +01008057static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08008058{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008059 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008060 struct intel_crtc *intel_crtc;
8061 int i;
8062
8063 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8064 if (intel_crtc == NULL)
8065 return;
8066
8067 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8068
8069 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08008070 for (i = 0; i < 256; i++) {
8071 intel_crtc->lut_r[i] = i;
8072 intel_crtc->lut_g[i] = i;
8073 intel_crtc->lut_b[i] = i;
8074 }
8075
Jesse Barnes80824002009-09-10 15:28:06 -07008076 /* Swap pipes & planes for FBC on pre-965 */
8077 intel_crtc->pipe = pipe;
8078 intel_crtc->plane = pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02008079 intel_crtc->cpu_transcoder = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01008080 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008081 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01008082 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07008083 }
8084
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008085 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8086 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8087 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8088 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8089
Jesse Barnes5a354202011-06-24 12:19:22 -07008090 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07008091
Jesse Barnes79e53942008-11-07 14:24:08 -08008092 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08008093}
8094
Carl Worth08d7b3d2009-04-29 14:43:54 -07008095int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00008096 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07008097{
Carl Worth08d7b3d2009-04-29 14:43:54 -07008098 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02008099 struct drm_mode_object *drmmode_obj;
8100 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008101
Daniel Vetter1cff8f62012-04-24 09:55:08 +02008102 if (!drm_core_check_feature(dev, DRIVER_MODESET))
8103 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008104
Daniel Vetterc05422d2009-08-11 16:05:30 +02008105 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8106 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07008107
Daniel Vetterc05422d2009-08-11 16:05:30 +02008108 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07008109 DRM_ERROR("no such CRTC id\n");
8110 return -EINVAL;
8111 }
8112
Daniel Vetterc05422d2009-08-11 16:05:30 +02008113 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8114 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008115
Daniel Vetterc05422d2009-08-11 16:05:30 +02008116 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008117}
8118
Daniel Vetter66a92782012-07-12 20:08:18 +02008119static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008120{
Daniel Vetter66a92782012-07-12 20:08:18 +02008121 struct drm_device *dev = encoder->base.dev;
8122 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008123 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008124 int entry = 0;
8125
Daniel Vetter66a92782012-07-12 20:08:18 +02008126 list_for_each_entry(source_encoder,
8127 &dev->mode_config.encoder_list, base.head) {
8128
8129 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008130 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02008131
8132 /* Intel hw has only one MUX where enocoders could be cloned. */
8133 if (encoder->cloneable && source_encoder->cloneable)
8134 index_mask |= (1 << entry);
8135
Jesse Barnes79e53942008-11-07 14:24:08 -08008136 entry++;
8137 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01008138
Jesse Barnes79e53942008-11-07 14:24:08 -08008139 return index_mask;
8140}
8141
Chris Wilson4d302442010-12-14 19:21:29 +00008142static bool has_edp_a(struct drm_device *dev)
8143{
8144 struct drm_i915_private *dev_priv = dev->dev_private;
8145
8146 if (!IS_MOBILE(dev))
8147 return false;
8148
8149 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8150 return false;
8151
8152 if (IS_GEN5(dev) &&
8153 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8154 return false;
8155
8156 return true;
8157}
8158
Jesse Barnes79e53942008-11-07 14:24:08 -08008159static void intel_setup_outputs(struct drm_device *dev)
8160{
Eric Anholt725e30a2009-01-22 13:01:02 -08008161 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008162 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008163 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008164 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08008165
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008166 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00008167 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8168 /* disable the panel fitter on everything but LVDS */
8169 I915_WRITE(PFIT_CONTROL, 0);
8170 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008171
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008172 if (!(HAS_DDI(dev) && (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02008173 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008174
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008175 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03008176 int found;
8177
8178 /* Haswell uses DDI functions to detect digital outputs */
8179 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
8180 /* DDI A only supports eDP */
8181 if (found)
8182 intel_ddi_init(dev, PORT_A);
8183
8184 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
8185 * register */
8186 found = I915_READ(SFUSE_STRAP);
8187
8188 if (found & SFUSE_STRAP_DDIB_DETECTED)
8189 intel_ddi_init(dev, PORT_B);
8190 if (found & SFUSE_STRAP_DDIC_DETECTED)
8191 intel_ddi_init(dev, PORT_C);
8192 if (found & SFUSE_STRAP_DDID_DETECTED)
8193 intel_ddi_init(dev, PORT_D);
8194 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008195 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02008196 dpd_is_edp = intel_dpd_is_edp(dev);
8197
8198 if (has_edp_a(dev))
8199 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008200
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008201 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08008202 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01008203 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008204 if (!found)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008205 intel_hdmi_init(dev, HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008206 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008207 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008208 }
8209
8210 if (I915_READ(HDMIC) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008211 intel_hdmi_init(dev, HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008212
Jesse Barnesb708a1d2012-06-11 14:39:56 -04008213 if (!dpd_is_edp && I915_READ(HDMID) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008214 intel_hdmi_init(dev, HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008215
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008216 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008217 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008218
Daniel Vetter270b3042012-10-27 15:52:05 +02008219 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008220 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008221 } else if (IS_VALLEYVIEW(dev)) {
8222 int found;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008223
Gajanan Bhat19c03922012-09-27 19:13:07 +05308224 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
8225 if (I915_READ(DP_C) & DP_DETECTED)
8226 intel_dp_init(dev, DP_C, PORT_C);
8227
Jesse Barnes4a87d652012-06-15 11:55:16 -07008228 if (I915_READ(SDVOB) & PORT_DETECTED) {
8229 /* SDVOB multiplex with HDMIB */
8230 found = intel_sdvo_init(dev, SDVOB, true);
8231 if (!found)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008232 intel_hdmi_init(dev, SDVOB, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008233 if (!found && (I915_READ(DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008234 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008235 }
8236
8237 if (I915_READ(SDVOC) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008238 intel_hdmi_init(dev, SDVOC, PORT_C);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008239
Zhenyu Wang103a1962009-11-27 11:44:36 +08008240 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008241 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008242
Eric Anholt725e30a2009-01-22 13:01:02 -08008243 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008244 DRM_DEBUG_KMS("probing SDVOB\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008245 found = intel_sdvo_init(dev, SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008246 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8247 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008248 intel_hdmi_init(dev, SDVOB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008249 }
Ma Ling27185ae2009-08-24 13:50:23 +08008250
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008251 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8252 DRM_DEBUG_KMS("probing DP_B\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008253 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008254 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008255 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008256
8257 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008258
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008259 if (I915_READ(SDVOB) & SDVO_DETECTED) {
8260 DRM_DEBUG_KMS("probing SDVOC\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008261 found = intel_sdvo_init(dev, SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008262 }
Ma Ling27185ae2009-08-24 13:50:23 +08008263
8264 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
8265
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008266 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8267 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008268 intel_hdmi_init(dev, SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008269 }
8270 if (SUPPORTS_INTEGRATED_DP(dev)) {
8271 DRM_DEBUG_KMS("probing DP_C\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008272 intel_dp_init(dev, DP_C, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008273 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008274 }
Ma Ling27185ae2009-08-24 13:50:23 +08008275
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008276 if (SUPPORTS_INTEGRATED_DP(dev) &&
8277 (I915_READ(DP_D) & DP_DETECTED)) {
8278 DRM_DEBUG_KMS("probing DP_D\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008279 intel_dp_init(dev, DP_D, PORT_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008280 }
Eric Anholtbad720f2009-10-22 16:11:14 -07008281 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008282 intel_dvo_init(dev);
8283
Zhenyu Wang103a1962009-11-27 11:44:36 +08008284 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008285 intel_tv_init(dev);
8286
Chris Wilson4ef69c72010-09-09 15:14:28 +01008287 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8288 encoder->base.possible_crtcs = encoder->crtc_mask;
8289 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02008290 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08008291 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008292
Paulo Zanonidde86e22012-12-01 12:04:25 -02008293 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02008294
8295 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008296}
8297
8298static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8299{
8300 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008301
8302 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008303 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008304
8305 kfree(intel_fb);
8306}
8307
8308static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008309 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008310 unsigned int *handle)
8311{
8312 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008313 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008314
Chris Wilson05394f32010-11-08 19:18:58 +00008315 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008316}
8317
8318static const struct drm_framebuffer_funcs intel_fb_funcs = {
8319 .destroy = intel_user_framebuffer_destroy,
8320 .create_handle = intel_user_framebuffer_create_handle,
8321};
8322
Dave Airlie38651672010-03-30 05:34:13 +00008323int intel_framebuffer_init(struct drm_device *dev,
8324 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008325 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008326 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008327{
Jesse Barnes79e53942008-11-07 14:24:08 -08008328 int ret;
8329
Chris Wilson05394f32010-11-08 19:18:58 +00008330 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01008331 return -EINVAL;
8332
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008333 if (mode_cmd->pitches[0] & 63)
Chris Wilson57cd6502010-08-08 12:34:44 +01008334 return -EINVAL;
8335
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008336 /* FIXME <= Gen4 stride limits are bit unclear */
8337 if (mode_cmd->pitches[0] > 32768)
8338 return -EINVAL;
8339
8340 if (obj->tiling_mode != I915_TILING_NONE &&
8341 mode_cmd->pitches[0] != obj->stride)
8342 return -EINVAL;
8343
Ville Syrjälä57779d02012-10-31 17:50:14 +02008344 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008345 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02008346 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008347 case DRM_FORMAT_RGB565:
8348 case DRM_FORMAT_XRGB8888:
8349 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008350 break;
8351 case DRM_FORMAT_XRGB1555:
8352 case DRM_FORMAT_ARGB1555:
8353 if (INTEL_INFO(dev)->gen > 3)
8354 return -EINVAL;
8355 break;
8356 case DRM_FORMAT_XBGR8888:
8357 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008358 case DRM_FORMAT_XRGB2101010:
8359 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008360 case DRM_FORMAT_XBGR2101010:
8361 case DRM_FORMAT_ABGR2101010:
8362 if (INTEL_INFO(dev)->gen < 4)
8363 return -EINVAL;
Jesse Barnesb5626742011-06-24 12:19:27 -07008364 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02008365 case DRM_FORMAT_YUYV:
8366 case DRM_FORMAT_UYVY:
8367 case DRM_FORMAT_YVYU:
8368 case DRM_FORMAT_VYUY:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008369 if (INTEL_INFO(dev)->gen < 6)
8370 return -EINVAL;
Chris Wilson57cd6502010-08-08 12:34:44 +01008371 break;
8372 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008373 DRM_DEBUG_KMS("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01008374 return -EINVAL;
8375 }
8376
Ville Syrjälä90f9a332012-10-31 17:50:19 +02008377 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
8378 if (mode_cmd->offsets[0] != 0)
8379 return -EINVAL;
8380
Jesse Barnes79e53942008-11-07 14:24:08 -08008381 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8382 if (ret) {
8383 DRM_ERROR("framebuffer init failed %d\n", ret);
8384 return ret;
8385 }
8386
8387 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08008388 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008389 return 0;
8390}
8391
Jesse Barnes79e53942008-11-07 14:24:08 -08008392static struct drm_framebuffer *
8393intel_user_framebuffer_create(struct drm_device *dev,
8394 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008395 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08008396{
Chris Wilson05394f32010-11-08 19:18:58 +00008397 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008398
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008399 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8400 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00008401 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01008402 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08008403
Chris Wilsond2dff872011-04-19 08:36:26 +01008404 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08008405}
8406
Jesse Barnes79e53942008-11-07 14:24:08 -08008407static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08008408 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00008409 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08008410};
8411
Jesse Barnese70236a2009-09-21 10:42:27 -07008412/* Set up chip specific display functions */
8413static void intel_init_display(struct drm_device *dev)
8414{
8415 struct drm_i915_private *dev_priv = dev->dev_private;
8416
8417 /* We always want a DPMS function */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008418 if (HAS_DDI(dev)) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008419 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02008420 dev_priv->display.crtc_enable = haswell_crtc_enable;
8421 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008422 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008423 dev_priv->display.update_plane = ironlake_update_plane;
8424 } else if (HAS_PCH_SPLIT(dev)) {
Eric Anholtf564048e2011-03-30 13:01:02 -07008425 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008426 dev_priv->display.crtc_enable = ironlake_crtc_enable;
8427 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008428 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008429 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008430 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07008431 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008432 dev_priv->display.crtc_enable = i9xx_crtc_enable;
8433 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008434 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008435 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008436 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008437
Jesse Barnese70236a2009-09-21 10:42:27 -07008438 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07008439 if (IS_VALLEYVIEW(dev))
8440 dev_priv->display.get_display_clock_speed =
8441 valleyview_get_display_clock_speed;
8442 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07008443 dev_priv->display.get_display_clock_speed =
8444 i945_get_display_clock_speed;
8445 else if (IS_I915G(dev))
8446 dev_priv->display.get_display_clock_speed =
8447 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008448 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008449 dev_priv->display.get_display_clock_speed =
8450 i9xx_misc_get_display_clock_speed;
8451 else if (IS_I915GM(dev))
8452 dev_priv->display.get_display_clock_speed =
8453 i915gm_get_display_clock_speed;
8454 else if (IS_I865G(dev))
8455 dev_priv->display.get_display_clock_speed =
8456 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02008457 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008458 dev_priv->display.get_display_clock_speed =
8459 i855_get_display_clock_speed;
8460 else /* 852, 830 */
8461 dev_priv->display.get_display_clock_speed =
8462 i830_get_display_clock_speed;
8463
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008464 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01008465 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008466 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008467 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08008468 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008469 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008470 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07008471 } else if (IS_IVYBRIDGE(dev)) {
8472 /* FIXME: detect B0+ stepping and use auto training */
8473 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008474 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02008475 dev_priv->display.modeset_global_resources =
8476 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03008477 } else if (IS_HASWELL(dev)) {
8478 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08008479 dev_priv->display.write_eld = haswell_write_eld;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02008480 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07008481 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08008482 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07008483 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008484
8485 /* Default just returns -ENODEV to indicate unsupported */
8486 dev_priv->display.queue_flip = intel_default_queue_flip;
8487
8488 switch (INTEL_INFO(dev)->gen) {
8489 case 2:
8490 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8491 break;
8492
8493 case 3:
8494 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8495 break;
8496
8497 case 4:
8498 case 5:
8499 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8500 break;
8501
8502 case 6:
8503 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8504 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008505 case 7:
8506 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8507 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008508 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008509}
8510
Jesse Barnesb690e962010-07-19 13:53:12 -07008511/*
8512 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8513 * resume, or other times. This quirk makes sure that's the case for
8514 * affected systems.
8515 */
Akshay Joshi0206e352011-08-16 15:34:10 -04008516static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07008517{
8518 struct drm_i915_private *dev_priv = dev->dev_private;
8519
8520 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008521 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008522}
8523
Keith Packard435793d2011-07-12 14:56:22 -07008524/*
8525 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8526 */
8527static void quirk_ssc_force_disable(struct drm_device *dev)
8528{
8529 struct drm_i915_private *dev_priv = dev->dev_private;
8530 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008531 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07008532}
8533
Carsten Emde4dca20e2012-03-15 15:56:26 +01008534/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01008535 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
8536 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01008537 */
8538static void quirk_invert_brightness(struct drm_device *dev)
8539{
8540 struct drm_i915_private *dev_priv = dev->dev_private;
8541 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008542 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008543}
8544
8545struct intel_quirk {
8546 int device;
8547 int subsystem_vendor;
8548 int subsystem_device;
8549 void (*hook)(struct drm_device *dev);
8550};
8551
Egbert Eich5f85f1762012-10-14 15:46:38 +02008552/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
8553struct intel_dmi_quirk {
8554 void (*hook)(struct drm_device *dev);
8555 const struct dmi_system_id (*dmi_id_list)[];
8556};
8557
8558static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
8559{
8560 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
8561 return 1;
8562}
8563
8564static const struct intel_dmi_quirk intel_dmi_quirks[] = {
8565 {
8566 .dmi_id_list = &(const struct dmi_system_id[]) {
8567 {
8568 .callback = intel_dmi_reverse_brightness,
8569 .ident = "NCR Corporation",
8570 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
8571 DMI_MATCH(DMI_PRODUCT_NAME, ""),
8572 },
8573 },
8574 { } /* terminating entry */
8575 },
8576 .hook = quirk_invert_brightness,
8577 },
8578};
8579
Ben Widawskyc43b5632012-04-16 14:07:40 -07008580static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07008581 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04008582 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07008583
Jesse Barnesb690e962010-07-19 13:53:12 -07008584 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
8585 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
8586
Jesse Barnesb690e962010-07-19 13:53:12 -07008587 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
8588 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
8589
Daniel Vetterccd0d362012-10-10 23:13:59 +02008590 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07008591 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02008592 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07008593
8594 /* Lenovo U160 cannot use SSC on LVDS */
8595 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02008596
8597 /* Sony Vaio Y cannot use SSC on LVDS */
8598 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01008599
8600 /* Acer Aspire 5734Z must invert backlight brightness */
8601 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02008602
8603 /* Acer/eMachines G725 */
8604 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07008605};
8606
8607static void intel_init_quirks(struct drm_device *dev)
8608{
8609 struct pci_dev *d = dev->pdev;
8610 int i;
8611
8612 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
8613 struct intel_quirk *q = &intel_quirks[i];
8614
8615 if (d->device == q->device &&
8616 (d->subsystem_vendor == q->subsystem_vendor ||
8617 q->subsystem_vendor == PCI_ANY_ID) &&
8618 (d->subsystem_device == q->subsystem_device ||
8619 q->subsystem_device == PCI_ANY_ID))
8620 q->hook(dev);
8621 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02008622 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
8623 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
8624 intel_dmi_quirks[i].hook(dev);
8625 }
Jesse Barnesb690e962010-07-19 13:53:12 -07008626}
8627
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008628/* Disable the VGA plane that we never use */
8629static void i915_disable_vga(struct drm_device *dev)
8630{
8631 struct drm_i915_private *dev_priv = dev->dev_private;
8632 u8 sr1;
8633 u32 vga_reg;
8634
8635 if (HAS_PCH_SPLIT(dev))
8636 vga_reg = CPU_VGACNTRL;
8637 else
8638 vga_reg = VGACNTRL;
8639
8640 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07008641 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008642 sr1 = inb(VGA_SR_DATA);
8643 outb(sr1 | 1<<5, VGA_SR_DATA);
8644 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
8645 udelay(300);
8646
8647 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
8648 POSTING_READ(vga_reg);
8649}
8650
Daniel Vetterf8175862012-04-10 15:50:11 +02008651void intel_modeset_init_hw(struct drm_device *dev)
8652{
Eugeni Dodonov0232e922012-07-06 15:42:36 -03008653 /* We attempt to init the necessary power wells early in the initialization
8654 * time, so the subsystems that expect power to be enabled can work.
8655 */
8656 intel_init_power_wells(dev);
8657
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03008658 intel_prepare_ddi(dev);
8659
Daniel Vetterf8175862012-04-10 15:50:11 +02008660 intel_init_clock_gating(dev);
8661
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008662 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008663 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008664 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02008665}
8666
Jesse Barnes79e53942008-11-07 14:24:08 -08008667void intel_modeset_init(struct drm_device *dev)
8668{
Jesse Barnes652c3932009-08-17 13:31:43 -07008669 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008670 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008671
8672 drm_mode_config_init(dev);
8673
8674 dev->mode_config.min_width = 0;
8675 dev->mode_config.min_height = 0;
8676
Dave Airlie019d96c2011-09-29 16:20:42 +01008677 dev->mode_config.preferred_depth = 24;
8678 dev->mode_config.prefer_shadow = 1;
8679
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02008680 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08008681
Jesse Barnesb690e962010-07-19 13:53:12 -07008682 intel_init_quirks(dev);
8683
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008684 intel_init_pm(dev);
8685
Jesse Barnese70236a2009-09-21 10:42:27 -07008686 intel_init_display(dev);
8687
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008688 if (IS_GEN2(dev)) {
8689 dev->mode_config.max_width = 2048;
8690 dev->mode_config.max_height = 2048;
8691 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07008692 dev->mode_config.max_width = 4096;
8693 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08008694 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008695 dev->mode_config.max_width = 8192;
8696 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08008697 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08008698 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008699
Zhao Yakui28c97732009-10-09 11:39:41 +08008700 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10008701 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08008702
Dave Airliea3524f12010-06-06 18:59:41 +10008703 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008704 intel_crtc_init(dev, i);
Jesse Barnes00c2064b2012-01-13 15:48:39 -08008705 ret = intel_plane_init(dev, i);
8706 if (ret)
8707 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08008708 }
8709
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008710 intel_cpu_pll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008711 intel_pch_pll_init(dev);
8712
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008713 /* Just disable it once at startup */
8714 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008715 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00008716
8717 /* Just in case the BIOS is doing something questionable. */
8718 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01008719}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008720
Daniel Vetter24929352012-07-02 20:28:59 +02008721static void
8722intel_connector_break_all_links(struct intel_connector *connector)
8723{
8724 connector->base.dpms = DRM_MODE_DPMS_OFF;
8725 connector->base.encoder = NULL;
8726 connector->encoder->connectors_active = false;
8727 connector->encoder->base.crtc = NULL;
8728}
8729
Daniel Vetter7fad7982012-07-04 17:51:47 +02008730static void intel_enable_pipe_a(struct drm_device *dev)
8731{
8732 struct intel_connector *connector;
8733 struct drm_connector *crt = NULL;
8734 struct intel_load_detect_pipe load_detect_temp;
8735
8736 /* We can't just switch on the pipe A, we need to set things up with a
8737 * proper mode and output configuration. As a gross hack, enable pipe A
8738 * by enabling the load detect pipe once. */
8739 list_for_each_entry(connector,
8740 &dev->mode_config.connector_list,
8741 base.head) {
8742 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
8743 crt = &connector->base;
8744 break;
8745 }
8746 }
8747
8748 if (!crt)
8749 return;
8750
8751 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
8752 intel_release_load_detect_pipe(crt, &load_detect_temp);
8753
8754
8755}
8756
Daniel Vetterfa555832012-10-10 23:14:00 +02008757static bool
8758intel_check_plane_mapping(struct intel_crtc *crtc)
8759{
8760 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8761 u32 reg, val;
8762
8763 if (dev_priv->num_pipe == 1)
8764 return true;
8765
8766 reg = DSPCNTR(!crtc->plane);
8767 val = I915_READ(reg);
8768
8769 if ((val & DISPLAY_PLANE_ENABLE) &&
8770 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
8771 return false;
8772
8773 return true;
8774}
8775
Daniel Vetter24929352012-07-02 20:28:59 +02008776static void intel_sanitize_crtc(struct intel_crtc *crtc)
8777{
8778 struct drm_device *dev = crtc->base.dev;
8779 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02008780 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02008781
Daniel Vetter24929352012-07-02 20:28:59 +02008782 /* Clear any frame start delays used for debugging left by the BIOS */
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008783 reg = PIPECONF(crtc->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02008784 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
8785
8786 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02008787 * disable the crtc (and hence change the state) if it is wrong. Note
8788 * that gen4+ has a fixed plane -> pipe mapping. */
8789 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02008790 struct intel_connector *connector;
8791 bool plane;
8792
Daniel Vetter24929352012-07-02 20:28:59 +02008793 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
8794 crtc->base.base.id);
8795
8796 /* Pipe has the wrong plane attached and the plane is active.
8797 * Temporarily change the plane mapping and disable everything
8798 * ... */
8799 plane = crtc->plane;
8800 crtc->plane = !plane;
8801 dev_priv->display.crtc_disable(&crtc->base);
8802 crtc->plane = plane;
8803
8804 /* ... and break all links. */
8805 list_for_each_entry(connector, &dev->mode_config.connector_list,
8806 base.head) {
8807 if (connector->encoder->base.crtc != &crtc->base)
8808 continue;
8809
8810 intel_connector_break_all_links(connector);
8811 }
8812
8813 WARN_ON(crtc->active);
8814 crtc->base.enabled = false;
8815 }
Daniel Vetter24929352012-07-02 20:28:59 +02008816
Daniel Vetter7fad7982012-07-04 17:51:47 +02008817 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
8818 crtc->pipe == PIPE_A && !crtc->active) {
8819 /* BIOS forgot to enable pipe A, this mostly happens after
8820 * resume. Force-enable the pipe to fix this, the update_dpms
8821 * call below we restore the pipe to the right state, but leave
8822 * the required bits on. */
8823 intel_enable_pipe_a(dev);
8824 }
8825
Daniel Vetter24929352012-07-02 20:28:59 +02008826 /* Adjust the state of the output pipe according to whether we
8827 * have active connectors/encoders. */
8828 intel_crtc_update_dpms(&crtc->base);
8829
8830 if (crtc->active != crtc->base.enabled) {
8831 struct intel_encoder *encoder;
8832
8833 /* This can happen either due to bugs in the get_hw_state
8834 * functions or because the pipe is force-enabled due to the
8835 * pipe A quirk. */
8836 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
8837 crtc->base.base.id,
8838 crtc->base.enabled ? "enabled" : "disabled",
8839 crtc->active ? "enabled" : "disabled");
8840
8841 crtc->base.enabled = crtc->active;
8842
8843 /* Because we only establish the connector -> encoder ->
8844 * crtc links if something is active, this means the
8845 * crtc is now deactivated. Break the links. connector
8846 * -> encoder links are only establish when things are
8847 * actually up, hence no need to break them. */
8848 WARN_ON(crtc->active);
8849
8850 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
8851 WARN_ON(encoder->connectors_active);
8852 encoder->base.crtc = NULL;
8853 }
8854 }
8855}
8856
8857static void intel_sanitize_encoder(struct intel_encoder *encoder)
8858{
8859 struct intel_connector *connector;
8860 struct drm_device *dev = encoder->base.dev;
8861
8862 /* We need to check both for a crtc link (meaning that the
8863 * encoder is active and trying to read from a pipe) and the
8864 * pipe itself being active. */
8865 bool has_active_crtc = encoder->base.crtc &&
8866 to_intel_crtc(encoder->base.crtc)->active;
8867
8868 if (encoder->connectors_active && !has_active_crtc) {
8869 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
8870 encoder->base.base.id,
8871 drm_get_encoder_name(&encoder->base));
8872
8873 /* Connector is active, but has no active pipe. This is
8874 * fallout from our resume register restoring. Disable
8875 * the encoder manually again. */
8876 if (encoder->base.crtc) {
8877 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
8878 encoder->base.base.id,
8879 drm_get_encoder_name(&encoder->base));
8880 encoder->disable(encoder);
8881 }
8882
8883 /* Inconsistent output/port/pipe state happens presumably due to
8884 * a bug in one of the get_hw_state functions. Or someplace else
8885 * in our code, like the register restore mess on resume. Clamp
8886 * things to off as a safer default. */
8887 list_for_each_entry(connector,
8888 &dev->mode_config.connector_list,
8889 base.head) {
8890 if (connector->encoder != encoder)
8891 continue;
8892
8893 intel_connector_break_all_links(connector);
8894 }
8895 }
8896 /* Enabled encoders without active connectors will be fixed in
8897 * the crtc fixup. */
8898}
8899
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01008900static void i915_redisable_vga(struct drm_device *dev)
8901{
8902 struct drm_i915_private *dev_priv = dev->dev_private;
8903 u32 vga_reg;
8904
8905 if (HAS_PCH_SPLIT(dev))
8906 vga_reg = CPU_VGACNTRL;
8907 else
8908 vga_reg = VGACNTRL;
8909
8910 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
8911 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
8912 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
8913 POSTING_READ(vga_reg);
8914 }
8915}
8916
Daniel Vetter24929352012-07-02 20:28:59 +02008917/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
8918 * and i915 state tracking structures. */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01008919void intel_modeset_setup_hw_state(struct drm_device *dev,
8920 bool force_restore)
Daniel Vetter24929352012-07-02 20:28:59 +02008921{
8922 struct drm_i915_private *dev_priv = dev->dev_private;
8923 enum pipe pipe;
8924 u32 tmp;
8925 struct intel_crtc *crtc;
8926 struct intel_encoder *encoder;
8927 struct intel_connector *connector;
8928
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008929 if (HAS_DDI(dev)) {
Paulo Zanonie28d54c2012-10-24 16:09:25 -02008930 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8931
8932 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8933 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8934 case TRANS_DDI_EDP_INPUT_A_ON:
8935 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8936 pipe = PIPE_A;
8937 break;
8938 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8939 pipe = PIPE_B;
8940 break;
8941 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8942 pipe = PIPE_C;
8943 break;
8944 }
8945
8946 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8947 crtc->cpu_transcoder = TRANSCODER_EDP;
8948
8949 DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
8950 pipe_name(pipe));
8951 }
8952 }
8953
Daniel Vetter24929352012-07-02 20:28:59 +02008954 for_each_pipe(pipe) {
8955 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
8956
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008957 tmp = I915_READ(PIPECONF(crtc->cpu_transcoder));
Daniel Vetter24929352012-07-02 20:28:59 +02008958 if (tmp & PIPECONF_ENABLE)
8959 crtc->active = true;
8960 else
8961 crtc->active = false;
8962
8963 crtc->base.enabled = crtc->active;
8964
8965 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
8966 crtc->base.base.id,
8967 crtc->active ? "enabled" : "disabled");
8968 }
8969
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008970 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008971 intel_ddi_setup_hw_pll_state(dev);
8972
Daniel Vetter24929352012-07-02 20:28:59 +02008973 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8974 base.head) {
8975 pipe = 0;
8976
8977 if (encoder->get_hw_state(encoder, &pipe)) {
8978 encoder->base.crtc =
8979 dev_priv->pipe_to_crtc_mapping[pipe];
8980 } else {
8981 encoder->base.crtc = NULL;
8982 }
8983
8984 encoder->connectors_active = false;
8985 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
8986 encoder->base.base.id,
8987 drm_get_encoder_name(&encoder->base),
8988 encoder->base.crtc ? "enabled" : "disabled",
8989 pipe);
8990 }
8991
8992 list_for_each_entry(connector, &dev->mode_config.connector_list,
8993 base.head) {
8994 if (connector->get_hw_state(connector)) {
8995 connector->base.dpms = DRM_MODE_DPMS_ON;
8996 connector->encoder->connectors_active = true;
8997 connector->base.encoder = &connector->encoder->base;
8998 } else {
8999 connector->base.dpms = DRM_MODE_DPMS_OFF;
9000 connector->base.encoder = NULL;
9001 }
9002 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9003 connector->base.base.id,
9004 drm_get_connector_name(&connector->base),
9005 connector->base.encoder ? "enabled" : "disabled");
9006 }
9007
9008 /* HW state is read out, now we need to sanitize this mess. */
9009 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9010 base.head) {
9011 intel_sanitize_encoder(encoder);
9012 }
9013
9014 for_each_pipe(pipe) {
9015 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9016 intel_sanitize_crtc(crtc);
9017 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009018
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009019 if (force_restore) {
9020 for_each_pipe(pipe) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009021 intel_crtc_restore_mode(dev_priv->pipe_to_crtc_mapping[pipe]);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009022 }
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009023
9024 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009025 } else {
9026 intel_modeset_update_staged_output_state(dev);
9027 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009028
9029 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +02009030
9031 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009032}
9033
9034void intel_modeset_gem_init(struct drm_device *dev)
9035{
Chris Wilson1833b132012-05-09 11:56:28 +01009036 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02009037
9038 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +02009039
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009040 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08009041}
9042
9043void intel_modeset_cleanup(struct drm_device *dev)
9044{
Jesse Barnes652c3932009-08-17 13:31:43 -07009045 struct drm_i915_private *dev_priv = dev->dev_private;
9046 struct drm_crtc *crtc;
9047 struct intel_crtc *intel_crtc;
9048
Keith Packardf87ea762010-10-03 19:36:26 -07009049 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009050 mutex_lock(&dev->struct_mutex);
9051
Jesse Barnes723bfd72010-10-07 16:01:13 -07009052 intel_unregister_dsm_handler();
9053
9054
Jesse Barnes652c3932009-08-17 13:31:43 -07009055 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9056 /* Skip inactive CRTCs */
9057 if (!crtc->fb)
9058 continue;
9059
9060 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02009061 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009062 }
9063
Chris Wilson973d04f2011-07-08 12:22:37 +01009064 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07009065
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009066 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00009067
Daniel Vetter930ebb42012-06-29 23:32:16 +02009068 ironlake_teardown_rc6(dev);
9069
Jesse Barnes57f350b2012-03-28 13:39:25 -07009070 if (IS_VALLEYVIEW(dev))
9071 vlv_init_dpio(dev);
9072
Kristian Høgsberg69341a52009-11-11 12:19:17 -05009073 mutex_unlock(&dev->struct_mutex);
9074
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009075 /* Disable the irq before mode object teardown, for the irq might
9076 * enqueue unpin/hotplug work. */
9077 drm_irq_uninstall(dev);
9078 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02009079 cancel_work_sync(&dev_priv->rps.work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009080
Chris Wilson1630fe72011-07-08 12:22:42 +01009081 /* flush any delayed tasks or pending work */
9082 flush_scheduled_work();
9083
Jesse Barnes79e53942008-11-07 14:24:08 -08009084 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +01009085
9086 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009087}
9088
Dave Airlie28d52042009-09-21 14:33:58 +10009089/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08009090 * Return which encoder is currently attached for connector.
9091 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01009092struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08009093{
Chris Wilsondf0e9242010-09-09 16:20:55 +01009094 return &intel_attached_encoder(connector)->base;
9095}
Jesse Barnes79e53942008-11-07 14:24:08 -08009096
Chris Wilsondf0e9242010-09-09 16:20:55 +01009097void intel_connector_attach_encoder(struct intel_connector *connector,
9098 struct intel_encoder *encoder)
9099{
9100 connector->encoder = encoder;
9101 drm_mode_connector_attach_encoder(&connector->base,
9102 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009103}
Dave Airlie28d52042009-09-21 14:33:58 +10009104
9105/*
9106 * set vga decode state - true == enable VGA decode
9107 */
9108int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9109{
9110 struct drm_i915_private *dev_priv = dev->dev_private;
9111 u16 gmch_ctrl;
9112
9113 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9114 if (state)
9115 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9116 else
9117 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9118 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9119 return 0;
9120}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009121
9122#ifdef CONFIG_DEBUG_FS
9123#include <linux/seq_file.h>
9124
9125struct intel_display_error_state {
9126 struct intel_cursor_error_state {
9127 u32 control;
9128 u32 position;
9129 u32 base;
9130 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +01009131 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009132
9133 struct intel_pipe_error_state {
9134 u32 conf;
9135 u32 source;
9136
9137 u32 htotal;
9138 u32 hblank;
9139 u32 hsync;
9140 u32 vtotal;
9141 u32 vblank;
9142 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +01009143 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009144
9145 struct intel_plane_error_state {
9146 u32 control;
9147 u32 stride;
9148 u32 size;
9149 u32 pos;
9150 u32 addr;
9151 u32 surface;
9152 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +01009153 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009154};
9155
9156struct intel_display_error_state *
9157intel_display_capture_error_state(struct drm_device *dev)
9158{
Akshay Joshi0206e352011-08-16 15:34:10 -04009159 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009160 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009161 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009162 int i;
9163
9164 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9165 if (error == NULL)
9166 return NULL;
9167
Damien Lespiau52331302012-08-15 19:23:25 +01009168 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009169 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
9170
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009171 error->cursor[i].control = I915_READ(CURCNTR(i));
9172 error->cursor[i].position = I915_READ(CURPOS(i));
9173 error->cursor[i].base = I915_READ(CURBASE(i));
9174
9175 error->plane[i].control = I915_READ(DSPCNTR(i));
9176 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
9177 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04009178 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009179 error->plane[i].addr = I915_READ(DSPADDR(i));
9180 if (INTEL_INFO(dev)->gen >= 4) {
9181 error->plane[i].surface = I915_READ(DSPSURF(i));
9182 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9183 }
9184
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009185 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009186 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009187 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
9188 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
9189 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
9190 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
9191 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
9192 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009193 }
9194
9195 return error;
9196}
9197
9198void
9199intel_display_print_error_state(struct seq_file *m,
9200 struct drm_device *dev,
9201 struct intel_display_error_state *error)
9202{
Damien Lespiau52331302012-08-15 19:23:25 +01009203 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009204 int i;
9205
Damien Lespiau52331302012-08-15 19:23:25 +01009206 seq_printf(m, "Num Pipes: %d\n", dev_priv->num_pipe);
9207 for_each_pipe(i) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009208 seq_printf(m, "Pipe [%d]:\n", i);
9209 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9210 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
9211 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9212 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9213 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9214 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9215 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9216 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9217
9218 seq_printf(m, "Plane [%d]:\n", i);
9219 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9220 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9221 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
9222 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9223 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9224 if (INTEL_INFO(dev)->gen >= 4) {
9225 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9226 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9227 }
9228
9229 seq_printf(m, "Cursor [%d]:\n", i);
9230 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9231 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9232 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9233 }
9234}
9235#endif