blob: 0a02e044b65373d42d9a3232db0e6f6b4c8735cd [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080042#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043
Akshay Joshi0206e352011-08-16 15:34:10 -040044bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
Daniel Vetter3dec0092010-08-20 21:40:52 +020045static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010046static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080047
48typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040049 /* given values */
50 int n;
51 int m1, m2;
52 int p1, p2;
53 /* derived values */
54 int dot;
55 int vco;
56 int m;
57 int p;
Jesse Barnes79e53942008-11-07 14:24:08 -080058} intel_clock_t;
59
60typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040061 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080062} intel_range_t;
63
64typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040065 int dot_limit;
66 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080067} intel_p2_t;
68
69#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080070typedef struct intel_limit intel_limit_t;
71struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040072 intel_range_t dot, vco, n, m, m1, m2, p, p1;
73 intel_p2_t p2;
74 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
Sean Paulcec2f352012-01-10 15:09:36 -080075 int, int, intel_clock_t *, intel_clock_t *);
Ma Lingd4906092009-03-18 20:13:27 +080076};
Jesse Barnes79e53942008-11-07 14:24:08 -080077
Jesse Barnes2377b742010-07-07 14:06:43 -070078/* FDI */
79#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
80
Daniel Vetterd2acd212012-10-20 20:57:43 +020081int
82intel_pch_rawclk(struct drm_device *dev)
83{
84 struct drm_i915_private *dev_priv = dev->dev_private;
85
86 WARN_ON(!HAS_PCH_SPLIT(dev));
87
88 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
89}
90
Ma Lingd4906092009-03-18 20:13:27 +080091static bool
92intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080093 int target, int refclk, intel_clock_t *match_clock,
94 intel_clock_t *best_clock);
Ma Lingd4906092009-03-18 20:13:27 +080095static bool
96intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080097 int target, int refclk, intel_clock_t *match_clock,
98 intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080099
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700100static bool
101intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800102 int target, int refclk, intel_clock_t *match_clock,
103 intel_clock_t *best_clock);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800104static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500105intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800106 int target, int refclk, intel_clock_t *match_clock,
107 intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700108
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700109static bool
110intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
111 int target, int refclk, intel_clock_t *match_clock,
112 intel_clock_t *best_clock);
113
Chris Wilson021357a2010-09-07 20:54:59 +0100114static inline u32 /* units of 100MHz */
115intel_fdi_link_freq(struct drm_device *dev)
116{
Chris Wilson8b99e682010-10-13 09:59:17 +0100117 if (IS_GEN5(dev)) {
118 struct drm_i915_private *dev_priv = dev->dev_private;
119 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
120 } else
121 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100122}
123
Keith Packarde4b36692009-06-05 19:22:17 -0700124static const intel_limit_t intel_limits_i8xx_dvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 .dot = { .min = 25000, .max = 350000 },
126 .vco = { .min = 930000, .max = 1400000 },
127 .n = { .min = 3, .max = 16 },
128 .m = { .min = 96, .max = 140 },
129 .m1 = { .min = 18, .max = 26 },
130 .m2 = { .min = 6, .max = 16 },
131 .p = { .min = 4, .max = 128 },
132 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700133 .p2 = { .dot_limit = 165000,
134 .p2_slow = 4, .p2_fast = 2 },
Ma Lingd4906092009-03-18 20:13:27 +0800135 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700136};
137
138static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400139 .dot = { .min = 25000, .max = 350000 },
140 .vco = { .min = 930000, .max = 1400000 },
141 .n = { .min = 3, .max = 16 },
142 .m = { .min = 96, .max = 140 },
143 .m1 = { .min = 18, .max = 26 },
144 .m2 = { .min = 6, .max = 16 },
145 .p = { .min = 4, .max = 128 },
146 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700147 .p2 = { .dot_limit = 165000,
148 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800149 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700150};
Eric Anholt273e27c2011-03-30 13:01:10 -0700151
Keith Packarde4b36692009-06-05 19:22:17 -0700152static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400153 .dot = { .min = 20000, .max = 400000 },
154 .vco = { .min = 1400000, .max = 2800000 },
155 .n = { .min = 1, .max = 6 },
156 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100157 .m1 = { .min = 8, .max = 18 },
158 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400159 .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 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100171 .m1 = { .min = 8, .max = 18 },
172 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400173 .p = { .min = 7, .max = 98 },
174 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700175 .p2 = { .dot_limit = 112000,
176 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800177 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700178};
179
Eric Anholt273e27c2011-03-30 13:01:10 -0700180
Keith Packarde4b36692009-06-05 19:22:17 -0700181static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700182 .dot = { .min = 25000, .max = 270000 },
183 .vco = { .min = 1750000, .max = 3500000},
184 .n = { .min = 1, .max = 4 },
185 .m = { .min = 104, .max = 138 },
186 .m1 = { .min = 17, .max = 23 },
187 .m2 = { .min = 5, .max = 11 },
188 .p = { .min = 10, .max = 30 },
189 .p1 = { .min = 1, .max = 3},
190 .p2 = { .dot_limit = 270000,
191 .p2_slow = 10,
192 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800193 },
Ma Lingd4906092009-03-18 20:13:27 +0800194 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700195};
196
197static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700198 .dot = { .min = 22000, .max = 400000 },
199 .vco = { .min = 1750000, .max = 3500000},
200 .n = { .min = 1, .max = 4 },
201 .m = { .min = 104, .max = 138 },
202 .m1 = { .min = 16, .max = 23 },
203 .m2 = { .min = 5, .max = 11 },
204 .p = { .min = 5, .max = 80 },
205 .p1 = { .min = 1, .max = 8},
206 .p2 = { .dot_limit = 165000,
207 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800208 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700209};
210
211static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700212 .dot = { .min = 20000, .max = 115000 },
213 .vco = { .min = 1750000, .max = 3500000 },
214 .n = { .min = 1, .max = 3 },
215 .m = { .min = 104, .max = 138 },
216 .m1 = { .min = 17, .max = 23 },
217 .m2 = { .min = 5, .max = 11 },
218 .p = { .min = 28, .max = 112 },
219 .p1 = { .min = 2, .max = 8 },
220 .p2 = { .dot_limit = 0,
221 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800222 },
Ma Lingd4906092009-03-18 20:13:27 +0800223 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700224};
225
226static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700227 .dot = { .min = 80000, .max = 224000 },
228 .vco = { .min = 1750000, .max = 3500000 },
229 .n = { .min = 1, .max = 3 },
230 .m = { .min = 104, .max = 138 },
231 .m1 = { .min = 17, .max = 23 },
232 .m2 = { .min = 5, .max = 11 },
233 .p = { .min = 14, .max = 42 },
234 .p1 = { .min = 2, .max = 6 },
235 .p2 = { .dot_limit = 0,
236 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800237 },
Ma Lingd4906092009-03-18 20:13:27 +0800238 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700239};
240
241static const intel_limit_t intel_limits_g4x_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400242 .dot = { .min = 161670, .max = 227000 },
243 .vco = { .min = 1750000, .max = 3500000},
244 .n = { .min = 1, .max = 2 },
245 .m = { .min = 97, .max = 108 },
246 .m1 = { .min = 0x10, .max = 0x12 },
247 .m2 = { .min = 0x05, .max = 0x06 },
248 .p = { .min = 10, .max = 20 },
249 .p1 = { .min = 1, .max = 2},
250 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700251 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400252 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700253};
254
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500255static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400256 .dot = { .min = 20000, .max = 400000},
257 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700258 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400259 .n = { .min = 3, .max = 6 },
260 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400262 .m1 = { .min = 0, .max = 0 },
263 .m2 = { .min = 0, .max = 254 },
264 .p = { .min = 5, .max = 80 },
265 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700266 .p2 = { .dot_limit = 200000,
267 .p2_slow = 10, .p2_fast = 5 },
Shaohua Li61157072009-04-03 15:24:43 +0800268 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700269};
270
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500271static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400272 .dot = { .min = 20000, .max = 400000 },
273 .vco = { .min = 1700000, .max = 3500000 },
274 .n = { .min = 3, .max = 6 },
275 .m = { .min = 2, .max = 256 },
276 .m1 = { .min = 0, .max = 0 },
277 .m2 = { .min = 0, .max = 254 },
278 .p = { .min = 7, .max = 112 },
279 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700280 .p2 = { .dot_limit = 112000,
281 .p2_slow = 14, .p2_fast = 14 },
Shaohua Li61157072009-04-03 15:24:43 +0800282 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700283};
284
Eric Anholt273e27c2011-03-30 13:01:10 -0700285/* Ironlake / Sandybridge
286 *
287 * We calculate clock using (register_value + 2) for N/M1/M2, so here
288 * the range value for them is (actual_value - 2).
289 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800290static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700291 .dot = { .min = 25000, .max = 350000 },
292 .vco = { .min = 1760000, .max = 3510000 },
293 .n = { .min = 1, .max = 5 },
294 .m = { .min = 79, .max = 127 },
295 .m1 = { .min = 12, .max = 22 },
296 .m2 = { .min = 5, .max = 9 },
297 .p = { .min = 5, .max = 80 },
298 .p1 = { .min = 1, .max = 8 },
299 .p2 = { .dot_limit = 225000,
300 .p2_slow = 10, .p2_fast = 5 },
Zhao Yakui45476682009-12-31 16:06:04 +0800301 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700302};
303
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800304static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700305 .dot = { .min = 25000, .max = 350000 },
306 .vco = { .min = 1760000, .max = 3510000 },
307 .n = { .min = 1, .max = 3 },
308 .m = { .min = 79, .max = 118 },
309 .m1 = { .min = 12, .max = 22 },
310 .m2 = { .min = 5, .max = 9 },
311 .p = { .min = 28, .max = 112 },
312 .p1 = { .min = 2, .max = 8 },
313 .p2 = { .dot_limit = 225000,
314 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800315 .find_pll = intel_g4x_find_best_PLL,
316};
317
318static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700319 .dot = { .min = 25000, .max = 350000 },
320 .vco = { .min = 1760000, .max = 3510000 },
321 .n = { .min = 1, .max = 3 },
322 .m = { .min = 79, .max = 127 },
323 .m1 = { .min = 12, .max = 22 },
324 .m2 = { .min = 5, .max = 9 },
325 .p = { .min = 14, .max = 56 },
326 .p1 = { .min = 2, .max = 8 },
327 .p2 = { .dot_limit = 225000,
328 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800329 .find_pll = intel_g4x_find_best_PLL,
330};
331
Eric Anholt273e27c2011-03-30 13:01:10 -0700332/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800333static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700334 .dot = { .min = 25000, .max = 350000 },
335 .vco = { .min = 1760000, .max = 3510000 },
336 .n = { .min = 1, .max = 2 },
337 .m = { .min = 79, .max = 126 },
338 .m1 = { .min = 12, .max = 22 },
339 .m2 = { .min = 5, .max = 9 },
340 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400341 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700342 .p2 = { .dot_limit = 225000,
343 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800344 .find_pll = intel_g4x_find_best_PLL,
345};
346
347static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700348 .dot = { .min = 25000, .max = 350000 },
349 .vco = { .min = 1760000, .max = 3510000 },
350 .n = { .min = 1, .max = 3 },
351 .m = { .min = 79, .max = 126 },
352 .m1 = { .min = 12, .max = 22 },
353 .m2 = { .min = 5, .max = 9 },
354 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400355 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700356 .p2 = { .dot_limit = 225000,
357 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800358 .find_pll = intel_g4x_find_best_PLL,
359};
360
361static const intel_limit_t intel_limits_ironlake_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400362 .dot = { .min = 25000, .max = 350000 },
363 .vco = { .min = 1760000, .max = 3510000},
364 .n = { .min = 1, .max = 2 },
365 .m = { .min = 81, .max = 90 },
366 .m1 = { .min = 12, .max = 22 },
367 .m2 = { .min = 5, .max = 9 },
368 .p = { .min = 10, .max = 20 },
369 .p1 = { .min = 1, .max = 2},
370 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400372 .find_pll = intel_find_pll_ironlake_dp,
Jesse Barnes79e53942008-11-07 14:24:08 -0800373};
374
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700375static const intel_limit_t intel_limits_vlv_dac = {
376 .dot = { .min = 25000, .max = 270000 },
377 .vco = { .min = 4000000, .max = 6000000 },
378 .n = { .min = 1, .max = 7 },
379 .m = { .min = 22, .max = 450 }, /* guess */
380 .m1 = { .min = 2, .max = 3 },
381 .m2 = { .min = 11, .max = 156 },
382 .p = { .min = 10, .max = 30 },
383 .p1 = { .min = 2, .max = 3 },
384 .p2 = { .dot_limit = 270000,
385 .p2_slow = 2, .p2_fast = 20 },
386 .find_pll = intel_vlv_find_best_pll,
387};
388
389static const intel_limit_t intel_limits_vlv_hdmi = {
390 .dot = { .min = 20000, .max = 165000 },
Vijay Purushothaman17dc9252012-09-27 19:13:09 +0530391 .vco = { .min = 4000000, .max = 5994000},
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700392 .n = { .min = 1, .max = 7 },
393 .m = { .min = 60, .max = 300 }, /* guess */
394 .m1 = { .min = 2, .max = 3 },
395 .m2 = { .min = 11, .max = 156 },
396 .p = { .min = 10, .max = 30 },
397 .p1 = { .min = 2, .max = 3 },
398 .p2 = { .dot_limit = 270000,
399 .p2_slow = 2, .p2_fast = 20 },
400 .find_pll = intel_vlv_find_best_pll,
401};
402
403static const intel_limit_t intel_limits_vlv_dp = {
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530404 .dot = { .min = 25000, .max = 270000 },
405 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700406 .n = { .min = 1, .max = 7 },
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530407 .m = { .min = 22, .max = 450 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700408 .m1 = { .min = 2, .max = 3 },
409 .m2 = { .min = 11, .max = 156 },
410 .p = { .min = 10, .max = 30 },
411 .p1 = { .min = 2, .max = 3 },
412 .p2 = { .dot_limit = 270000,
413 .p2_slow = 2, .p2_fast = 20 },
414 .find_pll = intel_vlv_find_best_pll,
415};
416
Jesse Barnes57f350b2012-03-28 13:39:25 -0700417u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
418{
Daniel Vetter09153002012-12-12 14:06:44 +0100419 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnes57f350b2012-03-28 13:39:25 -0700420
Jesse Barnes57f350b2012-03-28 13:39:25 -0700421 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
422 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100423 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700424 }
425
426 I915_WRITE(DPIO_REG, reg);
427 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
428 DPIO_BYTE);
429 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
430 DRM_ERROR("DPIO read wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100431 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700432 }
Jesse Barnes57f350b2012-03-28 13:39:25 -0700433
Daniel Vetter09153002012-12-12 14:06:44 +0100434 return I915_READ(DPIO_DATA);
Jesse Barnes57f350b2012-03-28 13:39:25 -0700435}
436
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700437static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
438 u32 val)
439{
Daniel Vetter09153002012-12-12 14:06:44 +0100440 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700441
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700442 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
443 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100444 return;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700445 }
446
447 I915_WRITE(DPIO_DATA, val);
448 I915_WRITE(DPIO_REG, reg);
449 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
450 DPIO_BYTE);
451 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
452 DRM_ERROR("DPIO write wait timed out\n");
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700453}
454
Jesse Barnes57f350b2012-03-28 13:39:25 -0700455static void vlv_init_dpio(struct drm_device *dev)
456{
457 struct drm_i915_private *dev_priv = dev->dev_private;
458
459 /* Reset the DPIO config */
460 I915_WRITE(DPIO_CTL, 0);
461 POSTING_READ(DPIO_CTL);
462 I915_WRITE(DPIO_CTL, 1);
463 POSTING_READ(DPIO_CTL);
464}
465
Chris Wilson1b894b52010-12-14 20:04:54 +0000466static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
467 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800468{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800469 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800470 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800471
472 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100473 if (intel_is_dual_link_lvds(dev)) {
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800474 /* LVDS dual channel */
Chris Wilson1b894b52010-12-14 20:04:54 +0000475 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800476 limit = &intel_limits_ironlake_dual_lvds_100m;
477 else
478 limit = &intel_limits_ironlake_dual_lvds;
479 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000480 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800481 limit = &intel_limits_ironlake_single_lvds_100m;
482 else
483 limit = &intel_limits_ironlake_single_lvds;
484 }
485 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Jani Nikula547dc042012-11-02 11:24:03 +0200486 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Zhao Yakui45476682009-12-31 16:06:04 +0800487 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800488 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800489 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800490
491 return limit;
492}
493
Ma Ling044c7c42009-03-18 20:13:23 +0800494static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
495{
496 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800497 const intel_limit_t *limit;
498
499 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100500 if (intel_is_dual_link_lvds(dev))
Ma Ling044c7c42009-03-18 20:13:23 +0800501 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700502 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800503 else
504 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700505 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800506 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
507 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700508 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800509 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700510 limit = &intel_limits_g4x_sdvo;
Akshay Joshi0206e352011-08-16 15:34:10 -0400511 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800513 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800515
516 return limit;
517}
518
Chris Wilson1b894b52010-12-14 20:04:54 +0000519static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800520{
521 struct drm_device *dev = crtc->dev;
522 const intel_limit_t *limit;
523
Eric Anholtbad720f2009-10-22 16:11:14 -0700524 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000525 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800526 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800527 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500528 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800529 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500530 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800531 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500532 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700533 } else if (IS_VALLEYVIEW(dev)) {
534 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
535 limit = &intel_limits_vlv_dac;
536 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
537 limit = &intel_limits_vlv_hdmi;
538 else
539 limit = &intel_limits_vlv_dp;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100540 } else if (!IS_GEN2(dev)) {
541 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
542 limit = &intel_limits_i9xx_lvds;
543 else
544 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800545 } else {
546 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700547 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800548 else
Keith Packarde4b36692009-06-05 19:22:17 -0700549 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800550 }
551 return limit;
552}
553
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500554/* m1 is reserved as 0 in Pineview, n is a ring counter */
555static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800556{
Shaohua Li21778322009-02-23 15:19:16 +0800557 clock->m = clock->m2 + 2;
558 clock->p = clock->p1 * clock->p2;
559 clock->vco = refclk * clock->m / clock->n;
560 clock->dot = clock->vco / clock->p;
561}
562
563static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
564{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500565 if (IS_PINEVIEW(dev)) {
566 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800567 return;
568 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800569 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
570 clock->p = clock->p1 * clock->p2;
571 clock->vco = refclk * clock->m / (clock->n + 2);
572 clock->dot = clock->vco / clock->p;
573}
574
Jesse Barnes79e53942008-11-07 14:24:08 -0800575/**
576 * Returns whether any output on the specified pipe is of the specified type
577 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100578bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800579{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100580 struct drm_device *dev = crtc->dev;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100581 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800582
Daniel Vetter6c2b7c12012-07-05 09:50:24 +0200583 for_each_encoder_on_crtc(dev, crtc, encoder)
584 if (encoder->type == type)
Chris Wilson4ef69c72010-09-09 15:14:28 +0100585 return true;
586
587 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800588}
589
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800590#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800591/**
592 * Returns whether the given set of divisors are valid for a given refclk with
593 * the given connectors.
594 */
595
Chris Wilson1b894b52010-12-14 20:04:54 +0000596static bool intel_PLL_is_valid(struct drm_device *dev,
597 const intel_limit_t *limit,
598 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800599{
Jesse Barnes79e53942008-11-07 14:24:08 -0800600 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400601 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800602 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400603 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800604 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400605 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800606 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400607 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500608 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400609 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400615 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
617 * connector, etc., rather than just a single range.
618 */
619 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400620 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800621
622 return true;
623}
624
Ma Lingd4906092009-03-18 20:13:27 +0800625static bool
626intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800627 int target, int refclk, intel_clock_t *match_clock,
628 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800629
Jesse Barnes79e53942008-11-07 14:24:08 -0800630{
631 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800633 int err = target;
634
Daniel Vettera210b022012-11-26 17:22:08 +0100635 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800636 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100637 * For LVDS just rely on its current settings for dual-channel.
638 * We haven't figured out how to reliably set up different
639 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800640 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100641 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800642 clock.p2 = limit->p2.p2_fast;
643 else
644 clock.p2 = limit->p2.p2_slow;
645 } else {
646 if (target < limit->p2.dot_limit)
647 clock.p2 = limit->p2.p2_slow;
648 else
649 clock.p2 = limit->p2.p2_fast;
650 }
651
Akshay Joshi0206e352011-08-16 15:34:10 -0400652 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800653
Zhao Yakui42158662009-11-20 11:24:18 +0800654 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
655 clock.m1++) {
656 for (clock.m2 = limit->m2.min;
657 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500658 /* m1 is always 0 in Pineview */
659 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800660 break;
661 for (clock.n = limit->n.min;
662 clock.n <= limit->n.max; clock.n++) {
663 for (clock.p1 = limit->p1.min;
664 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800665 int this_err;
666
Shaohua Li21778322009-02-23 15:19:16 +0800667 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000668 if (!intel_PLL_is_valid(dev, limit,
669 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800670 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800671 if (match_clock &&
672 clock.p != match_clock->p)
673 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800674
675 this_err = abs(clock.dot - target);
676 if (this_err < err) {
677 *best_clock = clock;
678 err = this_err;
679 }
680 }
681 }
682 }
683 }
684
685 return (err != target);
686}
687
Ma Lingd4906092009-03-18 20:13:27 +0800688static bool
689intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800690 int target, int refclk, intel_clock_t *match_clock,
691 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800692{
693 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800694 intel_clock_t clock;
695 int max_n;
696 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400697 /* approximately equals target * 0.00585 */
698 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800699 found = false;
700
701 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800702 int lvds_reg;
703
Eric Anholtc619eed2010-01-28 16:45:52 -0800704 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800705 lvds_reg = PCH_LVDS;
706 else
707 lvds_reg = LVDS;
Daniel Vetter1974cad2012-11-26 17:22:09 +0100708 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800709 clock.p2 = limit->p2.p2_fast;
710 else
711 clock.p2 = limit->p2.p2_slow;
712 } else {
713 if (target < limit->p2.dot_limit)
714 clock.p2 = limit->p2.p2_slow;
715 else
716 clock.p2 = limit->p2.p2_fast;
717 }
718
719 memset(best_clock, 0, sizeof(*best_clock));
720 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200721 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800722 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200723 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800724 for (clock.m1 = limit->m1.max;
725 clock.m1 >= limit->m1.min; clock.m1--) {
726 for (clock.m2 = limit->m2.max;
727 clock.m2 >= limit->m2.min; clock.m2--) {
728 for (clock.p1 = limit->p1.max;
729 clock.p1 >= limit->p1.min; clock.p1--) {
730 int this_err;
731
Shaohua Li21778322009-02-23 15:19:16 +0800732 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000733 if (!intel_PLL_is_valid(dev, limit,
734 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800735 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800736 if (match_clock &&
737 clock.p != match_clock->p)
738 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000739
740 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800741 if (this_err < err_most) {
742 *best_clock = clock;
743 err_most = this_err;
744 max_n = clock.n;
745 found = true;
746 }
747 }
748 }
749 }
750 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800751 return found;
752}
Ma Lingd4906092009-03-18 20:13:27 +0800753
Zhenyu Wang2c072452009-06-05 15:38:42 +0800754static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500755intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800756 int target, int refclk, intel_clock_t *match_clock,
757 intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800758{
759 struct drm_device *dev = crtc->dev;
760 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800761
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800762 if (target < 200000) {
763 clock.n = 1;
764 clock.p1 = 2;
765 clock.p2 = 10;
766 clock.m1 = 12;
767 clock.m2 = 9;
768 } else {
769 clock.n = 2;
770 clock.p1 = 1;
771 clock.p2 = 10;
772 clock.m1 = 14;
773 clock.m2 = 8;
774 }
775 intel_clock(dev, refclk, &clock);
776 memcpy(best_clock, &clock, sizeof(intel_clock_t));
777 return true;
778}
779
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700780/* DisplayPort has only two frequencies, 162MHz and 270MHz */
781static bool
782intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800783 int target, int refclk, intel_clock_t *match_clock,
784 intel_clock_t *best_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700785{
Chris Wilson5eddb702010-09-11 13:48:45 +0100786 intel_clock_t clock;
787 if (target < 200000) {
788 clock.p1 = 2;
789 clock.p2 = 10;
790 clock.n = 2;
791 clock.m1 = 23;
792 clock.m2 = 8;
793 } else {
794 clock.p1 = 1;
795 clock.p2 = 10;
796 clock.n = 1;
797 clock.m1 = 14;
798 clock.m2 = 2;
799 }
800 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
801 clock.p = (clock.p1 * clock.p2);
802 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
803 clock.vco = 0;
804 memcpy(best_clock, &clock, sizeof(intel_clock_t));
805 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700806}
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700807static bool
808intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
809 int target, int refclk, intel_clock_t *match_clock,
810 intel_clock_t *best_clock)
811{
812 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
813 u32 m, n, fastclk;
814 u32 updrate, minupdate, fracbits, p;
815 unsigned long bestppm, ppm, absppm;
816 int dotclk, flag;
817
Alan Coxaf447bd2012-07-25 13:49:18 +0100818 flag = 0;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700819 dotclk = target * 1000;
820 bestppm = 1000000;
821 ppm = absppm = 0;
822 fastclk = dotclk / (2*100);
823 updrate = 0;
824 minupdate = 19200;
825 fracbits = 1;
826 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
827 bestm1 = bestm2 = bestp1 = bestp2 = 0;
828
829 /* based on hardware requirement, prefer smaller n to precision */
830 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
831 updrate = refclk / n;
832 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
833 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
834 if (p2 > 10)
835 p2 = p2 - 1;
836 p = p1 * p2;
837 /* based on hardware requirement, prefer bigger m1,m2 values */
838 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
839 m2 = (((2*(fastclk * p * n / m1 )) +
840 refclk) / (2*refclk));
841 m = m1 * m2;
842 vco = updrate * m;
843 if (vco >= limit->vco.min && vco < limit->vco.max) {
844 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
845 absppm = (ppm > 0) ? ppm : (-ppm);
846 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
847 bestppm = 0;
848 flag = 1;
849 }
850 if (absppm < bestppm - 10) {
851 bestppm = absppm;
852 flag = 1;
853 }
854 if (flag) {
855 bestn = n;
856 bestm1 = m1;
857 bestm2 = m2;
858 bestp1 = p1;
859 bestp2 = p2;
860 flag = 0;
861 }
862 }
863 }
864 }
865 }
866 }
867 best_clock->n = bestn;
868 best_clock->m1 = bestm1;
869 best_clock->m2 = bestm2;
870 best_clock->p1 = bestp1;
871 best_clock->p2 = bestp2;
872
873 return true;
874}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700875
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200876enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
877 enum pipe pipe)
878{
879 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
881
882 return intel_crtc->cpu_transcoder;
883}
884
Paulo Zanonia928d532012-05-04 17:18:15 -0300885static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
886{
887 struct drm_i915_private *dev_priv = dev->dev_private;
888 u32 frame, frame_reg = PIPEFRAME(pipe);
889
890 frame = I915_READ(frame_reg);
891
892 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
893 DRM_DEBUG_KMS("vblank wait timed out\n");
894}
895
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700896/**
897 * intel_wait_for_vblank - wait for vblank on a given pipe
898 * @dev: drm device
899 * @pipe: pipe to wait for
900 *
901 * Wait for vblank to occur on a given pipe. Needed for various bits of
902 * mode setting code.
903 */
904void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800905{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700906 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800907 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700908
Paulo Zanonia928d532012-05-04 17:18:15 -0300909 if (INTEL_INFO(dev)->gen >= 5) {
910 ironlake_wait_for_vblank(dev, pipe);
911 return;
912 }
913
Chris Wilson300387c2010-09-05 20:25:43 +0100914 /* Clear existing vblank status. Note this will clear any other
915 * sticky status fields as well.
916 *
917 * This races with i915_driver_irq_handler() with the result
918 * that either function could miss a vblank event. Here it is not
919 * fatal, as we will either wait upon the next vblank interrupt or
920 * timeout. Generally speaking intel_wait_for_vblank() is only
921 * called during modeset at which time the GPU should be idle and
922 * should *not* be performing page flips and thus not waiting on
923 * vblanks...
924 * Currently, the result of us stealing a vblank from the irq
925 * handler is that a single frame will be skipped during swapbuffers.
926 */
927 I915_WRITE(pipestat_reg,
928 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
929
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700930 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100931 if (wait_for(I915_READ(pipestat_reg) &
932 PIPE_VBLANK_INTERRUPT_STATUS,
933 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700934 DRM_DEBUG_KMS("vblank wait timed out\n");
935}
936
Keith Packardab7ad7f2010-10-03 00:33:06 -0700937/*
938 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700939 * @dev: drm device
940 * @pipe: pipe to wait for
941 *
942 * After disabling a pipe, we can't wait for vblank in the usual way,
943 * spinning on the vblank interrupt status bit, since we won't actually
944 * see an interrupt when the pipe is disabled.
945 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700946 * On Gen4 and above:
947 * wait for the pipe register state bit to turn off
948 *
949 * Otherwise:
950 * wait for the display line value to settle (it usually
951 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100952 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700953 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100954void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700955{
956 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200957 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
958 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700959
Keith Packardab7ad7f2010-10-03 00:33:06 -0700960 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200961 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700962
Keith Packardab7ad7f2010-10-03 00:33:06 -0700963 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100964 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
965 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200966 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700967 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300968 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100969 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700970 unsigned long timeout = jiffies + msecs_to_jiffies(100);
971
Paulo Zanoni837ba002012-05-04 17:18:14 -0300972 if (IS_GEN2(dev))
973 line_mask = DSL_LINEMASK_GEN2;
974 else
975 line_mask = DSL_LINEMASK_GEN3;
976
Keith Packardab7ad7f2010-10-03 00:33:06 -0700977 /* Wait for the display line to settle */
978 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300979 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700980 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300981 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700982 time_after(timeout, jiffies));
983 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200984 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700985 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800986}
987
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 Zanoni69310162013-01-29 16:35:19 -02001217 if (IS_HASWELL(dev_priv->dev) && cpu_transcoder != TRANSCODER_EDP &&
1218 !(I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_ENABLE)) {
1219 cur_state = false;
1220 } else {
1221 reg = PIPECONF(cpu_transcoder);
1222 val = I915_READ(reg);
1223 cur_state = !!(val & PIPECONF_ENABLE);
1224 }
1225
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001226 WARN(cur_state != state,
1227 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001228 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001229}
1230
Chris Wilson931872f2012-01-16 23:01:13 +00001231static void assert_plane(struct drm_i915_private *dev_priv,
1232 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001233{
1234 int reg;
1235 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001236 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001237
1238 reg = DSPCNTR(plane);
1239 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001240 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1241 WARN(cur_state != state,
1242 "plane %c assertion failure (expected %s, current %s)\n",
1243 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001244}
1245
Chris Wilson931872f2012-01-16 23:01:13 +00001246#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1247#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1248
Jesse Barnesb24e7172011-01-04 15:09:30 -08001249static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1250 enum pipe pipe)
1251{
1252 int reg, i;
1253 u32 val;
1254 int cur_pipe;
1255
Jesse Barnes19ec1352011-02-02 12:28:02 -08001256 /* Planes are fixed to pipes on ILK+ */
Adam Jackson28c057942011-10-07 14:38:42 -04001257 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1258 reg = DSPCNTR(pipe);
1259 val = I915_READ(reg);
1260 WARN((val & DISPLAY_PLANE_ENABLE),
1261 "plane %c assertion failure, should be disabled but not\n",
1262 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001263 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001264 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001265
Jesse Barnesb24e7172011-01-04 15:09:30 -08001266 /* Need to check both planes against the pipe */
1267 for (i = 0; i < 2; i++) {
1268 reg = DSPCNTR(i);
1269 val = I915_READ(reg);
1270 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1271 DISPPLANE_SEL_PIPE_SHIFT;
1272 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001273 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1274 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001275 }
1276}
1277
Jesse Barnes92f25842011-01-04 15:09:34 -08001278static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1279{
1280 u32 val;
1281 bool enabled;
1282
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001283 if (HAS_PCH_LPT(dev_priv->dev)) {
1284 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1285 return;
1286 }
1287
Jesse Barnes92f25842011-01-04 15:09:34 -08001288 val = I915_READ(PCH_DREF_CONTROL);
1289 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1290 DREF_SUPERSPREAD_SOURCE_MASK));
1291 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1292}
1293
1294static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1295 enum pipe pipe)
1296{
1297 int reg;
1298 u32 val;
1299 bool enabled;
1300
1301 reg = TRANSCONF(pipe);
1302 val = I915_READ(reg);
1303 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001304 WARN(enabled,
1305 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1306 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001307}
1308
Keith Packard4e634382011-08-06 10:39:45 -07001309static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1310 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001311{
1312 if ((val & DP_PORT_EN) == 0)
1313 return false;
1314
1315 if (HAS_PCH_CPT(dev_priv->dev)) {
1316 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1317 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1318 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1319 return false;
1320 } else {
1321 if ((val & DP_PIPE_MASK) != (pipe << 30))
1322 return false;
1323 }
1324 return true;
1325}
1326
Keith Packard1519b992011-08-06 10:35:34 -07001327static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1328 enum pipe pipe, u32 val)
1329{
1330 if ((val & PORT_ENABLE) == 0)
1331 return false;
1332
1333 if (HAS_PCH_CPT(dev_priv->dev)) {
1334 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1335 return false;
1336 } else {
1337 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1338 return false;
1339 }
1340 return true;
1341}
1342
1343static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1344 enum pipe pipe, u32 val)
1345{
1346 if ((val & LVDS_PORT_EN) == 0)
1347 return false;
1348
1349 if (HAS_PCH_CPT(dev_priv->dev)) {
1350 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1351 return false;
1352 } else {
1353 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1354 return false;
1355 }
1356 return true;
1357}
1358
1359static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1360 enum pipe pipe, u32 val)
1361{
1362 if ((val & ADPA_DAC_ENABLE) == 0)
1363 return false;
1364 if (HAS_PCH_CPT(dev_priv->dev)) {
1365 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1366 return false;
1367 } else {
1368 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1369 return false;
1370 }
1371 return true;
1372}
1373
Jesse Barnes291906f2011-02-02 12:28:03 -08001374static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001375 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001376{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001377 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001378 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001379 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001380 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001381
Daniel Vetter75c5da22012-09-10 21:58:29 +02001382 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1383 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001384 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001385}
1386
1387static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1388 enum pipe pipe, int reg)
1389{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001390 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001391 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001392 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001393 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001394
Daniel Vetter75c5da22012-09-10 21:58:29 +02001395 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & PORT_ENABLE) == 0
1396 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001397 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001398}
1399
1400static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1401 enum pipe pipe)
1402{
1403 int reg;
1404 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001405
Keith Packardf0575e92011-07-25 22:12:43 -07001406 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1407 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1408 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001409
1410 reg = PCH_ADPA;
1411 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001412 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001413 "PCH VGA 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 reg = PCH_LVDS;
1417 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001418 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001419 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001420 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001421
1422 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1423 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1424 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1425}
1426
Jesse Barnesb24e7172011-01-04 15:09:30 -08001427/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001428 * intel_enable_pll - enable a PLL
1429 * @dev_priv: i915 private structure
1430 * @pipe: pipe PLL to enable
1431 *
1432 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1433 * make sure the PLL reg is writable first though, since the panel write
1434 * protect mechanism may be enabled.
1435 *
1436 * Note! This is for pre-ILK only.
Thomas Richter7434a252012-07-18 19:22:30 +02001437 *
1438 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001439 */
1440static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1441{
1442 int reg;
1443 u32 val;
1444
1445 /* No really, not for ILK+ */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001446 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001447
1448 /* PLL is protected by panel, make sure we can write it */
1449 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1450 assert_panel_unlocked(dev_priv, pipe);
1451
1452 reg = DPLL(pipe);
1453 val = I915_READ(reg);
1454 val |= DPLL_VCO_ENABLE;
1455
1456 /* We do this three times for luck */
1457 I915_WRITE(reg, val);
1458 POSTING_READ(reg);
1459 udelay(150); /* wait for warmup */
1460 I915_WRITE(reg, val);
1461 POSTING_READ(reg);
1462 udelay(150); /* wait for warmup */
1463 I915_WRITE(reg, val);
1464 POSTING_READ(reg);
1465 udelay(150); /* wait for warmup */
1466}
1467
1468/**
1469 * intel_disable_pll - disable a PLL
1470 * @dev_priv: i915 private structure
1471 * @pipe: pipe PLL to disable
1472 *
1473 * Disable the PLL for @pipe, making sure the pipe is off first.
1474 *
1475 * Note! This is for pre-ILK only.
1476 */
1477static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1478{
1479 int reg;
1480 u32 val;
1481
1482 /* Don't disable pipe A or pipe A PLLs if needed */
1483 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1484 return;
1485
1486 /* Make sure the pipe isn't still relying on us */
1487 assert_pipe_disabled(dev_priv, pipe);
1488
1489 reg = DPLL(pipe);
1490 val = I915_READ(reg);
1491 val &= ~DPLL_VCO_ENABLE;
1492 I915_WRITE(reg, val);
1493 POSTING_READ(reg);
1494}
1495
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001496/* SBI access */
1497static void
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001498intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value,
1499 enum intel_sbi_destination destination)
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001500{
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001501 u32 tmp;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001502
Daniel Vetter09153002012-12-12 14:06:44 +01001503 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001504
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001505 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001506 100)) {
1507 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001508 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001509 }
1510
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001511 I915_WRITE(SBI_ADDR, (reg << 16));
1512 I915_WRITE(SBI_DATA, value);
1513
1514 if (destination == SBI_ICLK)
1515 tmp = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRWR;
1516 else
1517 tmp = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IOWR;
1518 I915_WRITE(SBI_CTL_STAT, SBI_BUSY | tmp);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001519
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001520 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001521 100)) {
1522 DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001523 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001524 }
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001525}
1526
1527static u32
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001528intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg,
1529 enum intel_sbi_destination destination)
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001530{
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001531 u32 value = 0;
Daniel Vetter09153002012-12-12 14:06:44 +01001532 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001533
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001534 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001535 100)) {
1536 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001537 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001538 }
1539
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001540 I915_WRITE(SBI_ADDR, (reg << 16));
1541
1542 if (destination == SBI_ICLK)
1543 value = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRRD;
1544 else
1545 value = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IORD;
1546 I915_WRITE(SBI_CTL_STAT, value | SBI_BUSY);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001547
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001548 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001549 100)) {
1550 DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001551 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001552 }
1553
Daniel Vetter09153002012-12-12 14:06:44 +01001554 return I915_READ(SBI_DATA);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001555}
1556
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001557/**
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001558 * ironlake_enable_pch_pll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001559 * @dev_priv: i915 private structure
1560 * @pipe: pipe PLL to enable
1561 *
1562 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1563 * drives the transcoder clock.
1564 */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001565static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001566{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001567 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Chris Wilson48da64a2012-05-13 20:16:12 +01001568 struct intel_pch_pll *pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001569 int reg;
1570 u32 val;
1571
Chris Wilson48da64a2012-05-13 20:16:12 +01001572 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001573 BUG_ON(dev_priv->info->gen < 5);
Chris Wilson48da64a2012-05-13 20:16:12 +01001574 pll = intel_crtc->pch_pll;
1575 if (pll == NULL)
1576 return;
1577
1578 if (WARN_ON(pll->refcount == 0))
1579 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001580
1581 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1582 pll->pll_reg, pll->active, pll->on,
1583 intel_crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001584
1585 /* PCH refclock must be enabled first */
1586 assert_pch_refclk_enabled(dev_priv);
1587
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001588 if (pll->active++ && pll->on) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001589 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001590 return;
1591 }
1592
1593 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1594
1595 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001596 val = I915_READ(reg);
1597 val |= DPLL_VCO_ENABLE;
1598 I915_WRITE(reg, val);
1599 POSTING_READ(reg);
1600 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001601
1602 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001603}
1604
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001605static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001606{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001607 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1608 struct intel_pch_pll *pll = intel_crtc->pch_pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001609 int reg;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001610 u32 val;
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001611
Jesse Barnes92f25842011-01-04 15:09:34 -08001612 /* PCH only available on ILK+ */
1613 BUG_ON(dev_priv->info->gen < 5);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001614 if (pll == NULL)
1615 return;
1616
Chris Wilson48da64a2012-05-13 20:16:12 +01001617 if (WARN_ON(pll->refcount == 0))
1618 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001619
1620 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1621 pll->pll_reg, pll->active, pll->on,
1622 intel_crtc->base.base.id);
1623
Chris Wilson48da64a2012-05-13 20:16:12 +01001624 if (WARN_ON(pll->active == 0)) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001625 assert_pch_pll_disabled(dev_priv, pll, NULL);
Chris Wilson48da64a2012-05-13 20:16:12 +01001626 return;
1627 }
1628
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001629 if (--pll->active) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001630 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001631 return;
1632 }
1633
1634 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
Jesse Barnes92f25842011-01-04 15:09:34 -08001635
1636 /* Make sure transcoder isn't still depending on us */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001637 assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001638
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001639 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001640 val = I915_READ(reg);
1641 val &= ~DPLL_VCO_ENABLE;
1642 I915_WRITE(reg, val);
1643 POSTING_READ(reg);
1644 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001645
1646 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001647}
1648
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001649static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1650 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001651{
Daniel Vetter23670b322012-11-01 09:15:30 +01001652 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001653 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetter23670b322012-11-01 09:15:30 +01001654 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001655
1656 /* PCH only available on ILK+ */
1657 BUG_ON(dev_priv->info->gen < 5);
1658
1659 /* Make sure PCH DPLL is enabled */
Chris Wilson92b27b02012-05-20 18:10:50 +01001660 assert_pch_pll_enabled(dev_priv,
1661 to_intel_crtc(crtc)->pch_pll,
1662 to_intel_crtc(crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001663
1664 /* FDI must be feeding us bits for PCH ports */
1665 assert_fdi_tx_enabled(dev_priv, pipe);
1666 assert_fdi_rx_enabled(dev_priv, pipe);
1667
Daniel Vetter23670b322012-11-01 09:15:30 +01001668 if (HAS_PCH_CPT(dev)) {
1669 /* Workaround: Set the timing override bit before enabling the
1670 * pch transcoder. */
1671 reg = TRANS_CHICKEN2(pipe);
1672 val = I915_READ(reg);
1673 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1674 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001675 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001676
Jesse Barnes040484a2011-01-03 12:14:26 -08001677 reg = TRANSCONF(pipe);
1678 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001679 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001680
1681 if (HAS_PCH_IBX(dev_priv->dev)) {
1682 /*
1683 * make the BPC in transcoder be consistent with
1684 * that in pipeconf reg.
1685 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001686 val &= ~PIPECONF_BPC_MASK;
1687 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001688 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001689
1690 val &= ~TRANS_INTERLACE_MASK;
1691 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001692 if (HAS_PCH_IBX(dev_priv->dev) &&
1693 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1694 val |= TRANS_LEGACY_INTERLACED_ILK;
1695 else
1696 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001697 else
1698 val |= TRANS_PROGRESSIVE;
1699
Jesse Barnes040484a2011-01-03 12:14:26 -08001700 I915_WRITE(reg, val | TRANS_ENABLE);
1701 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1702 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1703}
1704
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001705static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001706 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001707{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001708 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001709
1710 /* PCH only available on ILK+ */
1711 BUG_ON(dev_priv->info->gen < 5);
1712
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001713 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001714 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001715 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001716
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001717 /* Workaround: set timing override bit. */
1718 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001719 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001720 I915_WRITE(_TRANSA_CHICKEN2, val);
1721
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001722 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001723 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001724
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001725 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1726 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001727 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001728 else
1729 val |= TRANS_PROGRESSIVE;
1730
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001731 I915_WRITE(TRANSCONF(TRANSCODER_A), val);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001732 if (wait_for(I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE, 100))
1733 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001734}
1735
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001736static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1737 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001738{
Daniel Vetter23670b322012-11-01 09:15:30 +01001739 struct drm_device *dev = dev_priv->dev;
1740 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001741
1742 /* FDI relies on the transcoder */
1743 assert_fdi_tx_disabled(dev_priv, pipe);
1744 assert_fdi_rx_disabled(dev_priv, pipe);
1745
Jesse Barnes291906f2011-02-02 12:28:03 -08001746 /* Ports must be off as well */
1747 assert_pch_ports_disabled(dev_priv, pipe);
1748
Jesse Barnes040484a2011-01-03 12:14:26 -08001749 reg = TRANSCONF(pipe);
1750 val = I915_READ(reg);
1751 val &= ~TRANS_ENABLE;
1752 I915_WRITE(reg, val);
1753 /* wait for PCH transcoder off, transcoder state */
1754 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes4c9c18c2011-10-13 09:46:32 -07001755 DRM_ERROR("failed to disable transcoder %d\n", pipe);
Daniel Vetter23670b322012-11-01 09:15:30 +01001756
1757 if (!HAS_PCH_IBX(dev)) {
1758 /* Workaround: Clear the timing override chicken bit again. */
1759 reg = TRANS_CHICKEN2(pipe);
1760 val = I915_READ(reg);
1761 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1762 I915_WRITE(reg, val);
1763 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001764}
1765
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001766static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001767{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001768 u32 val;
1769
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001770 val = I915_READ(_TRANSACONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001771 val &= ~TRANS_ENABLE;
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001772 I915_WRITE(_TRANSACONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001773 /* wait for PCH transcoder off, transcoder state */
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001774 if (wait_for((I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE) == 0, 50))
1775 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001776
1777 /* Workaround: clear timing override bit. */
1778 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001779 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001780 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001781}
1782
1783/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001784 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001785 * @dev_priv: i915 private structure
1786 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001787 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001788 *
1789 * Enable @pipe, making sure that various hardware specific requirements
1790 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1791 *
1792 * @pipe should be %PIPE_A or %PIPE_B.
1793 *
1794 * Will wait until the pipe is actually running (i.e. first vblank) before
1795 * returning.
1796 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001797static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1798 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001799{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001800 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1801 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001802 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001803 int reg;
1804 u32 val;
1805
Paulo Zanoni681e5812012-12-06 11:12:38 -02001806 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001807 pch_transcoder = TRANSCODER_A;
1808 else
1809 pch_transcoder = pipe;
1810
Jesse Barnesb24e7172011-01-04 15:09:30 -08001811 /*
1812 * A pipe without a PLL won't actually be able to drive bits from
1813 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1814 * need the check.
1815 */
1816 if (!HAS_PCH_SPLIT(dev_priv->dev))
1817 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001818 else {
1819 if (pch_port) {
1820 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001821 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001822 assert_fdi_tx_pll_enabled(dev_priv,
1823 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001824 }
1825 /* FIXME: assert CPU port conditions for SNB+ */
1826 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001827
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001828 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001829 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001830 if (val & PIPECONF_ENABLE)
1831 return;
1832
1833 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001834 intel_wait_for_vblank(dev_priv->dev, pipe);
1835}
1836
1837/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001838 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001839 * @dev_priv: i915 private structure
1840 * @pipe: pipe to disable
1841 *
1842 * Disable @pipe, making sure that various hardware specific requirements
1843 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1844 *
1845 * @pipe should be %PIPE_A or %PIPE_B.
1846 *
1847 * Will wait until the pipe has shut down before returning.
1848 */
1849static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1850 enum pipe pipe)
1851{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001852 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1853 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001854 int reg;
1855 u32 val;
1856
1857 /*
1858 * Make sure planes won't keep trying to pump pixels to us,
1859 * or we might hang the display.
1860 */
1861 assert_planes_disabled(dev_priv, pipe);
1862
1863 /* Don't disable pipe A or pipe A PLLs if needed */
1864 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1865 return;
1866
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001867 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001868 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001869 if ((val & PIPECONF_ENABLE) == 0)
1870 return;
1871
1872 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001873 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1874}
1875
Keith Packardd74362c2011-07-28 14:47:14 -07001876/*
1877 * Plane regs are double buffered, going from enabled->disabled needs a
1878 * trigger in order to latch. The display address reg provides this.
1879 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001880void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001881 enum plane plane)
1882{
Damien Lespiau14f86142012-10-29 15:24:49 +00001883 if (dev_priv->info->gen >= 4)
1884 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1885 else
1886 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001887}
1888
Jesse Barnesb24e7172011-01-04 15:09:30 -08001889/**
1890 * intel_enable_plane - enable a display plane on a given pipe
1891 * @dev_priv: i915 private structure
1892 * @plane: plane to enable
1893 * @pipe: pipe being fed
1894 *
1895 * Enable @plane on @pipe, making sure that @pipe is running first.
1896 */
1897static void intel_enable_plane(struct drm_i915_private *dev_priv,
1898 enum plane plane, enum pipe pipe)
1899{
1900 int reg;
1901 u32 val;
1902
1903 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1904 assert_pipe_enabled(dev_priv, pipe);
1905
1906 reg = DSPCNTR(plane);
1907 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001908 if (val & DISPLAY_PLANE_ENABLE)
1909 return;
1910
1911 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001912 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001913 intel_wait_for_vblank(dev_priv->dev, pipe);
1914}
1915
Jesse Barnesb24e7172011-01-04 15:09:30 -08001916/**
1917 * intel_disable_plane - disable a display plane
1918 * @dev_priv: i915 private structure
1919 * @plane: plane to disable
1920 * @pipe: pipe consuming the data
1921 *
1922 * Disable @plane; should be an independent operation.
1923 */
1924static void intel_disable_plane(struct drm_i915_private *dev_priv,
1925 enum plane plane, enum pipe pipe)
1926{
1927 int reg;
1928 u32 val;
1929
1930 reg = DSPCNTR(plane);
1931 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001932 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1933 return;
1934
1935 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001936 intel_flush_display_plane(dev_priv, plane);
1937 intel_wait_for_vblank(dev_priv->dev, pipe);
1938}
1939
Chris Wilson127bd2a2010-07-23 23:32:05 +01001940int
Chris Wilson48b956c2010-09-14 12:50:34 +01001941intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001942 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001943 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001944{
Chris Wilsonce453d82011-02-21 14:43:56 +00001945 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001946 u32 alignment;
1947 int ret;
1948
Chris Wilson05394f32010-11-08 19:18:58 +00001949 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001950 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001951 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1952 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001953 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001954 alignment = 4 * 1024;
1955 else
1956 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001957 break;
1958 case I915_TILING_X:
1959 /* pin() will align the object as required by fence */
1960 alignment = 0;
1961 break;
1962 case I915_TILING_Y:
1963 /* FIXME: Is this true? */
1964 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1965 return -EINVAL;
1966 default:
1967 BUG();
1968 }
1969
Chris Wilsonce453d82011-02-21 14:43:56 +00001970 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001971 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001972 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001973 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001974
1975 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1976 * fence, whereas 965+ only requires a fence if using
1977 * framebuffer compression. For simplicity, we always install
1978 * a fence as the cost is not that onerous.
1979 */
Chris Wilson06d98132012-04-17 15:31:24 +01001980 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001981 if (ret)
1982 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001983
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001984 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001985
Chris Wilsonce453d82011-02-21 14:43:56 +00001986 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001987 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001988
1989err_unpin:
1990 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001991err_interruptible:
1992 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001993 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001994}
1995
Chris Wilson1690e1e2011-12-14 13:57:08 +01001996void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1997{
1998 i915_gem_object_unpin_fence(obj);
1999 i915_gem_object_unpin(obj);
2000}
2001
Daniel Vetterc2c75132012-07-05 12:17:30 +02002002/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2003 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002004unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2005 unsigned int tiling_mode,
2006 unsigned int cpp,
2007 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002008{
Chris Wilsonbc752862013-02-21 20:04:31 +00002009 if (tiling_mode != I915_TILING_NONE) {
2010 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002011
Chris Wilsonbc752862013-02-21 20:04:31 +00002012 tile_rows = *y / 8;
2013 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002014
Chris Wilsonbc752862013-02-21 20:04:31 +00002015 tiles = *x / (512/cpp);
2016 *x %= 512/cpp;
2017
2018 return tile_rows * pitch * 8 + tiles * 4096;
2019 } else {
2020 unsigned int offset;
2021
2022 offset = *y * pitch + *x * cpp;
2023 *y = 0;
2024 *x = (offset & 4095) / cpp;
2025 return offset & -4096;
2026 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002027}
2028
Jesse Barnes17638cd2011-06-24 12:19:23 -07002029static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2030 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002031{
2032 struct drm_device *dev = crtc->dev;
2033 struct drm_i915_private *dev_priv = dev->dev_private;
2034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2035 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002036 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002037 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002038 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002039 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002040 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002041
2042 switch (plane) {
2043 case 0:
2044 case 1:
2045 break;
2046 default:
2047 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2048 return -EINVAL;
2049 }
2050
2051 intel_fb = to_intel_framebuffer(fb);
2052 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002053
Chris Wilson5eddb702010-09-11 13:48:45 +01002054 reg = DSPCNTR(plane);
2055 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002056 /* Mask out pixel format bits in case we change it */
2057 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002058 switch (fb->pixel_format) {
2059 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002060 dspcntr |= DISPPLANE_8BPP;
2061 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002062 case DRM_FORMAT_XRGB1555:
2063 case DRM_FORMAT_ARGB1555:
2064 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002065 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002066 case DRM_FORMAT_RGB565:
2067 dspcntr |= DISPPLANE_BGRX565;
2068 break;
2069 case DRM_FORMAT_XRGB8888:
2070 case DRM_FORMAT_ARGB8888:
2071 dspcntr |= DISPPLANE_BGRX888;
2072 break;
2073 case DRM_FORMAT_XBGR8888:
2074 case DRM_FORMAT_ABGR8888:
2075 dspcntr |= DISPPLANE_RGBX888;
2076 break;
2077 case DRM_FORMAT_XRGB2101010:
2078 case DRM_FORMAT_ARGB2101010:
2079 dspcntr |= DISPPLANE_BGRX101010;
2080 break;
2081 case DRM_FORMAT_XBGR2101010:
2082 case DRM_FORMAT_ABGR2101010:
2083 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002084 break;
2085 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002086 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes81255562010-08-02 12:07:50 -07002087 return -EINVAL;
2088 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002089
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002090 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002091 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002092 dspcntr |= DISPPLANE_TILED;
2093 else
2094 dspcntr &= ~DISPPLANE_TILED;
2095 }
2096
Chris Wilson5eddb702010-09-11 13:48:45 +01002097 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002098
Daniel Vettere506a0c2012-07-05 12:17:29 +02002099 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002100
Daniel Vetterc2c75132012-07-05 12:17:30 +02002101 if (INTEL_INFO(dev)->gen >= 4) {
2102 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002103 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2104 fb->bits_per_pixel / 8,
2105 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002106 linear_offset -= intel_crtc->dspaddr_offset;
2107 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002108 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002109 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002110
2111 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2112 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002113 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002114 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002115 I915_MODIFY_DISPBASE(DSPSURF(plane),
2116 obj->gtt_offset + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002117 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002118 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002119 } else
Daniel Vettere506a0c2012-07-05 12:17:29 +02002120 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002121 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002122
Jesse Barnes17638cd2011-06-24 12:19:23 -07002123 return 0;
2124}
2125
2126static int ironlake_update_plane(struct drm_crtc *crtc,
2127 struct drm_framebuffer *fb, int x, int y)
2128{
2129 struct drm_device *dev = crtc->dev;
2130 struct drm_i915_private *dev_priv = dev->dev_private;
2131 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2132 struct intel_framebuffer *intel_fb;
2133 struct drm_i915_gem_object *obj;
2134 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002135 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002136 u32 dspcntr;
2137 u32 reg;
2138
2139 switch (plane) {
2140 case 0:
2141 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002142 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002143 break;
2144 default:
2145 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2146 return -EINVAL;
2147 }
2148
2149 intel_fb = to_intel_framebuffer(fb);
2150 obj = intel_fb->obj;
2151
2152 reg = DSPCNTR(plane);
2153 dspcntr = I915_READ(reg);
2154 /* Mask out pixel format bits in case we change it */
2155 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002156 switch (fb->pixel_format) {
2157 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002158 dspcntr |= DISPPLANE_8BPP;
2159 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002160 case DRM_FORMAT_RGB565:
2161 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002162 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002163 case DRM_FORMAT_XRGB8888:
2164 case DRM_FORMAT_ARGB8888:
2165 dspcntr |= DISPPLANE_BGRX888;
2166 break;
2167 case DRM_FORMAT_XBGR8888:
2168 case DRM_FORMAT_ABGR8888:
2169 dspcntr |= DISPPLANE_RGBX888;
2170 break;
2171 case DRM_FORMAT_XRGB2101010:
2172 case DRM_FORMAT_ARGB2101010:
2173 dspcntr |= DISPPLANE_BGRX101010;
2174 break;
2175 case DRM_FORMAT_XBGR2101010:
2176 case DRM_FORMAT_ABGR2101010:
2177 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002178 break;
2179 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002180 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002181 return -EINVAL;
2182 }
2183
2184 if (obj->tiling_mode != I915_TILING_NONE)
2185 dspcntr |= DISPPLANE_TILED;
2186 else
2187 dspcntr &= ~DISPPLANE_TILED;
2188
2189 /* must disable */
2190 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2191
2192 I915_WRITE(reg, dspcntr);
2193
Daniel Vettere506a0c2012-07-05 12:17:29 +02002194 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002195 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002196 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2197 fb->bits_per_pixel / 8,
2198 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002199 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002200
Daniel Vettere506a0c2012-07-05 12:17:29 +02002201 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2202 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002203 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002204 I915_MODIFY_DISPBASE(DSPSURF(plane),
2205 obj->gtt_offset + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002206 if (IS_HASWELL(dev)) {
2207 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2208 } else {
2209 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2210 I915_WRITE(DSPLINOFF(plane), linear_offset);
2211 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002212 POSTING_READ(reg);
2213
2214 return 0;
2215}
2216
2217/* Assume fb object is pinned & idle & fenced and just update base pointers */
2218static int
2219intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2220 int x, int y, enum mode_set_atomic state)
2221{
2222 struct drm_device *dev = crtc->dev;
2223 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002224
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002225 if (dev_priv->display.disable_fbc)
2226 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002227 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002228
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002229 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002230}
2231
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002232static int
Chris Wilson14667a42012-04-03 17:58:35 +01002233intel_finish_fb(struct drm_framebuffer *old_fb)
2234{
2235 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2236 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2237 bool was_interruptible = dev_priv->mm.interruptible;
2238 int ret;
2239
Chris Wilson14667a42012-04-03 17:58:35 +01002240 /* Big Hammer, we also need to ensure that any pending
2241 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2242 * current scanout is retired before unpinning the old
2243 * framebuffer.
2244 *
2245 * This should only fail upon a hung GPU, in which case we
2246 * can safely continue.
2247 */
2248 dev_priv->mm.interruptible = false;
2249 ret = i915_gem_object_finish_gpu(obj);
2250 dev_priv->mm.interruptible = was_interruptible;
2251
2252 return ret;
2253}
2254
Ville Syrjälä198598d2012-10-31 17:50:24 +02002255static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2256{
2257 struct drm_device *dev = crtc->dev;
2258 struct drm_i915_master_private *master_priv;
2259 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2260
2261 if (!dev->primary->master)
2262 return;
2263
2264 master_priv = dev->primary->master->driver_priv;
2265 if (!master_priv->sarea_priv)
2266 return;
2267
2268 switch (intel_crtc->pipe) {
2269 case 0:
2270 master_priv->sarea_priv->pipeA_x = x;
2271 master_priv->sarea_priv->pipeA_y = y;
2272 break;
2273 case 1:
2274 master_priv->sarea_priv->pipeB_x = x;
2275 master_priv->sarea_priv->pipeB_y = y;
2276 break;
2277 default:
2278 break;
2279 }
2280}
2281
Chris Wilson14667a42012-04-03 17:58:35 +01002282static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002283intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002284 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002285{
2286 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002287 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002289 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002290 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002291
2292 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002293 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002294 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002295 return 0;
2296 }
2297
Eugeni Dodonov5826eca2012-05-09 15:37:12 -03002298 if(intel_crtc->plane > dev_priv->num_pipe) {
2299 DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
2300 intel_crtc->plane,
2301 dev_priv->num_pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002302 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002303 }
2304
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002305 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002306 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002307 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002308 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002309 if (ret != 0) {
2310 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002311 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002312 return ret;
2313 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002314
Daniel Vetter94352cf2012-07-05 22:51:56 +02002315 if (crtc->fb)
2316 intel_finish_fb(crtc->fb);
Chris Wilson265db952010-09-20 15:41:01 +01002317
Daniel Vetter94352cf2012-07-05 22:51:56 +02002318 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002319 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002320 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002321 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002322 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002323 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002324 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002325
Daniel Vetter94352cf2012-07-05 22:51:56 +02002326 old_fb = crtc->fb;
2327 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002328 crtc->x = x;
2329 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002330
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002331 if (old_fb) {
2332 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002333 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002334 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002335
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002336 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002337 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002338
Ville Syrjälä198598d2012-10-31 17:50:24 +02002339 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002340
2341 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002342}
2343
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002344static void intel_fdi_normal_train(struct drm_crtc *crtc)
2345{
2346 struct drm_device *dev = crtc->dev;
2347 struct drm_i915_private *dev_priv = dev->dev_private;
2348 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2349 int pipe = intel_crtc->pipe;
2350 u32 reg, temp;
2351
2352 /* enable normal train */
2353 reg = FDI_TX_CTL(pipe);
2354 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002355 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002356 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2357 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002358 } else {
2359 temp &= ~FDI_LINK_TRAIN_NONE;
2360 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002361 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002362 I915_WRITE(reg, temp);
2363
2364 reg = FDI_RX_CTL(pipe);
2365 temp = I915_READ(reg);
2366 if (HAS_PCH_CPT(dev)) {
2367 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2368 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2369 } else {
2370 temp &= ~FDI_LINK_TRAIN_NONE;
2371 temp |= FDI_LINK_TRAIN_NONE;
2372 }
2373 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2374
2375 /* wait one idle pattern time */
2376 POSTING_READ(reg);
2377 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002378
2379 /* IVB wants error correction enabled */
2380 if (IS_IVYBRIDGE(dev))
2381 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2382 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002383}
2384
Daniel Vetter01a415f2012-10-27 15:58:40 +02002385static void ivb_modeset_global_resources(struct drm_device *dev)
2386{
2387 struct drm_i915_private *dev_priv = dev->dev_private;
2388 struct intel_crtc *pipe_B_crtc =
2389 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2390 struct intel_crtc *pipe_C_crtc =
2391 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2392 uint32_t temp;
2393
2394 /* When everything is off disable fdi C so that we could enable fdi B
2395 * with all lanes. XXX: This misses the case where a pipe is not using
2396 * any pch resources and so doesn't need any fdi lanes. */
2397 if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
2398 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2399 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2400
2401 temp = I915_READ(SOUTH_CHICKEN1);
2402 temp &= ~FDI_BC_BIFURCATION_SELECT;
2403 DRM_DEBUG_KMS("disabling fdi C rx\n");
2404 I915_WRITE(SOUTH_CHICKEN1, temp);
2405 }
2406}
2407
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002408/* The FDI link training functions for ILK/Ibexpeak. */
2409static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2410{
2411 struct drm_device *dev = crtc->dev;
2412 struct drm_i915_private *dev_priv = dev->dev_private;
2413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2414 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002415 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002416 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002417
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002418 /* FDI needs bits from pipe & plane first */
2419 assert_pipe_enabled(dev_priv, pipe);
2420 assert_plane_enabled(dev_priv, plane);
2421
Adam Jacksone1a44742010-06-25 15:32:14 -04002422 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2423 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002424 reg = FDI_RX_IMR(pipe);
2425 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002426 temp &= ~FDI_RX_SYMBOL_LOCK;
2427 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002428 I915_WRITE(reg, temp);
2429 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002430 udelay(150);
2431
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002432 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002433 reg = FDI_TX_CTL(pipe);
2434 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002435 temp &= ~(7 << 19);
2436 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002437 temp &= ~FDI_LINK_TRAIN_NONE;
2438 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002439 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002440
Chris Wilson5eddb702010-09-11 13:48:45 +01002441 reg = FDI_RX_CTL(pipe);
2442 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002443 temp &= ~FDI_LINK_TRAIN_NONE;
2444 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002445 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2446
2447 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002448 udelay(150);
2449
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002450 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002451 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2452 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2453 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002454
Chris Wilson5eddb702010-09-11 13:48:45 +01002455 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002456 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002457 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002458 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2459
2460 if ((temp & FDI_RX_BIT_LOCK)) {
2461 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002462 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002463 break;
2464 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002465 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002466 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002467 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002468
2469 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002470 reg = FDI_TX_CTL(pipe);
2471 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002472 temp &= ~FDI_LINK_TRAIN_NONE;
2473 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002474 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002475
Chris Wilson5eddb702010-09-11 13:48:45 +01002476 reg = FDI_RX_CTL(pipe);
2477 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002478 temp &= ~FDI_LINK_TRAIN_NONE;
2479 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002480 I915_WRITE(reg, temp);
2481
2482 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002483 udelay(150);
2484
Chris Wilson5eddb702010-09-11 13:48:45 +01002485 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002486 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002487 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002488 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2489
2490 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002491 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002492 DRM_DEBUG_KMS("FDI train 2 done.\n");
2493 break;
2494 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002495 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002496 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002497 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002498
2499 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002500
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002501}
2502
Akshay Joshi0206e352011-08-16 15:34:10 -04002503static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002504 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2505 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2506 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2507 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2508};
2509
2510/* The FDI link training functions for SNB/Cougarpoint. */
2511static void gen6_fdi_link_train(struct drm_crtc *crtc)
2512{
2513 struct drm_device *dev = crtc->dev;
2514 struct drm_i915_private *dev_priv = dev->dev_private;
2515 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2516 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002517 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002518
Adam Jacksone1a44742010-06-25 15:32:14 -04002519 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2520 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002521 reg = FDI_RX_IMR(pipe);
2522 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002523 temp &= ~FDI_RX_SYMBOL_LOCK;
2524 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002525 I915_WRITE(reg, temp);
2526
2527 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002528 udelay(150);
2529
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002530 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002531 reg = FDI_TX_CTL(pipe);
2532 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002533 temp &= ~(7 << 19);
2534 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002535 temp &= ~FDI_LINK_TRAIN_NONE;
2536 temp |= FDI_LINK_TRAIN_PATTERN_1;
2537 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2538 /* SNB-B */
2539 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002540 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002541
Daniel Vetterd74cf322012-10-26 10:58:13 +02002542 I915_WRITE(FDI_RX_MISC(pipe),
2543 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2544
Chris Wilson5eddb702010-09-11 13:48:45 +01002545 reg = FDI_RX_CTL(pipe);
2546 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002547 if (HAS_PCH_CPT(dev)) {
2548 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2549 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2550 } else {
2551 temp &= ~FDI_LINK_TRAIN_NONE;
2552 temp |= FDI_LINK_TRAIN_PATTERN_1;
2553 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002554 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2555
2556 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002557 udelay(150);
2558
Akshay Joshi0206e352011-08-16 15:34:10 -04002559 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002560 reg = FDI_TX_CTL(pipe);
2561 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002562 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2563 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002564 I915_WRITE(reg, temp);
2565
2566 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002567 udelay(500);
2568
Sean Paulfa37d392012-03-02 12:53:39 -05002569 for (retry = 0; retry < 5; retry++) {
2570 reg = FDI_RX_IIR(pipe);
2571 temp = I915_READ(reg);
2572 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2573 if (temp & FDI_RX_BIT_LOCK) {
2574 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2575 DRM_DEBUG_KMS("FDI train 1 done.\n");
2576 break;
2577 }
2578 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002579 }
Sean Paulfa37d392012-03-02 12:53:39 -05002580 if (retry < 5)
2581 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002582 }
2583 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002584 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002585
2586 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002587 reg = FDI_TX_CTL(pipe);
2588 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002589 temp &= ~FDI_LINK_TRAIN_NONE;
2590 temp |= FDI_LINK_TRAIN_PATTERN_2;
2591 if (IS_GEN6(dev)) {
2592 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2593 /* SNB-B */
2594 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2595 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002596 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002597
Chris Wilson5eddb702010-09-11 13:48:45 +01002598 reg = FDI_RX_CTL(pipe);
2599 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002600 if (HAS_PCH_CPT(dev)) {
2601 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2602 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2603 } else {
2604 temp &= ~FDI_LINK_TRAIN_NONE;
2605 temp |= FDI_LINK_TRAIN_PATTERN_2;
2606 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002607 I915_WRITE(reg, temp);
2608
2609 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002610 udelay(150);
2611
Akshay Joshi0206e352011-08-16 15:34:10 -04002612 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002613 reg = FDI_TX_CTL(pipe);
2614 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002615 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2616 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002617 I915_WRITE(reg, temp);
2618
2619 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002620 udelay(500);
2621
Sean Paulfa37d392012-03-02 12:53:39 -05002622 for (retry = 0; retry < 5; retry++) {
2623 reg = FDI_RX_IIR(pipe);
2624 temp = I915_READ(reg);
2625 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2626 if (temp & FDI_RX_SYMBOL_LOCK) {
2627 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2628 DRM_DEBUG_KMS("FDI train 2 done.\n");
2629 break;
2630 }
2631 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002632 }
Sean Paulfa37d392012-03-02 12:53:39 -05002633 if (retry < 5)
2634 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002635 }
2636 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002637 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002638
2639 DRM_DEBUG_KMS("FDI train done.\n");
2640}
2641
Jesse Barnes357555c2011-04-28 15:09:55 -07002642/* Manual link training for Ivy Bridge A0 parts */
2643static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2644{
2645 struct drm_device *dev = crtc->dev;
2646 struct drm_i915_private *dev_priv = dev->dev_private;
2647 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2648 int pipe = intel_crtc->pipe;
2649 u32 reg, temp, i;
2650
2651 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2652 for train result */
2653 reg = FDI_RX_IMR(pipe);
2654 temp = I915_READ(reg);
2655 temp &= ~FDI_RX_SYMBOL_LOCK;
2656 temp &= ~FDI_RX_BIT_LOCK;
2657 I915_WRITE(reg, temp);
2658
2659 POSTING_READ(reg);
2660 udelay(150);
2661
Daniel Vetter01a415f2012-10-27 15:58:40 +02002662 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2663 I915_READ(FDI_RX_IIR(pipe)));
2664
Jesse Barnes357555c2011-04-28 15:09:55 -07002665 /* enable CPU FDI TX and PCH FDI RX */
2666 reg = FDI_TX_CTL(pipe);
2667 temp = I915_READ(reg);
2668 temp &= ~(7 << 19);
2669 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2670 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2671 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2672 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2673 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
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_TX_ENABLE);
2676
Daniel Vetterd74cf322012-10-26 10:58:13 +02002677 I915_WRITE(FDI_RX_MISC(pipe),
2678 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2679
Jesse Barnes357555c2011-04-28 15:09:55 -07002680 reg = FDI_RX_CTL(pipe);
2681 temp = I915_READ(reg);
2682 temp &= ~FDI_LINK_TRAIN_AUTO;
2683 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2684 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002685 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002686 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2687
2688 POSTING_READ(reg);
2689 udelay(150);
2690
Akshay Joshi0206e352011-08-16 15:34:10 -04002691 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002692 reg = FDI_TX_CTL(pipe);
2693 temp = I915_READ(reg);
2694 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2695 temp |= snb_b_fdi_train_param[i];
2696 I915_WRITE(reg, temp);
2697
2698 POSTING_READ(reg);
2699 udelay(500);
2700
2701 reg = FDI_RX_IIR(pipe);
2702 temp = I915_READ(reg);
2703 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2704
2705 if (temp & FDI_RX_BIT_LOCK ||
2706 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2707 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002708 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002709 break;
2710 }
2711 }
2712 if (i == 4)
2713 DRM_ERROR("FDI train 1 fail!\n");
2714
2715 /* Train 2 */
2716 reg = FDI_TX_CTL(pipe);
2717 temp = I915_READ(reg);
2718 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2719 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2720 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2721 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2722 I915_WRITE(reg, temp);
2723
2724 reg = FDI_RX_CTL(pipe);
2725 temp = I915_READ(reg);
2726 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2727 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2728 I915_WRITE(reg, temp);
2729
2730 POSTING_READ(reg);
2731 udelay(150);
2732
Akshay Joshi0206e352011-08-16 15:34:10 -04002733 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002734 reg = FDI_TX_CTL(pipe);
2735 temp = I915_READ(reg);
2736 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2737 temp |= snb_b_fdi_train_param[i];
2738 I915_WRITE(reg, temp);
2739
2740 POSTING_READ(reg);
2741 udelay(500);
2742
2743 reg = FDI_RX_IIR(pipe);
2744 temp = I915_READ(reg);
2745 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2746
2747 if (temp & FDI_RX_SYMBOL_LOCK) {
2748 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002749 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002750 break;
2751 }
2752 }
2753 if (i == 4)
2754 DRM_ERROR("FDI train 2 fail!\n");
2755
2756 DRM_DEBUG_KMS("FDI train done.\n");
2757}
2758
Daniel Vetter88cefb62012-08-12 19:27:14 +02002759static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002760{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002761 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002762 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002763 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002764 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002765
Jesse Barnesc64e3112010-09-10 11:27:03 -07002766
Jesse Barnes0e23b992010-09-10 11:10:00 -07002767 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002768 reg = FDI_RX_CTL(pipe);
2769 temp = I915_READ(reg);
2770 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002771 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002772 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002773 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2774
2775 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002776 udelay(200);
2777
2778 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002779 temp = I915_READ(reg);
2780 I915_WRITE(reg, temp | FDI_PCDCLK);
2781
2782 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002783 udelay(200);
2784
Paulo Zanoni20749732012-11-23 15:30:38 -02002785 /* Enable CPU FDI TX PLL, always on for Ironlake */
2786 reg = FDI_TX_CTL(pipe);
2787 temp = I915_READ(reg);
2788 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2789 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002790
Paulo Zanoni20749732012-11-23 15:30:38 -02002791 POSTING_READ(reg);
2792 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002793 }
2794}
2795
Daniel Vetter88cefb62012-08-12 19:27:14 +02002796static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2797{
2798 struct drm_device *dev = intel_crtc->base.dev;
2799 struct drm_i915_private *dev_priv = dev->dev_private;
2800 int pipe = intel_crtc->pipe;
2801 u32 reg, temp;
2802
2803 /* Switch from PCDclk to Rawclk */
2804 reg = FDI_RX_CTL(pipe);
2805 temp = I915_READ(reg);
2806 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2807
2808 /* Disable CPU FDI TX PLL */
2809 reg = FDI_TX_CTL(pipe);
2810 temp = I915_READ(reg);
2811 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2812
2813 POSTING_READ(reg);
2814 udelay(100);
2815
2816 reg = FDI_RX_CTL(pipe);
2817 temp = I915_READ(reg);
2818 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2819
2820 /* Wait for the clocks to turn off. */
2821 POSTING_READ(reg);
2822 udelay(100);
2823}
2824
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002825static void ironlake_fdi_disable(struct drm_crtc *crtc)
2826{
2827 struct drm_device *dev = crtc->dev;
2828 struct drm_i915_private *dev_priv = dev->dev_private;
2829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2830 int pipe = intel_crtc->pipe;
2831 u32 reg, temp;
2832
2833 /* disable CPU FDI tx and PCH FDI rx */
2834 reg = FDI_TX_CTL(pipe);
2835 temp = I915_READ(reg);
2836 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2837 POSTING_READ(reg);
2838
2839 reg = FDI_RX_CTL(pipe);
2840 temp = I915_READ(reg);
2841 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002842 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002843 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2844
2845 POSTING_READ(reg);
2846 udelay(100);
2847
2848 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002849 if (HAS_PCH_IBX(dev)) {
2850 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002851 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002852
2853 /* still set train pattern 1 */
2854 reg = FDI_TX_CTL(pipe);
2855 temp = I915_READ(reg);
2856 temp &= ~FDI_LINK_TRAIN_NONE;
2857 temp |= FDI_LINK_TRAIN_PATTERN_1;
2858 I915_WRITE(reg, temp);
2859
2860 reg = FDI_RX_CTL(pipe);
2861 temp = I915_READ(reg);
2862 if (HAS_PCH_CPT(dev)) {
2863 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2864 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2865 } else {
2866 temp &= ~FDI_LINK_TRAIN_NONE;
2867 temp |= FDI_LINK_TRAIN_PATTERN_1;
2868 }
2869 /* BPC in FDI rx is consistent with that in PIPECONF */
2870 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002871 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002872 I915_WRITE(reg, temp);
2873
2874 POSTING_READ(reg);
2875 udelay(100);
2876}
2877
Chris Wilson5bb61642012-09-27 21:25:58 +01002878static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2879{
2880 struct drm_device *dev = crtc->dev;
2881 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002883 unsigned long flags;
2884 bool pending;
2885
Ville Syrjälä10d83732013-01-29 18:13:34 +02002886 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2887 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002888 return false;
2889
2890 spin_lock_irqsave(&dev->event_lock, flags);
2891 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2892 spin_unlock_irqrestore(&dev->event_lock, flags);
2893
2894 return pending;
2895}
2896
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002897static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2898{
Chris Wilson0f911282012-04-17 10:05:38 +01002899 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002900 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002901
2902 if (crtc->fb == NULL)
2903 return;
2904
Daniel Vetter2c10d572012-12-20 21:24:07 +01002905 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2906
Chris Wilson5bb61642012-09-27 21:25:58 +01002907 wait_event(dev_priv->pending_flip_queue,
2908 !intel_crtc_has_pending_flip(crtc));
2909
Chris Wilson0f911282012-04-17 10:05:38 +01002910 mutex_lock(&dev->struct_mutex);
2911 intel_finish_fb(crtc->fb);
2912 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002913}
2914
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002915static bool ironlake_crtc_driving_pch(struct drm_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08002916{
2917 struct drm_device *dev = crtc->dev;
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002918 struct intel_encoder *intel_encoder;
Jesse Barnes040484a2011-01-03 12:14:26 -08002919
2920 /*
2921 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2922 * must be driven by its own crtc; no sharing is possible.
2923 */
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002924 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002925 switch (intel_encoder->type) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002926 case INTEL_OUTPUT_EDP:
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002927 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnes040484a2011-01-03 12:14:26 -08002928 return false;
2929 continue;
2930 }
2931 }
2932
2933 return true;
2934}
2935
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002936static bool haswell_crtc_driving_pch(struct drm_crtc *crtc)
2937{
2938 return intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG);
2939}
2940
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002941/* Program iCLKIP clock to the desired frequency */
2942static void lpt_program_iclkip(struct drm_crtc *crtc)
2943{
2944 struct drm_device *dev = crtc->dev;
2945 struct drm_i915_private *dev_priv = dev->dev_private;
2946 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2947 u32 temp;
2948
Daniel Vetter09153002012-12-12 14:06:44 +01002949 mutex_lock(&dev_priv->dpio_lock);
2950
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002951 /* It is necessary to ungate the pixclk gate prior to programming
2952 * the divisors, and gate it back when it is done.
2953 */
2954 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2955
2956 /* Disable SSCCTL */
2957 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002958 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2959 SBI_SSCCTL_DISABLE,
2960 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002961
2962 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2963 if (crtc->mode.clock == 20000) {
2964 auxdiv = 1;
2965 divsel = 0x41;
2966 phaseinc = 0x20;
2967 } else {
2968 /* The iCLK virtual clock root frequency is in MHz,
2969 * but the crtc->mode.clock in in KHz. To get the divisors,
2970 * it is necessary to divide one by another, so we
2971 * convert the virtual clock precision to KHz here for higher
2972 * precision.
2973 */
2974 u32 iclk_virtual_root_freq = 172800 * 1000;
2975 u32 iclk_pi_range = 64;
2976 u32 desired_divisor, msb_divisor_value, pi_value;
2977
2978 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2979 msb_divisor_value = desired_divisor / iclk_pi_range;
2980 pi_value = desired_divisor % iclk_pi_range;
2981
2982 auxdiv = 0;
2983 divsel = msb_divisor_value - 2;
2984 phaseinc = pi_value;
2985 }
2986
2987 /* This should not happen with any sane values */
2988 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2989 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2990 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2991 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2992
2993 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2994 crtc->mode.clock,
2995 auxdiv,
2996 divsel,
2997 phasedir,
2998 phaseinc);
2999
3000 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003001 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003002 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3003 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3004 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3005 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3006 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3007 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003008 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003009
3010 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003011 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003012 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3013 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003014 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003015
3016 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003017 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003018 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003019 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003020
3021 /* Wait for initialization time */
3022 udelay(24);
3023
3024 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003025
3026 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003027}
3028
Jesse Barnesf67a5592011-01-05 10:31:48 -08003029/*
3030 * Enable PCH resources required for PCH ports:
3031 * - PCH PLLs
3032 * - FDI training & RX/TX
3033 * - update transcoder timings
3034 * - DP transcoding bits
3035 * - transcoder
3036 */
3037static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003038{
3039 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003040 struct drm_i915_private *dev_priv = dev->dev_private;
3041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3042 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003043 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003044
Chris Wilsone7e164d2012-05-11 09:21:25 +01003045 assert_transcoder_disabled(dev_priv, pipe);
3046
Daniel Vettercd986ab2012-10-26 10:58:12 +02003047 /* Write the TU size bits before fdi link training, so that error
3048 * detection works. */
3049 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3050 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3051
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003052 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003053 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003054
Daniel Vetter572deb32012-10-27 18:46:14 +02003055 /* XXX: pch pll's can be enabled any time before we enable the PCH
3056 * transcoder, and we actually should do this to not upset any PCH
3057 * transcoder that already use the clock when we share it.
3058 *
3059 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
3060 * unconditionally resets the pll - we need that to have the right LVDS
3061 * enable sequence. */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02003062 ironlake_enable_pch_pll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01003063
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003064 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003065 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003066
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003067 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003068 switch (pipe) {
3069 default:
3070 case 0:
3071 temp |= TRANSA_DPLL_ENABLE;
3072 sel = TRANSA_DPLLB_SEL;
3073 break;
3074 case 1:
3075 temp |= TRANSB_DPLL_ENABLE;
3076 sel = TRANSB_DPLLB_SEL;
3077 break;
3078 case 2:
3079 temp |= TRANSC_DPLL_ENABLE;
3080 sel = TRANSC_DPLLB_SEL;
3081 break;
Jesse Barnesd64311a2011-10-12 15:01:33 -07003082 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003083 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3084 temp |= sel;
3085 else
3086 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003087 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003088 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003089
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003090 /* set transcoder timing, panel must allow it */
3091 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003092 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3093 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3094 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3095
3096 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3097 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3098 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003099 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003100
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003101 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003102
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003103 /* For PCH DP, enable TRANS_DP_CTL */
3104 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003105 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3106 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003107 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003108 reg = TRANS_DP_CTL(pipe);
3109 temp = I915_READ(reg);
3110 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003111 TRANS_DP_SYNC_MASK |
3112 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003113 temp |= (TRANS_DP_OUTPUT_ENABLE |
3114 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003115 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003116
3117 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003118 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003119 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003120 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003121
3122 switch (intel_trans_dp_port_sel(crtc)) {
3123 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003124 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003125 break;
3126 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003127 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003128 break;
3129 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003130 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003131 break;
3132 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003133 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003134 }
3135
Chris Wilson5eddb702010-09-11 13:48:45 +01003136 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003137 }
3138
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003139 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003140}
3141
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003142static void lpt_pch_enable(struct drm_crtc *crtc)
3143{
3144 struct drm_device *dev = crtc->dev;
3145 struct drm_i915_private *dev_priv = dev->dev_private;
3146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003147 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003148
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003149 assert_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003150
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003151 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003152
Paulo Zanoni0540e482012-10-31 18:12:40 -02003153 /* Set transcoder timing. */
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003154 I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
3155 I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
3156 I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003157
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003158 I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
3159 I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
3160 I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
3161 I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003162
Paulo Zanoni937bb612012-10-31 18:12:47 -02003163 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003164}
3165
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003166static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3167{
3168 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3169
3170 if (pll == NULL)
3171 return;
3172
3173 if (pll->refcount == 0) {
3174 WARN(1, "bad PCH PLL refcount\n");
3175 return;
3176 }
3177
3178 --pll->refcount;
3179 intel_crtc->pch_pll = NULL;
3180}
3181
3182static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3183{
3184 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3185 struct intel_pch_pll *pll;
3186 int i;
3187
3188 pll = intel_crtc->pch_pll;
3189 if (pll) {
3190 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3191 intel_crtc->base.base.id, pll->pll_reg);
3192 goto prepare;
3193 }
3194
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003195 if (HAS_PCH_IBX(dev_priv->dev)) {
3196 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3197 i = intel_crtc->pipe;
3198 pll = &dev_priv->pch_plls[i];
3199
3200 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3201 intel_crtc->base.base.id, pll->pll_reg);
3202
3203 goto found;
3204 }
3205
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003206 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3207 pll = &dev_priv->pch_plls[i];
3208
3209 /* Only want to check enabled timings first */
3210 if (pll->refcount == 0)
3211 continue;
3212
3213 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3214 fp == I915_READ(pll->fp0_reg)) {
3215 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3216 intel_crtc->base.base.id,
3217 pll->pll_reg, pll->refcount, pll->active);
3218
3219 goto found;
3220 }
3221 }
3222
3223 /* Ok no matching timings, maybe there's a free one? */
3224 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3225 pll = &dev_priv->pch_plls[i];
3226 if (pll->refcount == 0) {
3227 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3228 intel_crtc->base.base.id, pll->pll_reg);
3229 goto found;
3230 }
3231 }
3232
3233 return NULL;
3234
3235found:
3236 intel_crtc->pch_pll = pll;
3237 pll->refcount++;
3238 DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
3239prepare: /* separate function? */
3240 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003241
Chris Wilsone04c7352012-05-02 20:43:56 +01003242 /* Wait for the clocks to stabilize before rewriting the regs */
3243 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003244 POSTING_READ(pll->pll_reg);
3245 udelay(150);
Chris Wilsone04c7352012-05-02 20:43:56 +01003246
3247 I915_WRITE(pll->fp0_reg, fp);
3248 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003249 pll->on = false;
3250 return pll;
3251}
3252
Jesse Barnesd4270e52011-10-11 10:43:02 -07003253void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3254{
3255 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003256 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003257 u32 temp;
3258
3259 temp = I915_READ(dslreg);
3260 udelay(500);
3261 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003262 if (wait_for(I915_READ(dslreg) != temp, 5))
3263 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3264 }
3265}
3266
Jesse Barnesf67a5592011-01-05 10:31:48 -08003267static void ironlake_crtc_enable(struct drm_crtc *crtc)
3268{
3269 struct drm_device *dev = crtc->dev;
3270 struct drm_i915_private *dev_priv = dev->dev_private;
3271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003272 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003273 int pipe = intel_crtc->pipe;
3274 int plane = intel_crtc->plane;
3275 u32 temp;
3276 bool is_pch_port;
3277
Daniel Vetter08a48462012-07-02 11:43:47 +02003278 WARN_ON(!crtc->enabled);
3279
Jesse Barnesf67a5592011-01-05 10:31:48 -08003280 if (intel_crtc->active)
3281 return;
3282
3283 intel_crtc->active = true;
3284 intel_update_watermarks(dev);
3285
3286 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3287 temp = I915_READ(PCH_LVDS);
3288 if ((temp & LVDS_PORT_EN) == 0)
3289 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3290 }
3291
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003292 is_pch_port = ironlake_crtc_driving_pch(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003293
Daniel Vetter46b6f812012-09-06 22:08:33 +02003294 if (is_pch_port) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003295 /* Note: FDI PLL enabling _must_ be done before we enable the
3296 * cpu pipes, hence this is separate from all the other fdi/pch
3297 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003298 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003299 } else {
3300 assert_fdi_tx_disabled(dev_priv, pipe);
3301 assert_fdi_rx_disabled(dev_priv, pipe);
3302 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003303
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003304 for_each_encoder_on_crtc(dev, crtc, encoder)
3305 if (encoder->pre_enable)
3306 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003307
3308 /* Enable panel fitting for LVDS */
3309 if (dev_priv->pch_pf_size &&
Jani Nikula547dc042012-11-02 11:24:03 +02003310 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3311 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnesf67a5592011-01-05 10:31:48 -08003312 /* Force use of hard-coded filter coefficients
3313 * as some pre-programmed values are broken,
3314 * e.g. x201.
3315 */
Paulo Zanoni13888d72012-11-20 13:27:41 -02003316 if (IS_IVYBRIDGE(dev))
3317 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3318 PF_PIPE_SEL_IVB(pipe));
3319 else
3320 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003321 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3322 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003323 }
3324
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003325 /*
3326 * On ILK+ LUT must be loaded before the pipe is running but with
3327 * clocks enabled
3328 */
3329 intel_crtc_load_lut(crtc);
3330
Jesse Barnesf67a5592011-01-05 10:31:48 -08003331 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3332 intel_enable_plane(dev_priv, plane, pipe);
3333
3334 if (is_pch_port)
3335 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003336
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003337 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003338 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003339 mutex_unlock(&dev->struct_mutex);
3340
Chris Wilson6b383a72010-09-13 13:54:26 +01003341 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003342
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003343 for_each_encoder_on_crtc(dev, crtc, encoder)
3344 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003345
3346 if (HAS_PCH_CPT(dev))
3347 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003348
3349 /*
3350 * There seems to be a race in PCH platform hw (at least on some
3351 * outputs) where an enabled pipe still completes any pageflip right
3352 * away (as if the pipe is off) instead of waiting for vblank. As soon
3353 * as the first vblank happend, everything works as expected. Hence just
3354 * wait for one vblank before returning to avoid strange things
3355 * happening.
3356 */
3357 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003358}
3359
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003360static void haswell_crtc_enable(struct drm_crtc *crtc)
3361{
3362 struct drm_device *dev = crtc->dev;
3363 struct drm_i915_private *dev_priv = dev->dev_private;
3364 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3365 struct intel_encoder *encoder;
3366 int pipe = intel_crtc->pipe;
3367 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003368 bool is_pch_port;
3369
3370 WARN_ON(!crtc->enabled);
3371
3372 if (intel_crtc->active)
3373 return;
3374
3375 intel_crtc->active = true;
3376 intel_update_watermarks(dev);
3377
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003378 is_pch_port = haswell_crtc_driving_pch(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003379
Paulo Zanoni83616632012-10-23 18:29:54 -02003380 if (is_pch_port)
Paulo Zanoni04945642012-11-01 21:00:59 -02003381 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003382
3383 for_each_encoder_on_crtc(dev, crtc, encoder)
3384 if (encoder->pre_enable)
3385 encoder->pre_enable(encoder);
3386
Paulo Zanoni1f544382012-10-24 11:32:00 -02003387 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003388
Paulo Zanoni1f544382012-10-24 11:32:00 -02003389 /* Enable panel fitting for eDP */
Jani Nikula547dc042012-11-02 11:24:03 +02003390 if (dev_priv->pch_pf_size &&
3391 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003392 /* Force use of hard-coded filter coefficients
3393 * as some pre-programmed values are broken,
3394 * e.g. x201.
3395 */
Paulo Zanoni54075a72012-11-20 13:27:42 -02003396 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3397 PF_PIPE_SEL_IVB(pipe));
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003398 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3399 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3400 }
3401
3402 /*
3403 * On ILK+ LUT must be loaded before the pipe is running but with
3404 * clocks enabled
3405 */
3406 intel_crtc_load_lut(crtc);
3407
Paulo Zanoni1f544382012-10-24 11:32:00 -02003408 intel_ddi_set_pipe_settings(crtc);
3409 intel_ddi_enable_pipe_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003410
3411 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3412 intel_enable_plane(dev_priv, plane, pipe);
3413
3414 if (is_pch_port)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003415 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003416
3417 mutex_lock(&dev->struct_mutex);
3418 intel_update_fbc(dev);
3419 mutex_unlock(&dev->struct_mutex);
3420
3421 intel_crtc_update_cursor(crtc, true);
3422
3423 for_each_encoder_on_crtc(dev, crtc, encoder)
3424 encoder->enable(encoder);
3425
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003426 /*
3427 * There seems to be a race in PCH platform hw (at least on some
3428 * outputs) where an enabled pipe still completes any pageflip right
3429 * away (as if the pipe is off) instead of waiting for vblank. As soon
3430 * as the first vblank happend, everything works as expected. Hence just
3431 * wait for one vblank before returning to avoid strange things
3432 * happening.
3433 */
3434 intel_wait_for_vblank(dev, intel_crtc->pipe);
3435}
3436
Jesse Barnes6be4a602010-09-10 10:26:01 -07003437static void ironlake_crtc_disable(struct drm_crtc *crtc)
3438{
3439 struct drm_device *dev = crtc->dev;
3440 struct drm_i915_private *dev_priv = dev->dev_private;
3441 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003442 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003443 int pipe = intel_crtc->pipe;
3444 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003445 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003446
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003447
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003448 if (!intel_crtc->active)
3449 return;
3450
Daniel Vetterea9d7582012-07-10 10:42:52 +02003451 for_each_encoder_on_crtc(dev, crtc, encoder)
3452 encoder->disable(encoder);
3453
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003454 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003455 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003456 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003457
Jesse Barnesb24e7172011-01-04 15:09:30 -08003458 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003459
Chris Wilson973d04f2011-07-08 12:22:37 +01003460 if (dev_priv->cfb_plane == plane)
3461 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003462
Jesse Barnesb24e7172011-01-04 15:09:30 -08003463 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003464
Jesse Barnes6be4a602010-09-10 10:26:01 -07003465 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003466 I915_WRITE(PF_CTL(pipe), 0);
3467 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003468
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003469 for_each_encoder_on_crtc(dev, crtc, encoder)
3470 if (encoder->post_disable)
3471 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003472
Chris Wilson5eddb702010-09-11 13:48:45 +01003473 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003474
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003475 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003476
3477 if (HAS_PCH_CPT(dev)) {
3478 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003479 reg = TRANS_DP_CTL(pipe);
3480 temp = I915_READ(reg);
3481 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003482 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003483 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003484
3485 /* disable DPLL_SEL */
3486 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003487 switch (pipe) {
3488 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003489 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003490 break;
3491 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003492 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003493 break;
3494 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003495 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003496 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003497 break;
3498 default:
3499 BUG(); /* wtf */
3500 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003501 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003502 }
3503
3504 /* disable PCH DPLL */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003505 intel_disable_pch_pll(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003506
Daniel Vetter88cefb62012-08-12 19:27:14 +02003507 ironlake_fdi_pll_disable(intel_crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +01003508
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003509 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003510 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003511
3512 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003513 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003514 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003515}
3516
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003517static void haswell_crtc_disable(struct drm_crtc *crtc)
3518{
3519 struct drm_device *dev = crtc->dev;
3520 struct drm_i915_private *dev_priv = dev->dev_private;
3521 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3522 struct intel_encoder *encoder;
3523 int pipe = intel_crtc->pipe;
3524 int plane = intel_crtc->plane;
Paulo Zanoniad80a812012-10-24 16:06:19 -02003525 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni83616632012-10-23 18:29:54 -02003526 bool is_pch_port;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003527
3528 if (!intel_crtc->active)
3529 return;
3530
Paulo Zanoni83616632012-10-23 18:29:54 -02003531 is_pch_port = haswell_crtc_driving_pch(crtc);
3532
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003533 for_each_encoder_on_crtc(dev, crtc, encoder)
3534 encoder->disable(encoder);
3535
3536 intel_crtc_wait_for_pending_flips(crtc);
3537 drm_vblank_off(dev, pipe);
3538 intel_crtc_update_cursor(crtc, false);
3539
3540 intel_disable_plane(dev_priv, plane, pipe);
3541
3542 if (dev_priv->cfb_plane == plane)
3543 intel_disable_fbc(dev);
3544
3545 intel_disable_pipe(dev_priv, pipe);
3546
Paulo Zanoniad80a812012-10-24 16:06:19 -02003547 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003548
3549 /* Disable PF */
3550 I915_WRITE(PF_CTL(pipe), 0);
3551 I915_WRITE(PF_WIN_SZ(pipe), 0);
3552
Paulo Zanoni1f544382012-10-24 11:32:00 -02003553 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003554
3555 for_each_encoder_on_crtc(dev, crtc, encoder)
3556 if (encoder->post_disable)
3557 encoder->post_disable(encoder);
3558
Paulo Zanoni83616632012-10-23 18:29:54 -02003559 if (is_pch_port) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003560 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003561 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003562 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003563
3564 intel_crtc->active = false;
3565 intel_update_watermarks(dev);
3566
3567 mutex_lock(&dev->struct_mutex);
3568 intel_update_fbc(dev);
3569 mutex_unlock(&dev->struct_mutex);
3570}
3571
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003572static void ironlake_crtc_off(struct drm_crtc *crtc)
3573{
3574 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3575 intel_put_pch_pll(intel_crtc);
3576}
3577
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003578static void haswell_crtc_off(struct drm_crtc *crtc)
3579{
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003580 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3581
3582 /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
3583 * start using it. */
Daniel Vetter1a240d42012-11-29 22:18:51 +01003584 intel_crtc->cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003585
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003586 intel_ddi_put_crtc_pll(crtc);
3587}
3588
Daniel Vetter02e792f2009-09-15 22:57:34 +02003589static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3590{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003591 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003592 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003593 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003594
Chris Wilson23f09ce2010-08-12 13:53:37 +01003595 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003596 dev_priv->mm.interruptible = false;
3597 (void) intel_overlay_switch_off(intel_crtc->overlay);
3598 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003599 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003600 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003601
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003602 /* Let userspace switch the overlay on again. In most cases userspace
3603 * has to recompute where to put it anyway.
3604 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003605}
3606
Egbert Eich61bc95c2013-03-04 09:24:38 -05003607/**
3608 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3609 * cursor plane briefly if not already running after enabling the display
3610 * plane.
3611 * This workaround avoids occasional blank screens when self refresh is
3612 * enabled.
3613 */
3614static void
3615g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3616{
3617 u32 cntl = I915_READ(CURCNTR(pipe));
3618
3619 if ((cntl & CURSOR_MODE) == 0) {
3620 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3621
3622 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3623 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3624 intel_wait_for_vblank(dev_priv->dev, pipe);
3625 I915_WRITE(CURCNTR(pipe), cntl);
3626 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3627 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3628 }
3629}
3630
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003631static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003632{
3633 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003634 struct drm_i915_private *dev_priv = dev->dev_private;
3635 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003636 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003637 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003638 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003639
Daniel Vetter08a48462012-07-02 11:43:47 +02003640 WARN_ON(!crtc->enabled);
3641
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003642 if (intel_crtc->active)
3643 return;
3644
3645 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003646 intel_update_watermarks(dev);
3647
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003648 intel_enable_pll(dev_priv, pipe);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003649
3650 for_each_encoder_on_crtc(dev, crtc, encoder)
3651 if (encoder->pre_enable)
3652 encoder->pre_enable(encoder);
3653
Jesse Barnes040484a2011-01-03 12:14:26 -08003654 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003655 intel_enable_plane(dev_priv, plane, pipe);
Egbert Eich61bc95c2013-03-04 09:24:38 -05003656 if (IS_G4X(dev))
3657 g4x_fixup_plane(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003658
3659 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003660 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003661
3662 /* Give the overlay scaler a chance to enable if it's on this pipe */
3663 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003664 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003665
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003666 for_each_encoder_on_crtc(dev, crtc, encoder)
3667 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003668}
3669
3670static void i9xx_crtc_disable(struct drm_crtc *crtc)
3671{
3672 struct drm_device *dev = crtc->dev;
3673 struct drm_i915_private *dev_priv = dev->dev_private;
3674 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003675 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003676 int pipe = intel_crtc->pipe;
3677 int plane = intel_crtc->plane;
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003678 u32 pctl;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003679
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003680
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003681 if (!intel_crtc->active)
3682 return;
3683
Daniel Vetterea9d7582012-07-10 10:42:52 +02003684 for_each_encoder_on_crtc(dev, crtc, encoder)
3685 encoder->disable(encoder);
3686
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003687 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003688 intel_crtc_wait_for_pending_flips(crtc);
3689 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003690 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003691 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003692
Chris Wilson973d04f2011-07-08 12:22:37 +01003693 if (dev_priv->cfb_plane == plane)
3694 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003695
Jesse Barnesb24e7172011-01-04 15:09:30 -08003696 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003697 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003698
3699 /* Disable pannel fitter if it is on this pipe. */
3700 pctl = I915_READ(PFIT_CONTROL);
3701 if ((pctl & PFIT_ENABLE) &&
3702 ((pctl & PFIT_PIPE_MASK) >> PFIT_PIPE_SHIFT) == pipe)
3703 I915_WRITE(PFIT_CONTROL, 0);
3704
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003705 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003706
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003707 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003708 intel_update_fbc(dev);
3709 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003710}
3711
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003712static void i9xx_crtc_off(struct drm_crtc *crtc)
3713{
3714}
3715
Daniel Vetter976f8a22012-07-08 22:34:21 +02003716static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3717 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003718{
3719 struct drm_device *dev = crtc->dev;
3720 struct drm_i915_master_private *master_priv;
3721 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3722 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003723
3724 if (!dev->primary->master)
3725 return;
3726
3727 master_priv = dev->primary->master->driver_priv;
3728 if (!master_priv->sarea_priv)
3729 return;
3730
Jesse Barnes79e53942008-11-07 14:24:08 -08003731 switch (pipe) {
3732 case 0:
3733 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3734 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3735 break;
3736 case 1:
3737 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3738 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3739 break;
3740 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003741 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003742 break;
3743 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003744}
3745
Daniel Vetter976f8a22012-07-08 22:34:21 +02003746/**
3747 * Sets the power management mode of the pipe and plane.
3748 */
3749void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003750{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003751 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003752 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003753 struct intel_encoder *intel_encoder;
3754 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003755
Daniel Vetter976f8a22012-07-08 22:34:21 +02003756 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3757 enable |= intel_encoder->connectors_active;
3758
3759 if (enable)
3760 dev_priv->display.crtc_enable(crtc);
3761 else
3762 dev_priv->display.crtc_disable(crtc);
3763
3764 intel_crtc_update_sarea(crtc, enable);
3765}
3766
3767static void intel_crtc_noop(struct drm_crtc *crtc)
3768{
3769}
3770
3771static void intel_crtc_disable(struct drm_crtc *crtc)
3772{
3773 struct drm_device *dev = crtc->dev;
3774 struct drm_connector *connector;
3775 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003777
3778 /* crtc should still be enabled when we disable it. */
3779 WARN_ON(!crtc->enabled);
3780
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003781 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003782 dev_priv->display.crtc_disable(crtc);
3783 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003784 dev_priv->display.off(crtc);
3785
Chris Wilson931872f2012-01-16 23:01:13 +00003786 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3787 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003788
3789 if (crtc->fb) {
3790 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003791 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003792 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003793 crtc->fb = NULL;
3794 }
3795
3796 /* Update computed state. */
3797 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3798 if (!connector->encoder || !connector->encoder->crtc)
3799 continue;
3800
3801 if (connector->encoder->crtc != crtc)
3802 continue;
3803
3804 connector->dpms = DRM_MODE_DPMS_OFF;
3805 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003806 }
3807}
3808
Daniel Vettera261b242012-07-26 19:21:47 +02003809void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003810{
Daniel Vettera261b242012-07-26 19:21:47 +02003811 struct drm_crtc *crtc;
3812
3813 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3814 if (crtc->enabled)
3815 intel_crtc_disable(crtc);
3816 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003817}
3818
Daniel Vetter1f703852012-07-11 16:51:39 +02003819void intel_encoder_noop(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003820{
Jesse Barnes79e53942008-11-07 14:24:08 -08003821}
3822
Chris Wilsonea5b2132010-08-04 13:50:23 +01003823void intel_encoder_destroy(struct drm_encoder *encoder)
3824{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003825 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003826
Chris Wilsonea5b2132010-08-04 13:50:23 +01003827 drm_encoder_cleanup(encoder);
3828 kfree(intel_encoder);
3829}
3830
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003831/* Simple dpms helper for encodres with just one connector, no cloning and only
3832 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3833 * state of the entire output pipe. */
3834void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3835{
3836 if (mode == DRM_MODE_DPMS_ON) {
3837 encoder->connectors_active = true;
3838
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003839 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003840 } else {
3841 encoder->connectors_active = false;
3842
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003843 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003844 }
3845}
3846
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003847/* Cross check the actual hw state with our own modeset state tracking (and it's
3848 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003849static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003850{
3851 if (connector->get_hw_state(connector)) {
3852 struct intel_encoder *encoder = connector->encoder;
3853 struct drm_crtc *crtc;
3854 bool encoder_enabled;
3855 enum pipe pipe;
3856
3857 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3858 connector->base.base.id,
3859 drm_get_connector_name(&connector->base));
3860
3861 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3862 "wrong connector dpms state\n");
3863 WARN(connector->base.encoder != &encoder->base,
3864 "active connector not linked to encoder\n");
3865 WARN(!encoder->connectors_active,
3866 "encoder->connectors_active not set\n");
3867
3868 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3869 WARN(!encoder_enabled, "encoder not enabled\n");
3870 if (WARN_ON(!encoder->base.crtc))
3871 return;
3872
3873 crtc = encoder->base.crtc;
3874
3875 WARN(!crtc->enabled, "crtc not enabled\n");
3876 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3877 WARN(pipe != to_intel_crtc(crtc)->pipe,
3878 "encoder active on the wrong pipe\n");
3879 }
3880}
3881
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003882/* Even simpler default implementation, if there's really no special case to
3883 * consider. */
3884void intel_connector_dpms(struct drm_connector *connector, int mode)
3885{
3886 struct intel_encoder *encoder = intel_attached_encoder(connector);
3887
3888 /* All the simple cases only support two dpms states. */
3889 if (mode != DRM_MODE_DPMS_ON)
3890 mode = DRM_MODE_DPMS_OFF;
3891
3892 if (mode == connector->dpms)
3893 return;
3894
3895 connector->dpms = mode;
3896
3897 /* Only need to change hw state when actually enabled */
3898 if (encoder->base.crtc)
3899 intel_encoder_dpms(encoder, mode);
3900 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003901 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003902
Daniel Vetterb9805142012-08-31 17:37:33 +02003903 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003904}
3905
Daniel Vetterf0947c32012-07-02 13:10:34 +02003906/* Simple connector->get_hw_state implementation for encoders that support only
3907 * one connector and no cloning and hence the encoder state determines the state
3908 * of the connector. */
3909bool intel_connector_get_hw_state(struct intel_connector *connector)
3910{
Daniel Vetter24929352012-07-02 20:28:59 +02003911 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003912 struct intel_encoder *encoder = connector->encoder;
3913
3914 return encoder->get_hw_state(encoder, &pipe);
3915}
3916
Jesse Barnes79e53942008-11-07 14:24:08 -08003917static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
Daniel Vetter35313cd2012-07-20 10:30:45 +02003918 const struct drm_display_mode *mode,
Jesse Barnes79e53942008-11-07 14:24:08 -08003919 struct drm_display_mode *adjusted_mode)
3920{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003921 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003922
Eric Anholtbad720f2009-10-22 16:11:14 -07003923 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003924 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003925 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3926 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003927 }
Chris Wilson89749352010-09-12 18:25:19 +01003928
Daniel Vetterf9bef082012-04-15 19:53:19 +02003929 /* All interlaced capable intel hw wants timings in frames. Note though
3930 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3931 * timings, so we need to be careful not to clobber these.*/
3932 if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3933 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003934
Chris Wilson44f46b422012-06-21 13:19:59 +03003935 /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
3936 * with a hsync front porch of 0.
3937 */
3938 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
3939 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
3940 return false;
3941
Jesse Barnes79e53942008-11-07 14:24:08 -08003942 return true;
3943}
3944
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07003945static int valleyview_get_display_clock_speed(struct drm_device *dev)
3946{
3947 return 400000; /* FIXME */
3948}
3949
Jesse Barnese70236a2009-09-21 10:42:27 -07003950static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003951{
Jesse Barnese70236a2009-09-21 10:42:27 -07003952 return 400000;
3953}
Jesse Barnes79e53942008-11-07 14:24:08 -08003954
Jesse Barnese70236a2009-09-21 10:42:27 -07003955static int i915_get_display_clock_speed(struct drm_device *dev)
3956{
3957 return 333000;
3958}
Jesse Barnes79e53942008-11-07 14:24:08 -08003959
Jesse Barnese70236a2009-09-21 10:42:27 -07003960static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3961{
3962 return 200000;
3963}
Jesse Barnes79e53942008-11-07 14:24:08 -08003964
Jesse Barnese70236a2009-09-21 10:42:27 -07003965static int i915gm_get_display_clock_speed(struct drm_device *dev)
3966{
3967 u16 gcfgc = 0;
3968
3969 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3970
3971 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003972 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003973 else {
3974 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3975 case GC_DISPLAY_CLOCK_333_MHZ:
3976 return 333000;
3977 default:
3978 case GC_DISPLAY_CLOCK_190_200_MHZ:
3979 return 190000;
3980 }
3981 }
3982}
Jesse Barnes79e53942008-11-07 14:24:08 -08003983
Jesse Barnese70236a2009-09-21 10:42:27 -07003984static int i865_get_display_clock_speed(struct drm_device *dev)
3985{
3986 return 266000;
3987}
3988
3989static int i855_get_display_clock_speed(struct drm_device *dev)
3990{
3991 u16 hpllcc = 0;
3992 /* Assume that the hardware is in the high speed state. This
3993 * should be the default.
3994 */
3995 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3996 case GC_CLOCK_133_200:
3997 case GC_CLOCK_100_200:
3998 return 200000;
3999 case GC_CLOCK_166_250:
4000 return 250000;
4001 case GC_CLOCK_100_133:
4002 return 133000;
4003 }
4004
4005 /* Shouldn't happen */
4006 return 0;
4007}
4008
4009static int i830_get_display_clock_speed(struct drm_device *dev)
4010{
4011 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004012}
4013
Zhenyu Wang2c072452009-06-05 15:38:42 +08004014static void
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004015intel_reduce_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004016{
4017 while (*num > 0xffffff || *den > 0xffffff) {
4018 *num >>= 1;
4019 *den >>= 1;
4020 }
4021}
4022
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004023void
4024intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4025 int pixel_clock, int link_clock,
4026 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004027{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004028 m_n->tu = 64;
Chris Wilson22ed1112010-12-04 01:01:29 +00004029 m_n->gmch_m = bits_per_pixel * pixel_clock;
4030 m_n->gmch_n = link_clock * nlanes * 8;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004031 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
Chris Wilson22ed1112010-12-04 01:01:29 +00004032 m_n->link_m = pixel_clock;
4033 m_n->link_n = link_clock;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004034 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004035}
4036
Chris Wilsona7615032011-01-12 17:04:08 +00004037static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4038{
Keith Packard72bbe582011-09-26 16:09:45 -07004039 if (i915_panel_use_ssc >= 0)
4040 return i915_panel_use_ssc != 0;
4041 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004042 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004043}
4044
Jesse Barnes5a354202011-06-24 12:19:22 -07004045/**
4046 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
4047 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004048 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07004049 *
4050 * A pipe may be connected to one or more outputs. Based on the depth of the
4051 * attached framebuffer, choose a good color depth to use on the pipe.
4052 *
4053 * If possible, match the pipe depth to the fb depth. In some cases, this
4054 * isn't ideal, because the connected output supports a lesser or restricted
4055 * set of depths. Resolve that here:
4056 * LVDS typically supports only 6bpc, so clamp down in that case
4057 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
4058 * Displays may support a restricted set as well, check EDID and clamp as
4059 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004060 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07004061 *
4062 * RETURNS:
4063 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
4064 * true if they don't match).
4065 */
4066static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004067 struct drm_framebuffer *fb,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004068 unsigned int *pipe_bpp,
4069 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07004070{
4071 struct drm_device *dev = crtc->dev;
4072 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes5a354202011-06-24 12:19:22 -07004073 struct drm_connector *connector;
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004074 struct intel_encoder *intel_encoder;
Jesse Barnes5a354202011-06-24 12:19:22 -07004075 unsigned int display_bpc = UINT_MAX, bpc;
4076
4077 /* Walk the encoders & connectors on this crtc, get min bpc */
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004078 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004079
4080 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
4081 unsigned int lvds_bpc;
4082
4083 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
4084 LVDS_A3_POWER_UP)
4085 lvds_bpc = 8;
4086 else
4087 lvds_bpc = 6;
4088
4089 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004090 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004091 display_bpc = lvds_bpc;
4092 }
4093 continue;
4094 }
4095
Jesse Barnes5a354202011-06-24 12:19:22 -07004096 /* Not one of the known troublemakers, check the EDID */
4097 list_for_each_entry(connector, &dev->mode_config.connector_list,
4098 head) {
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004099 if (connector->encoder != &intel_encoder->base)
Jesse Barnes5a354202011-06-24 12:19:22 -07004100 continue;
4101
Jesse Barnes62ac41a2011-07-28 12:55:14 -07004102 /* Don't use an invalid EDID bpc value */
4103 if (connector->display_info.bpc &&
4104 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004105 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 -07004106 display_bpc = connector->display_info.bpc;
4107 }
4108 }
4109
Jani Nikula2f4f6492012-11-12 14:33:44 +02004110 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
4111 /* Use VBT settings if we have an eDP panel */
4112 unsigned int edp_bpc = dev_priv->edp.bpp / 3;
4113
Jani Nikula9a30a612012-11-12 14:33:45 +02004114 if (edp_bpc && edp_bpc < display_bpc) {
Jani Nikula2f4f6492012-11-12 14:33:44 +02004115 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
4116 display_bpc = edp_bpc;
4117 }
4118 continue;
4119 }
4120
Jesse Barnes5a354202011-06-24 12:19:22 -07004121 /*
4122 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
4123 * through, clamp it down. (Note: >12bpc will be caught below.)
4124 */
4125 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
4126 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04004127 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004128 display_bpc = 12;
4129 } else {
Adam Jackson82820492011-10-10 16:33:34 -04004130 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004131 display_bpc = 8;
4132 }
4133 }
4134 }
4135
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004136 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4137 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
4138 display_bpc = 6;
4139 }
4140
Jesse Barnes5a354202011-06-24 12:19:22 -07004141 /*
4142 * We could just drive the pipe at the highest bpc all the time and
4143 * enable dithering as needed, but that costs bandwidth. So choose
4144 * the minimum value that expresses the full color range of the fb but
4145 * also stays within the max display bpc discovered above.
4146 */
4147
Daniel Vetter94352cf2012-07-05 22:51:56 +02004148 switch (fb->depth) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004149 case 8:
4150 bpc = 8; /* since we go through a colormap */
4151 break;
4152 case 15:
4153 case 16:
4154 bpc = 6; /* min is 18bpp */
4155 break;
4156 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07004157 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07004158 break;
4159 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07004160 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07004161 break;
4162 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07004163 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07004164 break;
4165 default:
4166 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
4167 bpc = min((unsigned int)8, display_bpc);
4168 break;
4169 }
4170
Keith Packard578393c2011-09-05 11:53:21 -07004171 display_bpc = min(display_bpc, bpc);
4172
Adam Jackson82820492011-10-10 16:33:34 -04004173 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
4174 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004175
Keith Packard578393c2011-09-05 11:53:21 -07004176 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07004177
4178 return display_bpc != bpc;
4179}
4180
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004181static int vlv_get_refclk(struct drm_crtc *crtc)
4182{
4183 struct drm_device *dev = crtc->dev;
4184 struct drm_i915_private *dev_priv = dev->dev_private;
4185 int refclk = 27000; /* for DP & HDMI */
4186
4187 return 100000; /* only one validated so far */
4188
4189 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4190 refclk = 96000;
4191 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4192 if (intel_panel_use_ssc(dev_priv))
4193 refclk = 100000;
4194 else
4195 refclk = 96000;
4196 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4197 refclk = 100000;
4198 }
4199
4200 return refclk;
4201}
4202
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004203static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4204{
4205 struct drm_device *dev = crtc->dev;
4206 struct drm_i915_private *dev_priv = dev->dev_private;
4207 int refclk;
4208
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004209 if (IS_VALLEYVIEW(dev)) {
4210 refclk = vlv_get_refclk(crtc);
4211 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004212 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4213 refclk = dev_priv->lvds_ssc_freq * 1000;
4214 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4215 refclk / 1000);
4216 } else if (!IS_GEN2(dev)) {
4217 refclk = 96000;
4218 } else {
4219 refclk = 48000;
4220 }
4221
4222 return refclk;
4223}
4224
4225static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
4226 intel_clock_t *clock)
4227{
4228 /* SDVO TV has fixed PLL values depend on its clock range,
4229 this mirrors vbios setting. */
4230 if (adjusted_mode->clock >= 100000
4231 && adjusted_mode->clock < 140500) {
4232 clock->p1 = 2;
4233 clock->p2 = 10;
4234 clock->n = 3;
4235 clock->m1 = 16;
4236 clock->m2 = 8;
4237 } else if (adjusted_mode->clock >= 140500
4238 && adjusted_mode->clock <= 200000) {
4239 clock->p1 = 1;
4240 clock->p2 = 10;
4241 clock->n = 6;
4242 clock->m1 = 12;
4243 clock->m2 = 8;
4244 }
4245}
4246
Jesse Barnesa7516a02011-12-15 12:30:37 -08004247static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
4248 intel_clock_t *clock,
4249 intel_clock_t *reduced_clock)
4250{
4251 struct drm_device *dev = crtc->dev;
4252 struct drm_i915_private *dev_priv = dev->dev_private;
4253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4254 int pipe = intel_crtc->pipe;
4255 u32 fp, fp2 = 0;
4256
4257 if (IS_PINEVIEW(dev)) {
4258 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
4259 if (reduced_clock)
4260 fp2 = (1 << reduced_clock->n) << 16 |
4261 reduced_clock->m1 << 8 | reduced_clock->m2;
4262 } else {
4263 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4264 if (reduced_clock)
4265 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4266 reduced_clock->m2;
4267 }
4268
4269 I915_WRITE(FP0(pipe), fp);
4270
4271 intel_crtc->lowfreq_avail = false;
4272 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4273 reduced_clock && i915_powersave) {
4274 I915_WRITE(FP1(pipe), fp2);
4275 intel_crtc->lowfreq_avail = true;
4276 } else {
4277 I915_WRITE(FP1(pipe), fp);
4278 }
4279}
4280
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004281static void vlv_update_pll(struct drm_crtc *crtc,
4282 struct drm_display_mode *mode,
4283 struct drm_display_mode *adjusted_mode,
4284 intel_clock_t *clock, intel_clock_t *reduced_clock,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304285 int num_connectors)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004286{
4287 struct drm_device *dev = crtc->dev;
4288 struct drm_i915_private *dev_priv = dev->dev_private;
4289 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4290 int pipe = intel_crtc->pipe;
4291 u32 dpll, mdiv, pdiv;
4292 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304293 bool is_sdvo;
4294 u32 temp;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004295
Daniel Vetter09153002012-12-12 14:06:44 +01004296 mutex_lock(&dev_priv->dpio_lock);
4297
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304298 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4299 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4300
4301 dpll = DPLL_VGA_MODE_DIS;
4302 dpll |= DPLL_EXT_BUFFER_ENABLE_VLV;
4303 dpll |= DPLL_REFA_CLK_ENABLE_VLV;
4304 dpll |= DPLL_INTEGRATED_CLOCK_VLV;
4305
4306 I915_WRITE(DPLL(pipe), dpll);
4307 POSTING_READ(DPLL(pipe));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004308
4309 bestn = clock->n;
4310 bestm1 = clock->m1;
4311 bestm2 = clock->m2;
4312 bestp1 = clock->p1;
4313 bestp2 = clock->p2;
4314
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304315 /*
4316 * In Valleyview PLL and program lane counter registers are exposed
4317 * through DPIO interface
4318 */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004319 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4320 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4321 mdiv |= ((bestn << DPIO_N_SHIFT));
4322 mdiv |= (1 << DPIO_POST_DIV_SHIFT);
4323 mdiv |= (1 << DPIO_K_SHIFT);
4324 mdiv |= DPIO_ENABLE_CALIBRATION;
4325 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4326
4327 intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
4328
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304329 pdiv = (1 << DPIO_REFSEL_OVERRIDE) | (5 << DPIO_PLL_MODESEL_SHIFT) |
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004330 (3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304331 (7 << DPIO_PLL_REFCLK_SEL_SHIFT) | (8 << DPIO_DRIVER_CTL_SHIFT) |
4332 (5 << DPIO_CLK_BIAS_CTL_SHIFT);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004333 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
4334
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304335 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x005f003b);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004336
4337 dpll |= DPLL_VCO_ENABLE;
4338 I915_WRITE(DPLL(pipe), dpll);
4339 POSTING_READ(DPLL(pipe));
4340 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4341 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4342
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304343 intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x620);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004344
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304345 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4346 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4347
4348 I915_WRITE(DPLL(pipe), dpll);
4349
4350 /* Wait for the clocks to stabilize. */
4351 POSTING_READ(DPLL(pipe));
4352 udelay(150);
4353
4354 temp = 0;
4355 if (is_sdvo) {
4356 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004357 if (temp > 1)
4358 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4359 else
4360 temp = 0;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004361 }
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304362 I915_WRITE(DPLL_MD(pipe), temp);
4363 POSTING_READ(DPLL_MD(pipe));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004364
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304365 /* Now program lane control registers */
4366 if(intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)
4367 || intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
4368 {
4369 temp = 0x1000C4;
4370 if(pipe == 1)
4371 temp |= (1 << 21);
4372 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL1, temp);
4373 }
4374 if(intel_pipe_has_type(crtc,INTEL_OUTPUT_EDP))
4375 {
4376 temp = 0x1000C4;
4377 if(pipe == 1)
4378 temp |= (1 << 21);
4379 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL2, temp);
4380 }
Daniel Vetter09153002012-12-12 14:06:44 +01004381
4382 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004383}
4384
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004385static void i9xx_update_pll(struct drm_crtc *crtc,
4386 struct drm_display_mode *mode,
4387 struct drm_display_mode *adjusted_mode,
4388 intel_clock_t *clock, intel_clock_t *reduced_clock,
4389 int num_connectors)
4390{
4391 struct drm_device *dev = crtc->dev;
4392 struct drm_i915_private *dev_priv = dev->dev_private;
4393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004394 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004395 int pipe = intel_crtc->pipe;
4396 u32 dpll;
4397 bool is_sdvo;
4398
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304399 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4400
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004401 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4402 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4403
4404 dpll = DPLL_VGA_MODE_DIS;
4405
4406 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4407 dpll |= DPLLB_MODE_LVDS;
4408 else
4409 dpll |= DPLLB_MODE_DAC_SERIAL;
4410 if (is_sdvo) {
4411 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4412 if (pixel_multiplier > 1) {
4413 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4414 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4415 }
4416 dpll |= DPLL_DVO_HIGH_SPEED;
4417 }
4418 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4419 dpll |= DPLL_DVO_HIGH_SPEED;
4420
4421 /* compute bitmask from p1 value */
4422 if (IS_PINEVIEW(dev))
4423 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4424 else {
4425 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4426 if (IS_G4X(dev) && reduced_clock)
4427 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4428 }
4429 switch (clock->p2) {
4430 case 5:
4431 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4432 break;
4433 case 7:
4434 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4435 break;
4436 case 10:
4437 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4438 break;
4439 case 14:
4440 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4441 break;
4442 }
4443 if (INTEL_INFO(dev)->gen >= 4)
4444 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4445
4446 if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4447 dpll |= PLL_REF_INPUT_TVCLKINBC;
4448 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4449 /* XXX: just matching BIOS for now */
4450 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4451 dpll |= 3;
4452 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4453 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4454 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4455 else
4456 dpll |= PLL_REF_INPUT_DREFCLK;
4457
4458 dpll |= DPLL_VCO_ENABLE;
4459 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4460 POSTING_READ(DPLL(pipe));
4461 udelay(150);
4462
Daniel Vetterdafd2262012-11-26 17:22:07 +01004463 for_each_encoder_on_crtc(dev, crtc, encoder)
4464 if (encoder->pre_pll_enable)
4465 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004466
4467 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4468 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4469
4470 I915_WRITE(DPLL(pipe), dpll);
4471
4472 /* Wait for the clocks to stabilize. */
4473 POSTING_READ(DPLL(pipe));
4474 udelay(150);
4475
4476 if (INTEL_INFO(dev)->gen >= 4) {
4477 u32 temp = 0;
4478 if (is_sdvo) {
4479 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4480 if (temp > 1)
4481 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4482 else
4483 temp = 0;
4484 }
4485 I915_WRITE(DPLL_MD(pipe), temp);
4486 } else {
4487 /* The pixel multiplier can only be updated once the
4488 * DPLL is enabled and the clocks are stable.
4489 *
4490 * So write it again.
4491 */
4492 I915_WRITE(DPLL(pipe), dpll);
4493 }
4494}
4495
4496static void i8xx_update_pll(struct drm_crtc *crtc,
4497 struct drm_display_mode *adjusted_mode,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304498 intel_clock_t *clock, intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004499 int num_connectors)
4500{
4501 struct drm_device *dev = crtc->dev;
4502 struct drm_i915_private *dev_priv = dev->dev_private;
4503 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004504 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004505 int pipe = intel_crtc->pipe;
4506 u32 dpll;
4507
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304508 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4509
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004510 dpll = DPLL_VGA_MODE_DIS;
4511
4512 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4513 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4514 } else {
4515 if (clock->p1 == 2)
4516 dpll |= PLL_P1_DIVIDE_BY_TWO;
4517 else
4518 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4519 if (clock->p2 == 4)
4520 dpll |= PLL_P2_DIVIDE_BY_4;
4521 }
4522
4523 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4524 /* XXX: just matching BIOS for now */
4525 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4526 dpll |= 3;
4527 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4528 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4529 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4530 else
4531 dpll |= PLL_REF_INPUT_DREFCLK;
4532
4533 dpll |= DPLL_VCO_ENABLE;
4534 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4535 POSTING_READ(DPLL(pipe));
4536 udelay(150);
4537
Daniel Vetterdafd2262012-11-26 17:22:07 +01004538 for_each_encoder_on_crtc(dev, crtc, encoder)
4539 if (encoder->pre_pll_enable)
4540 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004541
Daniel Vetter5b5896e2012-09-11 12:37:55 +02004542 I915_WRITE(DPLL(pipe), dpll);
4543
4544 /* Wait for the clocks to stabilize. */
4545 POSTING_READ(DPLL(pipe));
4546 udelay(150);
4547
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004548 /* The pixel multiplier can only be updated once the
4549 * DPLL is enabled and the clocks are stable.
4550 *
4551 * So write it again.
4552 */
4553 I915_WRITE(DPLL(pipe), dpll);
4554}
4555
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004556static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
4557 struct drm_display_mode *mode,
4558 struct drm_display_mode *adjusted_mode)
4559{
4560 struct drm_device *dev = intel_crtc->base.dev;
4561 struct drm_i915_private *dev_priv = dev->dev_private;
4562 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004563 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004564 uint32_t vsyncshift;
4565
4566 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4567 /* the chip adds 2 halflines automatically */
4568 adjusted_mode->crtc_vtotal -= 1;
4569 adjusted_mode->crtc_vblank_end -= 1;
4570 vsyncshift = adjusted_mode->crtc_hsync_start
4571 - adjusted_mode->crtc_htotal / 2;
4572 } else {
4573 vsyncshift = 0;
4574 }
4575
4576 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004577 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004578
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004579 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004580 (adjusted_mode->crtc_hdisplay - 1) |
4581 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004582 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004583 (adjusted_mode->crtc_hblank_start - 1) |
4584 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004585 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004586 (adjusted_mode->crtc_hsync_start - 1) |
4587 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4588
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004589 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004590 (adjusted_mode->crtc_vdisplay - 1) |
4591 ((adjusted_mode->crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004592 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004593 (adjusted_mode->crtc_vblank_start - 1) |
4594 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004595 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004596 (adjusted_mode->crtc_vsync_start - 1) |
4597 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4598
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004599 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4600 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4601 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4602 * bits. */
4603 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4604 (pipe == PIPE_B || pipe == PIPE_C))
4605 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4606
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004607 /* pipesrc controls the size that is scaled from, which should
4608 * always be the user's requested size.
4609 */
4610 I915_WRITE(PIPESRC(pipe),
4611 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4612}
4613
Eric Anholtf564048e2011-03-30 13:01:02 -07004614static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4615 struct drm_display_mode *mode,
4616 struct drm_display_mode *adjusted_mode,
4617 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004618 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004619{
4620 struct drm_device *dev = crtc->dev;
4621 struct drm_i915_private *dev_priv = dev->dev_private;
4622 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4623 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004624 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004625 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004626 intel_clock_t clock, reduced_clock;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004627 u32 dspcntr, pipeconf;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004628 bool ok, has_reduced_clock = false, is_sdvo = false;
4629 bool is_lvds = false, is_tv = false, is_dp = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004630 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004631 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004632 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004633
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004634 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004635 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004636 case INTEL_OUTPUT_LVDS:
4637 is_lvds = true;
4638 break;
4639 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08004640 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08004641 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01004642 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004643 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004644 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004645 case INTEL_OUTPUT_TVOUT:
4646 is_tv = true;
4647 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004648 case INTEL_OUTPUT_DISPLAYPORT:
4649 is_dp = true;
4650 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004651 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004652
Eric Anholtc751ce42010-03-25 11:48:48 -07004653 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004654 }
4655
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004656 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004657
Ma Lingd4906092009-03-18 20:13:27 +08004658 /*
4659 * Returns a set of divisors for the desired target clock with the given
4660 * refclk, or FALSE. The returned values represent the clock equation:
4661 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4662 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004663 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08004664 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4665 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004666 if (!ok) {
4667 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004668 return -EINVAL;
4669 }
4670
4671 /* Ensure that the cursor is valid for the new mode before changing... */
4672 intel_crtc_update_cursor(crtc, true);
4673
4674 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004675 /*
4676 * Ensure we match the reduced clock's P to the target clock.
4677 * If the clocks don't match, we can't switch the display clock
4678 * by using the FP0/FP1. In such case we will disable the LVDS
4679 * downclock feature.
4680 */
Eric Anholtf564048e2011-03-30 13:01:02 -07004681 has_reduced_clock = limit->find_pll(limit, crtc,
4682 dev_priv->lvds_downclock,
4683 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08004684 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004685 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004686 }
4687
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004688 if (is_sdvo && is_tv)
4689 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004690
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004691 if (IS_GEN2(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304692 i8xx_update_pll(crtc, adjusted_mode, &clock,
4693 has_reduced_clock ? &reduced_clock : NULL,
4694 num_connectors);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004695 else if (IS_VALLEYVIEW(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304696 vlv_update_pll(crtc, mode, adjusted_mode, &clock,
4697 has_reduced_clock ? &reduced_clock : NULL,
4698 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004699 else
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004700 i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
4701 has_reduced_clock ? &reduced_clock : NULL,
4702 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004703
4704 /* setup pipeconf */
4705 pipeconf = I915_READ(PIPECONF(pipe));
4706
4707 /* Set up the display plane register */
4708 dspcntr = DISPPLANE_GAMMA_ENABLE;
4709
Eric Anholt929c77f2011-03-30 13:01:04 -07004710 if (pipe == 0)
4711 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4712 else
4713 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07004714
4715 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4716 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4717 * core speed.
4718 *
4719 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4720 * pipe == 0 check?
4721 */
4722 if (mode->clock >
4723 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4724 pipeconf |= PIPECONF_DOUBLE_WIDE;
4725 else
4726 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4727 }
4728
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004729 /* default to 8bpc */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004730 pipeconf &= ~(PIPECONF_BPC_MASK | PIPECONF_DITHER_EN);
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004731 if (is_dp) {
Jani Nikula0c96c652012-09-26 18:43:10 +03004732 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004733 pipeconf |= PIPECONF_6BPC |
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004734 PIPECONF_DITHER_EN |
4735 PIPECONF_DITHER_TYPE_SP;
4736 }
4737 }
4738
Gajanan Bhat19c03922012-09-27 19:13:07 +05304739 if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4740 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004741 pipeconf |= PIPECONF_6BPC |
Gajanan Bhat19c03922012-09-27 19:13:07 +05304742 PIPECONF_ENABLE |
4743 I965_PIPECONF_ACTIVE;
4744 }
4745 }
4746
Eric Anholtf564048e2011-03-30 13:01:02 -07004747 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
4748 drm_mode_debug_printmodeline(mode);
4749
Jesse Barnesa7516a02011-12-15 12:30:37 -08004750 if (HAS_PIPE_CXSR(dev)) {
4751 if (intel_crtc->lowfreq_avail) {
Eric Anholtf564048e2011-03-30 13:01:02 -07004752 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4753 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004754 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07004755 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4756 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4757 }
4758 }
4759
Keith Packard617cf882012-02-08 13:53:38 -08004760 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetterdbb02572012-01-28 14:49:23 +01004761 if (!IS_GEN2(dev) &&
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004762 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Eric Anholtf564048e2011-03-30 13:01:02 -07004763 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004764 else
Keith Packard617cf882012-02-08 13:53:38 -08004765 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01004766
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004767 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Eric Anholtf564048e2011-03-30 13:01:02 -07004768
4769 /* pipesrc and dspsize control the size that is scaled from,
4770 * which should always be the user's requested size.
4771 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004772 I915_WRITE(DSPSIZE(plane),
4773 ((mode->vdisplay - 1) << 16) |
4774 (mode->hdisplay - 1));
4775 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004776
Eric Anholtf564048e2011-03-30 13:01:02 -07004777 I915_WRITE(PIPECONF(pipe), pipeconf);
4778 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07004779 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07004780
4781 intel_wait_for_vblank(dev, pipe);
4782
Eric Anholtf564048e2011-03-30 13:01:02 -07004783 I915_WRITE(DSPCNTR(plane), dspcntr);
4784 POSTING_READ(DSPCNTR(plane));
4785
Daniel Vetter94352cf2012-07-05 22:51:56 +02004786 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004787
4788 intel_update_watermarks(dev);
4789
Eric Anholtf564048e2011-03-30 13:01:02 -07004790 return ret;
4791}
4792
Paulo Zanonidde86e22012-12-01 12:04:25 -02004793static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07004794{
4795 struct drm_i915_private *dev_priv = dev->dev_private;
4796 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004797 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004798 u32 temp;
4799 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004800 bool has_cpu_edp = false;
4801 bool has_pch_edp = false;
4802 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07004803 bool has_ck505 = false;
4804 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004805
4806 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07004807 list_for_each_entry(encoder, &mode_config->encoder_list,
4808 base.head) {
4809 switch (encoder->type) {
4810 case INTEL_OUTPUT_LVDS:
4811 has_panel = true;
4812 has_lvds = true;
4813 break;
4814 case INTEL_OUTPUT_EDP:
4815 has_panel = true;
4816 if (intel_encoder_is_pch_edp(&encoder->base))
4817 has_pch_edp = true;
4818 else
4819 has_cpu_edp = true;
4820 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004821 }
4822 }
4823
Keith Packard99eb6a02011-09-26 14:29:12 -07004824 if (HAS_PCH_IBX(dev)) {
4825 has_ck505 = dev_priv->display_clock_mode;
4826 can_ssc = has_ck505;
4827 } else {
4828 has_ck505 = false;
4829 can_ssc = true;
4830 }
4831
4832 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4833 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4834 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004835
4836 /* Ironlake: try to setup display ref clock before DPLL
4837 * enabling. This is only under driver's control after
4838 * PCH B stepping, previous chipset stepping should be
4839 * ignoring this setting.
4840 */
4841 temp = I915_READ(PCH_DREF_CONTROL);
4842 /* Always enable nonspread source */
4843 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004844
Keith Packard99eb6a02011-09-26 14:29:12 -07004845 if (has_ck505)
4846 temp |= DREF_NONSPREAD_CK505_ENABLE;
4847 else
4848 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004849
Keith Packard199e5d72011-09-22 12:01:57 -07004850 if (has_panel) {
4851 temp &= ~DREF_SSC_SOURCE_MASK;
4852 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004853
Keith Packard199e5d72011-09-22 12:01:57 -07004854 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07004855 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004856 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004857 temp |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02004858 } else
4859 temp &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004860
4861 /* Get SSC going before enabling the outputs */
4862 I915_WRITE(PCH_DREF_CONTROL, temp);
4863 POSTING_READ(PCH_DREF_CONTROL);
4864 udelay(200);
4865
Jesse Barnes13d83a62011-08-03 12:59:20 -07004866 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4867
4868 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07004869 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07004870 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004871 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004872 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004873 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07004874 else
4875 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004876 } else
4877 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4878
4879 I915_WRITE(PCH_DREF_CONTROL, temp);
4880 POSTING_READ(PCH_DREF_CONTROL);
4881 udelay(200);
4882 } else {
4883 DRM_DEBUG_KMS("Disabling SSC entirely\n");
4884
4885 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4886
4887 /* Turn off CPU output */
4888 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4889
4890 I915_WRITE(PCH_DREF_CONTROL, temp);
4891 POSTING_READ(PCH_DREF_CONTROL);
4892 udelay(200);
4893
4894 /* Turn off the SSC source */
4895 temp &= ~DREF_SSC_SOURCE_MASK;
4896 temp |= DREF_SSC_SOURCE_DISABLE;
4897
4898 /* Turn off SSC1 */
4899 temp &= ~ DREF_SSC1_ENABLE;
4900
Jesse Barnes13d83a62011-08-03 12:59:20 -07004901 I915_WRITE(PCH_DREF_CONTROL, temp);
4902 POSTING_READ(PCH_DREF_CONTROL);
4903 udelay(200);
4904 }
4905}
4906
Paulo Zanonidde86e22012-12-01 12:04:25 -02004907/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
4908static void lpt_init_pch_refclk(struct drm_device *dev)
4909{
4910 struct drm_i915_private *dev_priv = dev->dev_private;
4911 struct drm_mode_config *mode_config = &dev->mode_config;
4912 struct intel_encoder *encoder;
4913 bool has_vga = false;
4914 bool is_sdv = false;
4915 u32 tmp;
4916
4917 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
4918 switch (encoder->type) {
4919 case INTEL_OUTPUT_ANALOG:
4920 has_vga = true;
4921 break;
4922 }
4923 }
4924
4925 if (!has_vga)
4926 return;
4927
Daniel Vetterc00db242013-01-22 15:33:27 +01004928 mutex_lock(&dev_priv->dpio_lock);
4929
Paulo Zanonidde86e22012-12-01 12:04:25 -02004930 /* XXX: Rip out SDV support once Haswell ships for real. */
4931 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
4932 is_sdv = true;
4933
4934 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4935 tmp &= ~SBI_SSCCTL_DISABLE;
4936 tmp |= SBI_SSCCTL_PATHALT;
4937 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4938
4939 udelay(24);
4940
4941 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4942 tmp &= ~SBI_SSCCTL_PATHALT;
4943 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4944
4945 if (!is_sdv) {
4946 tmp = I915_READ(SOUTH_CHICKEN2);
4947 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
4948 I915_WRITE(SOUTH_CHICKEN2, tmp);
4949
4950 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
4951 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
4952 DRM_ERROR("FDI mPHY reset assert timeout\n");
4953
4954 tmp = I915_READ(SOUTH_CHICKEN2);
4955 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
4956 I915_WRITE(SOUTH_CHICKEN2, tmp);
4957
4958 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
4959 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
4960 100))
4961 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
4962 }
4963
4964 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
4965 tmp &= ~(0xFF << 24);
4966 tmp |= (0x12 << 24);
4967 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
4968
4969 if (!is_sdv) {
4970 tmp = intel_sbi_read(dev_priv, 0x808C, SBI_MPHY);
4971 tmp &= ~(0x3 << 6);
4972 tmp |= (1 << 6) | (1 << 0);
4973 intel_sbi_write(dev_priv, 0x808C, tmp, SBI_MPHY);
4974 }
4975
4976 if (is_sdv) {
4977 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
4978 tmp |= 0x7FFF;
4979 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
4980 }
4981
4982 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
4983 tmp |= (1 << 11);
4984 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
4985
4986 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
4987 tmp |= (1 << 11);
4988 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
4989
4990 if (is_sdv) {
4991 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
4992 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
4993 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
4994
4995 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
4996 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
4997 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
4998
4999 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5000 tmp |= (0x3F << 8);
5001 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5002
5003 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5004 tmp |= (0x3F << 8);
5005 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5006 }
5007
5008 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5009 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5010 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5011
5012 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5013 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5014 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5015
5016 if (!is_sdv) {
5017 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5018 tmp &= ~(7 << 13);
5019 tmp |= (5 << 13);
5020 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5021
5022 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5023 tmp &= ~(7 << 13);
5024 tmp |= (5 << 13);
5025 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5026 }
5027
5028 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5029 tmp &= ~0xFF;
5030 tmp |= 0x1C;
5031 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5032
5033 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5034 tmp &= ~0xFF;
5035 tmp |= 0x1C;
5036 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5037
5038 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5039 tmp &= ~(0xFF << 16);
5040 tmp |= (0x1C << 16);
5041 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5042
5043 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5044 tmp &= ~(0xFF << 16);
5045 tmp |= (0x1C << 16);
5046 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5047
5048 if (!is_sdv) {
5049 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5050 tmp |= (1 << 27);
5051 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5052
5053 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5054 tmp |= (1 << 27);
5055 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5056
5057 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5058 tmp &= ~(0xF << 28);
5059 tmp |= (4 << 28);
5060 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5061
5062 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5063 tmp &= ~(0xF << 28);
5064 tmp |= (4 << 28);
5065 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5066 }
5067
5068 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5069 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5070 tmp |= SBI_DBUFF0_ENABLE;
5071 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005072
5073 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005074}
5075
5076/*
5077 * Initialize reference clocks when the driver loads
5078 */
5079void intel_init_pch_refclk(struct drm_device *dev)
5080{
5081 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5082 ironlake_init_pch_refclk(dev);
5083 else if (HAS_PCH_LPT(dev))
5084 lpt_init_pch_refclk(dev);
5085}
5086
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005087static int ironlake_get_refclk(struct drm_crtc *crtc)
5088{
5089 struct drm_device *dev = crtc->dev;
5090 struct drm_i915_private *dev_priv = dev->dev_private;
5091 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005092 struct intel_encoder *edp_encoder = NULL;
5093 int num_connectors = 0;
5094 bool is_lvds = false;
5095
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005096 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005097 switch (encoder->type) {
5098 case INTEL_OUTPUT_LVDS:
5099 is_lvds = true;
5100 break;
5101 case INTEL_OUTPUT_EDP:
5102 edp_encoder = encoder;
5103 break;
5104 }
5105 num_connectors++;
5106 }
5107
5108 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5109 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5110 dev_priv->lvds_ssc_freq);
5111 return dev_priv->lvds_ssc_freq * 1000;
5112 }
5113
5114 return 120000;
5115}
5116
Paulo Zanonic8203562012-09-12 10:06:29 -03005117static void ironlake_set_pipeconf(struct drm_crtc *crtc,
5118 struct drm_display_mode *adjusted_mode,
5119 bool dither)
5120{
5121 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5123 int pipe = intel_crtc->pipe;
5124 uint32_t val;
5125
5126 val = I915_READ(PIPECONF(pipe));
5127
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005128 val &= ~PIPECONF_BPC_MASK;
Paulo Zanonic8203562012-09-12 10:06:29 -03005129 switch (intel_crtc->bpp) {
5130 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005131 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005132 break;
5133 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005134 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005135 break;
5136 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005137 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005138 break;
5139 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005140 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005141 break;
5142 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005143 /* Case prevented by intel_choose_pipe_bpp_dither. */
5144 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005145 }
5146
5147 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5148 if (dither)
5149 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5150
5151 val &= ~PIPECONF_INTERLACE_MASK;
5152 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5153 val |= PIPECONF_INTERLACED_ILK;
5154 else
5155 val |= PIPECONF_PROGRESSIVE;
5156
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005157 if (adjusted_mode->private_flags & INTEL_MODE_LIMITED_COLOR_RANGE)
5158 val |= PIPECONF_COLOR_RANGE_SELECT;
5159 else
5160 val &= ~PIPECONF_COLOR_RANGE_SELECT;
5161
Paulo Zanonic8203562012-09-12 10:06:29 -03005162 I915_WRITE(PIPECONF(pipe), val);
5163 POSTING_READ(PIPECONF(pipe));
5164}
5165
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005166/*
5167 * Set up the pipe CSC unit.
5168 *
5169 * Currently only full range RGB to limited range RGB conversion
5170 * is supported, but eventually this should handle various
5171 * RGB<->YCbCr scenarios as well.
5172 */
5173static void intel_set_pipe_csc(struct drm_crtc *crtc,
5174 const struct drm_display_mode *adjusted_mode)
5175{
5176 struct drm_device *dev = crtc->dev;
5177 struct drm_i915_private *dev_priv = dev->dev_private;
5178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5179 int pipe = intel_crtc->pipe;
5180 uint16_t coeff = 0x7800; /* 1.0 */
5181
5182 /*
5183 * TODO: Check what kind of values actually come out of the pipe
5184 * with these coeff/postoff values and adjust to get the best
5185 * accuracy. Perhaps we even need to take the bpc value into
5186 * consideration.
5187 */
5188
5189 if (adjusted_mode->private_flags & INTEL_MODE_LIMITED_COLOR_RANGE)
5190 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5191
5192 /*
5193 * GY/GU and RY/RU should be the other way around according
5194 * to BSpec, but reality doesn't agree. Just set them up in
5195 * a way that results in the correct picture.
5196 */
5197 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5198 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5199
5200 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5201 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5202
5203 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5204 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5205
5206 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5207 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5208 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5209
5210 if (INTEL_INFO(dev)->gen > 6) {
5211 uint16_t postoff = 0;
5212
5213 if (adjusted_mode->private_flags & INTEL_MODE_LIMITED_COLOR_RANGE)
5214 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5215
5216 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5217 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5218 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5219
5220 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5221 } else {
5222 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5223
5224 if (adjusted_mode->private_flags & INTEL_MODE_LIMITED_COLOR_RANGE)
5225 mode |= CSC_BLACK_SCREEN_OFFSET;
5226
5227 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5228 }
5229}
5230
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005231static void haswell_set_pipeconf(struct drm_crtc *crtc,
5232 struct drm_display_mode *adjusted_mode,
5233 bool dither)
5234{
5235 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005237 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005238 uint32_t val;
5239
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005240 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005241
5242 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5243 if (dither)
5244 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5245
5246 val &= ~PIPECONF_INTERLACE_MASK_HSW;
5247 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5248 val |= PIPECONF_INTERLACED_ILK;
5249 else
5250 val |= PIPECONF_PROGRESSIVE;
5251
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005252 I915_WRITE(PIPECONF(cpu_transcoder), val);
5253 POSTING_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005254}
5255
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005256static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5257 struct drm_display_mode *adjusted_mode,
5258 intel_clock_t *clock,
5259 bool *has_reduced_clock,
5260 intel_clock_t *reduced_clock)
5261{
5262 struct drm_device *dev = crtc->dev;
5263 struct drm_i915_private *dev_priv = dev->dev_private;
5264 struct intel_encoder *intel_encoder;
5265 int refclk;
5266 const intel_limit_t *limit;
5267 bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
5268
5269 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5270 switch (intel_encoder->type) {
5271 case INTEL_OUTPUT_LVDS:
5272 is_lvds = true;
5273 break;
5274 case INTEL_OUTPUT_SDVO:
5275 case INTEL_OUTPUT_HDMI:
5276 is_sdvo = true;
5277 if (intel_encoder->needs_tv_clock)
5278 is_tv = true;
5279 break;
5280 case INTEL_OUTPUT_TVOUT:
5281 is_tv = true;
5282 break;
5283 }
5284 }
5285
5286 refclk = ironlake_get_refclk(crtc);
5287
5288 /*
5289 * Returns a set of divisors for the desired target clock with the given
5290 * refclk, or FALSE. The returned values represent the clock equation:
5291 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5292 */
5293 limit = intel_limit(crtc, refclk);
5294 ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5295 clock);
5296 if (!ret)
5297 return false;
5298
5299 if (is_lvds && dev_priv->lvds_downclock_avail) {
5300 /*
5301 * Ensure we match the reduced clock's P to the target clock.
5302 * If the clocks don't match, we can't switch the display clock
5303 * by using the FP0/FP1. In such case we will disable the LVDS
5304 * downclock feature.
5305 */
5306 *has_reduced_clock = limit->find_pll(limit, crtc,
5307 dev_priv->lvds_downclock,
5308 refclk,
5309 clock,
5310 reduced_clock);
5311 }
5312
5313 if (is_sdvo && is_tv)
5314 i9xx_adjust_sdvo_tv_clock(adjusted_mode, clock);
5315
5316 return true;
5317}
5318
Daniel Vetter01a415f2012-10-27 15:58:40 +02005319static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5320{
5321 struct drm_i915_private *dev_priv = dev->dev_private;
5322 uint32_t temp;
5323
5324 temp = I915_READ(SOUTH_CHICKEN1);
5325 if (temp & FDI_BC_BIFURCATION_SELECT)
5326 return;
5327
5328 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5329 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5330
5331 temp |= FDI_BC_BIFURCATION_SELECT;
5332 DRM_DEBUG_KMS("enabling fdi C rx\n");
5333 I915_WRITE(SOUTH_CHICKEN1, temp);
5334 POSTING_READ(SOUTH_CHICKEN1);
5335}
5336
5337static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
5338{
5339 struct drm_device *dev = intel_crtc->base.dev;
5340 struct drm_i915_private *dev_priv = dev->dev_private;
5341 struct intel_crtc *pipe_B_crtc =
5342 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5343
5344 DRM_DEBUG_KMS("checking fdi config on pipe %i, lanes %i\n",
5345 intel_crtc->pipe, intel_crtc->fdi_lanes);
5346 if (intel_crtc->fdi_lanes > 4) {
5347 DRM_DEBUG_KMS("invalid fdi lane config on pipe %i: %i lanes\n",
5348 intel_crtc->pipe, intel_crtc->fdi_lanes);
5349 /* Clamp lanes to avoid programming the hw with bogus values. */
5350 intel_crtc->fdi_lanes = 4;
5351
5352 return false;
5353 }
5354
5355 if (dev_priv->num_pipe == 2)
5356 return true;
5357
5358 switch (intel_crtc->pipe) {
5359 case PIPE_A:
5360 return true;
5361 case PIPE_B:
5362 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5363 intel_crtc->fdi_lanes > 2) {
5364 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5365 intel_crtc->pipe, intel_crtc->fdi_lanes);
5366 /* Clamp lanes to avoid programming the hw with bogus values. */
5367 intel_crtc->fdi_lanes = 2;
5368
5369 return false;
5370 }
5371
5372 if (intel_crtc->fdi_lanes > 2)
5373 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5374 else
5375 cpt_enable_fdi_bc_bifurcation(dev);
5376
5377 return true;
5378 case PIPE_C:
5379 if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
5380 if (intel_crtc->fdi_lanes > 2) {
5381 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5382 intel_crtc->pipe, intel_crtc->fdi_lanes);
5383 /* Clamp lanes to avoid programming the hw with bogus values. */
5384 intel_crtc->fdi_lanes = 2;
5385
5386 return false;
5387 }
5388 } else {
5389 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5390 return false;
5391 }
5392
5393 cpt_enable_fdi_bc_bifurcation(dev);
5394
5395 return true;
5396 default:
5397 BUG();
5398 }
5399}
5400
Paulo Zanonid4b19312012-11-29 11:29:32 -02005401int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5402{
5403 /*
5404 * Account for spread spectrum to avoid
5405 * oversubscribing the link. Max center spread
5406 * is 2.5%; use 5% for safety's sake.
5407 */
5408 u32 bps = target_clock * bpp * 21 / 20;
5409 return bps / (link_bw * 8) + 1;
5410}
5411
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005412static void ironlake_set_m_n(struct drm_crtc *crtc,
5413 struct drm_display_mode *mode,
5414 struct drm_display_mode *adjusted_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08005415{
5416 struct drm_device *dev = crtc->dev;
5417 struct drm_i915_private *dev_priv = dev->dev_private;
5418 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005419 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005420 struct intel_encoder *intel_encoder, *edp_encoder = NULL;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005421 struct intel_link_m_n m_n = {0};
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005422 int target_clock, pixel_multiplier, lane, link_bw;
5423 bool is_dp = false, is_cpu_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005424
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005425 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5426 switch (intel_encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005427 case INTEL_OUTPUT_DISPLAYPORT:
5428 is_dp = true;
5429 break;
5430 case INTEL_OUTPUT_EDP:
Jesse Barnese3aef172012-04-10 11:58:03 -07005431 is_dp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005432 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnese3aef172012-04-10 11:58:03 -07005433 is_cpu_edp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005434 edp_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005435 break;
5436 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005437 }
5438
Zhenyu Wang2c072452009-06-05 15:38:42 +08005439 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07005440 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5441 lane = 0;
5442 /* CPU eDP doesn't require FDI link, so just set DP M/N
5443 according to current link config */
Jesse Barnese3aef172012-04-10 11:58:03 -07005444 if (is_cpu_edp) {
Jesse Barnese3aef172012-04-10 11:58:03 -07005445 intel_edp_link_config(edp_encoder, &lane, &link_bw);
Eric Anholt8febb292011-03-30 13:01:07 -07005446 } else {
Eric Anholt8febb292011-03-30 13:01:07 -07005447 /* FDI is a binary signal running at ~2.7GHz, encoding
5448 * each output octet as 10 bits. The actual frequency
5449 * is stored as a divider into a 100MHz clock, and the
5450 * mode pixel clock is stored in units of 1KHz.
5451 * Hence the bw of each lane in terms of the mode signal
5452 * is:
5453 */
5454 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005455 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08005456
Daniel Vetter94bf2ce2012-06-04 18:39:19 +02005457 /* [e]DP over FDI requires target mode clock instead of link clock. */
5458 if (edp_encoder)
5459 target_clock = intel_edp_target_clock(edp_encoder, mode);
5460 else if (is_dp)
5461 target_clock = mode->clock;
5462 else
5463 target_clock = adjusted_mode->clock;
5464
Paulo Zanonid4b19312012-11-29 11:29:32 -02005465 if (!lane)
5466 lane = ironlake_get_lanes_required(target_clock, link_bw,
5467 intel_crtc->bpp);
Eric Anholt8febb292011-03-30 13:01:07 -07005468
5469 intel_crtc->fdi_lanes = lane;
5470
5471 if (pixel_multiplier > 1)
5472 link_bw *= pixel_multiplier;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005473 intel_link_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw, &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07005474
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005475 I915_WRITE(PIPE_DATA_M1(cpu_transcoder), TU_SIZE(m_n.tu) | m_n.gmch_m);
5476 I915_WRITE(PIPE_DATA_N1(cpu_transcoder), m_n.gmch_n);
5477 I915_WRITE(PIPE_LINK_M1(cpu_transcoder), m_n.link_m);
5478 I915_WRITE(PIPE_LINK_N1(cpu_transcoder), m_n.link_n);
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005479}
5480
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005481static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5482 struct drm_display_mode *adjusted_mode,
5483 intel_clock_t *clock, u32 fp)
5484{
5485 struct drm_crtc *crtc = &intel_crtc->base;
5486 struct drm_device *dev = crtc->dev;
5487 struct drm_i915_private *dev_priv = dev->dev_private;
5488 struct intel_encoder *intel_encoder;
5489 uint32_t dpll;
5490 int factor, pixel_multiplier, num_connectors = 0;
5491 bool is_lvds = false, is_sdvo = false, is_tv = false;
5492 bool is_dp = false, is_cpu_edp = false;
5493
5494 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5495 switch (intel_encoder->type) {
5496 case INTEL_OUTPUT_LVDS:
5497 is_lvds = true;
5498 break;
5499 case INTEL_OUTPUT_SDVO:
5500 case INTEL_OUTPUT_HDMI:
5501 is_sdvo = true;
5502 if (intel_encoder->needs_tv_clock)
5503 is_tv = true;
5504 break;
5505 case INTEL_OUTPUT_TVOUT:
5506 is_tv = true;
5507 break;
5508 case INTEL_OUTPUT_DISPLAYPORT:
5509 is_dp = true;
5510 break;
5511 case INTEL_OUTPUT_EDP:
5512 is_dp = true;
5513 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
5514 is_cpu_edp = true;
5515 break;
5516 }
5517
5518 num_connectors++;
5519 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005520
Chris Wilsonc1858122010-12-03 21:35:48 +00005521 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005522 factor = 21;
5523 if (is_lvds) {
5524 if ((intel_panel_use_ssc(dev_priv) &&
5525 dev_priv->lvds_ssc_freq == 100) ||
Daniel Vetter1974cad2012-11-26 17:22:09 +01005526 intel_is_dual_link_lvds(dev))
Eric Anholt8febb292011-03-30 13:01:07 -07005527 factor = 25;
5528 } else if (is_sdvo && is_tv)
5529 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005530
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005531 if (clock->m < factor * clock->n)
Eric Anholt8febb292011-03-30 13:01:07 -07005532 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005533
Chris Wilson5eddb702010-09-11 13:48:45 +01005534 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005535
Eric Anholta07d6782011-03-30 13:01:08 -07005536 if (is_lvds)
5537 dpll |= DPLLB_MODE_LVDS;
5538 else
5539 dpll |= DPLLB_MODE_DAC_SERIAL;
5540 if (is_sdvo) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005541 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
Eric Anholta07d6782011-03-30 13:01:08 -07005542 if (pixel_multiplier > 1) {
5543 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08005544 }
Eric Anholta07d6782011-03-30 13:01:08 -07005545 dpll |= DPLL_DVO_HIGH_SPEED;
5546 }
Jesse Barnese3aef172012-04-10 11:58:03 -07005547 if (is_dp && !is_cpu_edp)
Eric Anholta07d6782011-03-30 13:01:08 -07005548 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005549
Eric Anholta07d6782011-03-30 13:01:08 -07005550 /* compute bitmask from p1 value */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005551 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005552 /* also FPA1 */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005553 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005554
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005555 switch (clock->p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005556 case 5:
5557 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5558 break;
5559 case 7:
5560 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5561 break;
5562 case 10:
5563 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5564 break;
5565 case 14:
5566 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5567 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005568 }
5569
5570 if (is_sdvo && is_tv)
5571 dpll |= PLL_REF_INPUT_TVCLKINBC;
5572 else if (is_tv)
5573 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005574 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08005575 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00005576 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005577 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005578 else
5579 dpll |= PLL_REF_INPUT_DREFCLK;
5580
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005581 return dpll;
5582}
5583
Jesse Barnes79e53942008-11-07 14:24:08 -08005584static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5585 struct drm_display_mode *mode,
5586 struct drm_display_mode *adjusted_mode,
5587 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005588 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005589{
5590 struct drm_device *dev = crtc->dev;
5591 struct drm_i915_private *dev_priv = dev->dev_private;
5592 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5593 int pipe = intel_crtc->pipe;
5594 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005595 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005596 intel_clock_t clock, reduced_clock;
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005597 u32 dpll, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005598 bool ok, has_reduced_clock = false;
5599 bool is_lvds = false, is_dp = false, is_cpu_edp = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005600 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005601 int ret;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005602 bool dither, fdi_config_ok;
Jesse Barnes79e53942008-11-07 14:24:08 -08005603
5604 for_each_encoder_on_crtc(dev, crtc, encoder) {
5605 switch (encoder->type) {
5606 case INTEL_OUTPUT_LVDS:
5607 is_lvds = true;
5608 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005609 case INTEL_OUTPUT_DISPLAYPORT:
5610 is_dp = true;
5611 break;
5612 case INTEL_OUTPUT_EDP:
5613 is_dp = true;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005614 if (!intel_encoder_is_pch_edp(&encoder->base))
Jesse Barnes79e53942008-11-07 14:24:08 -08005615 is_cpu_edp = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005616 break;
5617 }
5618
5619 num_connectors++;
5620 }
5621
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005622 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5623 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5624
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005625 ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
5626 &has_reduced_clock, &reduced_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005627 if (!ok) {
5628 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5629 return -EINVAL;
5630 }
5631
5632 /* Ensure that the cursor is valid for the new mode before changing... */
5633 intel_crtc_update_cursor(crtc, true);
5634
Jesse Barnes79e53942008-11-07 14:24:08 -08005635 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005636 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5637 adjusted_mode);
Paulo Zanonic8203562012-09-12 10:06:29 -03005638 if (is_lvds && dev_priv->lvds_dither)
5639 dither = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005640
Jesse Barnes79e53942008-11-07 14:24:08 -08005641 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5642 if (has_reduced_clock)
5643 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5644 reduced_clock.m2;
5645
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005646 dpll = ironlake_compute_dpll(intel_crtc, adjusted_mode, &clock, fp);
Jesse Barnes79e53942008-11-07 14:24:08 -08005647
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07005648 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005649 drm_mode_debug_printmodeline(mode);
5650
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005651 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5652 if (!is_cpu_edp) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005653 struct intel_pch_pll *pll;
Chris Wilson5eddb702010-09-11 13:48:45 +01005654
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005655 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5656 if (pll == NULL) {
5657 DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
5658 pipe);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005659 return -EINVAL;
5660 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005661 } else
5662 intel_put_pch_pll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005663
Daniel Vetter2f0c2ad2012-11-29 15:59:35 +01005664 if (is_dp && !is_cpu_edp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005665 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08005666
Daniel Vetterdafd2262012-11-26 17:22:07 +01005667 for_each_encoder_on_crtc(dev, crtc, encoder)
5668 if (encoder->pre_pll_enable)
5669 encoder->pre_pll_enable(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08005670
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005671 if (intel_crtc->pch_pll) {
5672 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005673
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005674 /* Wait for the clocks to stabilize. */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005675 POSTING_READ(intel_crtc->pch_pll->pll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005676 udelay(150);
5677
Eric Anholt8febb292011-03-30 13:01:07 -07005678 /* The pixel multiplier can only be updated once the
5679 * DPLL is enabled and the clocks are stable.
5680 *
5681 * So write it again.
5682 */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005683 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08005684 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005685
Chris Wilson5eddb702010-09-11 13:48:45 +01005686 intel_crtc->lowfreq_avail = false;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005687 if (intel_crtc->pch_pll) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07005688 if (is_lvds && has_reduced_clock && i915_powersave) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005689 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005690 intel_crtc->lowfreq_avail = true;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005691 } else {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005692 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07005693 }
5694 }
5695
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005696 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005697
Daniel Vetter01a415f2012-10-27 15:58:40 +02005698 /* Note, this also computes intel_crtc->fdi_lanes which is used below in
5699 * ironlake_check_fdi_lanes. */
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005700 ironlake_set_m_n(crtc, mode, adjusted_mode);
Chris Wilson5eddb702010-09-11 13:48:45 +01005701
Daniel Vetter01a415f2012-10-27 15:58:40 +02005702 fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005703
Paulo Zanonic8203562012-09-12 10:06:29 -03005704 ironlake_set_pipeconf(crtc, adjusted_mode, dither);
Jesse Barnes79e53942008-11-07 14:24:08 -08005705
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005706 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005707
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005708 /* Set up the display plane register */
5709 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005710 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005711
Daniel Vetter94352cf2012-07-05 22:51:56 +02005712 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005713
5714 intel_update_watermarks(dev);
5715
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03005716 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5717
Daniel Vetter01a415f2012-10-27 15:58:40 +02005718 return fdi_config_ok ? ret : -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005719}
5720
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005721static void haswell_modeset_global_resources(struct drm_device *dev)
5722{
5723 struct drm_i915_private *dev_priv = dev->dev_private;
5724 bool enable = false;
5725 struct intel_crtc *crtc;
5726 struct intel_encoder *encoder;
5727
5728 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
5729 if (crtc->pipe != PIPE_A && crtc->base.enabled)
5730 enable = true;
5731 /* XXX: Should check for edp transcoder here, but thanks to init
5732 * sequence that's not yet available. Just in case desktop eDP
5733 * on PORT D is possible on haswell, too. */
5734 }
5735
5736 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
5737 base.head) {
5738 if (encoder->type != INTEL_OUTPUT_EDP &&
5739 encoder->connectors_active)
5740 enable = true;
5741 }
5742
5743 /* Even the eDP panel fitter is outside the always-on well. */
5744 if (dev_priv->pch_pf_size)
5745 enable = true;
5746
5747 intel_set_power_well(dev, enable);
5748}
5749
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005750static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5751 struct drm_display_mode *mode,
5752 struct drm_display_mode *adjusted_mode,
5753 int x, int y,
5754 struct drm_framebuffer *fb)
5755{
5756 struct drm_device *dev = crtc->dev;
5757 struct drm_i915_private *dev_priv = dev->dev_private;
5758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5759 int pipe = intel_crtc->pipe;
5760 int plane = intel_crtc->plane;
5761 int num_connectors = 0;
Daniel Vettered7ef432012-12-06 14:24:21 +01005762 bool is_dp = false, is_cpu_edp = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005763 struct intel_encoder *encoder;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005764 int ret;
5765 bool dither;
5766
5767 for_each_encoder_on_crtc(dev, crtc, encoder) {
5768 switch (encoder->type) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005769 case INTEL_OUTPUT_DISPLAYPORT:
5770 is_dp = true;
5771 break;
5772 case INTEL_OUTPUT_EDP:
5773 is_dp = true;
5774 if (!intel_encoder_is_pch_edp(&encoder->base))
5775 is_cpu_edp = true;
5776 break;
5777 }
5778
5779 num_connectors++;
5780 }
5781
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005782 /* We are not sure yet this won't happen. */
5783 WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
5784 INTEL_PCH_TYPE(dev));
5785
5786 WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
5787 num_connectors, pipe_name(pipe));
5788
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005789 WARN_ON(I915_READ(PIPECONF(intel_crtc->cpu_transcoder)) &
Paulo Zanoni1ce42922012-10-05 12:06:01 -03005790 (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
5791
5792 WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
5793
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005794 if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
5795 return -EINVAL;
5796
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005797 /* Ensure that the cursor is valid for the new mode before changing... */
5798 intel_crtc_update_cursor(crtc, true);
5799
5800 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005801 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5802 adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005803
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005804 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
5805 drm_mode_debug_printmodeline(mode);
5806
Daniel Vettered7ef432012-12-06 14:24:21 +01005807 if (is_dp && !is_cpu_edp)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005808 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005809
5810 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005811
5812 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5813
Paulo Zanoni1eb8dfe2012-10-18 12:42:10 -03005814 if (!is_dp || is_cpu_edp)
5815 ironlake_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005816
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005817 haswell_set_pipeconf(crtc, adjusted_mode, dither);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005818
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005819 intel_set_pipe_csc(crtc, adjusted_mode);
5820
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005821 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005822 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005823 POSTING_READ(DSPCNTR(plane));
5824
5825 ret = intel_pipe_set_base(crtc, x, y, fb);
5826
5827 intel_update_watermarks(dev);
5828
5829 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5830
Jesse Barnes79e53942008-11-07 14:24:08 -08005831 return ret;
5832}
5833
Eric Anholtf564048e2011-03-30 13:01:02 -07005834static int intel_crtc_mode_set(struct drm_crtc *crtc,
5835 struct drm_display_mode *mode,
5836 struct drm_display_mode *adjusted_mode,
5837 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005838 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07005839{
5840 struct drm_device *dev = crtc->dev;
5841 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01005842 struct drm_encoder_helper_funcs *encoder_funcs;
5843 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07005844 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5845 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07005846 int ret;
5847
Paulo Zanonicc464b22013-01-25 16:59:16 -02005848 if (IS_HASWELL(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
5849 intel_crtc->cpu_transcoder = TRANSCODER_EDP;
5850 else
5851 intel_crtc->cpu_transcoder = pipe;
5852
Eric Anholt0b701d22011-03-30 13:01:03 -07005853 drm_vblank_pre_modeset(dev, pipe);
5854
Eric Anholtf564048e2011-03-30 13:01:02 -07005855 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005856 x, y, fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08005857 drm_vblank_post_modeset(dev, pipe);
5858
Daniel Vetter9256aa12012-10-31 19:26:13 +01005859 if (ret != 0)
5860 return ret;
5861
5862 for_each_encoder_on_crtc(dev, crtc, encoder) {
5863 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
5864 encoder->base.base.id,
5865 drm_get_encoder_name(&encoder->base),
5866 mode->base.id, mode->name);
5867 encoder_funcs = encoder->base.helper_private;
5868 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
5869 }
5870
5871 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005872}
5873
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005874static bool intel_eld_uptodate(struct drm_connector *connector,
5875 int reg_eldv, uint32_t bits_eldv,
5876 int reg_elda, uint32_t bits_elda,
5877 int reg_edid)
5878{
5879 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5880 uint8_t *eld = connector->eld;
5881 uint32_t i;
5882
5883 i = I915_READ(reg_eldv);
5884 i &= bits_eldv;
5885
5886 if (!eld[0])
5887 return !i;
5888
5889 if (!i)
5890 return false;
5891
5892 i = I915_READ(reg_elda);
5893 i &= ~bits_elda;
5894 I915_WRITE(reg_elda, i);
5895
5896 for (i = 0; i < eld[2]; i++)
5897 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
5898 return false;
5899
5900 return true;
5901}
5902
Wu Fengguange0dac652011-09-05 14:25:34 +08005903static void g4x_write_eld(struct drm_connector *connector,
5904 struct drm_crtc *crtc)
5905{
5906 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5907 uint8_t *eld = connector->eld;
5908 uint32_t eldv;
5909 uint32_t len;
5910 uint32_t i;
5911
5912 i = I915_READ(G4X_AUD_VID_DID);
5913
5914 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
5915 eldv = G4X_ELDV_DEVCL_DEVBLC;
5916 else
5917 eldv = G4X_ELDV_DEVCTG;
5918
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005919 if (intel_eld_uptodate(connector,
5920 G4X_AUD_CNTL_ST, eldv,
5921 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
5922 G4X_HDMIW_HDMIEDID))
5923 return;
5924
Wu Fengguange0dac652011-09-05 14:25:34 +08005925 i = I915_READ(G4X_AUD_CNTL_ST);
5926 i &= ~(eldv | G4X_ELD_ADDR);
5927 len = (i >> 9) & 0x1f; /* ELD buffer size */
5928 I915_WRITE(G4X_AUD_CNTL_ST, i);
5929
5930 if (!eld[0])
5931 return;
5932
5933 len = min_t(uint8_t, eld[2], len);
5934 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5935 for (i = 0; i < len; i++)
5936 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
5937
5938 i = I915_READ(G4X_AUD_CNTL_ST);
5939 i |= eldv;
5940 I915_WRITE(G4X_AUD_CNTL_ST, i);
5941}
5942
Wang Xingchao83358c852012-08-16 22:43:37 +08005943static void haswell_write_eld(struct drm_connector *connector,
5944 struct drm_crtc *crtc)
5945{
5946 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5947 uint8_t *eld = connector->eld;
5948 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08005949 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08005950 uint32_t eldv;
5951 uint32_t i;
5952 int len;
5953 int pipe = to_intel_crtc(crtc)->pipe;
5954 int tmp;
5955
5956 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
5957 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
5958 int aud_config = HSW_AUD_CFG(pipe);
5959 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
5960
5961
5962 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
5963
5964 /* Audio output enable */
5965 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
5966 tmp = I915_READ(aud_cntrl_st2);
5967 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
5968 I915_WRITE(aud_cntrl_st2, tmp);
5969
5970 /* Wait for 1 vertical blank */
5971 intel_wait_for_vblank(dev, pipe);
5972
5973 /* Set ELD valid state */
5974 tmp = I915_READ(aud_cntrl_st2);
5975 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
5976 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
5977 I915_WRITE(aud_cntrl_st2, tmp);
5978 tmp = I915_READ(aud_cntrl_st2);
5979 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
5980
5981 /* Enable HDMI mode */
5982 tmp = I915_READ(aud_config);
5983 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
5984 /* clear N_programing_enable and N_value_index */
5985 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
5986 I915_WRITE(aud_config, tmp);
5987
5988 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5989
5990 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08005991 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08005992
5993 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5994 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5995 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
5996 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5997 } else
5998 I915_WRITE(aud_config, 0);
5999
6000 if (intel_eld_uptodate(connector,
6001 aud_cntrl_st2, eldv,
6002 aud_cntl_st, IBX_ELD_ADDRESS,
6003 hdmiw_hdmiedid))
6004 return;
6005
6006 i = I915_READ(aud_cntrl_st2);
6007 i &= ~eldv;
6008 I915_WRITE(aud_cntrl_st2, i);
6009
6010 if (!eld[0])
6011 return;
6012
6013 i = I915_READ(aud_cntl_st);
6014 i &= ~IBX_ELD_ADDRESS;
6015 I915_WRITE(aud_cntl_st, i);
6016 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6017 DRM_DEBUG_DRIVER("port num:%d\n", i);
6018
6019 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6020 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6021 for (i = 0; i < len; i++)
6022 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6023
6024 i = I915_READ(aud_cntrl_st2);
6025 i |= eldv;
6026 I915_WRITE(aud_cntrl_st2, i);
6027
6028}
6029
Wu Fengguange0dac652011-09-05 14:25:34 +08006030static void ironlake_write_eld(struct drm_connector *connector,
6031 struct drm_crtc *crtc)
6032{
6033 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6034 uint8_t *eld = connector->eld;
6035 uint32_t eldv;
6036 uint32_t i;
6037 int len;
6038 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006039 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006040 int aud_cntl_st;
6041 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006042 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006043
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006044 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006045 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6046 aud_config = IBX_AUD_CFG(pipe);
6047 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006048 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006049 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006050 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6051 aud_config = CPT_AUD_CFG(pipe);
6052 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006053 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006054 }
6055
Wang Xingchao9b138a82012-08-09 16:52:18 +08006056 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006057
6058 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006059 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006060 if (!i) {
6061 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6062 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006063 eldv = IBX_ELD_VALIDB;
6064 eldv |= IBX_ELD_VALIDB << 4;
6065 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006066 } else {
6067 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006068 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006069 }
6070
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006071 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6072 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6073 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006074 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6075 } else
6076 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006077
6078 if (intel_eld_uptodate(connector,
6079 aud_cntrl_st2, eldv,
6080 aud_cntl_st, IBX_ELD_ADDRESS,
6081 hdmiw_hdmiedid))
6082 return;
6083
Wu Fengguange0dac652011-09-05 14:25:34 +08006084 i = I915_READ(aud_cntrl_st2);
6085 i &= ~eldv;
6086 I915_WRITE(aud_cntrl_st2, i);
6087
6088 if (!eld[0])
6089 return;
6090
Wu Fengguange0dac652011-09-05 14:25:34 +08006091 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006092 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006093 I915_WRITE(aud_cntl_st, i);
6094
6095 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6096 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6097 for (i = 0; i < len; i++)
6098 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6099
6100 i = I915_READ(aud_cntrl_st2);
6101 i |= eldv;
6102 I915_WRITE(aud_cntrl_st2, i);
6103}
6104
6105void intel_write_eld(struct drm_encoder *encoder,
6106 struct drm_display_mode *mode)
6107{
6108 struct drm_crtc *crtc = encoder->crtc;
6109 struct drm_connector *connector;
6110 struct drm_device *dev = encoder->dev;
6111 struct drm_i915_private *dev_priv = dev->dev_private;
6112
6113 connector = drm_select_eld(encoder, mode);
6114 if (!connector)
6115 return;
6116
6117 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6118 connector->base.id,
6119 drm_get_connector_name(connector),
6120 connector->encoder->base.id,
6121 drm_get_encoder_name(connector->encoder));
6122
6123 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6124
6125 if (dev_priv->display.write_eld)
6126 dev_priv->display.write_eld(connector, crtc);
6127}
6128
Jesse Barnes79e53942008-11-07 14:24:08 -08006129/** Loads the palette/gamma unit for the CRTC with the prepared values */
6130void intel_crtc_load_lut(struct drm_crtc *crtc)
6131{
6132 struct drm_device *dev = crtc->dev;
6133 struct drm_i915_private *dev_priv = dev->dev_private;
6134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006135 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006136 int i;
6137
6138 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006139 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006140 return;
6141
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006142 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006143 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006144 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006145
Jesse Barnes79e53942008-11-07 14:24:08 -08006146 for (i = 0; i < 256; i++) {
6147 I915_WRITE(palreg + 4 * i,
6148 (intel_crtc->lut_r[i] << 16) |
6149 (intel_crtc->lut_g[i] << 8) |
6150 intel_crtc->lut_b[i]);
6151 }
6152}
6153
Chris Wilson560b85b2010-08-07 11:01:38 +01006154static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6155{
6156 struct drm_device *dev = crtc->dev;
6157 struct drm_i915_private *dev_priv = dev->dev_private;
6158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6159 bool visible = base != 0;
6160 u32 cntl;
6161
6162 if (intel_crtc->cursor_visible == visible)
6163 return;
6164
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006165 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006166 if (visible) {
6167 /* On these chipsets we can only modify the base whilst
6168 * the cursor is disabled.
6169 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006170 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006171
6172 cntl &= ~(CURSOR_FORMAT_MASK);
6173 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6174 cntl |= CURSOR_ENABLE |
6175 CURSOR_GAMMA_ENABLE |
6176 CURSOR_FORMAT_ARGB;
6177 } else
6178 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006179 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006180
6181 intel_crtc->cursor_visible = visible;
6182}
6183
6184static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6185{
6186 struct drm_device *dev = crtc->dev;
6187 struct drm_i915_private *dev_priv = dev->dev_private;
6188 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6189 int pipe = intel_crtc->pipe;
6190 bool visible = base != 0;
6191
6192 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006193 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006194 if (base) {
6195 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6196 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6197 cntl |= pipe << 28; /* Connect to correct pipe */
6198 } else {
6199 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6200 cntl |= CURSOR_MODE_DISABLE;
6201 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006202 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006203
6204 intel_crtc->cursor_visible = visible;
6205 }
6206 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006207 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006208}
6209
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006210static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6211{
6212 struct drm_device *dev = crtc->dev;
6213 struct drm_i915_private *dev_priv = dev->dev_private;
6214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6215 int pipe = intel_crtc->pipe;
6216 bool visible = base != 0;
6217
6218 if (intel_crtc->cursor_visible != visible) {
6219 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6220 if (base) {
6221 cntl &= ~CURSOR_MODE;
6222 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6223 } else {
6224 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6225 cntl |= CURSOR_MODE_DISABLE;
6226 }
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006227 if (IS_HASWELL(dev))
6228 cntl |= CURSOR_PIPE_CSC_ENABLE;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006229 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6230
6231 intel_crtc->cursor_visible = visible;
6232 }
6233 /* and commit changes on next vblank */
6234 I915_WRITE(CURBASE_IVB(pipe), base);
6235}
6236
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006237/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006238static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6239 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006240{
6241 struct drm_device *dev = crtc->dev;
6242 struct drm_i915_private *dev_priv = dev->dev_private;
6243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6244 int pipe = intel_crtc->pipe;
6245 int x = intel_crtc->cursor_x;
6246 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006247 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006248 bool visible;
6249
6250 pos = 0;
6251
Chris Wilson6b383a72010-09-13 13:54:26 +01006252 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006253 base = intel_crtc->cursor_addr;
6254 if (x > (int) crtc->fb->width)
6255 base = 0;
6256
6257 if (y > (int) crtc->fb->height)
6258 base = 0;
6259 } else
6260 base = 0;
6261
6262 if (x < 0) {
6263 if (x + intel_crtc->cursor_width < 0)
6264 base = 0;
6265
6266 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6267 x = -x;
6268 }
6269 pos |= x << CURSOR_X_SHIFT;
6270
6271 if (y < 0) {
6272 if (y + intel_crtc->cursor_height < 0)
6273 base = 0;
6274
6275 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6276 y = -y;
6277 }
6278 pos |= y << CURSOR_Y_SHIFT;
6279
6280 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006281 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006282 return;
6283
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006284 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006285 I915_WRITE(CURPOS_IVB(pipe), pos);
6286 ivb_update_cursor(crtc, base);
6287 } else {
6288 I915_WRITE(CURPOS(pipe), pos);
6289 if (IS_845G(dev) || IS_I865G(dev))
6290 i845_update_cursor(crtc, base);
6291 else
6292 i9xx_update_cursor(crtc, base);
6293 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006294}
6295
Jesse Barnes79e53942008-11-07 14:24:08 -08006296static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006297 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006298 uint32_t handle,
6299 uint32_t width, uint32_t height)
6300{
6301 struct drm_device *dev = crtc->dev;
6302 struct drm_i915_private *dev_priv = dev->dev_private;
6303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006304 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006305 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006306 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006307
Jesse Barnes79e53942008-11-07 14:24:08 -08006308 /* if we want to turn off the cursor ignore width and height */
6309 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006310 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006311 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006312 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006313 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006314 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006315 }
6316
6317 /* Currently we only support 64x64 cursors */
6318 if (width != 64 || height != 64) {
6319 DRM_ERROR("we currently only support 64x64 cursors\n");
6320 return -EINVAL;
6321 }
6322
Chris Wilson05394f32010-11-08 19:18:58 +00006323 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006324 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006325 return -ENOENT;
6326
Chris Wilson05394f32010-11-08 19:18:58 +00006327 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006328 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006329 ret = -ENOMEM;
6330 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006331 }
6332
Dave Airlie71acb5e2008-12-30 20:31:46 +10006333 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006334 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006335 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00006336 if (obj->tiling_mode) {
6337 DRM_ERROR("cursor cannot be tiled\n");
6338 ret = -EINVAL;
6339 goto fail_locked;
6340 }
6341
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006342 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006343 if (ret) {
6344 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006345 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006346 }
6347
Chris Wilsond9e86c02010-11-10 16:40:20 +00006348 ret = i915_gem_object_put_fence(obj);
6349 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006350 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006351 goto fail_unpin;
6352 }
6353
Chris Wilson05394f32010-11-08 19:18:58 +00006354 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006355 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006356 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006357 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006358 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6359 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006360 if (ret) {
6361 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006362 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006363 }
Chris Wilson05394f32010-11-08 19:18:58 +00006364 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006365 }
6366
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006367 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006368 I915_WRITE(CURSIZE, (height << 12) | width);
6369
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006370 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006371 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006372 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006373 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006374 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6375 } else
6376 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006377 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006378 }
Jesse Barnes80824002009-09-10 15:28:06 -07006379
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006380 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006381
6382 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006383 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006384 intel_crtc->cursor_width = width;
6385 intel_crtc->cursor_height = height;
6386
Chris Wilson6b383a72010-09-13 13:54:26 +01006387 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006388
Jesse Barnes79e53942008-11-07 14:24:08 -08006389 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006390fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006391 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006392fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006393 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006394fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006395 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006396 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006397}
6398
6399static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6400{
Jesse Barnes79e53942008-11-07 14:24:08 -08006401 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006402
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006403 intel_crtc->cursor_x = x;
6404 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006405
Chris Wilson6b383a72010-09-13 13:54:26 +01006406 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006407
6408 return 0;
6409}
6410
6411/** Sets the color ramps on behalf of RandR */
6412void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6413 u16 blue, int regno)
6414{
6415 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6416
6417 intel_crtc->lut_r[regno] = red >> 8;
6418 intel_crtc->lut_g[regno] = green >> 8;
6419 intel_crtc->lut_b[regno] = blue >> 8;
6420}
6421
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006422void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6423 u16 *blue, int regno)
6424{
6425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6426
6427 *red = intel_crtc->lut_r[regno] << 8;
6428 *green = intel_crtc->lut_g[regno] << 8;
6429 *blue = intel_crtc->lut_b[regno] << 8;
6430}
6431
Jesse Barnes79e53942008-11-07 14:24:08 -08006432static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006433 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006434{
James Simmons72034252010-08-03 01:33:19 +01006435 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006436 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006437
James Simmons72034252010-08-03 01:33:19 +01006438 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006439 intel_crtc->lut_r[i] = red[i] >> 8;
6440 intel_crtc->lut_g[i] = green[i] >> 8;
6441 intel_crtc->lut_b[i] = blue[i] >> 8;
6442 }
6443
6444 intel_crtc_load_lut(crtc);
6445}
6446
6447/**
6448 * Get a pipe with a simple mode set on it for doing load-based monitor
6449 * detection.
6450 *
6451 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07006452 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08006453 *
Eric Anholtc751ce42010-03-25 11:48:48 -07006454 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08006455 * configured for it. In the future, it could choose to temporarily disable
6456 * some outputs to free up a pipe for its use.
6457 *
6458 * \return crtc, or NULL if no pipes are available.
6459 */
6460
6461/* VESA 640x480x72Hz mode to set on the pipe */
6462static struct drm_display_mode load_detect_mode = {
6463 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6464 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6465};
6466
Chris Wilsond2dff872011-04-19 08:36:26 +01006467static struct drm_framebuffer *
6468intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006469 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006470 struct drm_i915_gem_object *obj)
6471{
6472 struct intel_framebuffer *intel_fb;
6473 int ret;
6474
6475 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6476 if (!intel_fb) {
6477 drm_gem_object_unreference_unlocked(&obj->base);
6478 return ERR_PTR(-ENOMEM);
6479 }
6480
6481 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6482 if (ret) {
6483 drm_gem_object_unreference_unlocked(&obj->base);
6484 kfree(intel_fb);
6485 return ERR_PTR(ret);
6486 }
6487
6488 return &intel_fb->base;
6489}
6490
6491static u32
6492intel_framebuffer_pitch_for_width(int width, int bpp)
6493{
6494 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6495 return ALIGN(pitch, 64);
6496}
6497
6498static u32
6499intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6500{
6501 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6502 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6503}
6504
6505static struct drm_framebuffer *
6506intel_framebuffer_create_for_mode(struct drm_device *dev,
6507 struct drm_display_mode *mode,
6508 int depth, int bpp)
6509{
6510 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006511 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006512
6513 obj = i915_gem_alloc_object(dev,
6514 intel_framebuffer_size_for_mode(mode, bpp));
6515 if (obj == NULL)
6516 return ERR_PTR(-ENOMEM);
6517
6518 mode_cmd.width = mode->hdisplay;
6519 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006520 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6521 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006522 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006523
6524 return intel_framebuffer_create(dev, &mode_cmd, obj);
6525}
6526
6527static struct drm_framebuffer *
6528mode_fits_in_fbdev(struct drm_device *dev,
6529 struct drm_display_mode *mode)
6530{
6531 struct drm_i915_private *dev_priv = dev->dev_private;
6532 struct drm_i915_gem_object *obj;
6533 struct drm_framebuffer *fb;
6534
6535 if (dev_priv->fbdev == NULL)
6536 return NULL;
6537
6538 obj = dev_priv->fbdev->ifb.obj;
6539 if (obj == NULL)
6540 return NULL;
6541
6542 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006543 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6544 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006545 return NULL;
6546
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006547 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006548 return NULL;
6549
6550 return fb;
6551}
6552
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006553bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006554 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006555 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006556{
6557 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006558 struct intel_encoder *intel_encoder =
6559 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006560 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006561 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006562 struct drm_crtc *crtc = NULL;
6563 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006564 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006565 int i = -1;
6566
Chris Wilsond2dff872011-04-19 08:36:26 +01006567 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6568 connector->base.id, drm_get_connector_name(connector),
6569 encoder->base.id, drm_get_encoder_name(encoder));
6570
Jesse Barnes79e53942008-11-07 14:24:08 -08006571 /*
6572 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006573 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006574 * - if the connector already has an assigned crtc, use it (but make
6575 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006576 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006577 * - try to find the first unused crtc that can drive this connector,
6578 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006579 */
6580
6581 /* See if we already have a CRTC for this connector */
6582 if (encoder->crtc) {
6583 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006584
Daniel Vetter7b240562012-12-12 00:35:33 +01006585 mutex_lock(&crtc->mutex);
6586
Daniel Vetter24218aa2012-08-12 19:27:11 +02006587 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006588 old->load_detect_temp = false;
6589
6590 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006591 if (connector->dpms != DRM_MODE_DPMS_ON)
6592 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006593
Chris Wilson71731882011-04-19 23:10:58 +01006594 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006595 }
6596
6597 /* Find an unused one (if possible) */
6598 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6599 i++;
6600 if (!(encoder->possible_crtcs & (1 << i)))
6601 continue;
6602 if (!possible_crtc->enabled) {
6603 crtc = possible_crtc;
6604 break;
6605 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006606 }
6607
6608 /*
6609 * If we didn't find an unused CRTC, don't use any.
6610 */
6611 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006612 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6613 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006614 }
6615
Daniel Vetter7b240562012-12-12 00:35:33 +01006616 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02006617 intel_encoder->new_crtc = to_intel_crtc(crtc);
6618 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006619
6620 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006621 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006622 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006623 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006624
Chris Wilson64927112011-04-20 07:25:26 +01006625 if (!mode)
6626 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006627
Chris Wilsond2dff872011-04-19 08:36:26 +01006628 /* We need a framebuffer large enough to accommodate all accesses
6629 * that the plane may generate whilst we perform load detection.
6630 * We can not rely on the fbcon either being present (we get called
6631 * during its initialisation to detect all boot displays, or it may
6632 * not even exist) or that it is large enough to satisfy the
6633 * requested mode.
6634 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006635 fb = mode_fits_in_fbdev(dev, mode);
6636 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006637 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006638 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6639 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006640 } else
6641 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006642 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006643 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01006644 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006645 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006646 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006647
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006648 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006649 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006650 if (old->release_fb)
6651 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01006652 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006653 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006654 }
Chris Wilson71731882011-04-19 23:10:58 +01006655
Jesse Barnes79e53942008-11-07 14:24:08 -08006656 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006657 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006658 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006659}
6660
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006661void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006662 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006663{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006664 struct intel_encoder *intel_encoder =
6665 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006666 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01006667 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08006668
Chris Wilsond2dff872011-04-19 08:36:26 +01006669 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6670 connector->base.id, drm_get_connector_name(connector),
6671 encoder->base.id, drm_get_encoder_name(encoder));
6672
Chris Wilson8261b192011-04-19 23:18:09 +01006673 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006674 to_intel_connector(connector)->new_encoder = NULL;
6675 intel_encoder->new_crtc = NULL;
6676 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006677
Daniel Vetter36206362012-12-10 20:42:17 +01006678 if (old->release_fb) {
6679 drm_framebuffer_unregister_private(old->release_fb);
6680 drm_framebuffer_unreference(old->release_fb);
6681 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006682
Daniel Vetter67c96402013-01-23 16:25:09 +00006683 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01006684 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006685 }
6686
Eric Anholtc751ce42010-03-25 11:48:48 -07006687 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006688 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6689 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01006690
6691 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08006692}
6693
6694/* Returns the clock of the currently programmed mode of the given pipe. */
6695static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6696{
6697 struct drm_i915_private *dev_priv = dev->dev_private;
6698 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6699 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006700 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006701 u32 fp;
6702 intel_clock_t clock;
6703
6704 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006705 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006706 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006707 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006708
6709 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006710 if (IS_PINEVIEW(dev)) {
6711 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6712 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006713 } else {
6714 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6715 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6716 }
6717
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006718 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006719 if (IS_PINEVIEW(dev))
6720 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6721 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006722 else
6723 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006724 DPLL_FPA01_P1_POST_DIV_SHIFT);
6725
6726 switch (dpll & DPLL_MODE_MASK) {
6727 case DPLLB_MODE_DAC_SERIAL:
6728 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6729 5 : 10;
6730 break;
6731 case DPLLB_MODE_LVDS:
6732 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6733 7 : 14;
6734 break;
6735 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006736 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006737 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6738 return 0;
6739 }
6740
6741 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08006742 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006743 } else {
6744 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6745
6746 if (is_lvds) {
6747 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6748 DPLL_FPA01_P1_POST_DIV_SHIFT);
6749 clock.p2 = 14;
6750
6751 if ((dpll & PLL_REF_INPUT_MASK) ==
6752 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6753 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08006754 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006755 } else
Shaohua Li21778322009-02-23 15:19:16 +08006756 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006757 } else {
6758 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6759 clock.p1 = 2;
6760 else {
6761 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6762 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6763 }
6764 if (dpll & PLL_P2_DIVIDE_BY_4)
6765 clock.p2 = 4;
6766 else
6767 clock.p2 = 2;
6768
Shaohua Li21778322009-02-23 15:19:16 +08006769 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006770 }
6771 }
6772
6773 /* XXX: It would be nice to validate the clocks, but we can't reuse
6774 * i830PllIsValid() because it relies on the xf86_config connector
6775 * configuration being accurate, which it isn't necessarily.
6776 */
6777
6778 return clock.dot;
6779}
6780
6781/** Returns the currently programmed mode of the given pipe. */
6782struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6783 struct drm_crtc *crtc)
6784{
Jesse Barnes548f2452011-02-17 10:40:53 -08006785 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006787 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006788 struct drm_display_mode *mode;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006789 int htot = I915_READ(HTOTAL(cpu_transcoder));
6790 int hsync = I915_READ(HSYNC(cpu_transcoder));
6791 int vtot = I915_READ(VTOTAL(cpu_transcoder));
6792 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08006793
6794 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6795 if (!mode)
6796 return NULL;
6797
6798 mode->clock = intel_crtc_clock_get(dev, crtc);
6799 mode->hdisplay = (htot & 0xffff) + 1;
6800 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6801 mode->hsync_start = (hsync & 0xffff) + 1;
6802 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6803 mode->vdisplay = (vtot & 0xffff) + 1;
6804 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6805 mode->vsync_start = (vsync & 0xffff) + 1;
6806 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6807
6808 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006809
6810 return mode;
6811}
6812
Daniel Vetter3dec0092010-08-20 21:40:52 +02006813static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07006814{
6815 struct drm_device *dev = crtc->dev;
6816 drm_i915_private_t *dev_priv = dev->dev_private;
6817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6818 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006819 int dpll_reg = DPLL(pipe);
6820 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07006821
Eric Anholtbad720f2009-10-22 16:11:14 -07006822 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006823 return;
6824
6825 if (!dev_priv->lvds_downclock_avail)
6826 return;
6827
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006828 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006829 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08006830 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006831
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006832 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006833
6834 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6835 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006836 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006837
Jesse Barnes652c3932009-08-17 13:31:43 -07006838 dpll = I915_READ(dpll_reg);
6839 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08006840 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006841 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006842}
6843
6844static void intel_decrease_pllclock(struct drm_crtc *crtc)
6845{
6846 struct drm_device *dev = crtc->dev;
6847 drm_i915_private_t *dev_priv = dev->dev_private;
6848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006849
Eric Anholtbad720f2009-10-22 16:11:14 -07006850 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006851 return;
6852
6853 if (!dev_priv->lvds_downclock_avail)
6854 return;
6855
6856 /*
6857 * Since this is called by a timer, we should never get here in
6858 * the manual case.
6859 */
6860 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01006861 int pipe = intel_crtc->pipe;
6862 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02006863 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01006864
Zhao Yakui44d98a62009-10-09 11:39:40 +08006865 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006866
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006867 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006868
Chris Wilson074b5e12012-05-02 12:07:06 +01006869 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006870 dpll |= DISPLAY_RATE_SELECT_FPA1;
6871 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006872 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006873 dpll = I915_READ(dpll_reg);
6874 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08006875 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006876 }
6877
6878}
6879
Chris Wilsonf047e392012-07-21 12:31:41 +01006880void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006881{
Chris Wilsonf047e392012-07-21 12:31:41 +01006882 i915_update_gfx_val(dev->dev_private);
6883}
6884
6885void intel_mark_idle(struct drm_device *dev)
6886{
Chris Wilson725a5b52013-01-08 11:02:57 +00006887 struct drm_crtc *crtc;
6888
6889 if (!i915_powersave)
6890 return;
6891
6892 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6893 if (!crtc->fb)
6894 continue;
6895
6896 intel_decrease_pllclock(crtc);
6897 }
Chris Wilsonf047e392012-07-21 12:31:41 +01006898}
6899
6900void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
6901{
6902 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07006903 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006904
6905 if (!i915_powersave)
6906 return;
6907
Jesse Barnes652c3932009-08-17 13:31:43 -07006908 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006909 if (!crtc->fb)
6910 continue;
6911
Chris Wilsonf047e392012-07-21 12:31:41 +01006912 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6913 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006914 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006915}
6916
Jesse Barnes79e53942008-11-07 14:24:08 -08006917static void intel_crtc_destroy(struct drm_crtc *crtc)
6918{
6919 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006920 struct drm_device *dev = crtc->dev;
6921 struct intel_unpin_work *work;
6922 unsigned long flags;
6923
6924 spin_lock_irqsave(&dev->event_lock, flags);
6925 work = intel_crtc->unpin_work;
6926 intel_crtc->unpin_work = NULL;
6927 spin_unlock_irqrestore(&dev->event_lock, flags);
6928
6929 if (work) {
6930 cancel_work_sync(&work->work);
6931 kfree(work);
6932 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006933
6934 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006935
Jesse Barnes79e53942008-11-07 14:24:08 -08006936 kfree(intel_crtc);
6937}
6938
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006939static void intel_unpin_work_fn(struct work_struct *__work)
6940{
6941 struct intel_unpin_work *work =
6942 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006943 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006944
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006945 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01006946 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00006947 drm_gem_object_unreference(&work->pending_flip_obj->base);
6948 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00006949
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006950 intel_update_fbc(dev);
6951 mutex_unlock(&dev->struct_mutex);
6952
6953 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
6954 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
6955
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006956 kfree(work);
6957}
6958
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006959static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01006960 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006961{
6962 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006963 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6964 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00006965 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006966 unsigned long flags;
6967
6968 /* Ignore early vblank irqs */
6969 if (intel_crtc == NULL)
6970 return;
6971
6972 spin_lock_irqsave(&dev->event_lock, flags);
6973 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00006974
6975 /* Ensure we don't miss a work->pending update ... */
6976 smp_rmb();
6977
6978 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006979 spin_unlock_irqrestore(&dev->event_lock, flags);
6980 return;
6981 }
6982
Chris Wilsone7d841c2012-12-03 11:36:30 +00006983 /* and that the unpin work is consistent wrt ->pending. */
6984 smp_rmb();
6985
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006986 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006987
Rob Clark45a066e2012-10-08 14:50:40 -05006988 if (work->event)
6989 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006990
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01006991 drm_vblank_put(dev, intel_crtc->pipe);
6992
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006993 spin_unlock_irqrestore(&dev->event_lock, flags);
6994
Chris Wilson05394f32010-11-08 19:18:58 +00006995 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00006996
Daniel Vetter2c10d572012-12-20 21:24:07 +01006997 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006998
6999 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007000
7001 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007002}
7003
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007004void intel_finish_page_flip(struct drm_device *dev, int pipe)
7005{
7006 drm_i915_private_t *dev_priv = dev->dev_private;
7007 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7008
Mario Kleiner49b14a52010-12-09 07:00:07 +01007009 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007010}
7011
7012void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7013{
7014 drm_i915_private_t *dev_priv = dev->dev_private;
7015 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7016
Mario Kleiner49b14a52010-12-09 07:00:07 +01007017 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007018}
7019
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007020void intel_prepare_page_flip(struct drm_device *dev, int plane)
7021{
7022 drm_i915_private_t *dev_priv = dev->dev_private;
7023 struct intel_crtc *intel_crtc =
7024 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7025 unsigned long flags;
7026
Chris Wilsone7d841c2012-12-03 11:36:30 +00007027 /* NB: An MMIO update of the plane base pointer will also
7028 * generate a page-flip completion irq, i.e. every modeset
7029 * is also accompanied by a spurious intel_prepare_page_flip().
7030 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007031 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007032 if (intel_crtc->unpin_work)
7033 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007034 spin_unlock_irqrestore(&dev->event_lock, flags);
7035}
7036
Chris Wilsone7d841c2012-12-03 11:36:30 +00007037inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7038{
7039 /* Ensure that the work item is consistent when activating it ... */
7040 smp_wmb();
7041 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7042 /* and that it is marked active as soon as the irq could fire. */
7043 smp_wmb();
7044}
7045
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007046static int intel_gen2_queue_flip(struct drm_device *dev,
7047 struct drm_crtc *crtc,
7048 struct drm_framebuffer *fb,
7049 struct drm_i915_gem_object *obj)
7050{
7051 struct drm_i915_private *dev_priv = dev->dev_private;
7052 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007053 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007054 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007055 int ret;
7056
Daniel Vetter6d90c952012-04-26 23:28:05 +02007057 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007058 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007059 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007060
Daniel Vetter6d90c952012-04-26 23:28:05 +02007061 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007062 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007063 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007064
7065 /* Can't queue multiple flips, so wait for the previous
7066 * one to finish before executing the next.
7067 */
7068 if (intel_crtc->plane)
7069 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7070 else
7071 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007072 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7073 intel_ring_emit(ring, MI_NOOP);
7074 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7075 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7076 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007077 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007078 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007079
7080 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007081 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007082 return 0;
7083
7084err_unpin:
7085 intel_unpin_fb_obj(obj);
7086err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007087 return ret;
7088}
7089
7090static int intel_gen3_queue_flip(struct drm_device *dev,
7091 struct drm_crtc *crtc,
7092 struct drm_framebuffer *fb,
7093 struct drm_i915_gem_object *obj)
7094{
7095 struct drm_i915_private *dev_priv = dev->dev_private;
7096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007097 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007098 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007099 int ret;
7100
Daniel Vetter6d90c952012-04-26 23:28:05 +02007101 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007102 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007103 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007104
Daniel Vetter6d90c952012-04-26 23:28:05 +02007105 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007106 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007107 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007108
7109 if (intel_crtc->plane)
7110 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7111 else
7112 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007113 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7114 intel_ring_emit(ring, MI_NOOP);
7115 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7116 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7117 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007118 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007119 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007120
Chris Wilsone7d841c2012-12-03 11:36:30 +00007121 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007122 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007123 return 0;
7124
7125err_unpin:
7126 intel_unpin_fb_obj(obj);
7127err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007128 return ret;
7129}
7130
7131static int intel_gen4_queue_flip(struct drm_device *dev,
7132 struct drm_crtc *crtc,
7133 struct drm_framebuffer *fb,
7134 struct drm_i915_gem_object *obj)
7135{
7136 struct drm_i915_private *dev_priv = dev->dev_private;
7137 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7138 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007139 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007140 int ret;
7141
Daniel Vetter6d90c952012-04-26 23:28:05 +02007142 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007143 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007144 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007145
Daniel Vetter6d90c952012-04-26 23:28:05 +02007146 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007147 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007148 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007149
7150 /* i965+ uses the linear or tiled offsets from the
7151 * Display Registers (which do not change across a page-flip)
7152 * so we need only reprogram the base address.
7153 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007154 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7155 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7156 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007157 intel_ring_emit(ring,
7158 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
7159 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007160
7161 /* XXX Enabling the panel-fitter across page-flip is so far
7162 * untested on non-native modes, so ignore it for now.
7163 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7164 */
7165 pf = 0;
7166 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007167 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007168
7169 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007170 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007171 return 0;
7172
7173err_unpin:
7174 intel_unpin_fb_obj(obj);
7175err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007176 return ret;
7177}
7178
7179static int intel_gen6_queue_flip(struct drm_device *dev,
7180 struct drm_crtc *crtc,
7181 struct drm_framebuffer *fb,
7182 struct drm_i915_gem_object *obj)
7183{
7184 struct drm_i915_private *dev_priv = dev->dev_private;
7185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007186 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007187 uint32_t pf, pipesrc;
7188 int ret;
7189
Daniel Vetter6d90c952012-04-26 23:28:05 +02007190 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007191 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007192 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007193
Daniel Vetter6d90c952012-04-26 23:28:05 +02007194 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007195 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007196 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007197
Daniel Vetter6d90c952012-04-26 23:28:05 +02007198 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7199 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7200 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007201 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007202
Chris Wilson99d9acd2012-04-17 20:37:00 +01007203 /* Contrary to the suggestions in the documentation,
7204 * "Enable Panel Fitter" does not seem to be required when page
7205 * flipping with a non-native mode, and worse causes a normal
7206 * modeset to fail.
7207 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7208 */
7209 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007210 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007211 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007212
7213 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007214 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007215 return 0;
7216
7217err_unpin:
7218 intel_unpin_fb_obj(obj);
7219err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007220 return ret;
7221}
7222
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007223/*
7224 * On gen7 we currently use the blit ring because (in early silicon at least)
7225 * the render ring doesn't give us interrpts for page flip completion, which
7226 * means clients will hang after the first flip is queued. Fortunately the
7227 * blit ring generates interrupts properly, so use it instead.
7228 */
7229static int intel_gen7_queue_flip(struct drm_device *dev,
7230 struct drm_crtc *crtc,
7231 struct drm_framebuffer *fb,
7232 struct drm_i915_gem_object *obj)
7233{
7234 struct drm_i915_private *dev_priv = dev->dev_private;
7235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7236 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007237 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007238 int ret;
7239
7240 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7241 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007242 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007243
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007244 switch(intel_crtc->plane) {
7245 case PLANE_A:
7246 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7247 break;
7248 case PLANE_B:
7249 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7250 break;
7251 case PLANE_C:
7252 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7253 break;
7254 default:
7255 WARN_ONCE(1, "unknown plane in flip command\n");
7256 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007257 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007258 }
7259
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007260 ret = intel_ring_begin(ring, 4);
7261 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007262 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007263
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007264 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007265 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Daniel Vetterc2c75132012-07-05 12:17:30 +02007266 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007267 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007268
7269 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007270 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007271 return 0;
7272
7273err_unpin:
7274 intel_unpin_fb_obj(obj);
7275err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007276 return ret;
7277}
7278
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007279static int intel_default_queue_flip(struct drm_device *dev,
7280 struct drm_crtc *crtc,
7281 struct drm_framebuffer *fb,
7282 struct drm_i915_gem_object *obj)
7283{
7284 return -ENODEV;
7285}
7286
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007287static int intel_crtc_page_flip(struct drm_crtc *crtc,
7288 struct drm_framebuffer *fb,
7289 struct drm_pending_vblank_event *event)
7290{
7291 struct drm_device *dev = crtc->dev;
7292 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007293 struct drm_framebuffer *old_fb = crtc->fb;
7294 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7296 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007297 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007298 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007299
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007300 /* Can't change pixel format via MI display flips. */
7301 if (fb->pixel_format != crtc->fb->pixel_format)
7302 return -EINVAL;
7303
7304 /*
7305 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7306 * Note that pitch changes could also affect these register.
7307 */
7308 if (INTEL_INFO(dev)->gen > 3 &&
7309 (fb->offsets[0] != crtc->fb->offsets[0] ||
7310 fb->pitches[0] != crtc->fb->pitches[0]))
7311 return -EINVAL;
7312
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007313 work = kzalloc(sizeof *work, GFP_KERNEL);
7314 if (work == NULL)
7315 return -ENOMEM;
7316
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007317 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007318 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007319 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007320 INIT_WORK(&work->work, intel_unpin_work_fn);
7321
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007322 ret = drm_vblank_get(dev, intel_crtc->pipe);
7323 if (ret)
7324 goto free_work;
7325
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007326 /* We borrow the event spin lock for protecting unpin_work */
7327 spin_lock_irqsave(&dev->event_lock, flags);
7328 if (intel_crtc->unpin_work) {
7329 spin_unlock_irqrestore(&dev->event_lock, flags);
7330 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007331 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007332
7333 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007334 return -EBUSY;
7335 }
7336 intel_crtc->unpin_work = work;
7337 spin_unlock_irqrestore(&dev->event_lock, flags);
7338
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007339 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7340 flush_workqueue(dev_priv->wq);
7341
Chris Wilson79158102012-05-23 11:13:58 +01007342 ret = i915_mutex_lock_interruptible(dev);
7343 if (ret)
7344 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007345
Jesse Barnes75dfca82010-02-10 15:09:44 -08007346 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007347 drm_gem_object_reference(&work->old_fb_obj->base);
7348 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007349
7350 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007351
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007352 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007353
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007354 work->enable_stall_check = true;
7355
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007356 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02007357 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007358
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007359 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7360 if (ret)
7361 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007362
Chris Wilson7782de32011-07-08 12:22:41 +01007363 intel_disable_fbc(dev);
Chris Wilsonf047e392012-07-21 12:31:41 +01007364 intel_mark_fb_busy(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007365 mutex_unlock(&dev->struct_mutex);
7366
Jesse Barnese5510fa2010-07-01 16:48:37 -07007367 trace_i915_flip_request(intel_crtc->plane, obj);
7368
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007369 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007370
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007371cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007372 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007373 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007374 drm_gem_object_unreference(&work->old_fb_obj->base);
7375 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007376 mutex_unlock(&dev->struct_mutex);
7377
Chris Wilson79158102012-05-23 11:13:58 +01007378cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007379 spin_lock_irqsave(&dev->event_lock, flags);
7380 intel_crtc->unpin_work = NULL;
7381 spin_unlock_irqrestore(&dev->event_lock, flags);
7382
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007383 drm_vblank_put(dev, intel_crtc->pipe);
7384free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007385 kfree(work);
7386
7387 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007388}
7389
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007390static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007391 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7392 .load_lut = intel_crtc_load_lut,
Daniel Vetter976f8a22012-07-08 22:34:21 +02007393 .disable = intel_crtc_noop,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007394};
7395
Daniel Vetter6ed0f792012-07-08 19:41:43 +02007396bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
7397{
7398 struct intel_encoder *other_encoder;
7399 struct drm_crtc *crtc = &encoder->new_crtc->base;
7400
7401 if (WARN_ON(!crtc))
7402 return false;
7403
7404 list_for_each_entry(other_encoder,
7405 &crtc->dev->mode_config.encoder_list,
7406 base.head) {
7407
7408 if (&other_encoder->new_crtc->base != crtc ||
7409 encoder == other_encoder)
7410 continue;
7411 else
7412 return true;
7413 }
7414
7415 return false;
7416}
7417
Daniel Vetter50f56112012-07-02 09:35:43 +02007418static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7419 struct drm_crtc *crtc)
7420{
7421 struct drm_device *dev;
7422 struct drm_crtc *tmp;
7423 int crtc_mask = 1;
7424
7425 WARN(!crtc, "checking null crtc?\n");
7426
7427 dev = crtc->dev;
7428
7429 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7430 if (tmp == crtc)
7431 break;
7432 crtc_mask <<= 1;
7433 }
7434
7435 if (encoder->possible_crtcs & crtc_mask)
7436 return true;
7437 return false;
7438}
7439
Daniel Vetter9a935852012-07-05 22:34:27 +02007440/**
7441 * intel_modeset_update_staged_output_state
7442 *
7443 * Updates the staged output configuration state, e.g. after we've read out the
7444 * current hw state.
7445 */
7446static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7447{
7448 struct intel_encoder *encoder;
7449 struct intel_connector *connector;
7450
7451 list_for_each_entry(connector, &dev->mode_config.connector_list,
7452 base.head) {
7453 connector->new_encoder =
7454 to_intel_encoder(connector->base.encoder);
7455 }
7456
7457 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7458 base.head) {
7459 encoder->new_crtc =
7460 to_intel_crtc(encoder->base.crtc);
7461 }
7462}
7463
7464/**
7465 * intel_modeset_commit_output_state
7466 *
7467 * This function copies the stage display pipe configuration to the real one.
7468 */
7469static void intel_modeset_commit_output_state(struct drm_device *dev)
7470{
7471 struct intel_encoder *encoder;
7472 struct intel_connector *connector;
7473
7474 list_for_each_entry(connector, &dev->mode_config.connector_list,
7475 base.head) {
7476 connector->base.encoder = &connector->new_encoder->base;
7477 }
7478
7479 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7480 base.head) {
7481 encoder->base.crtc = &encoder->new_crtc->base;
7482 }
7483}
7484
Daniel Vetter7758a112012-07-08 19:40:39 +02007485static struct drm_display_mode *
7486intel_modeset_adjusted_mode(struct drm_crtc *crtc,
7487 struct drm_display_mode *mode)
7488{
7489 struct drm_device *dev = crtc->dev;
7490 struct drm_display_mode *adjusted_mode;
7491 struct drm_encoder_helper_funcs *encoder_funcs;
7492 struct intel_encoder *encoder;
7493
7494 adjusted_mode = drm_mode_duplicate(dev, mode);
7495 if (!adjusted_mode)
7496 return ERR_PTR(-ENOMEM);
7497
7498 /* Pass our mode to the connectors and the CRTC to give them a chance to
7499 * adjust it according to limitations or connector properties, and also
7500 * a chance to reject the mode entirely.
7501 */
7502 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7503 base.head) {
7504
7505 if (&encoder->new_crtc->base != crtc)
7506 continue;
7507 encoder_funcs = encoder->base.helper_private;
7508 if (!(encoder_funcs->mode_fixup(&encoder->base, mode,
7509 adjusted_mode))) {
7510 DRM_DEBUG_KMS("Encoder fixup failed\n");
7511 goto fail;
7512 }
7513 }
7514
7515 if (!(intel_crtc_mode_fixup(crtc, mode, adjusted_mode))) {
7516 DRM_DEBUG_KMS("CRTC fixup failed\n");
7517 goto fail;
7518 }
7519 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
7520
7521 return adjusted_mode;
7522fail:
7523 drm_mode_destroy(dev, adjusted_mode);
7524 return ERR_PTR(-EINVAL);
7525}
7526
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007527/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7528 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7529static void
7530intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7531 unsigned *prepare_pipes, unsigned *disable_pipes)
7532{
7533 struct intel_crtc *intel_crtc;
7534 struct drm_device *dev = crtc->dev;
7535 struct intel_encoder *encoder;
7536 struct intel_connector *connector;
7537 struct drm_crtc *tmp_crtc;
7538
7539 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7540
7541 /* Check which crtcs have changed outputs connected to them, these need
7542 * to be part of the prepare_pipes mask. We don't (yet) support global
7543 * modeset across multiple crtcs, so modeset_pipes will only have one
7544 * bit set at most. */
7545 list_for_each_entry(connector, &dev->mode_config.connector_list,
7546 base.head) {
7547 if (connector->base.encoder == &connector->new_encoder->base)
7548 continue;
7549
7550 if (connector->base.encoder) {
7551 tmp_crtc = connector->base.encoder->crtc;
7552
7553 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7554 }
7555
7556 if (connector->new_encoder)
7557 *prepare_pipes |=
7558 1 << connector->new_encoder->new_crtc->pipe;
7559 }
7560
7561 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7562 base.head) {
7563 if (encoder->base.crtc == &encoder->new_crtc->base)
7564 continue;
7565
7566 if (encoder->base.crtc) {
7567 tmp_crtc = encoder->base.crtc;
7568
7569 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7570 }
7571
7572 if (encoder->new_crtc)
7573 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7574 }
7575
7576 /* Check for any pipes that will be fully disabled ... */
7577 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7578 base.head) {
7579 bool used = false;
7580
7581 /* Don't try to disable disabled crtcs. */
7582 if (!intel_crtc->base.enabled)
7583 continue;
7584
7585 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7586 base.head) {
7587 if (encoder->new_crtc == intel_crtc)
7588 used = true;
7589 }
7590
7591 if (!used)
7592 *disable_pipes |= 1 << intel_crtc->pipe;
7593 }
7594
7595
7596 /* set_mode is also used to update properties on life display pipes. */
7597 intel_crtc = to_intel_crtc(crtc);
7598 if (crtc->enabled)
7599 *prepare_pipes |= 1 << intel_crtc->pipe;
7600
7601 /* We only support modeset on one single crtc, hence we need to do that
7602 * only for the passed in crtc iff we change anything else than just
7603 * disable crtcs.
7604 *
7605 * This is actually not true, to be fully compatible with the old crtc
7606 * helper we automatically disable _any_ output (i.e. doesn't need to be
7607 * connected to the crtc we're modesetting on) if it's disconnected.
7608 * Which is a rather nutty api (since changed the output configuration
7609 * without userspace's explicit request can lead to confusion), but
7610 * alas. Hence we currently need to modeset on all pipes we prepare. */
7611 if (*prepare_pipes)
7612 *modeset_pipes = *prepare_pipes;
7613
7614 /* ... and mask these out. */
7615 *modeset_pipes &= ~(*disable_pipes);
7616 *prepare_pipes &= ~(*disable_pipes);
7617}
7618
Daniel Vetterea9d7582012-07-10 10:42:52 +02007619static bool intel_crtc_in_use(struct drm_crtc *crtc)
7620{
7621 struct drm_encoder *encoder;
7622 struct drm_device *dev = crtc->dev;
7623
7624 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
7625 if (encoder->crtc == crtc)
7626 return true;
7627
7628 return false;
7629}
7630
7631static void
7632intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
7633{
7634 struct intel_encoder *intel_encoder;
7635 struct intel_crtc *intel_crtc;
7636 struct drm_connector *connector;
7637
7638 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
7639 base.head) {
7640 if (!intel_encoder->base.crtc)
7641 continue;
7642
7643 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
7644
7645 if (prepare_pipes & (1 << intel_crtc->pipe))
7646 intel_encoder->connectors_active = false;
7647 }
7648
7649 intel_modeset_commit_output_state(dev);
7650
7651 /* Update computed state. */
7652 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7653 base.head) {
7654 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
7655 }
7656
7657 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7658 if (!connector->encoder || !connector->encoder->crtc)
7659 continue;
7660
7661 intel_crtc = to_intel_crtc(connector->encoder->crtc);
7662
7663 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02007664 struct drm_property *dpms_property =
7665 dev->mode_config.dpms_property;
7666
Daniel Vetterea9d7582012-07-10 10:42:52 +02007667 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05007668 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02007669 dpms_property,
7670 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02007671
7672 intel_encoder = to_intel_encoder(connector->encoder);
7673 intel_encoder->connectors_active = true;
7674 }
7675 }
7676
7677}
7678
Daniel Vetter25c5b262012-07-08 22:08:04 +02007679#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
7680 list_for_each_entry((intel_crtc), \
7681 &(dev)->mode_config.crtc_list, \
7682 base.head) \
7683 if (mask & (1 <<(intel_crtc)->pipe)) \
7684
Daniel Vetterb9805142012-08-31 17:37:33 +02007685void
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007686intel_modeset_check_state(struct drm_device *dev)
7687{
7688 struct intel_crtc *crtc;
7689 struct intel_encoder *encoder;
7690 struct intel_connector *connector;
7691
7692 list_for_each_entry(connector, &dev->mode_config.connector_list,
7693 base.head) {
7694 /* This also checks the encoder/connector hw state with the
7695 * ->get_hw_state callbacks. */
7696 intel_connector_check_state(connector);
7697
7698 WARN(&connector->new_encoder->base != connector->base.encoder,
7699 "connector's staged encoder doesn't match current encoder\n");
7700 }
7701
7702 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7703 base.head) {
7704 bool enabled = false;
7705 bool active = false;
7706 enum pipe pipe, tracked_pipe;
7707
7708 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
7709 encoder->base.base.id,
7710 drm_get_encoder_name(&encoder->base));
7711
7712 WARN(&encoder->new_crtc->base != encoder->base.crtc,
7713 "encoder's stage crtc doesn't match current crtc\n");
7714 WARN(encoder->connectors_active && !encoder->base.crtc,
7715 "encoder's active_connectors set, but no crtc\n");
7716
7717 list_for_each_entry(connector, &dev->mode_config.connector_list,
7718 base.head) {
7719 if (connector->base.encoder != &encoder->base)
7720 continue;
7721 enabled = true;
7722 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
7723 active = true;
7724 }
7725 WARN(!!encoder->base.crtc != enabled,
7726 "encoder's enabled state mismatch "
7727 "(expected %i, found %i)\n",
7728 !!encoder->base.crtc, enabled);
7729 WARN(active && !encoder->base.crtc,
7730 "active encoder with no crtc\n");
7731
7732 WARN(encoder->connectors_active != active,
7733 "encoder's computed active state doesn't match tracked active state "
7734 "(expected %i, found %i)\n", active, encoder->connectors_active);
7735
7736 active = encoder->get_hw_state(encoder, &pipe);
7737 WARN(active != encoder->connectors_active,
7738 "encoder's hw state doesn't match sw tracking "
7739 "(expected %i, found %i)\n",
7740 encoder->connectors_active, active);
7741
7742 if (!encoder->base.crtc)
7743 continue;
7744
7745 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
7746 WARN(active && pipe != tracked_pipe,
7747 "active encoder's pipe doesn't match"
7748 "(expected %i, found %i)\n",
7749 tracked_pipe, pipe);
7750
7751 }
7752
7753 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
7754 base.head) {
7755 bool enabled = false;
7756 bool active = false;
7757
7758 DRM_DEBUG_KMS("[CRTC:%d]\n",
7759 crtc->base.base.id);
7760
7761 WARN(crtc->active && !crtc->base.enabled,
7762 "active crtc, but not enabled in sw tracking\n");
7763
7764 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7765 base.head) {
7766 if (encoder->base.crtc != &crtc->base)
7767 continue;
7768 enabled = true;
7769 if (encoder->connectors_active)
7770 active = true;
7771 }
7772 WARN(active != crtc->active,
7773 "crtc's computed active state doesn't match tracked active state "
7774 "(expected %i, found %i)\n", active, crtc->active);
7775 WARN(enabled != crtc->base.enabled,
7776 "crtc's computed enabled state doesn't match tracked enabled state "
7777 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
7778
7779 assert_pipe(dev->dev_private, crtc->pipe, crtc->active);
7780 }
7781}
7782
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007783int intel_set_mode(struct drm_crtc *crtc,
7784 struct drm_display_mode *mode,
7785 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02007786{
7787 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02007788 drm_i915_private_t *dev_priv = dev->dev_private;
Tim Gardner3ac18232012-12-07 07:54:26 -07007789 struct drm_display_mode *adjusted_mode, *saved_mode, *saved_hwmode;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007790 struct intel_crtc *intel_crtc;
7791 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007792 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02007793
Tim Gardner3ac18232012-12-07 07:54:26 -07007794 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007795 if (!saved_mode)
7796 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07007797 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02007798
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007799 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02007800 &prepare_pipes, &disable_pipes);
7801
7802 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7803 modeset_pipes, prepare_pipes, disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007804
Daniel Vetter976f8a22012-07-08 22:34:21 +02007805 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
7806 intel_crtc_disable(&intel_crtc->base);
7807
Tim Gardner3ac18232012-12-07 07:54:26 -07007808 *saved_hwmode = crtc->hwmode;
7809 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007810
Daniel Vetter25c5b262012-07-08 22:08:04 +02007811 /* Hack: Because we don't (yet) support global modeset on multiple
7812 * crtcs, we don't keep track of the new mode for more than one crtc.
7813 * Hence simply check whether any bit is set in modeset_pipes in all the
7814 * pieces of code that are not yet converted to deal with mutliple crtcs
7815 * changing their mode at the same time. */
7816 adjusted_mode = NULL;
7817 if (modeset_pipes) {
7818 adjusted_mode = intel_modeset_adjusted_mode(crtc, mode);
7819 if (IS_ERR(adjusted_mode)) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007820 ret = PTR_ERR(adjusted_mode);
Tim Gardner3ac18232012-12-07 07:54:26 -07007821 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007822 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007823 }
7824
Daniel Vetterea9d7582012-07-10 10:42:52 +02007825 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
7826 if (intel_crtc->base.enabled)
7827 dev_priv->display.crtc_disable(&intel_crtc->base);
7828 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007829
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02007830 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
7831 * to set it here already despite that we pass it down the callchain.
7832 */
7833 if (modeset_pipes)
Daniel Vetter25c5b262012-07-08 22:08:04 +02007834 crtc->mode = *mode;
Daniel Vetter7758a112012-07-08 19:40:39 +02007835
Daniel Vetterea9d7582012-07-10 10:42:52 +02007836 /* Only after disabling all output pipelines that will be changed can we
7837 * update the the output configuration. */
7838 intel_modeset_update_state(dev, prepare_pipes);
7839
Daniel Vetter47fab732012-10-26 10:58:18 +02007840 if (dev_priv->display.modeset_global_resources)
7841 dev_priv->display.modeset_global_resources(dev);
7842
Daniel Vettera6778b32012-07-02 09:56:42 +02007843 /* Set up the DPLL and any encoders state that needs to adjust or depend
7844 * on the DPLL.
7845 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007846 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007847 ret = intel_crtc_mode_set(&intel_crtc->base,
7848 mode, adjusted_mode,
7849 x, y, fb);
7850 if (ret)
7851 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02007852 }
7853
7854 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007855 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
7856 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02007857
Daniel Vetter25c5b262012-07-08 22:08:04 +02007858 if (modeset_pipes) {
7859 /* Store real post-adjustment hardware mode. */
7860 crtc->hwmode = *adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007861
Daniel Vetter25c5b262012-07-08 22:08:04 +02007862 /* Calculate and store various constants which
7863 * are later needed by vblank and swap-completion
7864 * timestamping. They are derived from true hwmode.
7865 */
7866 drm_calc_timestamping_constants(crtc);
7867 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007868
7869 /* FIXME: add subpixel order */
7870done:
7871 drm_mode_destroy(dev, adjusted_mode);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007872 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07007873 crtc->hwmode = *saved_hwmode;
7874 crtc->mode = *saved_mode;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007875 } else {
7876 intel_modeset_check_state(dev);
Daniel Vettera6778b32012-07-02 09:56:42 +02007877 }
7878
Tim Gardner3ac18232012-12-07 07:54:26 -07007879out:
7880 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02007881 return ret;
7882}
7883
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007884void intel_crtc_restore_mode(struct drm_crtc *crtc)
7885{
7886 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
7887}
7888
Daniel Vetter25c5b262012-07-08 22:08:04 +02007889#undef for_each_intel_crtc_masked
7890
Daniel Vetterd9e55602012-07-04 22:16:09 +02007891static void intel_set_config_free(struct intel_set_config *config)
7892{
7893 if (!config)
7894 return;
7895
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007896 kfree(config->save_connector_encoders);
7897 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02007898 kfree(config);
7899}
7900
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007901static int intel_set_config_save_state(struct drm_device *dev,
7902 struct intel_set_config *config)
7903{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007904 struct drm_encoder *encoder;
7905 struct drm_connector *connector;
7906 int count;
7907
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007908 config->save_encoder_crtcs =
7909 kcalloc(dev->mode_config.num_encoder,
7910 sizeof(struct drm_crtc *), GFP_KERNEL);
7911 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007912 return -ENOMEM;
7913
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007914 config->save_connector_encoders =
7915 kcalloc(dev->mode_config.num_connector,
7916 sizeof(struct drm_encoder *), GFP_KERNEL);
7917 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007918 return -ENOMEM;
7919
7920 /* Copy data. Note that driver private data is not affected.
7921 * Should anything bad happen only the expected state is
7922 * restored, not the drivers personal bookkeeping.
7923 */
7924 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007925 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007926 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007927 }
7928
7929 count = 0;
7930 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007931 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007932 }
7933
7934 return 0;
7935}
7936
7937static void intel_set_config_restore_state(struct drm_device *dev,
7938 struct intel_set_config *config)
7939{
Daniel Vetter9a935852012-07-05 22:34:27 +02007940 struct intel_encoder *encoder;
7941 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007942 int count;
7943
7944 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007945 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7946 encoder->new_crtc =
7947 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007948 }
7949
7950 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007951 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
7952 connector->new_encoder =
7953 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007954 }
7955}
7956
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007957static void
7958intel_set_config_compute_mode_changes(struct drm_mode_set *set,
7959 struct intel_set_config *config)
7960{
7961
7962 /* We should be able to check here if the fb has the same properties
7963 * and then just flip_or_move it */
7964 if (set->crtc->fb != set->fb) {
7965 /* If we have no fb then treat it as a full mode set */
7966 if (set->crtc->fb == NULL) {
7967 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
7968 config->mode_changed = true;
7969 } else if (set->fb == NULL) {
7970 config->mode_changed = true;
7971 } else if (set->fb->depth != set->crtc->fb->depth) {
7972 config->mode_changed = true;
7973 } else if (set->fb->bits_per_pixel !=
7974 set->crtc->fb->bits_per_pixel) {
7975 config->mode_changed = true;
7976 } else
7977 config->fb_changed = true;
7978 }
7979
Daniel Vetter835c5872012-07-10 18:11:08 +02007980 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007981 config->fb_changed = true;
7982
7983 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
7984 DRM_DEBUG_KMS("modes are different, full mode set\n");
7985 drm_mode_debug_printmodeline(&set->crtc->mode);
7986 drm_mode_debug_printmodeline(set->mode);
7987 config->mode_changed = true;
7988 }
7989}
7990
Daniel Vetter2e431052012-07-04 22:42:15 +02007991static int
Daniel Vetter9a935852012-07-05 22:34:27 +02007992intel_modeset_stage_output_state(struct drm_device *dev,
7993 struct drm_mode_set *set,
7994 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02007995{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007996 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02007997 struct intel_connector *connector;
7998 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02007999 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02008000
Damien Lespiau9abdda72013-02-13 13:29:23 +00008001 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02008002 * of connectors. For paranoia, double-check this. */
8003 WARN_ON(!set->fb && (set->num_connectors != 0));
8004 WARN_ON(set->fb && (set->num_connectors == 0));
8005
Daniel Vetter50f56112012-07-02 09:35:43 +02008006 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008007 list_for_each_entry(connector, &dev->mode_config.connector_list,
8008 base.head) {
8009 /* Otherwise traverse passed in connector list and get encoders
8010 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008011 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008012 if (set->connectors[ro] == &connector->base) {
8013 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02008014 break;
8015 }
8016 }
8017
Daniel Vetter9a935852012-07-05 22:34:27 +02008018 /* If we disable the crtc, disable all its connectors. Also, if
8019 * the connector is on the changing crtc but not on the new
8020 * connector list, disable it. */
8021 if ((!set->fb || ro == set->num_connectors) &&
8022 connector->base.encoder &&
8023 connector->base.encoder->crtc == set->crtc) {
8024 connector->new_encoder = NULL;
8025
8026 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8027 connector->base.base.id,
8028 drm_get_connector_name(&connector->base));
8029 }
8030
8031
8032 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008033 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008034 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008035 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008036 }
8037 /* connector->new_encoder is now updated for all connectors. */
8038
8039 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008040 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008041 list_for_each_entry(connector, &dev->mode_config.connector_list,
8042 base.head) {
8043 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02008044 continue;
8045
Daniel Vetter9a935852012-07-05 22:34:27 +02008046 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02008047
8048 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008049 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02008050 new_crtc = set->crtc;
8051 }
8052
8053 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02008054 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8055 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008056 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02008057 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008058 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8059
8060 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8061 connector->base.base.id,
8062 drm_get_connector_name(&connector->base),
8063 new_crtc->base.id);
8064 }
8065
8066 /* Check for any encoders that needs to be disabled. */
8067 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8068 base.head) {
8069 list_for_each_entry(connector,
8070 &dev->mode_config.connector_list,
8071 base.head) {
8072 if (connector->new_encoder == encoder) {
8073 WARN_ON(!connector->new_encoder->new_crtc);
8074
8075 goto next_encoder;
8076 }
8077 }
8078 encoder->new_crtc = NULL;
8079next_encoder:
8080 /* Only now check for crtc changes so we don't miss encoders
8081 * that will be disabled. */
8082 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008083 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008084 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008085 }
8086 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008087 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008088
Daniel Vetter2e431052012-07-04 22:42:15 +02008089 return 0;
8090}
8091
8092static int intel_crtc_set_config(struct drm_mode_set *set)
8093{
8094 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02008095 struct drm_mode_set save_set;
8096 struct intel_set_config *config;
8097 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02008098
Daniel Vetter8d3e3752012-07-05 16:09:09 +02008099 BUG_ON(!set);
8100 BUG_ON(!set->crtc);
8101 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02008102
8103 if (!set->mode)
8104 set->fb = NULL;
8105
Daniel Vetter431e50f2012-07-10 17:53:42 +02008106 /* The fb helper likes to play gross jokes with ->mode_set_config.
8107 * Unfortunately the crtc helper doesn't do much at all for this case,
8108 * so we have to cope with this madness until the fb helper is fixed up. */
8109 if (set->fb && set->num_connectors == 0)
8110 return 0;
8111
Daniel Vetter2e431052012-07-04 22:42:15 +02008112 if (set->fb) {
8113 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8114 set->crtc->base.id, set->fb->base.id,
8115 (int)set->num_connectors, set->x, set->y);
8116 } else {
8117 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02008118 }
8119
8120 dev = set->crtc->dev;
8121
8122 ret = -ENOMEM;
8123 config = kzalloc(sizeof(*config), GFP_KERNEL);
8124 if (!config)
8125 goto out_config;
8126
8127 ret = intel_set_config_save_state(dev, config);
8128 if (ret)
8129 goto out_config;
8130
8131 save_set.crtc = set->crtc;
8132 save_set.mode = &set->crtc->mode;
8133 save_set.x = set->crtc->x;
8134 save_set.y = set->crtc->y;
8135 save_set.fb = set->crtc->fb;
8136
8137 /* Compute whether we need a full modeset, only an fb base update or no
8138 * change at all. In the future we might also check whether only the
8139 * mode changed, e.g. for LVDS where we only change the panel fitter in
8140 * such cases. */
8141 intel_set_config_compute_mode_changes(set, config);
8142
Daniel Vetter9a935852012-07-05 22:34:27 +02008143 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02008144 if (ret)
8145 goto fail;
8146
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008147 if (config->mode_changed) {
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008148 if (set->mode) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008149 DRM_DEBUG_KMS("attempting to set mode from"
8150 " userspace\n");
8151 drm_mode_debug_printmodeline(set->mode);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008152 }
8153
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008154 ret = intel_set_mode(set->crtc, set->mode,
8155 set->x, set->y, set->fb);
8156 if (ret) {
8157 DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
8158 set->crtc->base.id, ret);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008159 goto fail;
8160 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008161 } else if (config->fb_changed) {
Daniel Vetter4f660f42012-07-02 09:47:37 +02008162 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008163 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008164 }
8165
Daniel Vetterd9e55602012-07-04 22:16:09 +02008166 intel_set_config_free(config);
8167
Daniel Vetter50f56112012-07-02 09:35:43 +02008168 return 0;
8169
8170fail:
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008171 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008172
8173 /* Try to restore the config */
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008174 if (config->mode_changed &&
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008175 intel_set_mode(save_set.crtc, save_set.mode,
8176 save_set.x, save_set.y, save_set.fb))
Daniel Vetter50f56112012-07-02 09:35:43 +02008177 DRM_ERROR("failed to restore config after modeset failure\n");
8178
Daniel Vetterd9e55602012-07-04 22:16:09 +02008179out_config:
8180 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008181 return ret;
8182}
8183
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008184static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008185 .cursor_set = intel_crtc_cursor_set,
8186 .cursor_move = intel_crtc_cursor_move,
8187 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008188 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008189 .destroy = intel_crtc_destroy,
8190 .page_flip = intel_crtc_page_flip,
8191};
8192
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008193static void intel_cpu_pll_init(struct drm_device *dev)
8194{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008195 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008196 intel_ddi_pll_init(dev);
8197}
8198
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008199static void intel_pch_pll_init(struct drm_device *dev)
8200{
8201 drm_i915_private_t *dev_priv = dev->dev_private;
8202 int i;
8203
8204 if (dev_priv->num_pch_pll == 0) {
8205 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
8206 return;
8207 }
8208
8209 for (i = 0; i < dev_priv->num_pch_pll; i++) {
8210 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
8211 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
8212 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
8213 }
8214}
8215
Hannes Ederb358d0a2008-12-18 21:18:47 +01008216static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08008217{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008218 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008219 struct intel_crtc *intel_crtc;
8220 int i;
8221
8222 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8223 if (intel_crtc == NULL)
8224 return;
8225
8226 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8227
8228 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08008229 for (i = 0; i < 256; i++) {
8230 intel_crtc->lut_r[i] = i;
8231 intel_crtc->lut_g[i] = i;
8232 intel_crtc->lut_b[i] = i;
8233 }
8234
Jesse Barnes80824002009-09-10 15:28:06 -07008235 /* Swap pipes & planes for FBC on pre-965 */
8236 intel_crtc->pipe = pipe;
8237 intel_crtc->plane = pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02008238 intel_crtc->cpu_transcoder = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01008239 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008240 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01008241 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07008242 }
8243
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008244 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8245 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8246 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8247 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8248
Jesse Barnes5a354202011-06-24 12:19:22 -07008249 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07008250
Jesse Barnes79e53942008-11-07 14:24:08 -08008251 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08008252}
8253
Carl Worth08d7b3d2009-04-29 14:43:54 -07008254int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00008255 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07008256{
Carl Worth08d7b3d2009-04-29 14:43:54 -07008257 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02008258 struct drm_mode_object *drmmode_obj;
8259 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008260
Daniel Vetter1cff8f62012-04-24 09:55:08 +02008261 if (!drm_core_check_feature(dev, DRIVER_MODESET))
8262 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008263
Daniel Vetterc05422d2009-08-11 16:05:30 +02008264 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8265 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07008266
Daniel Vetterc05422d2009-08-11 16:05:30 +02008267 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07008268 DRM_ERROR("no such CRTC id\n");
8269 return -EINVAL;
8270 }
8271
Daniel Vetterc05422d2009-08-11 16:05:30 +02008272 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8273 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008274
Daniel Vetterc05422d2009-08-11 16:05:30 +02008275 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008276}
8277
Daniel Vetter66a92782012-07-12 20:08:18 +02008278static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008279{
Daniel Vetter66a92782012-07-12 20:08:18 +02008280 struct drm_device *dev = encoder->base.dev;
8281 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008282 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008283 int entry = 0;
8284
Daniel Vetter66a92782012-07-12 20:08:18 +02008285 list_for_each_entry(source_encoder,
8286 &dev->mode_config.encoder_list, base.head) {
8287
8288 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008289 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02008290
8291 /* Intel hw has only one MUX where enocoders could be cloned. */
8292 if (encoder->cloneable && source_encoder->cloneable)
8293 index_mask |= (1 << entry);
8294
Jesse Barnes79e53942008-11-07 14:24:08 -08008295 entry++;
8296 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01008297
Jesse Barnes79e53942008-11-07 14:24:08 -08008298 return index_mask;
8299}
8300
Chris Wilson4d302442010-12-14 19:21:29 +00008301static bool has_edp_a(struct drm_device *dev)
8302{
8303 struct drm_i915_private *dev_priv = dev->dev_private;
8304
8305 if (!IS_MOBILE(dev))
8306 return false;
8307
8308 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8309 return false;
8310
8311 if (IS_GEN5(dev) &&
8312 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8313 return false;
8314
8315 return true;
8316}
8317
Jesse Barnes79e53942008-11-07 14:24:08 -08008318static void intel_setup_outputs(struct drm_device *dev)
8319{
Eric Anholt725e30a2009-01-22 13:01:02 -08008320 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008321 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008322 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008323 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08008324
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008325 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00008326 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8327 /* disable the panel fitter on everything but LVDS */
8328 I915_WRITE(PFIT_CONTROL, 0);
8329 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008330
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008331 if (!(HAS_DDI(dev) && (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02008332 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008333
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008334 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03008335 int found;
8336
8337 /* Haswell uses DDI functions to detect digital outputs */
8338 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
8339 /* DDI A only supports eDP */
8340 if (found)
8341 intel_ddi_init(dev, PORT_A);
8342
8343 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
8344 * register */
8345 found = I915_READ(SFUSE_STRAP);
8346
8347 if (found & SFUSE_STRAP_DDIB_DETECTED)
8348 intel_ddi_init(dev, PORT_B);
8349 if (found & SFUSE_STRAP_DDIC_DETECTED)
8350 intel_ddi_init(dev, PORT_C);
8351 if (found & SFUSE_STRAP_DDID_DETECTED)
8352 intel_ddi_init(dev, PORT_D);
8353 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008354 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02008355 dpd_is_edp = intel_dpd_is_edp(dev);
8356
8357 if (has_edp_a(dev))
8358 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008359
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008360 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08008361 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01008362 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008363 if (!found)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008364 intel_hdmi_init(dev, HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008365 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008366 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008367 }
8368
8369 if (I915_READ(HDMIC) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008370 intel_hdmi_init(dev, HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008371
Jesse Barnesb708a1d2012-06-11 14:39:56 -04008372 if (!dpd_is_edp && I915_READ(HDMID) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008373 intel_hdmi_init(dev, HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008374
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008375 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008376 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008377
Daniel Vetter270b3042012-10-27 15:52:05 +02008378 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008379 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008380 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05308381 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008382 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
8383 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05308384
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008385 if (I915_READ(VLV_DISPLAY_BASE + SDVOB) & PORT_DETECTED) {
8386 intel_hdmi_init(dev, VLV_DISPLAY_BASE + SDVOB, PORT_B);
8387 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
8388 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008389 }
8390
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008391 if (I915_READ(VLV_DISPLAY_BASE + SDVOC) & PORT_DETECTED)
8392 intel_hdmi_init(dev, VLV_DISPLAY_BASE + SDVOC, PORT_C);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008393
Zhenyu Wang103a1962009-11-27 11:44:36 +08008394 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008395 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008396
Eric Anholt725e30a2009-01-22 13:01:02 -08008397 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008398 DRM_DEBUG_KMS("probing SDVOB\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008399 found = intel_sdvo_init(dev, SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008400 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8401 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008402 intel_hdmi_init(dev, SDVOB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008403 }
Ma Ling27185ae2009-08-24 13:50:23 +08008404
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008405 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8406 DRM_DEBUG_KMS("probing DP_B\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008407 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008408 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008409 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008410
8411 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008412
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008413 if (I915_READ(SDVOB) & SDVO_DETECTED) {
8414 DRM_DEBUG_KMS("probing SDVOC\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008415 found = intel_sdvo_init(dev, SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008416 }
Ma Ling27185ae2009-08-24 13:50:23 +08008417
8418 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
8419
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008420 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8421 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008422 intel_hdmi_init(dev, SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008423 }
8424 if (SUPPORTS_INTEGRATED_DP(dev)) {
8425 DRM_DEBUG_KMS("probing DP_C\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008426 intel_dp_init(dev, DP_C, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008427 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008428 }
Ma Ling27185ae2009-08-24 13:50:23 +08008429
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008430 if (SUPPORTS_INTEGRATED_DP(dev) &&
8431 (I915_READ(DP_D) & DP_DETECTED)) {
8432 DRM_DEBUG_KMS("probing DP_D\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008433 intel_dp_init(dev, DP_D, PORT_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008434 }
Eric Anholtbad720f2009-10-22 16:11:14 -07008435 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008436 intel_dvo_init(dev);
8437
Zhenyu Wang103a1962009-11-27 11:44:36 +08008438 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008439 intel_tv_init(dev);
8440
Chris Wilson4ef69c72010-09-09 15:14:28 +01008441 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8442 encoder->base.possible_crtcs = encoder->crtc_mask;
8443 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02008444 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08008445 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008446
Paulo Zanonidde86e22012-12-01 12:04:25 -02008447 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02008448
8449 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008450}
8451
8452static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8453{
8454 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008455
8456 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008457 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008458
8459 kfree(intel_fb);
8460}
8461
8462static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008463 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008464 unsigned int *handle)
8465{
8466 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008467 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008468
Chris Wilson05394f32010-11-08 19:18:58 +00008469 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008470}
8471
8472static const struct drm_framebuffer_funcs intel_fb_funcs = {
8473 .destroy = intel_user_framebuffer_destroy,
8474 .create_handle = intel_user_framebuffer_create_handle,
8475};
8476
Dave Airlie38651672010-03-30 05:34:13 +00008477int intel_framebuffer_init(struct drm_device *dev,
8478 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008479 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008480 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008481{
Jesse Barnes79e53942008-11-07 14:24:08 -08008482 int ret;
8483
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008484 if (obj->tiling_mode == I915_TILING_Y) {
8485 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01008486 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008487 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008488
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008489 if (mode_cmd->pitches[0] & 63) {
8490 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
8491 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01008492 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008493 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008494
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008495 /* FIXME <= Gen4 stride limits are bit unclear */
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008496 if (mode_cmd->pitches[0] > 32768) {
8497 DRM_DEBUG("pitch (%d) must be at less than 32768\n",
8498 mode_cmd->pitches[0]);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008499 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008500 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008501
8502 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008503 mode_cmd->pitches[0] != obj->stride) {
8504 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
8505 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008506 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008507 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008508
Ville Syrjälä57779d02012-10-31 17:50:14 +02008509 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008510 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02008511 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008512 case DRM_FORMAT_RGB565:
8513 case DRM_FORMAT_XRGB8888:
8514 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008515 break;
8516 case DRM_FORMAT_XRGB1555:
8517 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008518 if (INTEL_INFO(dev)->gen > 3) {
8519 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008520 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008521 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02008522 break;
8523 case DRM_FORMAT_XBGR8888:
8524 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008525 case DRM_FORMAT_XRGB2101010:
8526 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008527 case DRM_FORMAT_XBGR2101010:
8528 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008529 if (INTEL_INFO(dev)->gen < 4) {
8530 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008531 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008532 }
Jesse Barnesb5626742011-06-24 12:19:27 -07008533 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02008534 case DRM_FORMAT_YUYV:
8535 case DRM_FORMAT_UYVY:
8536 case DRM_FORMAT_YVYU:
8537 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008538 if (INTEL_INFO(dev)->gen < 5) {
8539 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008540 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008541 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008542 break;
8543 default:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008544 DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01008545 return -EINVAL;
8546 }
8547
Ville Syrjälä90f9a332012-10-31 17:50:19 +02008548 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
8549 if (mode_cmd->offsets[0] != 0)
8550 return -EINVAL;
8551
Daniel Vetterc7d73f62012-12-13 23:38:38 +01008552 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
8553 intel_fb->obj = obj;
8554
Jesse Barnes79e53942008-11-07 14:24:08 -08008555 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8556 if (ret) {
8557 DRM_ERROR("framebuffer init failed %d\n", ret);
8558 return ret;
8559 }
8560
Jesse Barnes79e53942008-11-07 14:24:08 -08008561 return 0;
8562}
8563
Jesse Barnes79e53942008-11-07 14:24:08 -08008564static struct drm_framebuffer *
8565intel_user_framebuffer_create(struct drm_device *dev,
8566 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008567 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08008568{
Chris Wilson05394f32010-11-08 19:18:58 +00008569 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008570
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008571 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8572 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00008573 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01008574 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08008575
Chris Wilsond2dff872011-04-19 08:36:26 +01008576 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08008577}
8578
Jesse Barnes79e53942008-11-07 14:24:08 -08008579static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08008580 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00008581 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08008582};
8583
Jesse Barnese70236a2009-09-21 10:42:27 -07008584/* Set up chip specific display functions */
8585static void intel_init_display(struct drm_device *dev)
8586{
8587 struct drm_i915_private *dev_priv = dev->dev_private;
8588
8589 /* We always want a DPMS function */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008590 if (HAS_DDI(dev)) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008591 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02008592 dev_priv->display.crtc_enable = haswell_crtc_enable;
8593 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008594 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008595 dev_priv->display.update_plane = ironlake_update_plane;
8596 } else if (HAS_PCH_SPLIT(dev)) {
Eric Anholtf564048e2011-03-30 13:01:02 -07008597 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008598 dev_priv->display.crtc_enable = ironlake_crtc_enable;
8599 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008600 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008601 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008602 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07008603 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008604 dev_priv->display.crtc_enable = i9xx_crtc_enable;
8605 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008606 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008607 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008608 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008609
Jesse Barnese70236a2009-09-21 10:42:27 -07008610 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07008611 if (IS_VALLEYVIEW(dev))
8612 dev_priv->display.get_display_clock_speed =
8613 valleyview_get_display_clock_speed;
8614 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07008615 dev_priv->display.get_display_clock_speed =
8616 i945_get_display_clock_speed;
8617 else if (IS_I915G(dev))
8618 dev_priv->display.get_display_clock_speed =
8619 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008620 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008621 dev_priv->display.get_display_clock_speed =
8622 i9xx_misc_get_display_clock_speed;
8623 else if (IS_I915GM(dev))
8624 dev_priv->display.get_display_clock_speed =
8625 i915gm_get_display_clock_speed;
8626 else if (IS_I865G(dev))
8627 dev_priv->display.get_display_clock_speed =
8628 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02008629 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008630 dev_priv->display.get_display_clock_speed =
8631 i855_get_display_clock_speed;
8632 else /* 852, 830 */
8633 dev_priv->display.get_display_clock_speed =
8634 i830_get_display_clock_speed;
8635
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008636 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01008637 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008638 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008639 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08008640 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008641 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008642 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07008643 } else if (IS_IVYBRIDGE(dev)) {
8644 /* FIXME: detect B0+ stepping and use auto training */
8645 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008646 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02008647 dev_priv->display.modeset_global_resources =
8648 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03008649 } else if (IS_HASWELL(dev)) {
8650 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08008651 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02008652 dev_priv->display.modeset_global_resources =
8653 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02008654 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07008655 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08008656 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07008657 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008658
8659 /* Default just returns -ENODEV to indicate unsupported */
8660 dev_priv->display.queue_flip = intel_default_queue_flip;
8661
8662 switch (INTEL_INFO(dev)->gen) {
8663 case 2:
8664 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8665 break;
8666
8667 case 3:
8668 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8669 break;
8670
8671 case 4:
8672 case 5:
8673 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8674 break;
8675
8676 case 6:
8677 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8678 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008679 case 7:
8680 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8681 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008682 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008683}
8684
Jesse Barnesb690e962010-07-19 13:53:12 -07008685/*
8686 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8687 * resume, or other times. This quirk makes sure that's the case for
8688 * affected systems.
8689 */
Akshay Joshi0206e352011-08-16 15:34:10 -04008690static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07008691{
8692 struct drm_i915_private *dev_priv = dev->dev_private;
8693
8694 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008695 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008696}
8697
Keith Packard435793d2011-07-12 14:56:22 -07008698/*
8699 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8700 */
8701static void quirk_ssc_force_disable(struct drm_device *dev)
8702{
8703 struct drm_i915_private *dev_priv = dev->dev_private;
8704 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008705 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07008706}
8707
Carsten Emde4dca20e2012-03-15 15:56:26 +01008708/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01008709 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
8710 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01008711 */
8712static void quirk_invert_brightness(struct drm_device *dev)
8713{
8714 struct drm_i915_private *dev_priv = dev->dev_private;
8715 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008716 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008717}
8718
8719struct intel_quirk {
8720 int device;
8721 int subsystem_vendor;
8722 int subsystem_device;
8723 void (*hook)(struct drm_device *dev);
8724};
8725
Egbert Eich5f85f1762012-10-14 15:46:38 +02008726/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
8727struct intel_dmi_quirk {
8728 void (*hook)(struct drm_device *dev);
8729 const struct dmi_system_id (*dmi_id_list)[];
8730};
8731
8732static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
8733{
8734 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
8735 return 1;
8736}
8737
8738static const struct intel_dmi_quirk intel_dmi_quirks[] = {
8739 {
8740 .dmi_id_list = &(const struct dmi_system_id[]) {
8741 {
8742 .callback = intel_dmi_reverse_brightness,
8743 .ident = "NCR Corporation",
8744 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
8745 DMI_MATCH(DMI_PRODUCT_NAME, ""),
8746 },
8747 },
8748 { } /* terminating entry */
8749 },
8750 .hook = quirk_invert_brightness,
8751 },
8752};
8753
Ben Widawskyc43b5632012-04-16 14:07:40 -07008754static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07008755 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04008756 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07008757
Jesse Barnesb690e962010-07-19 13:53:12 -07008758 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
8759 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
8760
Jesse Barnesb690e962010-07-19 13:53:12 -07008761 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
8762 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
8763
Daniel Vetterccd0d362012-10-10 23:13:59 +02008764 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07008765 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02008766 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07008767
8768 /* Lenovo U160 cannot use SSC on LVDS */
8769 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02008770
8771 /* Sony Vaio Y cannot use SSC on LVDS */
8772 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01008773
8774 /* Acer Aspire 5734Z must invert backlight brightness */
8775 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02008776
8777 /* Acer/eMachines G725 */
8778 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02008779
8780 /* Acer/eMachines e725 */
8781 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02008782
8783 /* Acer/Packard Bell NCL20 */
8784 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Daniel Vetterac4199e2013-02-15 18:35:30 +01008785
8786 /* Acer Aspire 4736Z */
8787 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07008788};
8789
8790static void intel_init_quirks(struct drm_device *dev)
8791{
8792 struct pci_dev *d = dev->pdev;
8793 int i;
8794
8795 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
8796 struct intel_quirk *q = &intel_quirks[i];
8797
8798 if (d->device == q->device &&
8799 (d->subsystem_vendor == q->subsystem_vendor ||
8800 q->subsystem_vendor == PCI_ANY_ID) &&
8801 (d->subsystem_device == q->subsystem_device ||
8802 q->subsystem_device == PCI_ANY_ID))
8803 q->hook(dev);
8804 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02008805 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
8806 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
8807 intel_dmi_quirks[i].hook(dev);
8808 }
Jesse Barnesb690e962010-07-19 13:53:12 -07008809}
8810
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008811/* Disable the VGA plane that we never use */
8812static void i915_disable_vga(struct drm_device *dev)
8813{
8814 struct drm_i915_private *dev_priv = dev->dev_private;
8815 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02008816 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008817
8818 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07008819 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008820 sr1 = inb(VGA_SR_DATA);
8821 outb(sr1 | 1<<5, VGA_SR_DATA);
8822 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
8823 udelay(300);
8824
8825 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
8826 POSTING_READ(vga_reg);
8827}
8828
Daniel Vetterf8175862012-04-10 15:50:11 +02008829void intel_modeset_init_hw(struct drm_device *dev)
8830{
Paulo Zanonifa42e232013-01-25 16:59:11 -02008831 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03008832
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03008833 intel_prepare_ddi(dev);
8834
Daniel Vetterf8175862012-04-10 15:50:11 +02008835 intel_init_clock_gating(dev);
8836
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008837 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008838 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008839 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02008840}
8841
Jesse Barnes79e53942008-11-07 14:24:08 -08008842void intel_modeset_init(struct drm_device *dev)
8843{
Jesse Barnes652c3932009-08-17 13:31:43 -07008844 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008845 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008846
8847 drm_mode_config_init(dev);
8848
8849 dev->mode_config.min_width = 0;
8850 dev->mode_config.min_height = 0;
8851
Dave Airlie019d96c2011-09-29 16:20:42 +01008852 dev->mode_config.preferred_depth = 24;
8853 dev->mode_config.prefer_shadow = 1;
8854
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02008855 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08008856
Jesse Barnesb690e962010-07-19 13:53:12 -07008857 intel_init_quirks(dev);
8858
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008859 intel_init_pm(dev);
8860
Jesse Barnese70236a2009-09-21 10:42:27 -07008861 intel_init_display(dev);
8862
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008863 if (IS_GEN2(dev)) {
8864 dev->mode_config.max_width = 2048;
8865 dev->mode_config.max_height = 2048;
8866 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07008867 dev->mode_config.max_width = 4096;
8868 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08008869 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008870 dev->mode_config.max_width = 8192;
8871 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08008872 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08008873 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008874
Zhao Yakui28c97732009-10-09 11:39:41 +08008875 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10008876 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08008877
Dave Airliea3524f12010-06-06 18:59:41 +10008878 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008879 intel_crtc_init(dev, i);
Jesse Barnes00c2064b2012-01-13 15:48:39 -08008880 ret = intel_plane_init(dev, i);
8881 if (ret)
8882 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08008883 }
8884
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008885 intel_cpu_pll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008886 intel_pch_pll_init(dev);
8887
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008888 /* Just disable it once at startup */
8889 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008890 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00008891
8892 /* Just in case the BIOS is doing something questionable. */
8893 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01008894}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008895
Daniel Vetter24929352012-07-02 20:28:59 +02008896static void
8897intel_connector_break_all_links(struct intel_connector *connector)
8898{
8899 connector->base.dpms = DRM_MODE_DPMS_OFF;
8900 connector->base.encoder = NULL;
8901 connector->encoder->connectors_active = false;
8902 connector->encoder->base.crtc = NULL;
8903}
8904
Daniel Vetter7fad7982012-07-04 17:51:47 +02008905static void intel_enable_pipe_a(struct drm_device *dev)
8906{
8907 struct intel_connector *connector;
8908 struct drm_connector *crt = NULL;
8909 struct intel_load_detect_pipe load_detect_temp;
8910
8911 /* We can't just switch on the pipe A, we need to set things up with a
8912 * proper mode and output configuration. As a gross hack, enable pipe A
8913 * by enabling the load detect pipe once. */
8914 list_for_each_entry(connector,
8915 &dev->mode_config.connector_list,
8916 base.head) {
8917 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
8918 crt = &connector->base;
8919 break;
8920 }
8921 }
8922
8923 if (!crt)
8924 return;
8925
8926 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
8927 intel_release_load_detect_pipe(crt, &load_detect_temp);
8928
8929
8930}
8931
Daniel Vetterfa555832012-10-10 23:14:00 +02008932static bool
8933intel_check_plane_mapping(struct intel_crtc *crtc)
8934{
8935 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8936 u32 reg, val;
8937
8938 if (dev_priv->num_pipe == 1)
8939 return true;
8940
8941 reg = DSPCNTR(!crtc->plane);
8942 val = I915_READ(reg);
8943
8944 if ((val & DISPLAY_PLANE_ENABLE) &&
8945 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
8946 return false;
8947
8948 return true;
8949}
8950
Daniel Vetter24929352012-07-02 20:28:59 +02008951static void intel_sanitize_crtc(struct intel_crtc *crtc)
8952{
8953 struct drm_device *dev = crtc->base.dev;
8954 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02008955 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02008956
Daniel Vetter24929352012-07-02 20:28:59 +02008957 /* Clear any frame start delays used for debugging left by the BIOS */
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008958 reg = PIPECONF(crtc->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02008959 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
8960
8961 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02008962 * disable the crtc (and hence change the state) if it is wrong. Note
8963 * that gen4+ has a fixed plane -> pipe mapping. */
8964 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02008965 struct intel_connector *connector;
8966 bool plane;
8967
Daniel Vetter24929352012-07-02 20:28:59 +02008968 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
8969 crtc->base.base.id);
8970
8971 /* Pipe has the wrong plane attached and the plane is active.
8972 * Temporarily change the plane mapping and disable everything
8973 * ... */
8974 plane = crtc->plane;
8975 crtc->plane = !plane;
8976 dev_priv->display.crtc_disable(&crtc->base);
8977 crtc->plane = plane;
8978
8979 /* ... and break all links. */
8980 list_for_each_entry(connector, &dev->mode_config.connector_list,
8981 base.head) {
8982 if (connector->encoder->base.crtc != &crtc->base)
8983 continue;
8984
8985 intel_connector_break_all_links(connector);
8986 }
8987
8988 WARN_ON(crtc->active);
8989 crtc->base.enabled = false;
8990 }
Daniel Vetter24929352012-07-02 20:28:59 +02008991
Daniel Vetter7fad7982012-07-04 17:51:47 +02008992 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
8993 crtc->pipe == PIPE_A && !crtc->active) {
8994 /* BIOS forgot to enable pipe A, this mostly happens after
8995 * resume. Force-enable the pipe to fix this, the update_dpms
8996 * call below we restore the pipe to the right state, but leave
8997 * the required bits on. */
8998 intel_enable_pipe_a(dev);
8999 }
9000
Daniel Vetter24929352012-07-02 20:28:59 +02009001 /* Adjust the state of the output pipe according to whether we
9002 * have active connectors/encoders. */
9003 intel_crtc_update_dpms(&crtc->base);
9004
9005 if (crtc->active != crtc->base.enabled) {
9006 struct intel_encoder *encoder;
9007
9008 /* This can happen either due to bugs in the get_hw_state
9009 * functions or because the pipe is force-enabled due to the
9010 * pipe A quirk. */
9011 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9012 crtc->base.base.id,
9013 crtc->base.enabled ? "enabled" : "disabled",
9014 crtc->active ? "enabled" : "disabled");
9015
9016 crtc->base.enabled = crtc->active;
9017
9018 /* Because we only establish the connector -> encoder ->
9019 * crtc links if something is active, this means the
9020 * crtc is now deactivated. Break the links. connector
9021 * -> encoder links are only establish when things are
9022 * actually up, hence no need to break them. */
9023 WARN_ON(crtc->active);
9024
9025 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9026 WARN_ON(encoder->connectors_active);
9027 encoder->base.crtc = NULL;
9028 }
9029 }
9030}
9031
9032static void intel_sanitize_encoder(struct intel_encoder *encoder)
9033{
9034 struct intel_connector *connector;
9035 struct drm_device *dev = encoder->base.dev;
9036
9037 /* We need to check both for a crtc link (meaning that the
9038 * encoder is active and trying to read from a pipe) and the
9039 * pipe itself being active. */
9040 bool has_active_crtc = encoder->base.crtc &&
9041 to_intel_crtc(encoder->base.crtc)->active;
9042
9043 if (encoder->connectors_active && !has_active_crtc) {
9044 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9045 encoder->base.base.id,
9046 drm_get_encoder_name(&encoder->base));
9047
9048 /* Connector is active, but has no active pipe. This is
9049 * fallout from our resume register restoring. Disable
9050 * the encoder manually again. */
9051 if (encoder->base.crtc) {
9052 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9053 encoder->base.base.id,
9054 drm_get_encoder_name(&encoder->base));
9055 encoder->disable(encoder);
9056 }
9057
9058 /* Inconsistent output/port/pipe state happens presumably due to
9059 * a bug in one of the get_hw_state functions. Or someplace else
9060 * in our code, like the register restore mess on resume. Clamp
9061 * things to off as a safer default. */
9062 list_for_each_entry(connector,
9063 &dev->mode_config.connector_list,
9064 base.head) {
9065 if (connector->encoder != encoder)
9066 continue;
9067
9068 intel_connector_break_all_links(connector);
9069 }
9070 }
9071 /* Enabled encoders without active connectors will be fixed in
9072 * the crtc fixup. */
9073}
9074
Daniel Vetter44cec742013-01-25 17:53:21 +01009075void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009076{
9077 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009078 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009079
9080 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9081 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +02009082 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009083 }
9084}
9085
Daniel Vetter24929352012-07-02 20:28:59 +02009086/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
9087 * and i915 state tracking structures. */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009088void intel_modeset_setup_hw_state(struct drm_device *dev,
9089 bool force_restore)
Daniel Vetter24929352012-07-02 20:28:59 +02009090{
9091 struct drm_i915_private *dev_priv = dev->dev_private;
9092 enum pipe pipe;
9093 u32 tmp;
9094 struct intel_crtc *crtc;
9095 struct intel_encoder *encoder;
9096 struct intel_connector *connector;
9097
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009098 if (HAS_DDI(dev)) {
Paulo Zanonie28d54c2012-10-24 16:09:25 -02009099 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9100
9101 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9102 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9103 case TRANS_DDI_EDP_INPUT_A_ON:
9104 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9105 pipe = PIPE_A;
9106 break;
9107 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9108 pipe = PIPE_B;
9109 break;
9110 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9111 pipe = PIPE_C;
9112 break;
9113 }
9114
9115 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9116 crtc->cpu_transcoder = TRANSCODER_EDP;
9117
9118 DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
9119 pipe_name(pipe));
9120 }
9121 }
9122
Daniel Vetter24929352012-07-02 20:28:59 +02009123 for_each_pipe(pipe) {
9124 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9125
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009126 tmp = I915_READ(PIPECONF(crtc->cpu_transcoder));
Daniel Vetter24929352012-07-02 20:28:59 +02009127 if (tmp & PIPECONF_ENABLE)
9128 crtc->active = true;
9129 else
9130 crtc->active = false;
9131
9132 crtc->base.enabled = crtc->active;
9133
9134 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9135 crtc->base.base.id,
9136 crtc->active ? "enabled" : "disabled");
9137 }
9138
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009139 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009140 intel_ddi_setup_hw_pll_state(dev);
9141
Daniel Vetter24929352012-07-02 20:28:59 +02009142 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9143 base.head) {
9144 pipe = 0;
9145
9146 if (encoder->get_hw_state(encoder, &pipe)) {
9147 encoder->base.crtc =
9148 dev_priv->pipe_to_crtc_mapping[pipe];
9149 } else {
9150 encoder->base.crtc = NULL;
9151 }
9152
9153 encoder->connectors_active = false;
9154 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9155 encoder->base.base.id,
9156 drm_get_encoder_name(&encoder->base),
9157 encoder->base.crtc ? "enabled" : "disabled",
9158 pipe);
9159 }
9160
9161 list_for_each_entry(connector, &dev->mode_config.connector_list,
9162 base.head) {
9163 if (connector->get_hw_state(connector)) {
9164 connector->base.dpms = DRM_MODE_DPMS_ON;
9165 connector->encoder->connectors_active = true;
9166 connector->base.encoder = &connector->encoder->base;
9167 } else {
9168 connector->base.dpms = DRM_MODE_DPMS_OFF;
9169 connector->base.encoder = NULL;
9170 }
9171 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9172 connector->base.base.id,
9173 drm_get_connector_name(&connector->base),
9174 connector->base.encoder ? "enabled" : "disabled");
9175 }
9176
9177 /* HW state is read out, now we need to sanitize this mess. */
9178 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9179 base.head) {
9180 intel_sanitize_encoder(encoder);
9181 }
9182
9183 for_each_pipe(pipe) {
9184 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9185 intel_sanitize_crtc(crtc);
9186 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009187
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009188 if (force_restore) {
9189 for_each_pipe(pipe) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009190 intel_crtc_restore_mode(dev_priv->pipe_to_crtc_mapping[pipe]);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009191 }
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009192
9193 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009194 } else {
9195 intel_modeset_update_staged_output_state(dev);
9196 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009197
9198 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +02009199
9200 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009201}
9202
9203void intel_modeset_gem_init(struct drm_device *dev)
9204{
Chris Wilson1833b132012-05-09 11:56:28 +01009205 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02009206
9207 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +02009208
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009209 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08009210}
9211
9212void intel_modeset_cleanup(struct drm_device *dev)
9213{
Jesse Barnes652c3932009-08-17 13:31:43 -07009214 struct drm_i915_private *dev_priv = dev->dev_private;
9215 struct drm_crtc *crtc;
9216 struct intel_crtc *intel_crtc;
9217
Keith Packardf87ea762010-10-03 19:36:26 -07009218 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009219 mutex_lock(&dev->struct_mutex);
9220
Jesse Barnes723bfd72010-10-07 16:01:13 -07009221 intel_unregister_dsm_handler();
9222
9223
Jesse Barnes652c3932009-08-17 13:31:43 -07009224 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9225 /* Skip inactive CRTCs */
9226 if (!crtc->fb)
9227 continue;
9228
9229 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02009230 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009231 }
9232
Chris Wilson973d04f2011-07-08 12:22:37 +01009233 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07009234
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009235 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00009236
Daniel Vetter930ebb42012-06-29 23:32:16 +02009237 ironlake_teardown_rc6(dev);
9238
Jesse Barnes57f350b2012-03-28 13:39:25 -07009239 if (IS_VALLEYVIEW(dev))
9240 vlv_init_dpio(dev);
9241
Kristian Høgsberg69341a52009-11-11 12:19:17 -05009242 mutex_unlock(&dev->struct_mutex);
9243
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009244 /* Disable the irq before mode object teardown, for the irq might
9245 * enqueue unpin/hotplug work. */
9246 drm_irq_uninstall(dev);
9247 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02009248 cancel_work_sync(&dev_priv->rps.work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009249
Chris Wilson1630fe72011-07-08 12:22:42 +01009250 /* flush any delayed tasks or pending work */
9251 flush_scheduled_work();
9252
Jesse Barnes79e53942008-11-07 14:24:08 -08009253 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +01009254
9255 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009256}
9257
Dave Airlie28d52042009-09-21 14:33:58 +10009258/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08009259 * Return which encoder is currently attached for connector.
9260 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01009261struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08009262{
Chris Wilsondf0e9242010-09-09 16:20:55 +01009263 return &intel_attached_encoder(connector)->base;
9264}
Jesse Barnes79e53942008-11-07 14:24:08 -08009265
Chris Wilsondf0e9242010-09-09 16:20:55 +01009266void intel_connector_attach_encoder(struct intel_connector *connector,
9267 struct intel_encoder *encoder)
9268{
9269 connector->encoder = encoder;
9270 drm_mode_connector_attach_encoder(&connector->base,
9271 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009272}
Dave Airlie28d52042009-09-21 14:33:58 +10009273
9274/*
9275 * set vga decode state - true == enable VGA decode
9276 */
9277int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9278{
9279 struct drm_i915_private *dev_priv = dev->dev_private;
9280 u16 gmch_ctrl;
9281
9282 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9283 if (state)
9284 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9285 else
9286 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9287 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9288 return 0;
9289}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009290
9291#ifdef CONFIG_DEBUG_FS
9292#include <linux/seq_file.h>
9293
9294struct intel_display_error_state {
9295 struct intel_cursor_error_state {
9296 u32 control;
9297 u32 position;
9298 u32 base;
9299 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +01009300 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009301
9302 struct intel_pipe_error_state {
9303 u32 conf;
9304 u32 source;
9305
9306 u32 htotal;
9307 u32 hblank;
9308 u32 hsync;
9309 u32 vtotal;
9310 u32 vblank;
9311 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +01009312 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009313
9314 struct intel_plane_error_state {
9315 u32 control;
9316 u32 stride;
9317 u32 size;
9318 u32 pos;
9319 u32 addr;
9320 u32 surface;
9321 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +01009322 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009323};
9324
9325struct intel_display_error_state *
9326intel_display_capture_error_state(struct drm_device *dev)
9327{
Akshay Joshi0206e352011-08-16 15:34:10 -04009328 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009329 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009330 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009331 int i;
9332
9333 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9334 if (error == NULL)
9335 return NULL;
9336
Damien Lespiau52331302012-08-15 19:23:25 +01009337 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009338 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
9339
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009340 error->cursor[i].control = I915_READ(CURCNTR(i));
9341 error->cursor[i].position = I915_READ(CURPOS(i));
9342 error->cursor[i].base = I915_READ(CURBASE(i));
9343
9344 error->plane[i].control = I915_READ(DSPCNTR(i));
9345 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
9346 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04009347 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009348 error->plane[i].addr = I915_READ(DSPADDR(i));
9349 if (INTEL_INFO(dev)->gen >= 4) {
9350 error->plane[i].surface = I915_READ(DSPSURF(i));
9351 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9352 }
9353
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009354 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009355 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009356 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
9357 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
9358 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
9359 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
9360 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
9361 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009362 }
9363
9364 return error;
9365}
9366
9367void
9368intel_display_print_error_state(struct seq_file *m,
9369 struct drm_device *dev,
9370 struct intel_display_error_state *error)
9371{
Damien Lespiau52331302012-08-15 19:23:25 +01009372 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009373 int i;
9374
Damien Lespiau52331302012-08-15 19:23:25 +01009375 seq_printf(m, "Num Pipes: %d\n", dev_priv->num_pipe);
9376 for_each_pipe(i) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009377 seq_printf(m, "Pipe [%d]:\n", i);
9378 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9379 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
9380 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9381 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9382 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9383 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9384 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9385 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9386
9387 seq_printf(m, "Plane [%d]:\n", i);
9388 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9389 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9390 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
9391 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9392 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9393 if (INTEL_INFO(dev)->gen >= 4) {
9394 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9395 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9396 }
9397
9398 seq_printf(m, "Cursor [%d]:\n", i);
9399 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9400 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9401 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9402 }
9403}
9404#endif