blob: d28f43f81200a8ff6f18738b69a4484d8566f993 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080042#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043
Akshay Joshi0206e352011-08-16 15:34:10 -040044bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
Daniel Vetter3dec0092010-08-20 21:40:52 +020045static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010046static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080047
48typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040049 /* given values */
50 int n;
51 int m1, m2;
52 int p1, p2;
53 /* derived values */
54 int dot;
55 int vco;
56 int m;
57 int p;
Jesse Barnes79e53942008-11-07 14:24:08 -080058} intel_clock_t;
59
60typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040061 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080062} intel_range_t;
63
64typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040065 int dot_limit;
66 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080067} intel_p2_t;
68
69#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080070typedef struct intel_limit intel_limit_t;
71struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040072 intel_range_t dot, vco, n, m, m1, m2, p, p1;
73 intel_p2_t p2;
74 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
Sean Paulcec2f352012-01-10 15:09:36 -080075 int, int, intel_clock_t *, intel_clock_t *);
Ma Lingd4906092009-03-18 20:13:27 +080076};
Jesse Barnes79e53942008-11-07 14:24:08 -080077
Jesse Barnes2377b742010-07-07 14:06:43 -070078/* FDI */
79#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
80
Daniel Vetterd2acd212012-10-20 20:57:43 +020081int
82intel_pch_rawclk(struct drm_device *dev)
83{
84 struct drm_i915_private *dev_priv = dev->dev_private;
85
86 WARN_ON(!HAS_PCH_SPLIT(dev));
87
88 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
89}
90
Ma Lingd4906092009-03-18 20:13:27 +080091static bool
92intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080093 int target, int refclk, intel_clock_t *match_clock,
94 intel_clock_t *best_clock);
Ma Lingd4906092009-03-18 20:13:27 +080095static bool
96intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080097 int target, int refclk, intel_clock_t *match_clock,
98 intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080099
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700100static bool
101intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800102 int target, int refclk, intel_clock_t *match_clock,
103 intel_clock_t *best_clock);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800104static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500105intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800106 int target, int refclk, intel_clock_t *match_clock,
107 intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700108
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700109static bool
110intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
111 int target, int refclk, intel_clock_t *match_clock,
112 intel_clock_t *best_clock);
113
Chris Wilson021357a2010-09-07 20:54:59 +0100114static inline u32 /* units of 100MHz */
115intel_fdi_link_freq(struct drm_device *dev)
116{
Chris Wilson8b99e682010-10-13 09:59:17 +0100117 if (IS_GEN5(dev)) {
118 struct drm_i915_private *dev_priv = dev->dev_private;
119 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
120 } else
121 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100122}
123
Keith Packarde4b36692009-06-05 19:22:17 -0700124static const intel_limit_t intel_limits_i8xx_dvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 .dot = { .min = 25000, .max = 350000 },
126 .vco = { .min = 930000, .max = 1400000 },
127 .n = { .min = 3, .max = 16 },
128 .m = { .min = 96, .max = 140 },
129 .m1 = { .min = 18, .max = 26 },
130 .m2 = { .min = 6, .max = 16 },
131 .p = { .min = 4, .max = 128 },
132 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700133 .p2 = { .dot_limit = 165000,
134 .p2_slow = 4, .p2_fast = 2 },
Ma Lingd4906092009-03-18 20:13:27 +0800135 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700136};
137
138static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400139 .dot = { .min = 25000, .max = 350000 },
140 .vco = { .min = 930000, .max = 1400000 },
141 .n = { .min = 3, .max = 16 },
142 .m = { .min = 96, .max = 140 },
143 .m1 = { .min = 18, .max = 26 },
144 .m2 = { .min = 6, .max = 16 },
145 .p = { .min = 4, .max = 128 },
146 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700147 .p2 = { .dot_limit = 165000,
148 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800149 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700150};
Eric Anholt273e27c2011-03-30 13:01:10 -0700151
Keith Packarde4b36692009-06-05 19:22:17 -0700152static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400153 .dot = { .min = 20000, .max = 400000 },
154 .vco = { .min = 1400000, .max = 2800000 },
155 .n = { .min = 1, .max = 6 },
156 .m = { .min = 70, .max = 120 },
157 .m1 = { .min = 10, .max = 22 },
158 .m2 = { .min = 5, .max = 9 },
159 .p = { .min = 5, .max = 80 },
160 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700161 .p2 = { .dot_limit = 200000,
162 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800163 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700164};
165
166static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400167 .dot = { .min = 20000, .max = 400000 },
168 .vco = { .min = 1400000, .max = 2800000 },
169 .n = { .min = 1, .max = 6 },
170 .m = { .min = 70, .max = 120 },
171 .m1 = { .min = 10, .max = 22 },
172 .m2 = { .min = 5, .max = 9 },
173 .p = { .min = 7, .max = 98 },
174 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700175 .p2 = { .dot_limit = 112000,
176 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800177 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700178};
179
Eric Anholt273e27c2011-03-30 13:01:10 -0700180
Keith Packarde4b36692009-06-05 19:22:17 -0700181static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700182 .dot = { .min = 25000, .max = 270000 },
183 .vco = { .min = 1750000, .max = 3500000},
184 .n = { .min = 1, .max = 4 },
185 .m = { .min = 104, .max = 138 },
186 .m1 = { .min = 17, .max = 23 },
187 .m2 = { .min = 5, .max = 11 },
188 .p = { .min = 10, .max = 30 },
189 .p1 = { .min = 1, .max = 3},
190 .p2 = { .dot_limit = 270000,
191 .p2_slow = 10,
192 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800193 },
Ma Lingd4906092009-03-18 20:13:27 +0800194 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700195};
196
197static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700198 .dot = { .min = 22000, .max = 400000 },
199 .vco = { .min = 1750000, .max = 3500000},
200 .n = { .min = 1, .max = 4 },
201 .m = { .min = 104, .max = 138 },
202 .m1 = { .min = 16, .max = 23 },
203 .m2 = { .min = 5, .max = 11 },
204 .p = { .min = 5, .max = 80 },
205 .p1 = { .min = 1, .max = 8},
206 .p2 = { .dot_limit = 165000,
207 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800208 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700209};
210
211static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700212 .dot = { .min = 20000, .max = 115000 },
213 .vco = { .min = 1750000, .max = 3500000 },
214 .n = { .min = 1, .max = 3 },
215 .m = { .min = 104, .max = 138 },
216 .m1 = { .min = 17, .max = 23 },
217 .m2 = { .min = 5, .max = 11 },
218 .p = { .min = 28, .max = 112 },
219 .p1 = { .min = 2, .max = 8 },
220 .p2 = { .dot_limit = 0,
221 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800222 },
Ma Lingd4906092009-03-18 20:13:27 +0800223 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700224};
225
226static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700227 .dot = { .min = 80000, .max = 224000 },
228 .vco = { .min = 1750000, .max = 3500000 },
229 .n = { .min = 1, .max = 3 },
230 .m = { .min = 104, .max = 138 },
231 .m1 = { .min = 17, .max = 23 },
232 .m2 = { .min = 5, .max = 11 },
233 .p = { .min = 14, .max = 42 },
234 .p1 = { .min = 2, .max = 6 },
235 .p2 = { .dot_limit = 0,
236 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800237 },
Ma Lingd4906092009-03-18 20:13:27 +0800238 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700239};
240
241static const intel_limit_t intel_limits_g4x_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400242 .dot = { .min = 161670, .max = 227000 },
243 .vco = { .min = 1750000, .max = 3500000},
244 .n = { .min = 1, .max = 2 },
245 .m = { .min = 97, .max = 108 },
246 .m1 = { .min = 0x10, .max = 0x12 },
247 .m2 = { .min = 0x05, .max = 0x06 },
248 .p = { .min = 10, .max = 20 },
249 .p1 = { .min = 1, .max = 2},
250 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700251 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400252 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700253};
254
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500255static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400256 .dot = { .min = 20000, .max = 400000},
257 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700258 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400259 .n = { .min = 3, .max = 6 },
260 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400262 .m1 = { .min = 0, .max = 0 },
263 .m2 = { .min = 0, .max = 254 },
264 .p = { .min = 5, .max = 80 },
265 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700266 .p2 = { .dot_limit = 200000,
267 .p2_slow = 10, .p2_fast = 5 },
Shaohua Li61157072009-04-03 15:24:43 +0800268 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700269};
270
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500271static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400272 .dot = { .min = 20000, .max = 400000 },
273 .vco = { .min = 1700000, .max = 3500000 },
274 .n = { .min = 3, .max = 6 },
275 .m = { .min = 2, .max = 256 },
276 .m1 = { .min = 0, .max = 0 },
277 .m2 = { .min = 0, .max = 254 },
278 .p = { .min = 7, .max = 112 },
279 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700280 .p2 = { .dot_limit = 112000,
281 .p2_slow = 14, .p2_fast = 14 },
Shaohua Li61157072009-04-03 15:24:43 +0800282 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700283};
284
Eric Anholt273e27c2011-03-30 13:01:10 -0700285/* Ironlake / Sandybridge
286 *
287 * We calculate clock using (register_value + 2) for N/M1/M2, so here
288 * the range value for them is (actual_value - 2).
289 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800290static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700291 .dot = { .min = 25000, .max = 350000 },
292 .vco = { .min = 1760000, .max = 3510000 },
293 .n = { .min = 1, .max = 5 },
294 .m = { .min = 79, .max = 127 },
295 .m1 = { .min = 12, .max = 22 },
296 .m2 = { .min = 5, .max = 9 },
297 .p = { .min = 5, .max = 80 },
298 .p1 = { .min = 1, .max = 8 },
299 .p2 = { .dot_limit = 225000,
300 .p2_slow = 10, .p2_fast = 5 },
Zhao Yakui45476682009-12-31 16:06:04 +0800301 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700302};
303
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800304static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700305 .dot = { .min = 25000, .max = 350000 },
306 .vco = { .min = 1760000, .max = 3510000 },
307 .n = { .min = 1, .max = 3 },
308 .m = { .min = 79, .max = 118 },
309 .m1 = { .min = 12, .max = 22 },
310 .m2 = { .min = 5, .max = 9 },
311 .p = { .min = 28, .max = 112 },
312 .p1 = { .min = 2, .max = 8 },
313 .p2 = { .dot_limit = 225000,
314 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800315 .find_pll = intel_g4x_find_best_PLL,
316};
317
318static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700319 .dot = { .min = 25000, .max = 350000 },
320 .vco = { .min = 1760000, .max = 3510000 },
321 .n = { .min = 1, .max = 3 },
322 .m = { .min = 79, .max = 127 },
323 .m1 = { .min = 12, .max = 22 },
324 .m2 = { .min = 5, .max = 9 },
325 .p = { .min = 14, .max = 56 },
326 .p1 = { .min = 2, .max = 8 },
327 .p2 = { .dot_limit = 225000,
328 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800329 .find_pll = intel_g4x_find_best_PLL,
330};
331
Eric Anholt273e27c2011-03-30 13:01:10 -0700332/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800333static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700334 .dot = { .min = 25000, .max = 350000 },
335 .vco = { .min = 1760000, .max = 3510000 },
336 .n = { .min = 1, .max = 2 },
337 .m = { .min = 79, .max = 126 },
338 .m1 = { .min = 12, .max = 22 },
339 .m2 = { .min = 5, .max = 9 },
340 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400341 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700342 .p2 = { .dot_limit = 225000,
343 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800344 .find_pll = intel_g4x_find_best_PLL,
345};
346
347static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700348 .dot = { .min = 25000, .max = 350000 },
349 .vco = { .min = 1760000, .max = 3510000 },
350 .n = { .min = 1, .max = 3 },
351 .m = { .min = 79, .max = 126 },
352 .m1 = { .min = 12, .max = 22 },
353 .m2 = { .min = 5, .max = 9 },
354 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400355 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700356 .p2 = { .dot_limit = 225000,
357 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800358 .find_pll = intel_g4x_find_best_PLL,
359};
360
361static const intel_limit_t intel_limits_ironlake_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400362 .dot = { .min = 25000, .max = 350000 },
363 .vco = { .min = 1760000, .max = 3510000},
364 .n = { .min = 1, .max = 2 },
365 .m = { .min = 81, .max = 90 },
366 .m1 = { .min = 12, .max = 22 },
367 .m2 = { .min = 5, .max = 9 },
368 .p = { .min = 10, .max = 20 },
369 .p1 = { .min = 1, .max = 2},
370 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400372 .find_pll = intel_find_pll_ironlake_dp,
Jesse Barnes79e53942008-11-07 14:24:08 -0800373};
374
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700375static const intel_limit_t intel_limits_vlv_dac = {
376 .dot = { .min = 25000, .max = 270000 },
377 .vco = { .min = 4000000, .max = 6000000 },
378 .n = { .min = 1, .max = 7 },
379 .m = { .min = 22, .max = 450 }, /* guess */
380 .m1 = { .min = 2, .max = 3 },
381 .m2 = { .min = 11, .max = 156 },
382 .p = { .min = 10, .max = 30 },
383 .p1 = { .min = 2, .max = 3 },
384 .p2 = { .dot_limit = 270000,
385 .p2_slow = 2, .p2_fast = 20 },
386 .find_pll = intel_vlv_find_best_pll,
387};
388
389static const intel_limit_t intel_limits_vlv_hdmi = {
390 .dot = { .min = 20000, .max = 165000 },
Vijay Purushothaman17dc9252012-09-27 19:13:09 +0530391 .vco = { .min = 4000000, .max = 5994000},
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700392 .n = { .min = 1, .max = 7 },
393 .m = { .min = 60, .max = 300 }, /* guess */
394 .m1 = { .min = 2, .max = 3 },
395 .m2 = { .min = 11, .max = 156 },
396 .p = { .min = 10, .max = 30 },
397 .p1 = { .min = 2, .max = 3 },
398 .p2 = { .dot_limit = 270000,
399 .p2_slow = 2, .p2_fast = 20 },
400 .find_pll = intel_vlv_find_best_pll,
401};
402
403static const intel_limit_t intel_limits_vlv_dp = {
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530404 .dot = { .min = 25000, .max = 270000 },
405 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700406 .n = { .min = 1, .max = 7 },
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530407 .m = { .min = 22, .max = 450 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700408 .m1 = { .min = 2, .max = 3 },
409 .m2 = { .min = 11, .max = 156 },
410 .p = { .min = 10, .max = 30 },
411 .p1 = { .min = 2, .max = 3 },
412 .p2 = { .dot_limit = 270000,
413 .p2_slow = 2, .p2_fast = 20 },
414 .find_pll = intel_vlv_find_best_pll,
415};
416
Jesse Barnes57f350b2012-03-28 13:39:25 -0700417u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
418{
Daniel Vetter09153002012-12-12 14:06:44 +0100419 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnes57f350b2012-03-28 13:39:25 -0700420
Jesse Barnes57f350b2012-03-28 13:39:25 -0700421 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
422 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100423 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700424 }
425
426 I915_WRITE(DPIO_REG, reg);
427 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
428 DPIO_BYTE);
429 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
430 DRM_ERROR("DPIO read wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100431 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700432 }
Jesse Barnes57f350b2012-03-28 13:39:25 -0700433
Daniel Vetter09153002012-12-12 14:06:44 +0100434 return I915_READ(DPIO_DATA);
Jesse Barnes57f350b2012-03-28 13:39:25 -0700435}
436
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700437static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
438 u32 val)
439{
Daniel Vetter09153002012-12-12 14:06:44 +0100440 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700441
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700442 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
443 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100444 return;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700445 }
446
447 I915_WRITE(DPIO_DATA, val);
448 I915_WRITE(DPIO_REG, reg);
449 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
450 DPIO_BYTE);
451 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
452 DRM_ERROR("DPIO write wait timed out\n");
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700453}
454
Jesse Barnes57f350b2012-03-28 13:39:25 -0700455static void vlv_init_dpio(struct drm_device *dev)
456{
457 struct drm_i915_private *dev_priv = dev->dev_private;
458
459 /* Reset the DPIO config */
460 I915_WRITE(DPIO_CTL, 0);
461 POSTING_READ(DPIO_CTL);
462 I915_WRITE(DPIO_CTL, 1);
463 POSTING_READ(DPIO_CTL);
464}
465
Chris Wilson1b894b52010-12-14 20:04:54 +0000466static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
467 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800468{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800469 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800470 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800471
472 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100473 if (intel_is_dual_link_lvds(dev)) {
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800474 /* LVDS dual channel */
Chris Wilson1b894b52010-12-14 20:04:54 +0000475 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800476 limit = &intel_limits_ironlake_dual_lvds_100m;
477 else
478 limit = &intel_limits_ironlake_dual_lvds;
479 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000480 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800481 limit = &intel_limits_ironlake_single_lvds_100m;
482 else
483 limit = &intel_limits_ironlake_single_lvds;
484 }
485 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Jani Nikula547dc042012-11-02 11:24:03 +0200486 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Zhao Yakui45476682009-12-31 16:06:04 +0800487 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800488 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800489 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800490
491 return limit;
492}
493
Ma Ling044c7c42009-03-18 20:13:23 +0800494static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
495{
496 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800497 const intel_limit_t *limit;
498
499 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100500 if (intel_is_dual_link_lvds(dev))
Ma Ling044c7c42009-03-18 20:13:23 +0800501 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700502 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800503 else
504 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700505 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800506 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
507 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700508 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800509 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700510 limit = &intel_limits_g4x_sdvo;
Akshay Joshi0206e352011-08-16 15:34:10 -0400511 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800513 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800515
516 return limit;
517}
518
Chris Wilson1b894b52010-12-14 20:04:54 +0000519static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800520{
521 struct drm_device *dev = crtc->dev;
522 const intel_limit_t *limit;
523
Eric Anholtbad720f2009-10-22 16:11:14 -0700524 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000525 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800526 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800527 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500528 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800529 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500530 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800531 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500532 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700533 } else if (IS_VALLEYVIEW(dev)) {
534 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
535 limit = &intel_limits_vlv_dac;
536 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
537 limit = &intel_limits_vlv_hdmi;
538 else
539 limit = &intel_limits_vlv_dp;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100540 } else if (!IS_GEN2(dev)) {
541 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
542 limit = &intel_limits_i9xx_lvds;
543 else
544 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800545 } else {
546 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700547 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800548 else
Keith Packarde4b36692009-06-05 19:22:17 -0700549 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800550 }
551 return limit;
552}
553
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500554/* m1 is reserved as 0 in Pineview, n is a ring counter */
555static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800556{
Shaohua Li21778322009-02-23 15:19:16 +0800557 clock->m = clock->m2 + 2;
558 clock->p = clock->p1 * clock->p2;
559 clock->vco = refclk * clock->m / clock->n;
560 clock->dot = clock->vco / clock->p;
561}
562
563static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
564{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500565 if (IS_PINEVIEW(dev)) {
566 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800567 return;
568 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800569 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
570 clock->p = clock->p1 * clock->p2;
571 clock->vco = refclk * clock->m / (clock->n + 2);
572 clock->dot = clock->vco / clock->p;
573}
574
Jesse Barnes79e53942008-11-07 14:24:08 -0800575/**
576 * Returns whether any output on the specified pipe is of the specified type
577 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100578bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800579{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100580 struct drm_device *dev = crtc->dev;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100581 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800582
Daniel Vetter6c2b7c122012-07-05 09:50:24 +0200583 for_each_encoder_on_crtc(dev, crtc, encoder)
584 if (encoder->type == type)
Chris Wilson4ef69c72010-09-09 15:14:28 +0100585 return true;
586
587 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800588}
589
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800590#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800591/**
592 * Returns whether the given set of divisors are valid for a given refclk with
593 * the given connectors.
594 */
595
Chris Wilson1b894b52010-12-14 20:04:54 +0000596static bool intel_PLL_is_valid(struct drm_device *dev,
597 const intel_limit_t *limit,
598 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800599{
Jesse Barnes79e53942008-11-07 14:24:08 -0800600 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400601 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800602 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400603 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800604 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400605 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800606 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400607 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500608 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400609 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400615 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
617 * connector, etc., rather than just a single range.
618 */
619 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400620 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800621
622 return true;
623}
624
Ma Lingd4906092009-03-18 20:13:27 +0800625static bool
626intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800627 int target, int refclk, intel_clock_t *match_clock,
628 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800629
Jesse Barnes79e53942008-11-07 14:24:08 -0800630{
631 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800633 int err = target;
634
Daniel Vettera210b022012-11-26 17:22:08 +0100635 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800636 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100637 * For LVDS just rely on its current settings for dual-channel.
638 * We haven't figured out how to reliably set up different
639 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800640 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100641 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800642 clock.p2 = limit->p2.p2_fast;
643 else
644 clock.p2 = limit->p2.p2_slow;
645 } else {
646 if (target < limit->p2.dot_limit)
647 clock.p2 = limit->p2.p2_slow;
648 else
649 clock.p2 = limit->p2.p2_fast;
650 }
651
Akshay Joshi0206e352011-08-16 15:34:10 -0400652 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800653
Zhao Yakui42158662009-11-20 11:24:18 +0800654 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
655 clock.m1++) {
656 for (clock.m2 = limit->m2.min;
657 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500658 /* m1 is always 0 in Pineview */
659 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800660 break;
661 for (clock.n = limit->n.min;
662 clock.n <= limit->n.max; clock.n++) {
663 for (clock.p1 = limit->p1.min;
664 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800665 int this_err;
666
Shaohua Li21778322009-02-23 15:19:16 +0800667 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000668 if (!intel_PLL_is_valid(dev, limit,
669 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800670 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800671 if (match_clock &&
672 clock.p != match_clock->p)
673 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800674
675 this_err = abs(clock.dot - target);
676 if (this_err < err) {
677 *best_clock = clock;
678 err = this_err;
679 }
680 }
681 }
682 }
683 }
684
685 return (err != target);
686}
687
Ma Lingd4906092009-03-18 20:13:27 +0800688static bool
689intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800690 int target, int refclk, intel_clock_t *match_clock,
691 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800692{
693 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800694 intel_clock_t clock;
695 int max_n;
696 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400697 /* approximately equals target * 0.00585 */
698 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800699 found = false;
700
701 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800702 int lvds_reg;
703
Eric Anholtc619eed2010-01-28 16:45:52 -0800704 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800705 lvds_reg = PCH_LVDS;
706 else
707 lvds_reg = LVDS;
Daniel Vetter1974cad2012-11-26 17:22:09 +0100708 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800709 clock.p2 = limit->p2.p2_fast;
710 else
711 clock.p2 = limit->p2.p2_slow;
712 } else {
713 if (target < limit->p2.dot_limit)
714 clock.p2 = limit->p2.p2_slow;
715 else
716 clock.p2 = limit->p2.p2_fast;
717 }
718
719 memset(best_clock, 0, sizeof(*best_clock));
720 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200721 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800722 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200723 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800724 for (clock.m1 = limit->m1.max;
725 clock.m1 >= limit->m1.min; clock.m1--) {
726 for (clock.m2 = limit->m2.max;
727 clock.m2 >= limit->m2.min; clock.m2--) {
728 for (clock.p1 = limit->p1.max;
729 clock.p1 >= limit->p1.min; clock.p1--) {
730 int this_err;
731
Shaohua Li21778322009-02-23 15:19:16 +0800732 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000733 if (!intel_PLL_is_valid(dev, limit,
734 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800735 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800736 if (match_clock &&
737 clock.p != match_clock->p)
738 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000739
740 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800741 if (this_err < err_most) {
742 *best_clock = clock;
743 err_most = this_err;
744 max_n = clock.n;
745 found = true;
746 }
747 }
748 }
749 }
750 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800751 return found;
752}
Ma Lingd4906092009-03-18 20:13:27 +0800753
Zhenyu Wang2c072452009-06-05 15:38:42 +0800754static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500755intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800756 int target, int refclk, intel_clock_t *match_clock,
757 intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800758{
759 struct drm_device *dev = crtc->dev;
760 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800761
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800762 if (target < 200000) {
763 clock.n = 1;
764 clock.p1 = 2;
765 clock.p2 = 10;
766 clock.m1 = 12;
767 clock.m2 = 9;
768 } else {
769 clock.n = 2;
770 clock.p1 = 1;
771 clock.p2 = 10;
772 clock.m1 = 14;
773 clock.m2 = 8;
774 }
775 intel_clock(dev, refclk, &clock);
776 memcpy(best_clock, &clock, sizeof(intel_clock_t));
777 return true;
778}
779
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700780/* DisplayPort has only two frequencies, 162MHz and 270MHz */
781static bool
782intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800783 int target, int refclk, intel_clock_t *match_clock,
784 intel_clock_t *best_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700785{
Chris Wilson5eddb702010-09-11 13:48:45 +0100786 intel_clock_t clock;
787 if (target < 200000) {
788 clock.p1 = 2;
789 clock.p2 = 10;
790 clock.n = 2;
791 clock.m1 = 23;
792 clock.m2 = 8;
793 } else {
794 clock.p1 = 1;
795 clock.p2 = 10;
796 clock.n = 1;
797 clock.m1 = 14;
798 clock.m2 = 2;
799 }
800 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
801 clock.p = (clock.p1 * clock.p2);
802 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
803 clock.vco = 0;
804 memcpy(best_clock, &clock, sizeof(intel_clock_t));
805 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700806}
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700807static bool
808intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
809 int target, int refclk, intel_clock_t *match_clock,
810 intel_clock_t *best_clock)
811{
812 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
813 u32 m, n, fastclk;
814 u32 updrate, minupdate, fracbits, p;
815 unsigned long bestppm, ppm, absppm;
816 int dotclk, flag;
817
Alan Coxaf447bd2012-07-25 13:49:18 +0100818 flag = 0;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700819 dotclk = target * 1000;
820 bestppm = 1000000;
821 ppm = absppm = 0;
822 fastclk = dotclk / (2*100);
823 updrate = 0;
824 minupdate = 19200;
825 fracbits = 1;
826 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
827 bestm1 = bestm2 = bestp1 = bestp2 = 0;
828
829 /* based on hardware requirement, prefer smaller n to precision */
830 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
831 updrate = refclk / n;
832 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
833 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
834 if (p2 > 10)
835 p2 = p2 - 1;
836 p = p1 * p2;
837 /* based on hardware requirement, prefer bigger m1,m2 values */
838 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
839 m2 = (((2*(fastclk * p * n / m1 )) +
840 refclk) / (2*refclk));
841 m = m1 * m2;
842 vco = updrate * m;
843 if (vco >= limit->vco.min && vco < limit->vco.max) {
844 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
845 absppm = (ppm > 0) ? ppm : (-ppm);
846 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
847 bestppm = 0;
848 flag = 1;
849 }
850 if (absppm < bestppm - 10) {
851 bestppm = absppm;
852 flag = 1;
853 }
854 if (flag) {
855 bestn = n;
856 bestm1 = m1;
857 bestm2 = m2;
858 bestp1 = p1;
859 bestp2 = p2;
860 flag = 0;
861 }
862 }
863 }
864 }
865 }
866 }
867 best_clock->n = bestn;
868 best_clock->m1 = bestm1;
869 best_clock->m2 = bestm2;
870 best_clock->p1 = bestp1;
871 best_clock->p2 = bestp2;
872
873 return true;
874}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700875
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200876enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
877 enum pipe pipe)
878{
879 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
881
882 return intel_crtc->cpu_transcoder;
883}
884
Paulo Zanonia928d532012-05-04 17:18:15 -0300885static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
886{
887 struct drm_i915_private *dev_priv = dev->dev_private;
888 u32 frame, frame_reg = PIPEFRAME(pipe);
889
890 frame = I915_READ(frame_reg);
891
892 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
893 DRM_DEBUG_KMS("vblank wait timed out\n");
894}
895
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700896/**
897 * intel_wait_for_vblank - wait for vblank on a given pipe
898 * @dev: drm device
899 * @pipe: pipe to wait for
900 *
901 * Wait for vblank to occur on a given pipe. Needed for various bits of
902 * mode setting code.
903 */
904void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800905{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700906 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800907 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700908
Paulo Zanonia928d532012-05-04 17:18:15 -0300909 if (INTEL_INFO(dev)->gen >= 5) {
910 ironlake_wait_for_vblank(dev, pipe);
911 return;
912 }
913
Chris Wilson300387c2010-09-05 20:25:43 +0100914 /* Clear existing vblank status. Note this will clear any other
915 * sticky status fields as well.
916 *
917 * This races with i915_driver_irq_handler() with the result
918 * that either function could miss a vblank event. Here it is not
919 * fatal, as we will either wait upon the next vblank interrupt or
920 * timeout. Generally speaking intel_wait_for_vblank() is only
921 * called during modeset at which time the GPU should be idle and
922 * should *not* be performing page flips and thus not waiting on
923 * vblanks...
924 * Currently, the result of us stealing a vblank from the irq
925 * handler is that a single frame will be skipped during swapbuffers.
926 */
927 I915_WRITE(pipestat_reg,
928 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
929
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700930 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100931 if (wait_for(I915_READ(pipestat_reg) &
932 PIPE_VBLANK_INTERRUPT_STATUS,
933 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700934 DRM_DEBUG_KMS("vblank wait timed out\n");
935}
936
Keith Packardab7ad7f2010-10-03 00:33:06 -0700937/*
938 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700939 * @dev: drm device
940 * @pipe: pipe to wait for
941 *
942 * After disabling a pipe, we can't wait for vblank in the usual way,
943 * spinning on the vblank interrupt status bit, since we won't actually
944 * see an interrupt when the pipe is disabled.
945 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700946 * On Gen4 and above:
947 * wait for the pipe register state bit to turn off
948 *
949 * Otherwise:
950 * wait for the display line value to settle (it usually
951 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100952 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700953 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100954void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700955{
956 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200957 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
958 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700959
Keith Packardab7ad7f2010-10-03 00:33:06 -0700960 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200961 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700962
Keith Packardab7ad7f2010-10-03 00:33:06 -0700963 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100964 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
965 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200966 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700967 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300968 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100969 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700970 unsigned long timeout = jiffies + msecs_to_jiffies(100);
971
Paulo Zanoni837ba002012-05-04 17:18:14 -0300972 if (IS_GEN2(dev))
973 line_mask = DSL_LINEMASK_GEN2;
974 else
975 line_mask = DSL_LINEMASK_GEN3;
976
Keith Packardab7ad7f2010-10-03 00:33:06 -0700977 /* Wait for the display line to settle */
978 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300979 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700980 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300981 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700982 time_after(timeout, jiffies));
983 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200984 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700985 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800986}
987
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000988/*
989 * ibx_digital_port_connected - is the specified port connected?
990 * @dev_priv: i915 private structure
991 * @port: the port to test
992 *
993 * Returns true if @port is connected, false otherwise.
994 */
995bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
996 struct intel_digital_port *port)
997{
998 u32 bit;
999
Damien Lespiauc36346e2012-12-13 16:09:03 +00001000 if (HAS_PCH_IBX(dev_priv->dev)) {
1001 switch(port->port) {
1002 case PORT_B:
1003 bit = SDE_PORTB_HOTPLUG;
1004 break;
1005 case PORT_C:
1006 bit = SDE_PORTC_HOTPLUG;
1007 break;
1008 case PORT_D:
1009 bit = SDE_PORTD_HOTPLUG;
1010 break;
1011 default:
1012 return true;
1013 }
1014 } else {
1015 switch(port->port) {
1016 case PORT_B:
1017 bit = SDE_PORTB_HOTPLUG_CPT;
1018 break;
1019 case PORT_C:
1020 bit = SDE_PORTC_HOTPLUG_CPT;
1021 break;
1022 case PORT_D:
1023 bit = SDE_PORTD_HOTPLUG_CPT;
1024 break;
1025 default:
1026 return true;
1027 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001028 }
1029
1030 return I915_READ(SDEISR) & bit;
1031}
1032
Jesse Barnesb24e7172011-01-04 15:09:30 -08001033static const char *state_string(bool enabled)
1034{
1035 return enabled ? "on" : "off";
1036}
1037
1038/* Only for pre-ILK configs */
1039static void assert_pll(struct drm_i915_private *dev_priv,
1040 enum pipe pipe, bool state)
1041{
1042 int reg;
1043 u32 val;
1044 bool cur_state;
1045
1046 reg = DPLL(pipe);
1047 val = I915_READ(reg);
1048 cur_state = !!(val & DPLL_VCO_ENABLE);
1049 WARN(cur_state != state,
1050 "PLL state assertion failure (expected %s, current %s)\n",
1051 state_string(state), state_string(cur_state));
1052}
1053#define assert_pll_enabled(d, p) assert_pll(d, p, true)
1054#define assert_pll_disabled(d, p) assert_pll(d, p, false)
1055
Jesse Barnes040484a2011-01-03 12:14:26 -08001056/* For ILK+ */
1057static void assert_pch_pll(struct drm_i915_private *dev_priv,
Chris Wilson92b27b02012-05-20 18:10:50 +01001058 struct intel_pch_pll *pll,
1059 struct intel_crtc *crtc,
1060 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001061{
Jesse Barnes040484a2011-01-03 12:14:26 -08001062 u32 val;
1063 bool cur_state;
1064
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001065 if (HAS_PCH_LPT(dev_priv->dev)) {
1066 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1067 return;
1068 }
1069
Chris Wilson92b27b02012-05-20 18:10:50 +01001070 if (WARN (!pll,
1071 "asserting PCH PLL %s with no PLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001072 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001073
Chris Wilson92b27b02012-05-20 18:10:50 +01001074 val = I915_READ(pll->pll_reg);
1075 cur_state = !!(val & DPLL_VCO_ENABLE);
1076 WARN(cur_state != state,
1077 "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
1078 pll->pll_reg, state_string(state), state_string(cur_state), val);
1079
1080 /* Make sure the selected PLL is correctly attached to the transcoder */
1081 if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001082 u32 pch_dpll;
1083
1084 pch_dpll = I915_READ(PCH_DPLL_SEL);
Chris Wilson92b27b02012-05-20 18:10:50 +01001085 cur_state = pll->pll_reg == _PCH_DPLL_B;
1086 if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
1087 "PLL[%d] not attached to this transcoder %d: %08x\n",
1088 cur_state, crtc->pipe, pch_dpll)) {
1089 cur_state = !!(val >> (4*crtc->pipe + 3));
1090 WARN(cur_state != state,
1091 "PLL[%d] not %s on this transcoder %d: %08x\n",
1092 pll->pll_reg == _PCH_DPLL_B,
1093 state_string(state),
1094 crtc->pipe,
1095 val);
1096 }
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001097 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001098}
Chris Wilson92b27b02012-05-20 18:10:50 +01001099#define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
1100#define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
Jesse Barnes040484a2011-01-03 12:14:26 -08001101
1102static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1103 enum pipe pipe, bool state)
1104{
1105 int reg;
1106 u32 val;
1107 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001108 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1109 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001110
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001111 if (HAS_DDI(dev_priv->dev)) {
1112 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001113 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001114 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001115 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001116 } else {
1117 reg = FDI_TX_CTL(pipe);
1118 val = I915_READ(reg);
1119 cur_state = !!(val & FDI_TX_ENABLE);
1120 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001121 WARN(cur_state != state,
1122 "FDI TX state assertion failure (expected %s, current %s)\n",
1123 state_string(state), state_string(cur_state));
1124}
1125#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1126#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1127
1128static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1129 enum pipe pipe, bool state)
1130{
1131 int reg;
1132 u32 val;
1133 bool cur_state;
1134
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001135 reg = FDI_RX_CTL(pipe);
1136 val = I915_READ(reg);
1137 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001138 WARN(cur_state != state,
1139 "FDI RX state assertion failure (expected %s, current %s)\n",
1140 state_string(state), state_string(cur_state));
1141}
1142#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1143#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1144
1145static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1146 enum pipe pipe)
1147{
1148 int reg;
1149 u32 val;
1150
1151 /* ILK FDI PLL is always enabled */
1152 if (dev_priv->info->gen == 5)
1153 return;
1154
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001155 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001156 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001157 return;
1158
Jesse Barnes040484a2011-01-03 12:14:26 -08001159 reg = FDI_TX_CTL(pipe);
1160 val = I915_READ(reg);
1161 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1162}
1163
1164static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1165 enum pipe pipe)
1166{
1167 int reg;
1168 u32 val;
1169
1170 reg = FDI_RX_CTL(pipe);
1171 val = I915_READ(reg);
1172 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1173}
1174
Jesse Barnesea0760c2011-01-04 15:09:32 -08001175static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1176 enum pipe pipe)
1177{
1178 int pp_reg, lvds_reg;
1179 u32 val;
1180 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001181 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001182
1183 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1184 pp_reg = PCH_PP_CONTROL;
1185 lvds_reg = PCH_LVDS;
1186 } else {
1187 pp_reg = PP_CONTROL;
1188 lvds_reg = LVDS;
1189 }
1190
1191 val = I915_READ(pp_reg);
1192 if (!(val & PANEL_POWER_ON) ||
1193 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1194 locked = false;
1195
1196 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1197 panel_pipe = PIPE_B;
1198
1199 WARN(panel_pipe == pipe && locked,
1200 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001201 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001202}
1203
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001204void assert_pipe(struct drm_i915_private *dev_priv,
1205 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001206{
1207 int reg;
1208 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001209 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001210 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1211 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001212
Daniel Vetter8e636782012-01-22 01:36:48 +01001213 /* if we need the pipe A quirk it must be always on */
1214 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1215 state = true;
1216
Paulo 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 Barnesa0c4da22012-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. */
Damien Lespiau5a35e992012-10-26 18:20:12 +01002004unsigned long intel_gen4_compute_offset_xtiled(int *x, int *y,
2005 unsigned int bpp,
2006 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002007{
2008 int tile_rows, tiles;
2009
2010 tile_rows = *y / 8;
2011 *y %= 8;
2012 tiles = *x / (512/bpp);
2013 *x %= 512/bpp;
2014
2015 return tile_rows * pitch * 8 + tiles * 4096;
2016}
2017
Jesse Barnes17638cd2011-06-24 12:19:23 -07002018static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2019 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002020{
2021 struct drm_device *dev = crtc->dev;
2022 struct drm_i915_private *dev_priv = dev->dev_private;
2023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2024 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002025 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002026 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002027 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002028 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002029 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002030
2031 switch (plane) {
2032 case 0:
2033 case 1:
2034 break;
2035 default:
2036 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2037 return -EINVAL;
2038 }
2039
2040 intel_fb = to_intel_framebuffer(fb);
2041 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002042
Chris Wilson5eddb702010-09-11 13:48:45 +01002043 reg = DSPCNTR(plane);
2044 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002045 /* Mask out pixel format bits in case we change it */
2046 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002047 switch (fb->pixel_format) {
2048 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002049 dspcntr |= DISPPLANE_8BPP;
2050 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002051 case DRM_FORMAT_XRGB1555:
2052 case DRM_FORMAT_ARGB1555:
2053 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002054 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002055 case DRM_FORMAT_RGB565:
2056 dspcntr |= DISPPLANE_BGRX565;
2057 break;
2058 case DRM_FORMAT_XRGB8888:
2059 case DRM_FORMAT_ARGB8888:
2060 dspcntr |= DISPPLANE_BGRX888;
2061 break;
2062 case DRM_FORMAT_XBGR8888:
2063 case DRM_FORMAT_ABGR8888:
2064 dspcntr |= DISPPLANE_RGBX888;
2065 break;
2066 case DRM_FORMAT_XRGB2101010:
2067 case DRM_FORMAT_ARGB2101010:
2068 dspcntr |= DISPPLANE_BGRX101010;
2069 break;
2070 case DRM_FORMAT_XBGR2101010:
2071 case DRM_FORMAT_ABGR2101010:
2072 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002073 break;
2074 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002075 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes81255562010-08-02 12:07:50 -07002076 return -EINVAL;
2077 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002078
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002079 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002080 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002081 dspcntr |= DISPPLANE_TILED;
2082 else
2083 dspcntr &= ~DISPPLANE_TILED;
2084 }
2085
Chris Wilson5eddb702010-09-11 13:48:45 +01002086 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002087
Daniel Vettere506a0c2012-07-05 12:17:29 +02002088 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002089
Daniel Vetterc2c75132012-07-05 12:17:30 +02002090 if (INTEL_INFO(dev)->gen >= 4) {
2091 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002092 intel_gen4_compute_offset_xtiled(&x, &y,
2093 fb->bits_per_pixel / 8,
2094 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002095 linear_offset -= intel_crtc->dspaddr_offset;
2096 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002097 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002098 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002099
2100 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2101 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002102 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002103 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002104 I915_MODIFY_DISPBASE(DSPSURF(plane),
2105 obj->gtt_offset + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002106 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002107 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002108 } else
Daniel Vettere506a0c2012-07-05 12:17:29 +02002109 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002110 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002111
Jesse Barnes17638cd2011-06-24 12:19:23 -07002112 return 0;
2113}
2114
2115static int ironlake_update_plane(struct drm_crtc *crtc,
2116 struct drm_framebuffer *fb, int x, int y)
2117{
2118 struct drm_device *dev = crtc->dev;
2119 struct drm_i915_private *dev_priv = dev->dev_private;
2120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2121 struct intel_framebuffer *intel_fb;
2122 struct drm_i915_gem_object *obj;
2123 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002124 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002125 u32 dspcntr;
2126 u32 reg;
2127
2128 switch (plane) {
2129 case 0:
2130 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002131 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002132 break;
2133 default:
2134 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2135 return -EINVAL;
2136 }
2137
2138 intel_fb = to_intel_framebuffer(fb);
2139 obj = intel_fb->obj;
2140
2141 reg = DSPCNTR(plane);
2142 dspcntr = I915_READ(reg);
2143 /* Mask out pixel format bits in case we change it */
2144 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002145 switch (fb->pixel_format) {
2146 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002147 dspcntr |= DISPPLANE_8BPP;
2148 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002149 case DRM_FORMAT_RGB565:
2150 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002151 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002152 case DRM_FORMAT_XRGB8888:
2153 case DRM_FORMAT_ARGB8888:
2154 dspcntr |= DISPPLANE_BGRX888;
2155 break;
2156 case DRM_FORMAT_XBGR8888:
2157 case DRM_FORMAT_ABGR8888:
2158 dspcntr |= DISPPLANE_RGBX888;
2159 break;
2160 case DRM_FORMAT_XRGB2101010:
2161 case DRM_FORMAT_ARGB2101010:
2162 dspcntr |= DISPPLANE_BGRX101010;
2163 break;
2164 case DRM_FORMAT_XBGR2101010:
2165 case DRM_FORMAT_ABGR2101010:
2166 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002167 break;
2168 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002169 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002170 return -EINVAL;
2171 }
2172
2173 if (obj->tiling_mode != I915_TILING_NONE)
2174 dspcntr |= DISPPLANE_TILED;
2175 else
2176 dspcntr &= ~DISPPLANE_TILED;
2177
2178 /* must disable */
2179 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2180
2181 I915_WRITE(reg, dspcntr);
2182
Daniel Vettere506a0c2012-07-05 12:17:29 +02002183 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002184 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002185 intel_gen4_compute_offset_xtiled(&x, &y,
2186 fb->bits_per_pixel / 8,
2187 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002188 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002189
Daniel Vettere506a0c2012-07-05 12:17:29 +02002190 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2191 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002192 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002193 I915_MODIFY_DISPBASE(DSPSURF(plane),
2194 obj->gtt_offset + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002195 if (IS_HASWELL(dev)) {
2196 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2197 } else {
2198 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2199 I915_WRITE(DSPLINOFF(plane), linear_offset);
2200 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002201 POSTING_READ(reg);
2202
2203 return 0;
2204}
2205
2206/* Assume fb object is pinned & idle & fenced and just update base pointers */
2207static int
2208intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2209 int x, int y, enum mode_set_atomic state)
2210{
2211 struct drm_device *dev = crtc->dev;
2212 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002213
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002214 if (dev_priv->display.disable_fbc)
2215 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002216 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002217
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002218 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002219}
2220
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002221static int
Chris Wilson14667a42012-04-03 17:58:35 +01002222intel_finish_fb(struct drm_framebuffer *old_fb)
2223{
2224 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2225 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2226 bool was_interruptible = dev_priv->mm.interruptible;
2227 int ret;
2228
Daniel Vetter2c10d572012-12-20 21:24:07 +01002229 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2230
Chris Wilson14667a42012-04-03 17:58:35 +01002231 wait_event(dev_priv->pending_flip_queue,
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002232 i915_reset_in_progress(&dev_priv->gpu_error) ||
Chris Wilson14667a42012-04-03 17:58:35 +01002233 atomic_read(&obj->pending_flip) == 0);
2234
2235 /* Big Hammer, we also need to ensure that any pending
2236 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2237 * current scanout is retired before unpinning the old
2238 * framebuffer.
2239 *
2240 * This should only fail upon a hung GPU, in which case we
2241 * can safely continue.
2242 */
2243 dev_priv->mm.interruptible = false;
2244 ret = i915_gem_object_finish_gpu(obj);
2245 dev_priv->mm.interruptible = was_interruptible;
2246
2247 return ret;
2248}
2249
Ville Syrjälä198598d2012-10-31 17:50:24 +02002250static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2251{
2252 struct drm_device *dev = crtc->dev;
2253 struct drm_i915_master_private *master_priv;
2254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2255
2256 if (!dev->primary->master)
2257 return;
2258
2259 master_priv = dev->primary->master->driver_priv;
2260 if (!master_priv->sarea_priv)
2261 return;
2262
2263 switch (intel_crtc->pipe) {
2264 case 0:
2265 master_priv->sarea_priv->pipeA_x = x;
2266 master_priv->sarea_priv->pipeA_y = y;
2267 break;
2268 case 1:
2269 master_priv->sarea_priv->pipeB_x = x;
2270 master_priv->sarea_priv->pipeB_y = y;
2271 break;
2272 default:
2273 break;
2274 }
2275}
2276
Chris Wilson14667a42012-04-03 17:58:35 +01002277static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002278intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002279 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002280{
2281 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002282 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002283 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002284 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002285 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002286
2287 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002288 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002289 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002290 return 0;
2291 }
2292
Eugeni Dodonov5826eca2012-05-09 15:37:12 -03002293 if(intel_crtc->plane > dev_priv->num_pipe) {
2294 DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
2295 intel_crtc->plane,
2296 dev_priv->num_pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002297 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002298 }
2299
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002300 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002301 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002302 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002303 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002304 if (ret != 0) {
2305 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002306 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002307 return ret;
2308 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002309
Daniel Vetter94352cf2012-07-05 22:51:56 +02002310 if (crtc->fb)
2311 intel_finish_fb(crtc->fb);
Chris Wilson265db952010-09-20 15:41:01 +01002312
Daniel Vetter94352cf2012-07-05 22:51:56 +02002313 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002314 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002315 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002316 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002317 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002318 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002319 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002320
Daniel Vetter94352cf2012-07-05 22:51:56 +02002321 old_fb = crtc->fb;
2322 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002323 crtc->x = x;
2324 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002325
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002326 if (old_fb) {
2327 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002328 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002329 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002330
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002331 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002332 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002333
Ville Syrjälä198598d2012-10-31 17:50:24 +02002334 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002335
2336 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002337}
2338
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002339static void intel_fdi_normal_train(struct drm_crtc *crtc)
2340{
2341 struct drm_device *dev = crtc->dev;
2342 struct drm_i915_private *dev_priv = dev->dev_private;
2343 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2344 int pipe = intel_crtc->pipe;
2345 u32 reg, temp;
2346
2347 /* enable normal train */
2348 reg = FDI_TX_CTL(pipe);
2349 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002350 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002351 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2352 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002353 } else {
2354 temp &= ~FDI_LINK_TRAIN_NONE;
2355 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002356 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002357 I915_WRITE(reg, temp);
2358
2359 reg = FDI_RX_CTL(pipe);
2360 temp = I915_READ(reg);
2361 if (HAS_PCH_CPT(dev)) {
2362 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2363 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2364 } else {
2365 temp &= ~FDI_LINK_TRAIN_NONE;
2366 temp |= FDI_LINK_TRAIN_NONE;
2367 }
2368 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2369
2370 /* wait one idle pattern time */
2371 POSTING_READ(reg);
2372 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002373
2374 /* IVB wants error correction enabled */
2375 if (IS_IVYBRIDGE(dev))
2376 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2377 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002378}
2379
Daniel Vetter01a415f2012-10-27 15:58:40 +02002380static void ivb_modeset_global_resources(struct drm_device *dev)
2381{
2382 struct drm_i915_private *dev_priv = dev->dev_private;
2383 struct intel_crtc *pipe_B_crtc =
2384 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2385 struct intel_crtc *pipe_C_crtc =
2386 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2387 uint32_t temp;
2388
2389 /* When everything is off disable fdi C so that we could enable fdi B
2390 * with all lanes. XXX: This misses the case where a pipe is not using
2391 * any pch resources and so doesn't need any fdi lanes. */
2392 if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
2393 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2394 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2395
2396 temp = I915_READ(SOUTH_CHICKEN1);
2397 temp &= ~FDI_BC_BIFURCATION_SELECT;
2398 DRM_DEBUG_KMS("disabling fdi C rx\n");
2399 I915_WRITE(SOUTH_CHICKEN1, temp);
2400 }
2401}
2402
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002403/* The FDI link training functions for ILK/Ibexpeak. */
2404static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2405{
2406 struct drm_device *dev = crtc->dev;
2407 struct drm_i915_private *dev_priv = dev->dev_private;
2408 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2409 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002410 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002411 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002412
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002413 /* FDI needs bits from pipe & plane first */
2414 assert_pipe_enabled(dev_priv, pipe);
2415 assert_plane_enabled(dev_priv, plane);
2416
Adam Jacksone1a44742010-06-25 15:32:14 -04002417 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2418 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002419 reg = FDI_RX_IMR(pipe);
2420 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002421 temp &= ~FDI_RX_SYMBOL_LOCK;
2422 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002423 I915_WRITE(reg, temp);
2424 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002425 udelay(150);
2426
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002427 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002428 reg = FDI_TX_CTL(pipe);
2429 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002430 temp &= ~(7 << 19);
2431 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002432 temp &= ~FDI_LINK_TRAIN_NONE;
2433 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002434 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002435
Chris Wilson5eddb702010-09-11 13:48:45 +01002436 reg = FDI_RX_CTL(pipe);
2437 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002438 temp &= ~FDI_LINK_TRAIN_NONE;
2439 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002440 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2441
2442 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002443 udelay(150);
2444
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002445 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002446 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2447 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2448 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002449
Chris Wilson5eddb702010-09-11 13:48:45 +01002450 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002451 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002452 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002453 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2454
2455 if ((temp & FDI_RX_BIT_LOCK)) {
2456 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002457 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002458 break;
2459 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002460 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002461 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002462 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002463
2464 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002465 reg = FDI_TX_CTL(pipe);
2466 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002467 temp &= ~FDI_LINK_TRAIN_NONE;
2468 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002469 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002470
Chris Wilson5eddb702010-09-11 13:48:45 +01002471 reg = FDI_RX_CTL(pipe);
2472 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002473 temp &= ~FDI_LINK_TRAIN_NONE;
2474 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002475 I915_WRITE(reg, temp);
2476
2477 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002478 udelay(150);
2479
Chris Wilson5eddb702010-09-11 13:48:45 +01002480 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002481 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002482 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002483 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2484
2485 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002486 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002487 DRM_DEBUG_KMS("FDI train 2 done.\n");
2488 break;
2489 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002490 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002491 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002492 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002493
2494 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002495
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002496}
2497
Akshay Joshi0206e352011-08-16 15:34:10 -04002498static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002499 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2500 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2501 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2502 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2503};
2504
2505/* The FDI link training functions for SNB/Cougarpoint. */
2506static void gen6_fdi_link_train(struct drm_crtc *crtc)
2507{
2508 struct drm_device *dev = crtc->dev;
2509 struct drm_i915_private *dev_priv = dev->dev_private;
2510 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2511 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002512 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002513
Adam Jacksone1a44742010-06-25 15:32:14 -04002514 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2515 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002516 reg = FDI_RX_IMR(pipe);
2517 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002518 temp &= ~FDI_RX_SYMBOL_LOCK;
2519 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002520 I915_WRITE(reg, temp);
2521
2522 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002523 udelay(150);
2524
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002525 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002526 reg = FDI_TX_CTL(pipe);
2527 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002528 temp &= ~(7 << 19);
2529 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002530 temp &= ~FDI_LINK_TRAIN_NONE;
2531 temp |= FDI_LINK_TRAIN_PATTERN_1;
2532 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2533 /* SNB-B */
2534 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002535 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002536
Daniel Vetterd74cf322012-10-26 10:58:13 +02002537 I915_WRITE(FDI_RX_MISC(pipe),
2538 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2539
Chris Wilson5eddb702010-09-11 13:48:45 +01002540 reg = FDI_RX_CTL(pipe);
2541 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002542 if (HAS_PCH_CPT(dev)) {
2543 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2544 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2545 } else {
2546 temp &= ~FDI_LINK_TRAIN_NONE;
2547 temp |= FDI_LINK_TRAIN_PATTERN_1;
2548 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002549 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2550
2551 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002552 udelay(150);
2553
Akshay Joshi0206e352011-08-16 15:34:10 -04002554 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002555 reg = FDI_TX_CTL(pipe);
2556 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002557 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2558 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002559 I915_WRITE(reg, temp);
2560
2561 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002562 udelay(500);
2563
Sean Paulfa37d392012-03-02 12:53:39 -05002564 for (retry = 0; retry < 5; retry++) {
2565 reg = FDI_RX_IIR(pipe);
2566 temp = I915_READ(reg);
2567 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2568 if (temp & FDI_RX_BIT_LOCK) {
2569 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2570 DRM_DEBUG_KMS("FDI train 1 done.\n");
2571 break;
2572 }
2573 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002574 }
Sean Paulfa37d392012-03-02 12:53:39 -05002575 if (retry < 5)
2576 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002577 }
2578 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002579 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002580
2581 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002582 reg = FDI_TX_CTL(pipe);
2583 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002584 temp &= ~FDI_LINK_TRAIN_NONE;
2585 temp |= FDI_LINK_TRAIN_PATTERN_2;
2586 if (IS_GEN6(dev)) {
2587 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2588 /* SNB-B */
2589 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2590 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002591 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002592
Chris Wilson5eddb702010-09-11 13:48:45 +01002593 reg = FDI_RX_CTL(pipe);
2594 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002595 if (HAS_PCH_CPT(dev)) {
2596 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2597 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2598 } else {
2599 temp &= ~FDI_LINK_TRAIN_NONE;
2600 temp |= FDI_LINK_TRAIN_PATTERN_2;
2601 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002602 I915_WRITE(reg, temp);
2603
2604 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002605 udelay(150);
2606
Akshay Joshi0206e352011-08-16 15:34:10 -04002607 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002608 reg = FDI_TX_CTL(pipe);
2609 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002610 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2611 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002612 I915_WRITE(reg, temp);
2613
2614 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002615 udelay(500);
2616
Sean Paulfa37d392012-03-02 12:53:39 -05002617 for (retry = 0; retry < 5; retry++) {
2618 reg = FDI_RX_IIR(pipe);
2619 temp = I915_READ(reg);
2620 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2621 if (temp & FDI_RX_SYMBOL_LOCK) {
2622 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2623 DRM_DEBUG_KMS("FDI train 2 done.\n");
2624 break;
2625 }
2626 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002627 }
Sean Paulfa37d392012-03-02 12:53:39 -05002628 if (retry < 5)
2629 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002630 }
2631 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002632 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002633
2634 DRM_DEBUG_KMS("FDI train done.\n");
2635}
2636
Jesse Barnes357555c2011-04-28 15:09:55 -07002637/* Manual link training for Ivy Bridge A0 parts */
2638static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2639{
2640 struct drm_device *dev = crtc->dev;
2641 struct drm_i915_private *dev_priv = dev->dev_private;
2642 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2643 int pipe = intel_crtc->pipe;
2644 u32 reg, temp, i;
2645
2646 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2647 for train result */
2648 reg = FDI_RX_IMR(pipe);
2649 temp = I915_READ(reg);
2650 temp &= ~FDI_RX_SYMBOL_LOCK;
2651 temp &= ~FDI_RX_BIT_LOCK;
2652 I915_WRITE(reg, temp);
2653
2654 POSTING_READ(reg);
2655 udelay(150);
2656
Daniel Vetter01a415f2012-10-27 15:58:40 +02002657 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2658 I915_READ(FDI_RX_IIR(pipe)));
2659
Jesse Barnes357555c2011-04-28 15:09:55 -07002660 /* enable CPU FDI TX and PCH FDI RX */
2661 reg = FDI_TX_CTL(pipe);
2662 temp = I915_READ(reg);
2663 temp &= ~(7 << 19);
2664 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2665 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2666 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2667 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2668 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002669 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002670 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2671
Daniel Vetterd74cf322012-10-26 10:58:13 +02002672 I915_WRITE(FDI_RX_MISC(pipe),
2673 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2674
Jesse Barnes357555c2011-04-28 15:09:55 -07002675 reg = FDI_RX_CTL(pipe);
2676 temp = I915_READ(reg);
2677 temp &= ~FDI_LINK_TRAIN_AUTO;
2678 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2679 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002680 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002681 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2682
2683 POSTING_READ(reg);
2684 udelay(150);
2685
Akshay Joshi0206e352011-08-16 15:34:10 -04002686 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002687 reg = FDI_TX_CTL(pipe);
2688 temp = I915_READ(reg);
2689 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2690 temp |= snb_b_fdi_train_param[i];
2691 I915_WRITE(reg, temp);
2692
2693 POSTING_READ(reg);
2694 udelay(500);
2695
2696 reg = FDI_RX_IIR(pipe);
2697 temp = I915_READ(reg);
2698 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2699
2700 if (temp & FDI_RX_BIT_LOCK ||
2701 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2702 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002703 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002704 break;
2705 }
2706 }
2707 if (i == 4)
2708 DRM_ERROR("FDI train 1 fail!\n");
2709
2710 /* Train 2 */
2711 reg = FDI_TX_CTL(pipe);
2712 temp = I915_READ(reg);
2713 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2714 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2715 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2716 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2717 I915_WRITE(reg, temp);
2718
2719 reg = FDI_RX_CTL(pipe);
2720 temp = I915_READ(reg);
2721 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2722 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2723 I915_WRITE(reg, temp);
2724
2725 POSTING_READ(reg);
2726 udelay(150);
2727
Akshay Joshi0206e352011-08-16 15:34:10 -04002728 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002729 reg = FDI_TX_CTL(pipe);
2730 temp = I915_READ(reg);
2731 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2732 temp |= snb_b_fdi_train_param[i];
2733 I915_WRITE(reg, temp);
2734
2735 POSTING_READ(reg);
2736 udelay(500);
2737
2738 reg = FDI_RX_IIR(pipe);
2739 temp = I915_READ(reg);
2740 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2741
2742 if (temp & FDI_RX_SYMBOL_LOCK) {
2743 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002744 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002745 break;
2746 }
2747 }
2748 if (i == 4)
2749 DRM_ERROR("FDI train 2 fail!\n");
2750
2751 DRM_DEBUG_KMS("FDI train done.\n");
2752}
2753
Daniel Vetter88cefb62012-08-12 19:27:14 +02002754static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002755{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002756 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002757 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002758 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002759 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002760
Jesse Barnesc64e3112010-09-10 11:27:03 -07002761
Jesse Barnes0e23b992010-09-10 11:10:00 -07002762 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002763 reg = FDI_RX_CTL(pipe);
2764 temp = I915_READ(reg);
2765 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002766 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002767 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002768 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2769
2770 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002771 udelay(200);
2772
2773 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002774 temp = I915_READ(reg);
2775 I915_WRITE(reg, temp | FDI_PCDCLK);
2776
2777 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002778 udelay(200);
2779
Paulo Zanoni20749732012-11-23 15:30:38 -02002780 /* Enable CPU FDI TX PLL, always on for Ironlake */
2781 reg = FDI_TX_CTL(pipe);
2782 temp = I915_READ(reg);
2783 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2784 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002785
Paulo Zanoni20749732012-11-23 15:30:38 -02002786 POSTING_READ(reg);
2787 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002788 }
2789}
2790
Daniel Vetter88cefb62012-08-12 19:27:14 +02002791static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2792{
2793 struct drm_device *dev = intel_crtc->base.dev;
2794 struct drm_i915_private *dev_priv = dev->dev_private;
2795 int pipe = intel_crtc->pipe;
2796 u32 reg, temp;
2797
2798 /* Switch from PCDclk to Rawclk */
2799 reg = FDI_RX_CTL(pipe);
2800 temp = I915_READ(reg);
2801 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2802
2803 /* Disable CPU FDI TX PLL */
2804 reg = FDI_TX_CTL(pipe);
2805 temp = I915_READ(reg);
2806 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2807
2808 POSTING_READ(reg);
2809 udelay(100);
2810
2811 reg = FDI_RX_CTL(pipe);
2812 temp = I915_READ(reg);
2813 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2814
2815 /* Wait for the clocks to turn off. */
2816 POSTING_READ(reg);
2817 udelay(100);
2818}
2819
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002820static void ironlake_fdi_disable(struct drm_crtc *crtc)
2821{
2822 struct drm_device *dev = crtc->dev;
2823 struct drm_i915_private *dev_priv = dev->dev_private;
2824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2825 int pipe = intel_crtc->pipe;
2826 u32 reg, temp;
2827
2828 /* disable CPU FDI tx and PCH FDI rx */
2829 reg = FDI_TX_CTL(pipe);
2830 temp = I915_READ(reg);
2831 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2832 POSTING_READ(reg);
2833
2834 reg = FDI_RX_CTL(pipe);
2835 temp = I915_READ(reg);
2836 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002837 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002838 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2839
2840 POSTING_READ(reg);
2841 udelay(100);
2842
2843 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002844 if (HAS_PCH_IBX(dev)) {
2845 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002846 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002847
2848 /* still set train pattern 1 */
2849 reg = FDI_TX_CTL(pipe);
2850 temp = I915_READ(reg);
2851 temp &= ~FDI_LINK_TRAIN_NONE;
2852 temp |= FDI_LINK_TRAIN_PATTERN_1;
2853 I915_WRITE(reg, temp);
2854
2855 reg = FDI_RX_CTL(pipe);
2856 temp = I915_READ(reg);
2857 if (HAS_PCH_CPT(dev)) {
2858 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2859 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2860 } else {
2861 temp &= ~FDI_LINK_TRAIN_NONE;
2862 temp |= FDI_LINK_TRAIN_PATTERN_1;
2863 }
2864 /* BPC in FDI rx is consistent with that in PIPECONF */
2865 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002866 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002867 I915_WRITE(reg, temp);
2868
2869 POSTING_READ(reg);
2870 udelay(100);
2871}
2872
Chris Wilson5bb61642012-09-27 21:25:58 +01002873static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2874{
2875 struct drm_device *dev = crtc->dev;
2876 struct drm_i915_private *dev_priv = dev->dev_private;
2877 unsigned long flags;
2878 bool pending;
2879
Daniel Vetter1f83fee2012-11-15 17:17:22 +01002880 if (i915_reset_in_progress(&dev_priv->gpu_error))
Chris Wilson5bb61642012-09-27 21:25:58 +01002881 return false;
2882
2883 spin_lock_irqsave(&dev->event_lock, flags);
2884 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2885 spin_unlock_irqrestore(&dev->event_lock, flags);
2886
2887 return pending;
2888}
2889
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002890static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2891{
Chris Wilson0f911282012-04-17 10:05:38 +01002892 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002893 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002894
2895 if (crtc->fb == NULL)
2896 return;
2897
Daniel Vetter2c10d572012-12-20 21:24:07 +01002898 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2899
Chris Wilson5bb61642012-09-27 21:25:58 +01002900 wait_event(dev_priv->pending_flip_queue,
2901 !intel_crtc_has_pending_flip(crtc));
2902
Chris Wilson0f911282012-04-17 10:05:38 +01002903 mutex_lock(&dev->struct_mutex);
2904 intel_finish_fb(crtc->fb);
2905 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002906}
2907
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002908static bool ironlake_crtc_driving_pch(struct drm_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08002909{
2910 struct drm_device *dev = crtc->dev;
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002911 struct intel_encoder *intel_encoder;
Jesse Barnes040484a2011-01-03 12:14:26 -08002912
2913 /*
2914 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2915 * must be driven by its own crtc; no sharing is possible.
2916 */
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002917 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002918 switch (intel_encoder->type) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002919 case INTEL_OUTPUT_EDP:
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002920 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnes040484a2011-01-03 12:14:26 -08002921 return false;
2922 continue;
2923 }
2924 }
2925
2926 return true;
2927}
2928
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002929static bool haswell_crtc_driving_pch(struct drm_crtc *crtc)
2930{
2931 return intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG);
2932}
2933
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002934/* Program iCLKIP clock to the desired frequency */
2935static void lpt_program_iclkip(struct drm_crtc *crtc)
2936{
2937 struct drm_device *dev = crtc->dev;
2938 struct drm_i915_private *dev_priv = dev->dev_private;
2939 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2940 u32 temp;
2941
Daniel Vetter09153002012-12-12 14:06:44 +01002942 mutex_lock(&dev_priv->dpio_lock);
2943
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002944 /* It is necessary to ungate the pixclk gate prior to programming
2945 * the divisors, and gate it back when it is done.
2946 */
2947 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2948
2949 /* Disable SSCCTL */
2950 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002951 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2952 SBI_SSCCTL_DISABLE,
2953 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002954
2955 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2956 if (crtc->mode.clock == 20000) {
2957 auxdiv = 1;
2958 divsel = 0x41;
2959 phaseinc = 0x20;
2960 } else {
2961 /* The iCLK virtual clock root frequency is in MHz,
2962 * but the crtc->mode.clock in in KHz. To get the divisors,
2963 * it is necessary to divide one by another, so we
2964 * convert the virtual clock precision to KHz here for higher
2965 * precision.
2966 */
2967 u32 iclk_virtual_root_freq = 172800 * 1000;
2968 u32 iclk_pi_range = 64;
2969 u32 desired_divisor, msb_divisor_value, pi_value;
2970
2971 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2972 msb_divisor_value = desired_divisor / iclk_pi_range;
2973 pi_value = desired_divisor % iclk_pi_range;
2974
2975 auxdiv = 0;
2976 divsel = msb_divisor_value - 2;
2977 phaseinc = pi_value;
2978 }
2979
2980 /* This should not happen with any sane values */
2981 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2982 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2983 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2984 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2985
2986 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2987 crtc->mode.clock,
2988 auxdiv,
2989 divsel,
2990 phasedir,
2991 phaseinc);
2992
2993 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002994 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002995 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2996 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2997 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2998 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2999 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3000 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003001 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003002
3003 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003004 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003005 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3006 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003007 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003008
3009 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003010 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003011 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003012 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003013
3014 /* Wait for initialization time */
3015 udelay(24);
3016
3017 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003018
3019 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003020}
3021
Jesse Barnesf67a5592011-01-05 10:31:48 -08003022/*
3023 * Enable PCH resources required for PCH ports:
3024 * - PCH PLLs
3025 * - FDI training & RX/TX
3026 * - update transcoder timings
3027 * - DP transcoding bits
3028 * - transcoder
3029 */
3030static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003031{
3032 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003033 struct drm_i915_private *dev_priv = dev->dev_private;
3034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3035 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003036 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003037
Chris Wilsone7e164d2012-05-11 09:21:25 +01003038 assert_transcoder_disabled(dev_priv, pipe);
3039
Daniel Vettercd986ab2012-10-26 10:58:12 +02003040 /* Write the TU size bits before fdi link training, so that error
3041 * detection works. */
3042 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3043 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3044
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003045 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003046 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003047
Daniel Vetter572deb32012-10-27 18:46:14 +02003048 /* XXX: pch pll's can be enabled any time before we enable the PCH
3049 * transcoder, and we actually should do this to not upset any PCH
3050 * transcoder that already use the clock when we share it.
3051 *
3052 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
3053 * unconditionally resets the pll - we need that to have the right LVDS
3054 * enable sequence. */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02003055 ironlake_enable_pch_pll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01003056
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003057 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003058 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003059
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003060 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003061 switch (pipe) {
3062 default:
3063 case 0:
3064 temp |= TRANSA_DPLL_ENABLE;
3065 sel = TRANSA_DPLLB_SEL;
3066 break;
3067 case 1:
3068 temp |= TRANSB_DPLL_ENABLE;
3069 sel = TRANSB_DPLLB_SEL;
3070 break;
3071 case 2:
3072 temp |= TRANSC_DPLL_ENABLE;
3073 sel = TRANSC_DPLLB_SEL;
3074 break;
Jesse Barnesd64311a2011-10-12 15:01:33 -07003075 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003076 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3077 temp |= sel;
3078 else
3079 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003080 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003081 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003082
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003083 /* set transcoder timing, panel must allow it */
3084 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003085 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3086 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3087 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3088
3089 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3090 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3091 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003092 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003093
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003094 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003095
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003096 /* For PCH DP, enable TRANS_DP_CTL */
3097 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003098 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3099 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003100 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003101 reg = TRANS_DP_CTL(pipe);
3102 temp = I915_READ(reg);
3103 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003104 TRANS_DP_SYNC_MASK |
3105 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003106 temp |= (TRANS_DP_OUTPUT_ENABLE |
3107 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003108 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003109
3110 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003111 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003112 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003113 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003114
3115 switch (intel_trans_dp_port_sel(crtc)) {
3116 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003117 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003118 break;
3119 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003120 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003121 break;
3122 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003123 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003124 break;
3125 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003126 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003127 }
3128
Chris Wilson5eddb702010-09-11 13:48:45 +01003129 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003130 }
3131
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003132 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003133}
3134
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003135static void lpt_pch_enable(struct drm_crtc *crtc)
3136{
3137 struct drm_device *dev = crtc->dev;
3138 struct drm_i915_private *dev_priv = dev->dev_private;
3139 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003140 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003141
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003142 assert_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003143
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003144 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003145
Paulo Zanoni0540e482012-10-31 18:12:40 -02003146 /* Set transcoder timing. */
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003147 I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
3148 I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
3149 I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003150
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003151 I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
3152 I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
3153 I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
3154 I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003155
Paulo Zanoni937bb612012-10-31 18:12:47 -02003156 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003157}
3158
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003159static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3160{
3161 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3162
3163 if (pll == NULL)
3164 return;
3165
3166 if (pll->refcount == 0) {
3167 WARN(1, "bad PCH PLL refcount\n");
3168 return;
3169 }
3170
3171 --pll->refcount;
3172 intel_crtc->pch_pll = NULL;
3173}
3174
3175static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3176{
3177 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3178 struct intel_pch_pll *pll;
3179 int i;
3180
3181 pll = intel_crtc->pch_pll;
3182 if (pll) {
3183 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3184 intel_crtc->base.base.id, pll->pll_reg);
3185 goto prepare;
3186 }
3187
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003188 if (HAS_PCH_IBX(dev_priv->dev)) {
3189 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3190 i = intel_crtc->pipe;
3191 pll = &dev_priv->pch_plls[i];
3192
3193 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3194 intel_crtc->base.base.id, pll->pll_reg);
3195
3196 goto found;
3197 }
3198
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003199 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3200 pll = &dev_priv->pch_plls[i];
3201
3202 /* Only want to check enabled timings first */
3203 if (pll->refcount == 0)
3204 continue;
3205
3206 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3207 fp == I915_READ(pll->fp0_reg)) {
3208 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3209 intel_crtc->base.base.id,
3210 pll->pll_reg, pll->refcount, pll->active);
3211
3212 goto found;
3213 }
3214 }
3215
3216 /* Ok no matching timings, maybe there's a free one? */
3217 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3218 pll = &dev_priv->pch_plls[i];
3219 if (pll->refcount == 0) {
3220 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3221 intel_crtc->base.base.id, pll->pll_reg);
3222 goto found;
3223 }
3224 }
3225
3226 return NULL;
3227
3228found:
3229 intel_crtc->pch_pll = pll;
3230 pll->refcount++;
3231 DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
3232prepare: /* separate function? */
3233 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003234
Chris Wilsone04c7352012-05-02 20:43:56 +01003235 /* Wait for the clocks to stabilize before rewriting the regs */
3236 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003237 POSTING_READ(pll->pll_reg);
3238 udelay(150);
Chris Wilsone04c7352012-05-02 20:43:56 +01003239
3240 I915_WRITE(pll->fp0_reg, fp);
3241 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003242 pll->on = false;
3243 return pll;
3244}
3245
Jesse Barnesd4270e52011-10-11 10:43:02 -07003246void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3247{
3248 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003249 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003250 u32 temp;
3251
3252 temp = I915_READ(dslreg);
3253 udelay(500);
3254 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003255 if (wait_for(I915_READ(dslreg) != temp, 5))
3256 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3257 }
3258}
3259
Jesse Barnesf67a5592011-01-05 10:31:48 -08003260static void ironlake_crtc_enable(struct drm_crtc *crtc)
3261{
3262 struct drm_device *dev = crtc->dev;
3263 struct drm_i915_private *dev_priv = dev->dev_private;
3264 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003265 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003266 int pipe = intel_crtc->pipe;
3267 int plane = intel_crtc->plane;
3268 u32 temp;
3269 bool is_pch_port;
3270
Daniel Vetter08a48462012-07-02 11:43:47 +02003271 WARN_ON(!crtc->enabled);
3272
Jesse Barnesf67a5592011-01-05 10:31:48 -08003273 if (intel_crtc->active)
3274 return;
3275
3276 intel_crtc->active = true;
3277 intel_update_watermarks(dev);
3278
3279 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3280 temp = I915_READ(PCH_LVDS);
3281 if ((temp & LVDS_PORT_EN) == 0)
3282 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3283 }
3284
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003285 is_pch_port = ironlake_crtc_driving_pch(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003286
Daniel Vetter46b6f812012-09-06 22:08:33 +02003287 if (is_pch_port) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003288 /* Note: FDI PLL enabling _must_ be done before we enable the
3289 * cpu pipes, hence this is separate from all the other fdi/pch
3290 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003291 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003292 } else {
3293 assert_fdi_tx_disabled(dev_priv, pipe);
3294 assert_fdi_rx_disabled(dev_priv, pipe);
3295 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003296
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003297 for_each_encoder_on_crtc(dev, crtc, encoder)
3298 if (encoder->pre_enable)
3299 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003300
3301 /* Enable panel fitting for LVDS */
3302 if (dev_priv->pch_pf_size &&
Jani Nikula547dc042012-11-02 11:24:03 +02003303 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3304 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnesf67a5592011-01-05 10:31:48 -08003305 /* Force use of hard-coded filter coefficients
3306 * as some pre-programmed values are broken,
3307 * e.g. x201.
3308 */
Paulo Zanoni13888d72012-11-20 13:27:41 -02003309 if (IS_IVYBRIDGE(dev))
3310 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3311 PF_PIPE_SEL_IVB(pipe));
3312 else
3313 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003314 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3315 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003316 }
3317
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003318 /*
3319 * On ILK+ LUT must be loaded before the pipe is running but with
3320 * clocks enabled
3321 */
3322 intel_crtc_load_lut(crtc);
3323
Jesse Barnesf67a5592011-01-05 10:31:48 -08003324 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3325 intel_enable_plane(dev_priv, plane, pipe);
3326
3327 if (is_pch_port)
3328 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003329
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003330 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003331 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003332 mutex_unlock(&dev->struct_mutex);
3333
Chris Wilson6b383a72010-09-13 13:54:26 +01003334 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003335
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003336 for_each_encoder_on_crtc(dev, crtc, encoder)
3337 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003338
3339 if (HAS_PCH_CPT(dev))
3340 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003341
3342 /*
3343 * There seems to be a race in PCH platform hw (at least on some
3344 * outputs) where an enabled pipe still completes any pageflip right
3345 * away (as if the pipe is off) instead of waiting for vblank. As soon
3346 * as the first vblank happend, everything works as expected. Hence just
3347 * wait for one vblank before returning to avoid strange things
3348 * happening.
3349 */
3350 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003351}
3352
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003353static void haswell_crtc_enable(struct drm_crtc *crtc)
3354{
3355 struct drm_device *dev = crtc->dev;
3356 struct drm_i915_private *dev_priv = dev->dev_private;
3357 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3358 struct intel_encoder *encoder;
3359 int pipe = intel_crtc->pipe;
3360 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003361 bool is_pch_port;
3362
3363 WARN_ON(!crtc->enabled);
3364
3365 if (intel_crtc->active)
3366 return;
3367
3368 intel_crtc->active = true;
3369 intel_update_watermarks(dev);
3370
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003371 is_pch_port = haswell_crtc_driving_pch(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003372
Paulo Zanoni83616632012-10-23 18:29:54 -02003373 if (is_pch_port)
Paulo Zanoni04945642012-11-01 21:00:59 -02003374 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003375
3376 for_each_encoder_on_crtc(dev, crtc, encoder)
3377 if (encoder->pre_enable)
3378 encoder->pre_enable(encoder);
3379
Paulo Zanoni1f544382012-10-24 11:32:00 -02003380 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003381
Paulo Zanoni1f544382012-10-24 11:32:00 -02003382 /* Enable panel fitting for eDP */
Jani Nikula547dc042012-11-02 11:24:03 +02003383 if (dev_priv->pch_pf_size &&
3384 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003385 /* Force use of hard-coded filter coefficients
3386 * as some pre-programmed values are broken,
3387 * e.g. x201.
3388 */
Paulo Zanoni54075a72012-11-20 13:27:42 -02003389 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3390 PF_PIPE_SEL_IVB(pipe));
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003391 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3392 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3393 }
3394
3395 /*
3396 * On ILK+ LUT must be loaded before the pipe is running but with
3397 * clocks enabled
3398 */
3399 intel_crtc_load_lut(crtc);
3400
Paulo Zanoni1f544382012-10-24 11:32:00 -02003401 intel_ddi_set_pipe_settings(crtc);
3402 intel_ddi_enable_pipe_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003403
3404 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3405 intel_enable_plane(dev_priv, plane, pipe);
3406
3407 if (is_pch_port)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003408 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003409
3410 mutex_lock(&dev->struct_mutex);
3411 intel_update_fbc(dev);
3412 mutex_unlock(&dev->struct_mutex);
3413
3414 intel_crtc_update_cursor(crtc, true);
3415
3416 for_each_encoder_on_crtc(dev, crtc, encoder)
3417 encoder->enable(encoder);
3418
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003419 /*
3420 * There seems to be a race in PCH platform hw (at least on some
3421 * outputs) where an enabled pipe still completes any pageflip right
3422 * away (as if the pipe is off) instead of waiting for vblank. As soon
3423 * as the first vblank happend, everything works as expected. Hence just
3424 * wait for one vblank before returning to avoid strange things
3425 * happening.
3426 */
3427 intel_wait_for_vblank(dev, intel_crtc->pipe);
3428}
3429
Jesse Barnes6be4a602010-09-10 10:26:01 -07003430static void ironlake_crtc_disable(struct drm_crtc *crtc)
3431{
3432 struct drm_device *dev = crtc->dev;
3433 struct drm_i915_private *dev_priv = dev->dev_private;
3434 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003435 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003436 int pipe = intel_crtc->pipe;
3437 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003438 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003439
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003440
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003441 if (!intel_crtc->active)
3442 return;
3443
Daniel Vetterea9d7582012-07-10 10:42:52 +02003444 for_each_encoder_on_crtc(dev, crtc, encoder)
3445 encoder->disable(encoder);
3446
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003447 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003448 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003449 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003450
Jesse Barnesb24e7172011-01-04 15:09:30 -08003451 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003452
Chris Wilson973d04f2011-07-08 12:22:37 +01003453 if (dev_priv->cfb_plane == plane)
3454 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003455
Jesse Barnesb24e7172011-01-04 15:09:30 -08003456 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003457
Jesse Barnes6be4a602010-09-10 10:26:01 -07003458 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003459 I915_WRITE(PF_CTL(pipe), 0);
3460 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003461
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003462 for_each_encoder_on_crtc(dev, crtc, encoder)
3463 if (encoder->post_disable)
3464 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003465
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003467
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003468 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003469
3470 if (HAS_PCH_CPT(dev)) {
3471 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 reg = TRANS_DP_CTL(pipe);
3473 temp = I915_READ(reg);
3474 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003475 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003477
3478 /* disable DPLL_SEL */
3479 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003480 switch (pipe) {
3481 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003482 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003483 break;
3484 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003485 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003486 break;
3487 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003488 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003489 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003490 break;
3491 default:
3492 BUG(); /* wtf */
3493 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003494 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003495 }
3496
3497 /* disable PCH DPLL */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003498 intel_disable_pch_pll(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003499
Daniel Vetter88cefb62012-08-12 19:27:14 +02003500 ironlake_fdi_pll_disable(intel_crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +01003501
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003502 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003503 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003504
3505 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003506 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003507 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003508}
3509
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003510static void haswell_crtc_disable(struct drm_crtc *crtc)
3511{
3512 struct drm_device *dev = crtc->dev;
3513 struct drm_i915_private *dev_priv = dev->dev_private;
3514 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3515 struct intel_encoder *encoder;
3516 int pipe = intel_crtc->pipe;
3517 int plane = intel_crtc->plane;
Paulo Zanoniad80a812012-10-24 16:06:19 -02003518 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni83616632012-10-23 18:29:54 -02003519 bool is_pch_port;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003520
3521 if (!intel_crtc->active)
3522 return;
3523
Paulo Zanoni83616632012-10-23 18:29:54 -02003524 is_pch_port = haswell_crtc_driving_pch(crtc);
3525
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003526 for_each_encoder_on_crtc(dev, crtc, encoder)
3527 encoder->disable(encoder);
3528
3529 intel_crtc_wait_for_pending_flips(crtc);
3530 drm_vblank_off(dev, pipe);
3531 intel_crtc_update_cursor(crtc, false);
3532
3533 intel_disable_plane(dev_priv, plane, pipe);
3534
3535 if (dev_priv->cfb_plane == plane)
3536 intel_disable_fbc(dev);
3537
3538 intel_disable_pipe(dev_priv, pipe);
3539
Paulo Zanoniad80a812012-10-24 16:06:19 -02003540 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003541
3542 /* Disable PF */
3543 I915_WRITE(PF_CTL(pipe), 0);
3544 I915_WRITE(PF_WIN_SZ(pipe), 0);
3545
Paulo Zanoni1f544382012-10-24 11:32:00 -02003546 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003547
3548 for_each_encoder_on_crtc(dev, crtc, encoder)
3549 if (encoder->post_disable)
3550 encoder->post_disable(encoder);
3551
Paulo Zanoni83616632012-10-23 18:29:54 -02003552 if (is_pch_port) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003553 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003554 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003555 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003556
3557 intel_crtc->active = false;
3558 intel_update_watermarks(dev);
3559
3560 mutex_lock(&dev->struct_mutex);
3561 intel_update_fbc(dev);
3562 mutex_unlock(&dev->struct_mutex);
3563}
3564
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003565static void ironlake_crtc_off(struct drm_crtc *crtc)
3566{
3567 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3568 intel_put_pch_pll(intel_crtc);
3569}
3570
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003571static void haswell_crtc_off(struct drm_crtc *crtc)
3572{
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003573 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3574
3575 /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
3576 * start using it. */
Daniel Vetter1a240d42012-11-29 22:18:51 +01003577 intel_crtc->cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003578
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003579 intel_ddi_put_crtc_pll(crtc);
3580}
3581
Daniel Vetter02e792f2009-09-15 22:57:34 +02003582static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3583{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003584 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003585 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003586 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003587
Chris Wilson23f09ce2010-08-12 13:53:37 +01003588 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003589 dev_priv->mm.interruptible = false;
3590 (void) intel_overlay_switch_off(intel_crtc->overlay);
3591 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003592 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003593 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003594
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003595 /* Let userspace switch the overlay on again. In most cases userspace
3596 * has to recompute where to put it anyway.
3597 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003598}
3599
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003600static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003601{
3602 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003603 struct drm_i915_private *dev_priv = dev->dev_private;
3604 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003605 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003606 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003607 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003608
Daniel Vetter08a48462012-07-02 11:43:47 +02003609 WARN_ON(!crtc->enabled);
3610
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003611 if (intel_crtc->active)
3612 return;
3613
3614 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003615 intel_update_watermarks(dev);
3616
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003617 intel_enable_pll(dev_priv, pipe);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003618
3619 for_each_encoder_on_crtc(dev, crtc, encoder)
3620 if (encoder->pre_enable)
3621 encoder->pre_enable(encoder);
3622
Jesse Barnes040484a2011-01-03 12:14:26 -08003623 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003624 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003625
3626 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003627 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003628
3629 /* Give the overlay scaler a chance to enable if it's on this pipe */
3630 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003631 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003632
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003633 for_each_encoder_on_crtc(dev, crtc, encoder)
3634 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003635}
3636
3637static void i9xx_crtc_disable(struct drm_crtc *crtc)
3638{
3639 struct drm_device *dev = crtc->dev;
3640 struct drm_i915_private *dev_priv = dev->dev_private;
3641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003642 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003643 int pipe = intel_crtc->pipe;
3644 int plane = intel_crtc->plane;
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003645 u32 pctl;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003646
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003647
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003648 if (!intel_crtc->active)
3649 return;
3650
Daniel Vetterea9d7582012-07-10 10:42:52 +02003651 for_each_encoder_on_crtc(dev, crtc, encoder)
3652 encoder->disable(encoder);
3653
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003654 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003655 intel_crtc_wait_for_pending_flips(crtc);
3656 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003657 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003658 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003659
Chris Wilson973d04f2011-07-08 12:22:37 +01003660 if (dev_priv->cfb_plane == plane)
3661 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003662
Jesse Barnesb24e7172011-01-04 15:09:30 -08003663 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003664 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003665
3666 /* Disable pannel fitter if it is on this pipe. */
3667 pctl = I915_READ(PFIT_CONTROL);
3668 if ((pctl & PFIT_ENABLE) &&
3669 ((pctl & PFIT_PIPE_MASK) >> PFIT_PIPE_SHIFT) == pipe)
3670 I915_WRITE(PFIT_CONTROL, 0);
3671
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003672 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003673
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003674 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003675 intel_update_fbc(dev);
3676 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003677}
3678
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003679static void i9xx_crtc_off(struct drm_crtc *crtc)
3680{
3681}
3682
Daniel Vetter976f8a22012-07-08 22:34:21 +02003683static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3684 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003685{
3686 struct drm_device *dev = crtc->dev;
3687 struct drm_i915_master_private *master_priv;
3688 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3689 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003690
3691 if (!dev->primary->master)
3692 return;
3693
3694 master_priv = dev->primary->master->driver_priv;
3695 if (!master_priv->sarea_priv)
3696 return;
3697
Jesse Barnes79e53942008-11-07 14:24:08 -08003698 switch (pipe) {
3699 case 0:
3700 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3701 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3702 break;
3703 case 1:
3704 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3705 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3706 break;
3707 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003708 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003709 break;
3710 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003711}
3712
Daniel Vetter976f8a22012-07-08 22:34:21 +02003713/**
3714 * Sets the power management mode of the pipe and plane.
3715 */
3716void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003717{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003718 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003719 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003720 struct intel_encoder *intel_encoder;
3721 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003722
Daniel Vetter976f8a22012-07-08 22:34:21 +02003723 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3724 enable |= intel_encoder->connectors_active;
3725
3726 if (enable)
3727 dev_priv->display.crtc_enable(crtc);
3728 else
3729 dev_priv->display.crtc_disable(crtc);
3730
3731 intel_crtc_update_sarea(crtc, enable);
3732}
3733
3734static void intel_crtc_noop(struct drm_crtc *crtc)
3735{
3736}
3737
3738static void intel_crtc_disable(struct drm_crtc *crtc)
3739{
3740 struct drm_device *dev = crtc->dev;
3741 struct drm_connector *connector;
3742 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003743 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003744
3745 /* crtc should still be enabled when we disable it. */
3746 WARN_ON(!crtc->enabled);
3747
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003748 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003749 dev_priv->display.crtc_disable(crtc);
3750 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003751 dev_priv->display.off(crtc);
3752
Chris Wilson931872f2012-01-16 23:01:13 +00003753 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3754 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003755
3756 if (crtc->fb) {
3757 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003758 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003759 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003760 crtc->fb = NULL;
3761 }
3762
3763 /* Update computed state. */
3764 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3765 if (!connector->encoder || !connector->encoder->crtc)
3766 continue;
3767
3768 if (connector->encoder->crtc != crtc)
3769 continue;
3770
3771 connector->dpms = DRM_MODE_DPMS_OFF;
3772 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003773 }
3774}
3775
Daniel Vettera261b242012-07-26 19:21:47 +02003776void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003777{
Daniel Vettera261b242012-07-26 19:21:47 +02003778 struct drm_crtc *crtc;
3779
3780 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3781 if (crtc->enabled)
3782 intel_crtc_disable(crtc);
3783 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003784}
3785
Daniel Vetter1f703852012-07-11 16:51:39 +02003786void intel_encoder_noop(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003787{
Jesse Barnes79e53942008-11-07 14:24:08 -08003788}
3789
Chris Wilsonea5b2132010-08-04 13:50:23 +01003790void intel_encoder_destroy(struct drm_encoder *encoder)
3791{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003792 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003793
Chris Wilsonea5b2132010-08-04 13:50:23 +01003794 drm_encoder_cleanup(encoder);
3795 kfree(intel_encoder);
3796}
3797
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003798/* Simple dpms helper for encodres with just one connector, no cloning and only
3799 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3800 * state of the entire output pipe. */
3801void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3802{
3803 if (mode == DRM_MODE_DPMS_ON) {
3804 encoder->connectors_active = true;
3805
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003806 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003807 } else {
3808 encoder->connectors_active = false;
3809
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003810 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003811 }
3812}
3813
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003814/* Cross check the actual hw state with our own modeset state tracking (and it's
3815 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003816static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003817{
3818 if (connector->get_hw_state(connector)) {
3819 struct intel_encoder *encoder = connector->encoder;
3820 struct drm_crtc *crtc;
3821 bool encoder_enabled;
3822 enum pipe pipe;
3823
3824 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3825 connector->base.base.id,
3826 drm_get_connector_name(&connector->base));
3827
3828 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3829 "wrong connector dpms state\n");
3830 WARN(connector->base.encoder != &encoder->base,
3831 "active connector not linked to encoder\n");
3832 WARN(!encoder->connectors_active,
3833 "encoder->connectors_active not set\n");
3834
3835 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3836 WARN(!encoder_enabled, "encoder not enabled\n");
3837 if (WARN_ON(!encoder->base.crtc))
3838 return;
3839
3840 crtc = encoder->base.crtc;
3841
3842 WARN(!crtc->enabled, "crtc not enabled\n");
3843 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3844 WARN(pipe != to_intel_crtc(crtc)->pipe,
3845 "encoder active on the wrong pipe\n");
3846 }
3847}
3848
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003849/* Even simpler default implementation, if there's really no special case to
3850 * consider. */
3851void intel_connector_dpms(struct drm_connector *connector, int mode)
3852{
3853 struct intel_encoder *encoder = intel_attached_encoder(connector);
3854
3855 /* All the simple cases only support two dpms states. */
3856 if (mode != DRM_MODE_DPMS_ON)
3857 mode = DRM_MODE_DPMS_OFF;
3858
3859 if (mode == connector->dpms)
3860 return;
3861
3862 connector->dpms = mode;
3863
3864 /* Only need to change hw state when actually enabled */
3865 if (encoder->base.crtc)
3866 intel_encoder_dpms(encoder, mode);
3867 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003868 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003869
Daniel Vetterb9805142012-08-31 17:37:33 +02003870 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003871}
3872
Daniel Vetterf0947c32012-07-02 13:10:34 +02003873/* Simple connector->get_hw_state implementation for encoders that support only
3874 * one connector and no cloning and hence the encoder state determines the state
3875 * of the connector. */
3876bool intel_connector_get_hw_state(struct intel_connector *connector)
3877{
Daniel Vetter24929352012-07-02 20:28:59 +02003878 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003879 struct intel_encoder *encoder = connector->encoder;
3880
3881 return encoder->get_hw_state(encoder, &pipe);
3882}
3883
Jesse Barnes79e53942008-11-07 14:24:08 -08003884static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
Daniel Vetter35313cd2012-07-20 10:30:45 +02003885 const struct drm_display_mode *mode,
Jesse Barnes79e53942008-11-07 14:24:08 -08003886 struct drm_display_mode *adjusted_mode)
3887{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003888 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003889
Eric Anholtbad720f2009-10-22 16:11:14 -07003890 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003891 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003892 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3893 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003894 }
Chris Wilson89749352010-09-12 18:25:19 +01003895
Daniel Vetterf9bef082012-04-15 19:53:19 +02003896 /* All interlaced capable intel hw wants timings in frames. Note though
3897 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3898 * timings, so we need to be careful not to clobber these.*/
3899 if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3900 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003901
Chris Wilson44f46b422012-06-21 13:19:59 +03003902 /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
3903 * with a hsync front porch of 0.
3904 */
3905 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
3906 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
3907 return false;
3908
Jesse Barnes79e53942008-11-07 14:24:08 -08003909 return true;
3910}
3911
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07003912static int valleyview_get_display_clock_speed(struct drm_device *dev)
3913{
3914 return 400000; /* FIXME */
3915}
3916
Jesse Barnese70236a2009-09-21 10:42:27 -07003917static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003918{
Jesse Barnese70236a2009-09-21 10:42:27 -07003919 return 400000;
3920}
Jesse Barnes79e53942008-11-07 14:24:08 -08003921
Jesse Barnese70236a2009-09-21 10:42:27 -07003922static int i915_get_display_clock_speed(struct drm_device *dev)
3923{
3924 return 333000;
3925}
Jesse Barnes79e53942008-11-07 14:24:08 -08003926
Jesse Barnese70236a2009-09-21 10:42:27 -07003927static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3928{
3929 return 200000;
3930}
Jesse Barnes79e53942008-11-07 14:24:08 -08003931
Jesse Barnese70236a2009-09-21 10:42:27 -07003932static int i915gm_get_display_clock_speed(struct drm_device *dev)
3933{
3934 u16 gcfgc = 0;
3935
3936 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3937
3938 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003939 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003940 else {
3941 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3942 case GC_DISPLAY_CLOCK_333_MHZ:
3943 return 333000;
3944 default:
3945 case GC_DISPLAY_CLOCK_190_200_MHZ:
3946 return 190000;
3947 }
3948 }
3949}
Jesse Barnes79e53942008-11-07 14:24:08 -08003950
Jesse Barnese70236a2009-09-21 10:42:27 -07003951static int i865_get_display_clock_speed(struct drm_device *dev)
3952{
3953 return 266000;
3954}
3955
3956static int i855_get_display_clock_speed(struct drm_device *dev)
3957{
3958 u16 hpllcc = 0;
3959 /* Assume that the hardware is in the high speed state. This
3960 * should be the default.
3961 */
3962 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3963 case GC_CLOCK_133_200:
3964 case GC_CLOCK_100_200:
3965 return 200000;
3966 case GC_CLOCK_166_250:
3967 return 250000;
3968 case GC_CLOCK_100_133:
3969 return 133000;
3970 }
3971
3972 /* Shouldn't happen */
3973 return 0;
3974}
3975
3976static int i830_get_display_clock_speed(struct drm_device *dev)
3977{
3978 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08003979}
3980
Zhenyu Wang2c072452009-06-05 15:38:42 +08003981static void
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003982intel_reduce_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003983{
3984 while (*num > 0xffffff || *den > 0xffffff) {
3985 *num >>= 1;
3986 *den >>= 1;
3987 }
3988}
3989
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003990void
3991intel_link_compute_m_n(int bits_per_pixel, int nlanes,
3992 int pixel_clock, int link_clock,
3993 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003994{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003995 m_n->tu = 64;
Chris Wilson22ed1112010-12-04 01:01:29 +00003996 m_n->gmch_m = bits_per_pixel * pixel_clock;
3997 m_n->gmch_n = link_clock * nlanes * 8;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01003998 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
Chris Wilson22ed1112010-12-04 01:01:29 +00003999 m_n->link_m = pixel_clock;
4000 m_n->link_n = link_clock;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004001 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004002}
4003
Chris Wilsona7615032011-01-12 17:04:08 +00004004static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4005{
Keith Packard72bbe582011-09-26 16:09:45 -07004006 if (i915_panel_use_ssc >= 0)
4007 return i915_panel_use_ssc != 0;
4008 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004009 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004010}
4011
Jesse Barnes5a354202011-06-24 12:19:22 -07004012/**
4013 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
4014 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004015 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07004016 *
4017 * A pipe may be connected to one or more outputs. Based on the depth of the
4018 * attached framebuffer, choose a good color depth to use on the pipe.
4019 *
4020 * If possible, match the pipe depth to the fb depth. In some cases, this
4021 * isn't ideal, because the connected output supports a lesser or restricted
4022 * set of depths. Resolve that here:
4023 * LVDS typically supports only 6bpc, so clamp down in that case
4024 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
4025 * Displays may support a restricted set as well, check EDID and clamp as
4026 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004027 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07004028 *
4029 * RETURNS:
4030 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
4031 * true if they don't match).
4032 */
4033static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004034 struct drm_framebuffer *fb,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004035 unsigned int *pipe_bpp,
4036 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07004037{
4038 struct drm_device *dev = crtc->dev;
4039 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes5a354202011-06-24 12:19:22 -07004040 struct drm_connector *connector;
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004041 struct intel_encoder *intel_encoder;
Jesse Barnes5a354202011-06-24 12:19:22 -07004042 unsigned int display_bpc = UINT_MAX, bpc;
4043
4044 /* Walk the encoders & connectors on this crtc, get min bpc */
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004045 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004046
4047 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
4048 unsigned int lvds_bpc;
4049
4050 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
4051 LVDS_A3_POWER_UP)
4052 lvds_bpc = 8;
4053 else
4054 lvds_bpc = 6;
4055
4056 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004057 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004058 display_bpc = lvds_bpc;
4059 }
4060 continue;
4061 }
4062
Jesse Barnes5a354202011-06-24 12:19:22 -07004063 /* Not one of the known troublemakers, check the EDID */
4064 list_for_each_entry(connector, &dev->mode_config.connector_list,
4065 head) {
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004066 if (connector->encoder != &intel_encoder->base)
Jesse Barnes5a354202011-06-24 12:19:22 -07004067 continue;
4068
Jesse Barnes62ac41a2011-07-28 12:55:14 -07004069 /* Don't use an invalid EDID bpc value */
4070 if (connector->display_info.bpc &&
4071 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004072 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 -07004073 display_bpc = connector->display_info.bpc;
4074 }
4075 }
4076
Jani Nikula2f4f6492012-11-12 14:33:44 +02004077 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
4078 /* Use VBT settings if we have an eDP panel */
4079 unsigned int edp_bpc = dev_priv->edp.bpp / 3;
4080
Jani Nikula9a30a612012-11-12 14:33:45 +02004081 if (edp_bpc && edp_bpc < display_bpc) {
Jani Nikula2f4f6492012-11-12 14:33:44 +02004082 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
4083 display_bpc = edp_bpc;
4084 }
4085 continue;
4086 }
4087
Jesse Barnes5a354202011-06-24 12:19:22 -07004088 /*
4089 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
4090 * through, clamp it down. (Note: >12bpc will be caught below.)
4091 */
4092 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
4093 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04004094 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004095 display_bpc = 12;
4096 } else {
Adam Jackson82820492011-10-10 16:33:34 -04004097 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004098 display_bpc = 8;
4099 }
4100 }
4101 }
4102
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004103 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4104 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
4105 display_bpc = 6;
4106 }
4107
Jesse Barnes5a354202011-06-24 12:19:22 -07004108 /*
4109 * We could just drive the pipe at the highest bpc all the time and
4110 * enable dithering as needed, but that costs bandwidth. So choose
4111 * the minimum value that expresses the full color range of the fb but
4112 * also stays within the max display bpc discovered above.
4113 */
4114
Daniel Vetter94352cf2012-07-05 22:51:56 +02004115 switch (fb->depth) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004116 case 8:
4117 bpc = 8; /* since we go through a colormap */
4118 break;
4119 case 15:
4120 case 16:
4121 bpc = 6; /* min is 18bpp */
4122 break;
4123 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07004124 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07004125 break;
4126 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07004127 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07004128 break;
4129 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07004130 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07004131 break;
4132 default:
4133 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
4134 bpc = min((unsigned int)8, display_bpc);
4135 break;
4136 }
4137
Keith Packard578393c2011-09-05 11:53:21 -07004138 display_bpc = min(display_bpc, bpc);
4139
Adam Jackson82820492011-10-10 16:33:34 -04004140 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
4141 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004142
Keith Packard578393c2011-09-05 11:53:21 -07004143 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07004144
4145 return display_bpc != bpc;
4146}
4147
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004148static int vlv_get_refclk(struct drm_crtc *crtc)
4149{
4150 struct drm_device *dev = crtc->dev;
4151 struct drm_i915_private *dev_priv = dev->dev_private;
4152 int refclk = 27000; /* for DP & HDMI */
4153
4154 return 100000; /* only one validated so far */
4155
4156 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4157 refclk = 96000;
4158 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4159 if (intel_panel_use_ssc(dev_priv))
4160 refclk = 100000;
4161 else
4162 refclk = 96000;
4163 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4164 refclk = 100000;
4165 }
4166
4167 return refclk;
4168}
4169
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004170static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4171{
4172 struct drm_device *dev = crtc->dev;
4173 struct drm_i915_private *dev_priv = dev->dev_private;
4174 int refclk;
4175
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004176 if (IS_VALLEYVIEW(dev)) {
4177 refclk = vlv_get_refclk(crtc);
4178 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004179 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4180 refclk = dev_priv->lvds_ssc_freq * 1000;
4181 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4182 refclk / 1000);
4183 } else if (!IS_GEN2(dev)) {
4184 refclk = 96000;
4185 } else {
4186 refclk = 48000;
4187 }
4188
4189 return refclk;
4190}
4191
4192static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
4193 intel_clock_t *clock)
4194{
4195 /* SDVO TV has fixed PLL values depend on its clock range,
4196 this mirrors vbios setting. */
4197 if (adjusted_mode->clock >= 100000
4198 && adjusted_mode->clock < 140500) {
4199 clock->p1 = 2;
4200 clock->p2 = 10;
4201 clock->n = 3;
4202 clock->m1 = 16;
4203 clock->m2 = 8;
4204 } else if (adjusted_mode->clock >= 140500
4205 && adjusted_mode->clock <= 200000) {
4206 clock->p1 = 1;
4207 clock->p2 = 10;
4208 clock->n = 6;
4209 clock->m1 = 12;
4210 clock->m2 = 8;
4211 }
4212}
4213
Jesse Barnesa7516a02011-12-15 12:30:37 -08004214static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
4215 intel_clock_t *clock,
4216 intel_clock_t *reduced_clock)
4217{
4218 struct drm_device *dev = crtc->dev;
4219 struct drm_i915_private *dev_priv = dev->dev_private;
4220 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4221 int pipe = intel_crtc->pipe;
4222 u32 fp, fp2 = 0;
4223
4224 if (IS_PINEVIEW(dev)) {
4225 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
4226 if (reduced_clock)
4227 fp2 = (1 << reduced_clock->n) << 16 |
4228 reduced_clock->m1 << 8 | reduced_clock->m2;
4229 } else {
4230 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4231 if (reduced_clock)
4232 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4233 reduced_clock->m2;
4234 }
4235
4236 I915_WRITE(FP0(pipe), fp);
4237
4238 intel_crtc->lowfreq_avail = false;
4239 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4240 reduced_clock && i915_powersave) {
4241 I915_WRITE(FP1(pipe), fp2);
4242 intel_crtc->lowfreq_avail = true;
4243 } else {
4244 I915_WRITE(FP1(pipe), fp);
4245 }
4246}
4247
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004248static void vlv_update_pll(struct drm_crtc *crtc,
4249 struct drm_display_mode *mode,
4250 struct drm_display_mode *adjusted_mode,
4251 intel_clock_t *clock, intel_clock_t *reduced_clock,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304252 int num_connectors)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004253{
4254 struct drm_device *dev = crtc->dev;
4255 struct drm_i915_private *dev_priv = dev->dev_private;
4256 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4257 int pipe = intel_crtc->pipe;
4258 u32 dpll, mdiv, pdiv;
4259 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304260 bool is_sdvo;
4261 u32 temp;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004262
Daniel Vetter09153002012-12-12 14:06:44 +01004263 mutex_lock(&dev_priv->dpio_lock);
4264
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304265 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4266 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4267
4268 dpll = DPLL_VGA_MODE_DIS;
4269 dpll |= DPLL_EXT_BUFFER_ENABLE_VLV;
4270 dpll |= DPLL_REFA_CLK_ENABLE_VLV;
4271 dpll |= DPLL_INTEGRATED_CLOCK_VLV;
4272
4273 I915_WRITE(DPLL(pipe), dpll);
4274 POSTING_READ(DPLL(pipe));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004275
4276 bestn = clock->n;
4277 bestm1 = clock->m1;
4278 bestm2 = clock->m2;
4279 bestp1 = clock->p1;
4280 bestp2 = clock->p2;
4281
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304282 /*
4283 * In Valleyview PLL and program lane counter registers are exposed
4284 * through DPIO interface
4285 */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004286 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4287 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4288 mdiv |= ((bestn << DPIO_N_SHIFT));
4289 mdiv |= (1 << DPIO_POST_DIV_SHIFT);
4290 mdiv |= (1 << DPIO_K_SHIFT);
4291 mdiv |= DPIO_ENABLE_CALIBRATION;
4292 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4293
4294 intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
4295
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304296 pdiv = (1 << DPIO_REFSEL_OVERRIDE) | (5 << DPIO_PLL_MODESEL_SHIFT) |
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004297 (3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304298 (7 << DPIO_PLL_REFCLK_SEL_SHIFT) | (8 << DPIO_DRIVER_CTL_SHIFT) |
4299 (5 << DPIO_CLK_BIAS_CTL_SHIFT);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004300 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
4301
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304302 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x005f003b);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004303
4304 dpll |= DPLL_VCO_ENABLE;
4305 I915_WRITE(DPLL(pipe), dpll);
4306 POSTING_READ(DPLL(pipe));
4307 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4308 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4309
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304310 intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x620);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004311
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304312 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4313 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4314
4315 I915_WRITE(DPLL(pipe), dpll);
4316
4317 /* Wait for the clocks to stabilize. */
4318 POSTING_READ(DPLL(pipe));
4319 udelay(150);
4320
4321 temp = 0;
4322 if (is_sdvo) {
4323 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004324 if (temp > 1)
4325 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4326 else
4327 temp = 0;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004328 }
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304329 I915_WRITE(DPLL_MD(pipe), temp);
4330 POSTING_READ(DPLL_MD(pipe));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004331
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304332 /* Now program lane control registers */
4333 if(intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)
4334 || intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
4335 {
4336 temp = 0x1000C4;
4337 if(pipe == 1)
4338 temp |= (1 << 21);
4339 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL1, temp);
4340 }
4341 if(intel_pipe_has_type(crtc,INTEL_OUTPUT_EDP))
4342 {
4343 temp = 0x1000C4;
4344 if(pipe == 1)
4345 temp |= (1 << 21);
4346 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL2, temp);
4347 }
Daniel Vetter09153002012-12-12 14:06:44 +01004348
4349 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004350}
4351
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004352static void i9xx_update_pll(struct drm_crtc *crtc,
4353 struct drm_display_mode *mode,
4354 struct drm_display_mode *adjusted_mode,
4355 intel_clock_t *clock, intel_clock_t *reduced_clock,
4356 int num_connectors)
4357{
4358 struct drm_device *dev = crtc->dev;
4359 struct drm_i915_private *dev_priv = dev->dev_private;
4360 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004361 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004362 int pipe = intel_crtc->pipe;
4363 u32 dpll;
4364 bool is_sdvo;
4365
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304366 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4367
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004368 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4369 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4370
4371 dpll = DPLL_VGA_MODE_DIS;
4372
4373 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4374 dpll |= DPLLB_MODE_LVDS;
4375 else
4376 dpll |= DPLLB_MODE_DAC_SERIAL;
4377 if (is_sdvo) {
4378 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4379 if (pixel_multiplier > 1) {
4380 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4381 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4382 }
4383 dpll |= DPLL_DVO_HIGH_SPEED;
4384 }
4385 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4386 dpll |= DPLL_DVO_HIGH_SPEED;
4387
4388 /* compute bitmask from p1 value */
4389 if (IS_PINEVIEW(dev))
4390 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4391 else {
4392 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4393 if (IS_G4X(dev) && reduced_clock)
4394 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4395 }
4396 switch (clock->p2) {
4397 case 5:
4398 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4399 break;
4400 case 7:
4401 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4402 break;
4403 case 10:
4404 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4405 break;
4406 case 14:
4407 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4408 break;
4409 }
4410 if (INTEL_INFO(dev)->gen >= 4)
4411 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4412
4413 if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4414 dpll |= PLL_REF_INPUT_TVCLKINBC;
4415 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4416 /* XXX: just matching BIOS for now */
4417 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4418 dpll |= 3;
4419 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4420 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4421 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4422 else
4423 dpll |= PLL_REF_INPUT_DREFCLK;
4424
4425 dpll |= DPLL_VCO_ENABLE;
4426 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4427 POSTING_READ(DPLL(pipe));
4428 udelay(150);
4429
Daniel Vetterdafd2262012-11-26 17:22:07 +01004430 for_each_encoder_on_crtc(dev, crtc, encoder)
4431 if (encoder->pre_pll_enable)
4432 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004433
4434 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4435 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4436
4437 I915_WRITE(DPLL(pipe), dpll);
4438
4439 /* Wait for the clocks to stabilize. */
4440 POSTING_READ(DPLL(pipe));
4441 udelay(150);
4442
4443 if (INTEL_INFO(dev)->gen >= 4) {
4444 u32 temp = 0;
4445 if (is_sdvo) {
4446 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4447 if (temp > 1)
4448 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4449 else
4450 temp = 0;
4451 }
4452 I915_WRITE(DPLL_MD(pipe), temp);
4453 } else {
4454 /* The pixel multiplier can only be updated once the
4455 * DPLL is enabled and the clocks are stable.
4456 *
4457 * So write it again.
4458 */
4459 I915_WRITE(DPLL(pipe), dpll);
4460 }
4461}
4462
4463static void i8xx_update_pll(struct drm_crtc *crtc,
4464 struct drm_display_mode *adjusted_mode,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304465 intel_clock_t *clock, intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004466 int num_connectors)
4467{
4468 struct drm_device *dev = crtc->dev;
4469 struct drm_i915_private *dev_priv = dev->dev_private;
4470 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004471 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004472 int pipe = intel_crtc->pipe;
4473 u32 dpll;
4474
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304475 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4476
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004477 dpll = DPLL_VGA_MODE_DIS;
4478
4479 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4480 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4481 } else {
4482 if (clock->p1 == 2)
4483 dpll |= PLL_P1_DIVIDE_BY_TWO;
4484 else
4485 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4486 if (clock->p2 == 4)
4487 dpll |= PLL_P2_DIVIDE_BY_4;
4488 }
4489
4490 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4491 /* XXX: just matching BIOS for now */
4492 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4493 dpll |= 3;
4494 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4495 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4496 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4497 else
4498 dpll |= PLL_REF_INPUT_DREFCLK;
4499
4500 dpll |= DPLL_VCO_ENABLE;
4501 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4502 POSTING_READ(DPLL(pipe));
4503 udelay(150);
4504
Daniel Vetterdafd2262012-11-26 17:22:07 +01004505 for_each_encoder_on_crtc(dev, crtc, encoder)
4506 if (encoder->pre_pll_enable)
4507 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004508
Daniel Vetter5b5896e2012-09-11 12:37:55 +02004509 I915_WRITE(DPLL(pipe), dpll);
4510
4511 /* Wait for the clocks to stabilize. */
4512 POSTING_READ(DPLL(pipe));
4513 udelay(150);
4514
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004515 /* The pixel multiplier can only be updated once the
4516 * DPLL is enabled and the clocks are stable.
4517 *
4518 * So write it again.
4519 */
4520 I915_WRITE(DPLL(pipe), dpll);
4521}
4522
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004523static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
4524 struct drm_display_mode *mode,
4525 struct drm_display_mode *adjusted_mode)
4526{
4527 struct drm_device *dev = intel_crtc->base.dev;
4528 struct drm_i915_private *dev_priv = dev->dev_private;
4529 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004530 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004531 uint32_t vsyncshift;
4532
4533 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4534 /* the chip adds 2 halflines automatically */
4535 adjusted_mode->crtc_vtotal -= 1;
4536 adjusted_mode->crtc_vblank_end -= 1;
4537 vsyncshift = adjusted_mode->crtc_hsync_start
4538 - adjusted_mode->crtc_htotal / 2;
4539 } else {
4540 vsyncshift = 0;
4541 }
4542
4543 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004544 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004545
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004546 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004547 (adjusted_mode->crtc_hdisplay - 1) |
4548 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004549 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004550 (adjusted_mode->crtc_hblank_start - 1) |
4551 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004552 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004553 (adjusted_mode->crtc_hsync_start - 1) |
4554 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4555
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004556 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004557 (adjusted_mode->crtc_vdisplay - 1) |
4558 ((adjusted_mode->crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004559 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004560 (adjusted_mode->crtc_vblank_start - 1) |
4561 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004562 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004563 (adjusted_mode->crtc_vsync_start - 1) |
4564 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4565
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004566 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4567 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4568 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4569 * bits. */
4570 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4571 (pipe == PIPE_B || pipe == PIPE_C))
4572 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4573
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004574 /* pipesrc controls the size that is scaled from, which should
4575 * always be the user's requested size.
4576 */
4577 I915_WRITE(PIPESRC(pipe),
4578 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4579}
4580
Eric Anholtf564048e2011-03-30 13:01:02 -07004581static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4582 struct drm_display_mode *mode,
4583 struct drm_display_mode *adjusted_mode,
4584 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004585 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004586{
4587 struct drm_device *dev = crtc->dev;
4588 struct drm_i915_private *dev_priv = dev->dev_private;
4589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4590 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004591 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004592 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004593 intel_clock_t clock, reduced_clock;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004594 u32 dspcntr, pipeconf;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004595 bool ok, has_reduced_clock = false, is_sdvo = false;
4596 bool is_lvds = false, is_tv = false, is_dp = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004597 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004598 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004599 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004600
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004601 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004602 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004603 case INTEL_OUTPUT_LVDS:
4604 is_lvds = true;
4605 break;
4606 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08004607 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08004608 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01004609 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004610 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004611 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004612 case INTEL_OUTPUT_TVOUT:
4613 is_tv = true;
4614 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004615 case INTEL_OUTPUT_DISPLAYPORT:
4616 is_dp = true;
4617 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004618 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004619
Eric Anholtc751ce42010-03-25 11:48:48 -07004620 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004621 }
4622
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004623 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004624
Ma Lingd4906092009-03-18 20:13:27 +08004625 /*
4626 * Returns a set of divisors for the desired target clock with the given
4627 * refclk, or FALSE. The returned values represent the clock equation:
4628 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4629 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004630 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08004631 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4632 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004633 if (!ok) {
4634 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004635 return -EINVAL;
4636 }
4637
4638 /* Ensure that the cursor is valid for the new mode before changing... */
4639 intel_crtc_update_cursor(crtc, true);
4640
4641 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004642 /*
4643 * Ensure we match the reduced clock's P to the target clock.
4644 * If the clocks don't match, we can't switch the display clock
4645 * by using the FP0/FP1. In such case we will disable the LVDS
4646 * downclock feature.
4647 */
Eric Anholtf564048e2011-03-30 13:01:02 -07004648 has_reduced_clock = limit->find_pll(limit, crtc,
4649 dev_priv->lvds_downclock,
4650 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08004651 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004652 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004653 }
4654
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004655 if (is_sdvo && is_tv)
4656 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004657
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004658 if (IS_GEN2(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304659 i8xx_update_pll(crtc, adjusted_mode, &clock,
4660 has_reduced_clock ? &reduced_clock : NULL,
4661 num_connectors);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004662 else if (IS_VALLEYVIEW(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304663 vlv_update_pll(crtc, mode, adjusted_mode, &clock,
4664 has_reduced_clock ? &reduced_clock : NULL,
4665 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004666 else
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004667 i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
4668 has_reduced_clock ? &reduced_clock : NULL,
4669 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004670
4671 /* setup pipeconf */
4672 pipeconf = I915_READ(PIPECONF(pipe));
4673
4674 /* Set up the display plane register */
4675 dspcntr = DISPPLANE_GAMMA_ENABLE;
4676
Eric Anholt929c77f2011-03-30 13:01:04 -07004677 if (pipe == 0)
4678 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4679 else
4680 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07004681
4682 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4683 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4684 * core speed.
4685 *
4686 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4687 * pipe == 0 check?
4688 */
4689 if (mode->clock >
4690 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4691 pipeconf |= PIPECONF_DOUBLE_WIDE;
4692 else
4693 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4694 }
4695
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004696 /* default to 8bpc */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004697 pipeconf &= ~(PIPECONF_BPC_MASK | PIPECONF_DITHER_EN);
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004698 if (is_dp) {
Jani Nikula0c96c652012-09-26 18:43:10 +03004699 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004700 pipeconf |= PIPECONF_6BPC |
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004701 PIPECONF_DITHER_EN |
4702 PIPECONF_DITHER_TYPE_SP;
4703 }
4704 }
4705
Gajanan Bhat19c03922012-09-27 19:13:07 +05304706 if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4707 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004708 pipeconf |= PIPECONF_6BPC |
Gajanan Bhat19c03922012-09-27 19:13:07 +05304709 PIPECONF_ENABLE |
4710 I965_PIPECONF_ACTIVE;
4711 }
4712 }
4713
Eric Anholtf564048e2011-03-30 13:01:02 -07004714 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
4715 drm_mode_debug_printmodeline(mode);
4716
Jesse Barnesa7516a02011-12-15 12:30:37 -08004717 if (HAS_PIPE_CXSR(dev)) {
4718 if (intel_crtc->lowfreq_avail) {
Eric Anholtf564048e2011-03-30 13:01:02 -07004719 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4720 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004721 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07004722 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4723 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4724 }
4725 }
4726
Keith Packard617cf882012-02-08 13:53:38 -08004727 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetterdbb02572012-01-28 14:49:23 +01004728 if (!IS_GEN2(dev) &&
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004729 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Eric Anholtf564048e2011-03-30 13:01:02 -07004730 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004731 else
Keith Packard617cf882012-02-08 13:53:38 -08004732 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01004733
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004734 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Eric Anholtf564048e2011-03-30 13:01:02 -07004735
4736 /* pipesrc and dspsize control the size that is scaled from,
4737 * which should always be the user's requested size.
4738 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004739 I915_WRITE(DSPSIZE(plane),
4740 ((mode->vdisplay - 1) << 16) |
4741 (mode->hdisplay - 1));
4742 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004743
Eric Anholtf564048e2011-03-30 13:01:02 -07004744 I915_WRITE(PIPECONF(pipe), pipeconf);
4745 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07004746 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07004747
4748 intel_wait_for_vblank(dev, pipe);
4749
Eric Anholtf564048e2011-03-30 13:01:02 -07004750 I915_WRITE(DSPCNTR(plane), dspcntr);
4751 POSTING_READ(DSPCNTR(plane));
4752
Daniel Vetter94352cf2012-07-05 22:51:56 +02004753 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004754
4755 intel_update_watermarks(dev);
4756
Eric Anholtf564048e2011-03-30 13:01:02 -07004757 return ret;
4758}
4759
Paulo Zanonidde86e22012-12-01 12:04:25 -02004760static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07004761{
4762 struct drm_i915_private *dev_priv = dev->dev_private;
4763 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004764 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004765 u32 temp;
4766 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004767 bool has_cpu_edp = false;
4768 bool has_pch_edp = false;
4769 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07004770 bool has_ck505 = false;
4771 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004772
4773 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07004774 list_for_each_entry(encoder, &mode_config->encoder_list,
4775 base.head) {
4776 switch (encoder->type) {
4777 case INTEL_OUTPUT_LVDS:
4778 has_panel = true;
4779 has_lvds = true;
4780 break;
4781 case INTEL_OUTPUT_EDP:
4782 has_panel = true;
4783 if (intel_encoder_is_pch_edp(&encoder->base))
4784 has_pch_edp = true;
4785 else
4786 has_cpu_edp = true;
4787 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004788 }
4789 }
4790
Keith Packard99eb6a02011-09-26 14:29:12 -07004791 if (HAS_PCH_IBX(dev)) {
4792 has_ck505 = dev_priv->display_clock_mode;
4793 can_ssc = has_ck505;
4794 } else {
4795 has_ck505 = false;
4796 can_ssc = true;
4797 }
4798
4799 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4800 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4801 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004802
4803 /* Ironlake: try to setup display ref clock before DPLL
4804 * enabling. This is only under driver's control after
4805 * PCH B stepping, previous chipset stepping should be
4806 * ignoring this setting.
4807 */
4808 temp = I915_READ(PCH_DREF_CONTROL);
4809 /* Always enable nonspread source */
4810 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004811
Keith Packard99eb6a02011-09-26 14:29:12 -07004812 if (has_ck505)
4813 temp |= DREF_NONSPREAD_CK505_ENABLE;
4814 else
4815 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004816
Keith Packard199e5d72011-09-22 12:01:57 -07004817 if (has_panel) {
4818 temp &= ~DREF_SSC_SOURCE_MASK;
4819 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004820
Keith Packard199e5d72011-09-22 12:01:57 -07004821 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07004822 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004823 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004824 temp |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02004825 } else
4826 temp &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004827
4828 /* Get SSC going before enabling the outputs */
4829 I915_WRITE(PCH_DREF_CONTROL, temp);
4830 POSTING_READ(PCH_DREF_CONTROL);
4831 udelay(200);
4832
Jesse Barnes13d83a62011-08-03 12:59:20 -07004833 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4834
4835 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07004836 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07004837 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004838 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004839 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004840 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07004841 else
4842 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004843 } else
4844 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4845
4846 I915_WRITE(PCH_DREF_CONTROL, temp);
4847 POSTING_READ(PCH_DREF_CONTROL);
4848 udelay(200);
4849 } else {
4850 DRM_DEBUG_KMS("Disabling SSC entirely\n");
4851
4852 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4853
4854 /* Turn off CPU output */
4855 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4856
4857 I915_WRITE(PCH_DREF_CONTROL, temp);
4858 POSTING_READ(PCH_DREF_CONTROL);
4859 udelay(200);
4860
4861 /* Turn off the SSC source */
4862 temp &= ~DREF_SSC_SOURCE_MASK;
4863 temp |= DREF_SSC_SOURCE_DISABLE;
4864
4865 /* Turn off SSC1 */
4866 temp &= ~ DREF_SSC1_ENABLE;
4867
Jesse Barnes13d83a62011-08-03 12:59:20 -07004868 I915_WRITE(PCH_DREF_CONTROL, temp);
4869 POSTING_READ(PCH_DREF_CONTROL);
4870 udelay(200);
4871 }
4872}
4873
Paulo Zanonidde86e22012-12-01 12:04:25 -02004874/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
4875static void lpt_init_pch_refclk(struct drm_device *dev)
4876{
4877 struct drm_i915_private *dev_priv = dev->dev_private;
4878 struct drm_mode_config *mode_config = &dev->mode_config;
4879 struct intel_encoder *encoder;
4880 bool has_vga = false;
4881 bool is_sdv = false;
4882 u32 tmp;
4883
4884 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
4885 switch (encoder->type) {
4886 case INTEL_OUTPUT_ANALOG:
4887 has_vga = true;
4888 break;
4889 }
4890 }
4891
4892 if (!has_vga)
4893 return;
4894
Daniel Vetterc00db242013-01-22 15:33:27 +01004895 mutex_lock(&dev_priv->dpio_lock);
4896
Paulo Zanonidde86e22012-12-01 12:04:25 -02004897 /* XXX: Rip out SDV support once Haswell ships for real. */
4898 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
4899 is_sdv = true;
4900
4901 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4902 tmp &= ~SBI_SSCCTL_DISABLE;
4903 tmp |= SBI_SSCCTL_PATHALT;
4904 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4905
4906 udelay(24);
4907
4908 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4909 tmp &= ~SBI_SSCCTL_PATHALT;
4910 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4911
4912 if (!is_sdv) {
4913 tmp = I915_READ(SOUTH_CHICKEN2);
4914 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
4915 I915_WRITE(SOUTH_CHICKEN2, tmp);
4916
4917 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
4918 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
4919 DRM_ERROR("FDI mPHY reset assert timeout\n");
4920
4921 tmp = I915_READ(SOUTH_CHICKEN2);
4922 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
4923 I915_WRITE(SOUTH_CHICKEN2, tmp);
4924
4925 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
4926 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
4927 100))
4928 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
4929 }
4930
4931 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
4932 tmp &= ~(0xFF << 24);
4933 tmp |= (0x12 << 24);
4934 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
4935
4936 if (!is_sdv) {
4937 tmp = intel_sbi_read(dev_priv, 0x808C, SBI_MPHY);
4938 tmp &= ~(0x3 << 6);
4939 tmp |= (1 << 6) | (1 << 0);
4940 intel_sbi_write(dev_priv, 0x808C, tmp, SBI_MPHY);
4941 }
4942
4943 if (is_sdv) {
4944 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
4945 tmp |= 0x7FFF;
4946 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
4947 }
4948
4949 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
4950 tmp |= (1 << 11);
4951 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
4952
4953 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
4954 tmp |= (1 << 11);
4955 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
4956
4957 if (is_sdv) {
4958 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
4959 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
4960 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
4961
4962 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
4963 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
4964 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
4965
4966 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
4967 tmp |= (0x3F << 8);
4968 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
4969
4970 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
4971 tmp |= (0x3F << 8);
4972 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
4973 }
4974
4975 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
4976 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
4977 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
4978
4979 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
4980 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
4981 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
4982
4983 if (!is_sdv) {
4984 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
4985 tmp &= ~(7 << 13);
4986 tmp |= (5 << 13);
4987 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
4988
4989 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
4990 tmp &= ~(7 << 13);
4991 tmp |= (5 << 13);
4992 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
4993 }
4994
4995 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
4996 tmp &= ~0xFF;
4997 tmp |= 0x1C;
4998 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
4999
5000 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5001 tmp &= ~0xFF;
5002 tmp |= 0x1C;
5003 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5004
5005 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5006 tmp &= ~(0xFF << 16);
5007 tmp |= (0x1C << 16);
5008 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5009
5010 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5011 tmp &= ~(0xFF << 16);
5012 tmp |= (0x1C << 16);
5013 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5014
5015 if (!is_sdv) {
5016 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5017 tmp |= (1 << 27);
5018 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5019
5020 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5021 tmp |= (1 << 27);
5022 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5023
5024 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5025 tmp &= ~(0xF << 28);
5026 tmp |= (4 << 28);
5027 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5028
5029 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5030 tmp &= ~(0xF << 28);
5031 tmp |= (4 << 28);
5032 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5033 }
5034
5035 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5036 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5037 tmp |= SBI_DBUFF0_ENABLE;
5038 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005039
5040 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005041}
5042
5043/*
5044 * Initialize reference clocks when the driver loads
5045 */
5046void intel_init_pch_refclk(struct drm_device *dev)
5047{
5048 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5049 ironlake_init_pch_refclk(dev);
5050 else if (HAS_PCH_LPT(dev))
5051 lpt_init_pch_refclk(dev);
5052}
5053
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005054static int ironlake_get_refclk(struct drm_crtc *crtc)
5055{
5056 struct drm_device *dev = crtc->dev;
5057 struct drm_i915_private *dev_priv = dev->dev_private;
5058 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005059 struct intel_encoder *edp_encoder = NULL;
5060 int num_connectors = 0;
5061 bool is_lvds = false;
5062
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005063 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005064 switch (encoder->type) {
5065 case INTEL_OUTPUT_LVDS:
5066 is_lvds = true;
5067 break;
5068 case INTEL_OUTPUT_EDP:
5069 edp_encoder = encoder;
5070 break;
5071 }
5072 num_connectors++;
5073 }
5074
5075 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5076 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5077 dev_priv->lvds_ssc_freq);
5078 return dev_priv->lvds_ssc_freq * 1000;
5079 }
5080
5081 return 120000;
5082}
5083
Paulo Zanonic8203562012-09-12 10:06:29 -03005084static void ironlake_set_pipeconf(struct drm_crtc *crtc,
5085 struct drm_display_mode *adjusted_mode,
5086 bool dither)
5087{
5088 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5089 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5090 int pipe = intel_crtc->pipe;
5091 uint32_t val;
5092
5093 val = I915_READ(PIPECONF(pipe));
5094
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005095 val &= ~PIPECONF_BPC_MASK;
Paulo Zanonic8203562012-09-12 10:06:29 -03005096 switch (intel_crtc->bpp) {
5097 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005098 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005099 break;
5100 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005101 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005102 break;
5103 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005104 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005105 break;
5106 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005107 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005108 break;
5109 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005110 /* Case prevented by intel_choose_pipe_bpp_dither. */
5111 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005112 }
5113
5114 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5115 if (dither)
5116 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5117
5118 val &= ~PIPECONF_INTERLACE_MASK;
5119 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5120 val |= PIPECONF_INTERLACED_ILK;
5121 else
5122 val |= PIPECONF_PROGRESSIVE;
5123
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005124 if (adjusted_mode->private_flags & INTEL_MODE_LIMITED_COLOR_RANGE)
5125 val |= PIPECONF_COLOR_RANGE_SELECT;
5126 else
5127 val &= ~PIPECONF_COLOR_RANGE_SELECT;
5128
Paulo Zanonic8203562012-09-12 10:06:29 -03005129 I915_WRITE(PIPECONF(pipe), val);
5130 POSTING_READ(PIPECONF(pipe));
5131}
5132
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005133static void haswell_set_pipeconf(struct drm_crtc *crtc,
5134 struct drm_display_mode *adjusted_mode,
5135 bool dither)
5136{
5137 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5138 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005139 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005140 uint32_t val;
5141
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005142 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005143
5144 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5145 if (dither)
5146 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5147
5148 val &= ~PIPECONF_INTERLACE_MASK_HSW;
5149 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5150 val |= PIPECONF_INTERLACED_ILK;
5151 else
5152 val |= PIPECONF_PROGRESSIVE;
5153
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005154 I915_WRITE(PIPECONF(cpu_transcoder), val);
5155 POSTING_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005156}
5157
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005158static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5159 struct drm_display_mode *adjusted_mode,
5160 intel_clock_t *clock,
5161 bool *has_reduced_clock,
5162 intel_clock_t *reduced_clock)
5163{
5164 struct drm_device *dev = crtc->dev;
5165 struct drm_i915_private *dev_priv = dev->dev_private;
5166 struct intel_encoder *intel_encoder;
5167 int refclk;
5168 const intel_limit_t *limit;
5169 bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
5170
5171 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5172 switch (intel_encoder->type) {
5173 case INTEL_OUTPUT_LVDS:
5174 is_lvds = true;
5175 break;
5176 case INTEL_OUTPUT_SDVO:
5177 case INTEL_OUTPUT_HDMI:
5178 is_sdvo = true;
5179 if (intel_encoder->needs_tv_clock)
5180 is_tv = true;
5181 break;
5182 case INTEL_OUTPUT_TVOUT:
5183 is_tv = true;
5184 break;
5185 }
5186 }
5187
5188 refclk = ironlake_get_refclk(crtc);
5189
5190 /*
5191 * Returns a set of divisors for the desired target clock with the given
5192 * refclk, or FALSE. The returned values represent the clock equation:
5193 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5194 */
5195 limit = intel_limit(crtc, refclk);
5196 ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5197 clock);
5198 if (!ret)
5199 return false;
5200
5201 if (is_lvds && dev_priv->lvds_downclock_avail) {
5202 /*
5203 * Ensure we match the reduced clock's P to the target clock.
5204 * If the clocks don't match, we can't switch the display clock
5205 * by using the FP0/FP1. In such case we will disable the LVDS
5206 * downclock feature.
5207 */
5208 *has_reduced_clock = limit->find_pll(limit, crtc,
5209 dev_priv->lvds_downclock,
5210 refclk,
5211 clock,
5212 reduced_clock);
5213 }
5214
5215 if (is_sdvo && is_tv)
5216 i9xx_adjust_sdvo_tv_clock(adjusted_mode, clock);
5217
5218 return true;
5219}
5220
Daniel Vetter01a415f2012-10-27 15:58:40 +02005221static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5222{
5223 struct drm_i915_private *dev_priv = dev->dev_private;
5224 uint32_t temp;
5225
5226 temp = I915_READ(SOUTH_CHICKEN1);
5227 if (temp & FDI_BC_BIFURCATION_SELECT)
5228 return;
5229
5230 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5231 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5232
5233 temp |= FDI_BC_BIFURCATION_SELECT;
5234 DRM_DEBUG_KMS("enabling fdi C rx\n");
5235 I915_WRITE(SOUTH_CHICKEN1, temp);
5236 POSTING_READ(SOUTH_CHICKEN1);
5237}
5238
5239static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
5240{
5241 struct drm_device *dev = intel_crtc->base.dev;
5242 struct drm_i915_private *dev_priv = dev->dev_private;
5243 struct intel_crtc *pipe_B_crtc =
5244 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5245
5246 DRM_DEBUG_KMS("checking fdi config on pipe %i, lanes %i\n",
5247 intel_crtc->pipe, intel_crtc->fdi_lanes);
5248 if (intel_crtc->fdi_lanes > 4) {
5249 DRM_DEBUG_KMS("invalid fdi lane config on pipe %i: %i lanes\n",
5250 intel_crtc->pipe, intel_crtc->fdi_lanes);
5251 /* Clamp lanes to avoid programming the hw with bogus values. */
5252 intel_crtc->fdi_lanes = 4;
5253
5254 return false;
5255 }
5256
5257 if (dev_priv->num_pipe == 2)
5258 return true;
5259
5260 switch (intel_crtc->pipe) {
5261 case PIPE_A:
5262 return true;
5263 case PIPE_B:
5264 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5265 intel_crtc->fdi_lanes > 2) {
5266 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5267 intel_crtc->pipe, intel_crtc->fdi_lanes);
5268 /* Clamp lanes to avoid programming the hw with bogus values. */
5269 intel_crtc->fdi_lanes = 2;
5270
5271 return false;
5272 }
5273
5274 if (intel_crtc->fdi_lanes > 2)
5275 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5276 else
5277 cpt_enable_fdi_bc_bifurcation(dev);
5278
5279 return true;
5280 case PIPE_C:
5281 if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
5282 if (intel_crtc->fdi_lanes > 2) {
5283 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5284 intel_crtc->pipe, intel_crtc->fdi_lanes);
5285 /* Clamp lanes to avoid programming the hw with bogus values. */
5286 intel_crtc->fdi_lanes = 2;
5287
5288 return false;
5289 }
5290 } else {
5291 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5292 return false;
5293 }
5294
5295 cpt_enable_fdi_bc_bifurcation(dev);
5296
5297 return true;
5298 default:
5299 BUG();
5300 }
5301}
5302
Paulo Zanonid4b19312012-11-29 11:29:32 -02005303int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5304{
5305 /*
5306 * Account for spread spectrum to avoid
5307 * oversubscribing the link. Max center spread
5308 * is 2.5%; use 5% for safety's sake.
5309 */
5310 u32 bps = target_clock * bpp * 21 / 20;
5311 return bps / (link_bw * 8) + 1;
5312}
5313
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005314static void ironlake_set_m_n(struct drm_crtc *crtc,
5315 struct drm_display_mode *mode,
5316 struct drm_display_mode *adjusted_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08005317{
5318 struct drm_device *dev = crtc->dev;
5319 struct drm_i915_private *dev_priv = dev->dev_private;
5320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005321 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005322 struct intel_encoder *intel_encoder, *edp_encoder = NULL;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005323 struct intel_link_m_n m_n = {0};
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005324 int target_clock, pixel_multiplier, lane, link_bw;
5325 bool is_dp = false, is_cpu_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005326
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005327 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5328 switch (intel_encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005329 case INTEL_OUTPUT_DISPLAYPORT:
5330 is_dp = true;
5331 break;
5332 case INTEL_OUTPUT_EDP:
Jesse Barnese3aef172012-04-10 11:58:03 -07005333 is_dp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005334 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnese3aef172012-04-10 11:58:03 -07005335 is_cpu_edp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005336 edp_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005337 break;
5338 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005339 }
5340
Zhenyu Wang2c072452009-06-05 15:38:42 +08005341 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07005342 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5343 lane = 0;
5344 /* CPU eDP doesn't require FDI link, so just set DP M/N
5345 according to current link config */
Jesse Barnese3aef172012-04-10 11:58:03 -07005346 if (is_cpu_edp) {
Jesse Barnese3aef172012-04-10 11:58:03 -07005347 intel_edp_link_config(edp_encoder, &lane, &link_bw);
Eric Anholt8febb292011-03-30 13:01:07 -07005348 } else {
Eric Anholt8febb292011-03-30 13:01:07 -07005349 /* FDI is a binary signal running at ~2.7GHz, encoding
5350 * each output octet as 10 bits. The actual frequency
5351 * is stored as a divider into a 100MHz clock, and the
5352 * mode pixel clock is stored in units of 1KHz.
5353 * Hence the bw of each lane in terms of the mode signal
5354 * is:
5355 */
5356 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005357 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08005358
Daniel Vetter94bf2ce2012-06-04 18:39:19 +02005359 /* [e]DP over FDI requires target mode clock instead of link clock. */
5360 if (edp_encoder)
5361 target_clock = intel_edp_target_clock(edp_encoder, mode);
5362 else if (is_dp)
5363 target_clock = mode->clock;
5364 else
5365 target_clock = adjusted_mode->clock;
5366
Paulo Zanonid4b19312012-11-29 11:29:32 -02005367 if (!lane)
5368 lane = ironlake_get_lanes_required(target_clock, link_bw,
5369 intel_crtc->bpp);
Eric Anholt8febb292011-03-30 13:01:07 -07005370
5371 intel_crtc->fdi_lanes = lane;
5372
5373 if (pixel_multiplier > 1)
5374 link_bw *= pixel_multiplier;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005375 intel_link_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw, &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07005376
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005377 I915_WRITE(PIPE_DATA_M1(cpu_transcoder), TU_SIZE(m_n.tu) | m_n.gmch_m);
5378 I915_WRITE(PIPE_DATA_N1(cpu_transcoder), m_n.gmch_n);
5379 I915_WRITE(PIPE_LINK_M1(cpu_transcoder), m_n.link_m);
5380 I915_WRITE(PIPE_LINK_N1(cpu_transcoder), m_n.link_n);
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005381}
5382
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005383static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5384 struct drm_display_mode *adjusted_mode,
5385 intel_clock_t *clock, u32 fp)
5386{
5387 struct drm_crtc *crtc = &intel_crtc->base;
5388 struct drm_device *dev = crtc->dev;
5389 struct drm_i915_private *dev_priv = dev->dev_private;
5390 struct intel_encoder *intel_encoder;
5391 uint32_t dpll;
5392 int factor, pixel_multiplier, num_connectors = 0;
5393 bool is_lvds = false, is_sdvo = false, is_tv = false;
5394 bool is_dp = false, is_cpu_edp = false;
5395
5396 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5397 switch (intel_encoder->type) {
5398 case INTEL_OUTPUT_LVDS:
5399 is_lvds = true;
5400 break;
5401 case INTEL_OUTPUT_SDVO:
5402 case INTEL_OUTPUT_HDMI:
5403 is_sdvo = true;
5404 if (intel_encoder->needs_tv_clock)
5405 is_tv = true;
5406 break;
5407 case INTEL_OUTPUT_TVOUT:
5408 is_tv = true;
5409 break;
5410 case INTEL_OUTPUT_DISPLAYPORT:
5411 is_dp = true;
5412 break;
5413 case INTEL_OUTPUT_EDP:
5414 is_dp = true;
5415 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
5416 is_cpu_edp = true;
5417 break;
5418 }
5419
5420 num_connectors++;
5421 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005422
Chris Wilsonc1858122010-12-03 21:35:48 +00005423 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005424 factor = 21;
5425 if (is_lvds) {
5426 if ((intel_panel_use_ssc(dev_priv) &&
5427 dev_priv->lvds_ssc_freq == 100) ||
Daniel Vetter1974cad2012-11-26 17:22:09 +01005428 intel_is_dual_link_lvds(dev))
Eric Anholt8febb292011-03-30 13:01:07 -07005429 factor = 25;
5430 } else if (is_sdvo && is_tv)
5431 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005432
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005433 if (clock->m < factor * clock->n)
Eric Anholt8febb292011-03-30 13:01:07 -07005434 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005435
Chris Wilson5eddb702010-09-11 13:48:45 +01005436 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005437
Eric Anholta07d6782011-03-30 13:01:08 -07005438 if (is_lvds)
5439 dpll |= DPLLB_MODE_LVDS;
5440 else
5441 dpll |= DPLLB_MODE_DAC_SERIAL;
5442 if (is_sdvo) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005443 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
Eric Anholta07d6782011-03-30 13:01:08 -07005444 if (pixel_multiplier > 1) {
5445 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08005446 }
Eric Anholta07d6782011-03-30 13:01:08 -07005447 dpll |= DPLL_DVO_HIGH_SPEED;
5448 }
Jesse Barnese3aef172012-04-10 11:58:03 -07005449 if (is_dp && !is_cpu_edp)
Eric Anholta07d6782011-03-30 13:01:08 -07005450 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005451
Eric Anholta07d6782011-03-30 13:01:08 -07005452 /* compute bitmask from p1 value */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005453 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005454 /* also FPA1 */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005455 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005456
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005457 switch (clock->p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005458 case 5:
5459 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5460 break;
5461 case 7:
5462 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5463 break;
5464 case 10:
5465 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5466 break;
5467 case 14:
5468 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5469 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005470 }
5471
5472 if (is_sdvo && is_tv)
5473 dpll |= PLL_REF_INPUT_TVCLKINBC;
5474 else if (is_tv)
5475 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005476 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08005477 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00005478 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005479 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005480 else
5481 dpll |= PLL_REF_INPUT_DREFCLK;
5482
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005483 return dpll;
5484}
5485
Jesse Barnes79e53942008-11-07 14:24:08 -08005486static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5487 struct drm_display_mode *mode,
5488 struct drm_display_mode *adjusted_mode,
5489 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005490 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005491{
5492 struct drm_device *dev = crtc->dev;
5493 struct drm_i915_private *dev_priv = dev->dev_private;
5494 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5495 int pipe = intel_crtc->pipe;
5496 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005497 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005498 intel_clock_t clock, reduced_clock;
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005499 u32 dpll, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005500 bool ok, has_reduced_clock = false;
5501 bool is_lvds = false, is_dp = false, is_cpu_edp = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005502 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005503 int ret;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005504 bool dither, fdi_config_ok;
Jesse Barnes79e53942008-11-07 14:24:08 -08005505
5506 for_each_encoder_on_crtc(dev, crtc, encoder) {
5507 switch (encoder->type) {
5508 case INTEL_OUTPUT_LVDS:
5509 is_lvds = true;
5510 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005511 case INTEL_OUTPUT_DISPLAYPORT:
5512 is_dp = true;
5513 break;
5514 case INTEL_OUTPUT_EDP:
5515 is_dp = true;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005516 if (!intel_encoder_is_pch_edp(&encoder->base))
Jesse Barnes79e53942008-11-07 14:24:08 -08005517 is_cpu_edp = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005518 break;
5519 }
5520
5521 num_connectors++;
5522 }
5523
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005524 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5525 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5526
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005527 ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
5528 &has_reduced_clock, &reduced_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005529 if (!ok) {
5530 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5531 return -EINVAL;
5532 }
5533
5534 /* Ensure that the cursor is valid for the new mode before changing... */
5535 intel_crtc_update_cursor(crtc, true);
5536
Jesse Barnes79e53942008-11-07 14:24:08 -08005537 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005538 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5539 adjusted_mode);
Paulo Zanonic8203562012-09-12 10:06:29 -03005540 if (is_lvds && dev_priv->lvds_dither)
5541 dither = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005542
Jesse Barnes79e53942008-11-07 14:24:08 -08005543 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5544 if (has_reduced_clock)
5545 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5546 reduced_clock.m2;
5547
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005548 dpll = ironlake_compute_dpll(intel_crtc, adjusted_mode, &clock, fp);
Jesse Barnes79e53942008-11-07 14:24:08 -08005549
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07005550 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005551 drm_mode_debug_printmodeline(mode);
5552
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005553 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5554 if (!is_cpu_edp) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005555 struct intel_pch_pll *pll;
Chris Wilson5eddb702010-09-11 13:48:45 +01005556
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005557 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5558 if (pll == NULL) {
5559 DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
5560 pipe);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005561 return -EINVAL;
5562 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005563 } else
5564 intel_put_pch_pll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005565
Daniel Vetter2f0c2ad2012-11-29 15:59:35 +01005566 if (is_dp && !is_cpu_edp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005567 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08005568
Daniel Vetterdafd2262012-11-26 17:22:07 +01005569 for_each_encoder_on_crtc(dev, crtc, encoder)
5570 if (encoder->pre_pll_enable)
5571 encoder->pre_pll_enable(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08005572
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005573 if (intel_crtc->pch_pll) {
5574 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005575
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005576 /* Wait for the clocks to stabilize. */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005577 POSTING_READ(intel_crtc->pch_pll->pll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005578 udelay(150);
5579
Eric Anholt8febb292011-03-30 13:01:07 -07005580 /* The pixel multiplier can only be updated once the
5581 * DPLL is enabled and the clocks are stable.
5582 *
5583 * So write it again.
5584 */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005585 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08005586 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005587
Chris Wilson5eddb702010-09-11 13:48:45 +01005588 intel_crtc->lowfreq_avail = false;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005589 if (intel_crtc->pch_pll) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07005590 if (is_lvds && has_reduced_clock && i915_powersave) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005591 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005592 intel_crtc->lowfreq_avail = true;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005593 } else {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005594 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07005595 }
5596 }
5597
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005598 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005599
Daniel Vetter01a415f2012-10-27 15:58:40 +02005600 /* Note, this also computes intel_crtc->fdi_lanes which is used below in
5601 * ironlake_check_fdi_lanes. */
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005602 ironlake_set_m_n(crtc, mode, adjusted_mode);
Chris Wilson5eddb702010-09-11 13:48:45 +01005603
Daniel Vetter01a415f2012-10-27 15:58:40 +02005604 fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005605
Paulo Zanonic8203562012-09-12 10:06:29 -03005606 ironlake_set_pipeconf(crtc, adjusted_mode, dither);
Jesse Barnes79e53942008-11-07 14:24:08 -08005607
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005608 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005609
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005610 /* Set up the display plane register */
5611 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005612 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005613
Daniel Vetter94352cf2012-07-05 22:51:56 +02005614 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005615
5616 intel_update_watermarks(dev);
5617
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03005618 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5619
Daniel Vetter01a415f2012-10-27 15:58:40 +02005620 return fdi_config_ok ? ret : -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005621}
5622
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005623static void haswell_modeset_global_resources(struct drm_device *dev)
5624{
5625 struct drm_i915_private *dev_priv = dev->dev_private;
5626 bool enable = false;
5627 struct intel_crtc *crtc;
5628 struct intel_encoder *encoder;
5629
5630 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
5631 if (crtc->pipe != PIPE_A && crtc->base.enabled)
5632 enable = true;
5633 /* XXX: Should check for edp transcoder here, but thanks to init
5634 * sequence that's not yet available. Just in case desktop eDP
5635 * on PORT D is possible on haswell, too. */
5636 }
5637
5638 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
5639 base.head) {
5640 if (encoder->type != INTEL_OUTPUT_EDP &&
5641 encoder->connectors_active)
5642 enable = true;
5643 }
5644
5645 /* Even the eDP panel fitter is outside the always-on well. */
5646 if (dev_priv->pch_pf_size)
5647 enable = true;
5648
5649 intel_set_power_well(dev, enable);
5650}
5651
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005652static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5653 struct drm_display_mode *mode,
5654 struct drm_display_mode *adjusted_mode,
5655 int x, int y,
5656 struct drm_framebuffer *fb)
5657{
5658 struct drm_device *dev = crtc->dev;
5659 struct drm_i915_private *dev_priv = dev->dev_private;
5660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5661 int pipe = intel_crtc->pipe;
5662 int plane = intel_crtc->plane;
5663 int num_connectors = 0;
Daniel Vettered7ef432012-12-06 14:24:21 +01005664 bool is_dp = false, is_cpu_edp = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005665 struct intel_encoder *encoder;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005666 int ret;
5667 bool dither;
5668
5669 for_each_encoder_on_crtc(dev, crtc, encoder) {
5670 switch (encoder->type) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005671 case INTEL_OUTPUT_DISPLAYPORT:
5672 is_dp = true;
5673 break;
5674 case INTEL_OUTPUT_EDP:
5675 is_dp = true;
5676 if (!intel_encoder_is_pch_edp(&encoder->base))
5677 is_cpu_edp = true;
5678 break;
5679 }
5680
5681 num_connectors++;
5682 }
5683
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005684 /* We are not sure yet this won't happen. */
5685 WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
5686 INTEL_PCH_TYPE(dev));
5687
5688 WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
5689 num_connectors, pipe_name(pipe));
5690
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005691 WARN_ON(I915_READ(PIPECONF(intel_crtc->cpu_transcoder)) &
Paulo Zanoni1ce42922012-10-05 12:06:01 -03005692 (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
5693
5694 WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
5695
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005696 if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
5697 return -EINVAL;
5698
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005699 /* Ensure that the cursor is valid for the new mode before changing... */
5700 intel_crtc_update_cursor(crtc, true);
5701
5702 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005703 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5704 adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005705
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005706 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
5707 drm_mode_debug_printmodeline(mode);
5708
Daniel Vettered7ef432012-12-06 14:24:21 +01005709 if (is_dp && !is_cpu_edp)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005710 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005711
5712 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005713
5714 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5715
Paulo Zanoni1eb8dfe2012-10-18 12:42:10 -03005716 if (!is_dp || is_cpu_edp)
5717 ironlake_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005718
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005719 haswell_set_pipeconf(crtc, adjusted_mode, dither);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005720
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005721 /* Set up the display plane register */
5722 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
5723 POSTING_READ(DSPCNTR(plane));
5724
5725 ret = intel_pipe_set_base(crtc, x, y, fb);
5726
5727 intel_update_watermarks(dev);
5728
5729 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5730
Jesse Barnes79e53942008-11-07 14:24:08 -08005731 return ret;
5732}
5733
Eric Anholtf564048e2011-03-30 13:01:02 -07005734static int intel_crtc_mode_set(struct drm_crtc *crtc,
5735 struct drm_display_mode *mode,
5736 struct drm_display_mode *adjusted_mode,
5737 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005738 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07005739{
5740 struct drm_device *dev = crtc->dev;
5741 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01005742 struct drm_encoder_helper_funcs *encoder_funcs;
5743 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07005744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5745 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07005746 int ret;
5747
Paulo Zanonicc464b22013-01-25 16:59:16 -02005748 if (IS_HASWELL(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
5749 intel_crtc->cpu_transcoder = TRANSCODER_EDP;
5750 else
5751 intel_crtc->cpu_transcoder = pipe;
5752
Eric Anholt0b701d22011-03-30 13:01:03 -07005753 drm_vblank_pre_modeset(dev, pipe);
5754
Eric Anholtf564048e2011-03-30 13:01:02 -07005755 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005756 x, y, fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08005757 drm_vblank_post_modeset(dev, pipe);
5758
Daniel Vetter9256aa12012-10-31 19:26:13 +01005759 if (ret != 0)
5760 return ret;
5761
5762 for_each_encoder_on_crtc(dev, crtc, encoder) {
5763 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
5764 encoder->base.base.id,
5765 drm_get_encoder_name(&encoder->base),
5766 mode->base.id, mode->name);
5767 encoder_funcs = encoder->base.helper_private;
5768 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
5769 }
5770
5771 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005772}
5773
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005774static bool intel_eld_uptodate(struct drm_connector *connector,
5775 int reg_eldv, uint32_t bits_eldv,
5776 int reg_elda, uint32_t bits_elda,
5777 int reg_edid)
5778{
5779 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5780 uint8_t *eld = connector->eld;
5781 uint32_t i;
5782
5783 i = I915_READ(reg_eldv);
5784 i &= bits_eldv;
5785
5786 if (!eld[0])
5787 return !i;
5788
5789 if (!i)
5790 return false;
5791
5792 i = I915_READ(reg_elda);
5793 i &= ~bits_elda;
5794 I915_WRITE(reg_elda, i);
5795
5796 for (i = 0; i < eld[2]; i++)
5797 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
5798 return false;
5799
5800 return true;
5801}
5802
Wu Fengguange0dac652011-09-05 14:25:34 +08005803static void g4x_write_eld(struct drm_connector *connector,
5804 struct drm_crtc *crtc)
5805{
5806 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5807 uint8_t *eld = connector->eld;
5808 uint32_t eldv;
5809 uint32_t len;
5810 uint32_t i;
5811
5812 i = I915_READ(G4X_AUD_VID_DID);
5813
5814 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
5815 eldv = G4X_ELDV_DEVCL_DEVBLC;
5816 else
5817 eldv = G4X_ELDV_DEVCTG;
5818
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005819 if (intel_eld_uptodate(connector,
5820 G4X_AUD_CNTL_ST, eldv,
5821 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
5822 G4X_HDMIW_HDMIEDID))
5823 return;
5824
Wu Fengguange0dac652011-09-05 14:25:34 +08005825 i = I915_READ(G4X_AUD_CNTL_ST);
5826 i &= ~(eldv | G4X_ELD_ADDR);
5827 len = (i >> 9) & 0x1f; /* ELD buffer size */
5828 I915_WRITE(G4X_AUD_CNTL_ST, i);
5829
5830 if (!eld[0])
5831 return;
5832
5833 len = min_t(uint8_t, eld[2], len);
5834 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5835 for (i = 0; i < len; i++)
5836 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
5837
5838 i = I915_READ(G4X_AUD_CNTL_ST);
5839 i |= eldv;
5840 I915_WRITE(G4X_AUD_CNTL_ST, i);
5841}
5842
Wang Xingchao83358c852012-08-16 22:43:37 +08005843static void haswell_write_eld(struct drm_connector *connector,
5844 struct drm_crtc *crtc)
5845{
5846 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5847 uint8_t *eld = connector->eld;
5848 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08005849 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08005850 uint32_t eldv;
5851 uint32_t i;
5852 int len;
5853 int pipe = to_intel_crtc(crtc)->pipe;
5854 int tmp;
5855
5856 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
5857 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
5858 int aud_config = HSW_AUD_CFG(pipe);
5859 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
5860
5861
5862 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
5863
5864 /* Audio output enable */
5865 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
5866 tmp = I915_READ(aud_cntrl_st2);
5867 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
5868 I915_WRITE(aud_cntrl_st2, tmp);
5869
5870 /* Wait for 1 vertical blank */
5871 intel_wait_for_vblank(dev, pipe);
5872
5873 /* Set ELD valid state */
5874 tmp = I915_READ(aud_cntrl_st2);
5875 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
5876 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
5877 I915_WRITE(aud_cntrl_st2, tmp);
5878 tmp = I915_READ(aud_cntrl_st2);
5879 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
5880
5881 /* Enable HDMI mode */
5882 tmp = I915_READ(aud_config);
5883 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
5884 /* clear N_programing_enable and N_value_index */
5885 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
5886 I915_WRITE(aud_config, tmp);
5887
5888 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5889
5890 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08005891 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08005892
5893 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5894 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5895 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
5896 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5897 } else
5898 I915_WRITE(aud_config, 0);
5899
5900 if (intel_eld_uptodate(connector,
5901 aud_cntrl_st2, eldv,
5902 aud_cntl_st, IBX_ELD_ADDRESS,
5903 hdmiw_hdmiedid))
5904 return;
5905
5906 i = I915_READ(aud_cntrl_st2);
5907 i &= ~eldv;
5908 I915_WRITE(aud_cntrl_st2, i);
5909
5910 if (!eld[0])
5911 return;
5912
5913 i = I915_READ(aud_cntl_st);
5914 i &= ~IBX_ELD_ADDRESS;
5915 I915_WRITE(aud_cntl_st, i);
5916 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
5917 DRM_DEBUG_DRIVER("port num:%d\n", i);
5918
5919 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5920 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5921 for (i = 0; i < len; i++)
5922 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5923
5924 i = I915_READ(aud_cntrl_st2);
5925 i |= eldv;
5926 I915_WRITE(aud_cntrl_st2, i);
5927
5928}
5929
Wu Fengguange0dac652011-09-05 14:25:34 +08005930static void ironlake_write_eld(struct drm_connector *connector,
5931 struct drm_crtc *crtc)
5932{
5933 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5934 uint8_t *eld = connector->eld;
5935 uint32_t eldv;
5936 uint32_t i;
5937 int len;
5938 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06005939 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08005940 int aud_cntl_st;
5941 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08005942 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08005943
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08005944 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08005945 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
5946 aud_config = IBX_AUD_CFG(pipe);
5947 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005948 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08005949 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08005950 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
5951 aud_config = CPT_AUD_CFG(pipe);
5952 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005953 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08005954 }
5955
Wang Xingchao9b138a82012-08-09 16:52:18 +08005956 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08005957
5958 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08005959 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08005960 if (!i) {
5961 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
5962 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005963 eldv = IBX_ELD_VALIDB;
5964 eldv |= IBX_ELD_VALIDB << 4;
5965 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08005966 } else {
5967 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005968 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08005969 }
5970
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005971 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
5972 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
5973 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06005974 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
5975 } else
5976 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005977
5978 if (intel_eld_uptodate(connector,
5979 aud_cntrl_st2, eldv,
5980 aud_cntl_st, IBX_ELD_ADDRESS,
5981 hdmiw_hdmiedid))
5982 return;
5983
Wu Fengguange0dac652011-09-05 14:25:34 +08005984 i = I915_READ(aud_cntrl_st2);
5985 i &= ~eldv;
5986 I915_WRITE(aud_cntrl_st2, i);
5987
5988 if (!eld[0])
5989 return;
5990
Wu Fengguange0dac652011-09-05 14:25:34 +08005991 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08005992 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08005993 I915_WRITE(aud_cntl_st, i);
5994
5995 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
5996 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5997 for (i = 0; i < len; i++)
5998 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
5999
6000 i = I915_READ(aud_cntrl_st2);
6001 i |= eldv;
6002 I915_WRITE(aud_cntrl_st2, i);
6003}
6004
6005void intel_write_eld(struct drm_encoder *encoder,
6006 struct drm_display_mode *mode)
6007{
6008 struct drm_crtc *crtc = encoder->crtc;
6009 struct drm_connector *connector;
6010 struct drm_device *dev = encoder->dev;
6011 struct drm_i915_private *dev_priv = dev->dev_private;
6012
6013 connector = drm_select_eld(encoder, mode);
6014 if (!connector)
6015 return;
6016
6017 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6018 connector->base.id,
6019 drm_get_connector_name(connector),
6020 connector->encoder->base.id,
6021 drm_get_encoder_name(connector->encoder));
6022
6023 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6024
6025 if (dev_priv->display.write_eld)
6026 dev_priv->display.write_eld(connector, crtc);
6027}
6028
Jesse Barnes79e53942008-11-07 14:24:08 -08006029/** Loads the palette/gamma unit for the CRTC with the prepared values */
6030void intel_crtc_load_lut(struct drm_crtc *crtc)
6031{
6032 struct drm_device *dev = crtc->dev;
6033 struct drm_i915_private *dev_priv = dev->dev_private;
6034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006035 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006036 int i;
6037
6038 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006039 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006040 return;
6041
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006042 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006043 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006044 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006045
Jesse Barnes79e53942008-11-07 14:24:08 -08006046 for (i = 0; i < 256; i++) {
6047 I915_WRITE(palreg + 4 * i,
6048 (intel_crtc->lut_r[i] << 16) |
6049 (intel_crtc->lut_g[i] << 8) |
6050 intel_crtc->lut_b[i]);
6051 }
6052}
6053
Chris Wilson560b85b2010-08-07 11:01:38 +01006054static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6055{
6056 struct drm_device *dev = crtc->dev;
6057 struct drm_i915_private *dev_priv = dev->dev_private;
6058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6059 bool visible = base != 0;
6060 u32 cntl;
6061
6062 if (intel_crtc->cursor_visible == visible)
6063 return;
6064
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006065 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006066 if (visible) {
6067 /* On these chipsets we can only modify the base whilst
6068 * the cursor is disabled.
6069 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006070 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006071
6072 cntl &= ~(CURSOR_FORMAT_MASK);
6073 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6074 cntl |= CURSOR_ENABLE |
6075 CURSOR_GAMMA_ENABLE |
6076 CURSOR_FORMAT_ARGB;
6077 } else
6078 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006079 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006080
6081 intel_crtc->cursor_visible = visible;
6082}
6083
6084static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6085{
6086 struct drm_device *dev = crtc->dev;
6087 struct drm_i915_private *dev_priv = dev->dev_private;
6088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6089 int pipe = intel_crtc->pipe;
6090 bool visible = base != 0;
6091
6092 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006093 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006094 if (base) {
6095 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6096 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6097 cntl |= pipe << 28; /* Connect to correct pipe */
6098 } else {
6099 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6100 cntl |= CURSOR_MODE_DISABLE;
6101 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006102 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006103
6104 intel_crtc->cursor_visible = visible;
6105 }
6106 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006107 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006108}
6109
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006110static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6111{
6112 struct drm_device *dev = crtc->dev;
6113 struct drm_i915_private *dev_priv = dev->dev_private;
6114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6115 int pipe = intel_crtc->pipe;
6116 bool visible = base != 0;
6117
6118 if (intel_crtc->cursor_visible != visible) {
6119 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6120 if (base) {
6121 cntl &= ~CURSOR_MODE;
6122 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6123 } else {
6124 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6125 cntl |= CURSOR_MODE_DISABLE;
6126 }
6127 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6128
6129 intel_crtc->cursor_visible = visible;
6130 }
6131 /* and commit changes on next vblank */
6132 I915_WRITE(CURBASE_IVB(pipe), base);
6133}
6134
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006135/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006136static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6137 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006138{
6139 struct drm_device *dev = crtc->dev;
6140 struct drm_i915_private *dev_priv = dev->dev_private;
6141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6142 int pipe = intel_crtc->pipe;
6143 int x = intel_crtc->cursor_x;
6144 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006145 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006146 bool visible;
6147
6148 pos = 0;
6149
Chris Wilson6b383a72010-09-13 13:54:26 +01006150 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006151 base = intel_crtc->cursor_addr;
6152 if (x > (int) crtc->fb->width)
6153 base = 0;
6154
6155 if (y > (int) crtc->fb->height)
6156 base = 0;
6157 } else
6158 base = 0;
6159
6160 if (x < 0) {
6161 if (x + intel_crtc->cursor_width < 0)
6162 base = 0;
6163
6164 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6165 x = -x;
6166 }
6167 pos |= x << CURSOR_X_SHIFT;
6168
6169 if (y < 0) {
6170 if (y + intel_crtc->cursor_height < 0)
6171 base = 0;
6172
6173 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6174 y = -y;
6175 }
6176 pos |= y << CURSOR_Y_SHIFT;
6177
6178 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006179 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006180 return;
6181
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006182 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006183 I915_WRITE(CURPOS_IVB(pipe), pos);
6184 ivb_update_cursor(crtc, base);
6185 } else {
6186 I915_WRITE(CURPOS(pipe), pos);
6187 if (IS_845G(dev) || IS_I865G(dev))
6188 i845_update_cursor(crtc, base);
6189 else
6190 i9xx_update_cursor(crtc, base);
6191 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006192}
6193
Jesse Barnes79e53942008-11-07 14:24:08 -08006194static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006195 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006196 uint32_t handle,
6197 uint32_t width, uint32_t height)
6198{
6199 struct drm_device *dev = crtc->dev;
6200 struct drm_i915_private *dev_priv = dev->dev_private;
6201 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006202 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006203 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006204 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006205
Jesse Barnes79e53942008-11-07 14:24:08 -08006206 /* if we want to turn off the cursor ignore width and height */
6207 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006208 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006209 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006210 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006211 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006212 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006213 }
6214
6215 /* Currently we only support 64x64 cursors */
6216 if (width != 64 || height != 64) {
6217 DRM_ERROR("we currently only support 64x64 cursors\n");
6218 return -EINVAL;
6219 }
6220
Chris Wilson05394f32010-11-08 19:18:58 +00006221 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006222 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006223 return -ENOENT;
6224
Chris Wilson05394f32010-11-08 19:18:58 +00006225 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006226 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006227 ret = -ENOMEM;
6228 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006229 }
6230
Dave Airlie71acb5e2008-12-30 20:31:46 +10006231 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006232 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006233 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00006234 if (obj->tiling_mode) {
6235 DRM_ERROR("cursor cannot be tiled\n");
6236 ret = -EINVAL;
6237 goto fail_locked;
6238 }
6239
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006240 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006241 if (ret) {
6242 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006243 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006244 }
6245
Chris Wilsond9e86c02010-11-10 16:40:20 +00006246 ret = i915_gem_object_put_fence(obj);
6247 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006248 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006249 goto fail_unpin;
6250 }
6251
Chris Wilson05394f32010-11-08 19:18:58 +00006252 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006253 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006254 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006255 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006256 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6257 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006258 if (ret) {
6259 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006260 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006261 }
Chris Wilson05394f32010-11-08 19:18:58 +00006262 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006263 }
6264
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006265 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006266 I915_WRITE(CURSIZE, (height << 12) | width);
6267
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006268 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006269 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006270 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006271 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006272 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6273 } else
6274 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006275 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006276 }
Jesse Barnes80824002009-09-10 15:28:06 -07006277
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006278 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006279
6280 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006281 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006282 intel_crtc->cursor_width = width;
6283 intel_crtc->cursor_height = height;
6284
Chris Wilson6b383a72010-09-13 13:54:26 +01006285 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006286
Jesse Barnes79e53942008-11-07 14:24:08 -08006287 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006288fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006289 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006290fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006291 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006292fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006293 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006294 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006295}
6296
6297static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6298{
Jesse Barnes79e53942008-11-07 14:24:08 -08006299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006300
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006301 intel_crtc->cursor_x = x;
6302 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006303
Chris Wilson6b383a72010-09-13 13:54:26 +01006304 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006305
6306 return 0;
6307}
6308
6309/** Sets the color ramps on behalf of RandR */
6310void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6311 u16 blue, int regno)
6312{
6313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6314
6315 intel_crtc->lut_r[regno] = red >> 8;
6316 intel_crtc->lut_g[regno] = green >> 8;
6317 intel_crtc->lut_b[regno] = blue >> 8;
6318}
6319
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006320void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6321 u16 *blue, int regno)
6322{
6323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6324
6325 *red = intel_crtc->lut_r[regno] << 8;
6326 *green = intel_crtc->lut_g[regno] << 8;
6327 *blue = intel_crtc->lut_b[regno] << 8;
6328}
6329
Jesse Barnes79e53942008-11-07 14:24:08 -08006330static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006331 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006332{
James Simmons72034252010-08-03 01:33:19 +01006333 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006334 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006335
James Simmons72034252010-08-03 01:33:19 +01006336 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006337 intel_crtc->lut_r[i] = red[i] >> 8;
6338 intel_crtc->lut_g[i] = green[i] >> 8;
6339 intel_crtc->lut_b[i] = blue[i] >> 8;
6340 }
6341
6342 intel_crtc_load_lut(crtc);
6343}
6344
6345/**
6346 * Get a pipe with a simple mode set on it for doing load-based monitor
6347 * detection.
6348 *
6349 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07006350 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08006351 *
Eric Anholtc751ce42010-03-25 11:48:48 -07006352 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08006353 * configured for it. In the future, it could choose to temporarily disable
6354 * some outputs to free up a pipe for its use.
6355 *
6356 * \return crtc, or NULL if no pipes are available.
6357 */
6358
6359/* VESA 640x480x72Hz mode to set on the pipe */
6360static struct drm_display_mode load_detect_mode = {
6361 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6362 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6363};
6364
Chris Wilsond2dff872011-04-19 08:36:26 +01006365static struct drm_framebuffer *
6366intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006367 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006368 struct drm_i915_gem_object *obj)
6369{
6370 struct intel_framebuffer *intel_fb;
6371 int ret;
6372
6373 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6374 if (!intel_fb) {
6375 drm_gem_object_unreference_unlocked(&obj->base);
6376 return ERR_PTR(-ENOMEM);
6377 }
6378
6379 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6380 if (ret) {
6381 drm_gem_object_unreference_unlocked(&obj->base);
6382 kfree(intel_fb);
6383 return ERR_PTR(ret);
6384 }
6385
6386 return &intel_fb->base;
6387}
6388
6389static u32
6390intel_framebuffer_pitch_for_width(int width, int bpp)
6391{
6392 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6393 return ALIGN(pitch, 64);
6394}
6395
6396static u32
6397intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6398{
6399 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6400 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6401}
6402
6403static struct drm_framebuffer *
6404intel_framebuffer_create_for_mode(struct drm_device *dev,
6405 struct drm_display_mode *mode,
6406 int depth, int bpp)
6407{
6408 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006409 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006410
6411 obj = i915_gem_alloc_object(dev,
6412 intel_framebuffer_size_for_mode(mode, bpp));
6413 if (obj == NULL)
6414 return ERR_PTR(-ENOMEM);
6415
6416 mode_cmd.width = mode->hdisplay;
6417 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006418 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6419 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006420 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006421
6422 return intel_framebuffer_create(dev, &mode_cmd, obj);
6423}
6424
6425static struct drm_framebuffer *
6426mode_fits_in_fbdev(struct drm_device *dev,
6427 struct drm_display_mode *mode)
6428{
6429 struct drm_i915_private *dev_priv = dev->dev_private;
6430 struct drm_i915_gem_object *obj;
6431 struct drm_framebuffer *fb;
6432
6433 if (dev_priv->fbdev == NULL)
6434 return NULL;
6435
6436 obj = dev_priv->fbdev->ifb.obj;
6437 if (obj == NULL)
6438 return NULL;
6439
6440 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006441 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6442 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006443 return NULL;
6444
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006445 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006446 return NULL;
6447
6448 return fb;
6449}
6450
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006451bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006452 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006453 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006454{
6455 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006456 struct intel_encoder *intel_encoder =
6457 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006458 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006459 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006460 struct drm_crtc *crtc = NULL;
6461 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006462 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006463 int i = -1;
6464
Chris Wilsond2dff872011-04-19 08:36:26 +01006465 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6466 connector->base.id, drm_get_connector_name(connector),
6467 encoder->base.id, drm_get_encoder_name(encoder));
6468
Jesse Barnes79e53942008-11-07 14:24:08 -08006469 /*
6470 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006471 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006472 * - if the connector already has an assigned crtc, use it (but make
6473 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006474 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006475 * - try to find the first unused crtc that can drive this connector,
6476 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006477 */
6478
6479 /* See if we already have a CRTC for this connector */
6480 if (encoder->crtc) {
6481 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006482
Daniel Vetter7b240562012-12-12 00:35:33 +01006483 mutex_lock(&crtc->mutex);
6484
Daniel Vetter24218aa2012-08-12 19:27:11 +02006485 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006486 old->load_detect_temp = false;
6487
6488 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006489 if (connector->dpms != DRM_MODE_DPMS_ON)
6490 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006491
Chris Wilson71731882011-04-19 23:10:58 +01006492 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006493 }
6494
6495 /* Find an unused one (if possible) */
6496 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6497 i++;
6498 if (!(encoder->possible_crtcs & (1 << i)))
6499 continue;
6500 if (!possible_crtc->enabled) {
6501 crtc = possible_crtc;
6502 break;
6503 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006504 }
6505
6506 /*
6507 * If we didn't find an unused CRTC, don't use any.
6508 */
6509 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006510 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6511 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006512 }
6513
Daniel Vetter7b240562012-12-12 00:35:33 +01006514 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02006515 intel_encoder->new_crtc = to_intel_crtc(crtc);
6516 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006517
6518 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006519 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006520 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006521 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006522
Chris Wilson64927112011-04-20 07:25:26 +01006523 if (!mode)
6524 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006525
Chris Wilsond2dff872011-04-19 08:36:26 +01006526 /* We need a framebuffer large enough to accommodate all accesses
6527 * that the plane may generate whilst we perform load detection.
6528 * We can not rely on the fbcon either being present (we get called
6529 * during its initialisation to detect all boot displays, or it may
6530 * not even exist) or that it is large enough to satisfy the
6531 * requested mode.
6532 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006533 fb = mode_fits_in_fbdev(dev, mode);
6534 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006535 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006536 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6537 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006538 } else
6539 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006540 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006541 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01006542 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006543 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006544 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006545
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006546 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006547 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006548 if (old->release_fb)
6549 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01006550 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006551 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006552 }
Chris Wilson71731882011-04-19 23:10:58 +01006553
Jesse Barnes79e53942008-11-07 14:24:08 -08006554 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006555 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006556 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006557}
6558
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006559void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006560 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006561{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006562 struct intel_encoder *intel_encoder =
6563 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006564 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01006565 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08006566
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
Chris Wilson8261b192011-04-19 23:18:09 +01006571 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006572 to_intel_connector(connector)->new_encoder = NULL;
6573 intel_encoder->new_crtc = NULL;
6574 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006575
Daniel Vetter36206362012-12-10 20:42:17 +01006576 if (old->release_fb) {
6577 drm_framebuffer_unregister_private(old->release_fb);
6578 drm_framebuffer_unreference(old->release_fb);
6579 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006580
Daniel Vetter67c96402013-01-23 16:25:09 +00006581 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01006582 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006583 }
6584
Eric Anholtc751ce42010-03-25 11:48:48 -07006585 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006586 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6587 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01006588
6589 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08006590}
6591
6592/* Returns the clock of the currently programmed mode of the given pipe. */
6593static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6594{
6595 struct drm_i915_private *dev_priv = dev->dev_private;
6596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6597 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006598 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006599 u32 fp;
6600 intel_clock_t clock;
6601
6602 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006603 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006604 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006605 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006606
6607 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006608 if (IS_PINEVIEW(dev)) {
6609 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6610 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006611 } else {
6612 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6613 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6614 }
6615
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006616 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006617 if (IS_PINEVIEW(dev))
6618 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6619 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006620 else
6621 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006622 DPLL_FPA01_P1_POST_DIV_SHIFT);
6623
6624 switch (dpll & DPLL_MODE_MASK) {
6625 case DPLLB_MODE_DAC_SERIAL:
6626 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6627 5 : 10;
6628 break;
6629 case DPLLB_MODE_LVDS:
6630 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6631 7 : 14;
6632 break;
6633 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006634 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006635 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6636 return 0;
6637 }
6638
6639 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08006640 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006641 } else {
6642 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6643
6644 if (is_lvds) {
6645 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6646 DPLL_FPA01_P1_POST_DIV_SHIFT);
6647 clock.p2 = 14;
6648
6649 if ((dpll & PLL_REF_INPUT_MASK) ==
6650 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6651 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08006652 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006653 } else
Shaohua Li21778322009-02-23 15:19:16 +08006654 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006655 } else {
6656 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6657 clock.p1 = 2;
6658 else {
6659 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6660 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6661 }
6662 if (dpll & PLL_P2_DIVIDE_BY_4)
6663 clock.p2 = 4;
6664 else
6665 clock.p2 = 2;
6666
Shaohua Li21778322009-02-23 15:19:16 +08006667 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006668 }
6669 }
6670
6671 /* XXX: It would be nice to validate the clocks, but we can't reuse
6672 * i830PllIsValid() because it relies on the xf86_config connector
6673 * configuration being accurate, which it isn't necessarily.
6674 */
6675
6676 return clock.dot;
6677}
6678
6679/** Returns the currently programmed mode of the given pipe. */
6680struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6681 struct drm_crtc *crtc)
6682{
Jesse Barnes548f2452011-02-17 10:40:53 -08006683 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006684 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006685 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006686 struct drm_display_mode *mode;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006687 int htot = I915_READ(HTOTAL(cpu_transcoder));
6688 int hsync = I915_READ(HSYNC(cpu_transcoder));
6689 int vtot = I915_READ(VTOTAL(cpu_transcoder));
6690 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08006691
6692 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6693 if (!mode)
6694 return NULL;
6695
6696 mode->clock = intel_crtc_clock_get(dev, crtc);
6697 mode->hdisplay = (htot & 0xffff) + 1;
6698 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6699 mode->hsync_start = (hsync & 0xffff) + 1;
6700 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6701 mode->vdisplay = (vtot & 0xffff) + 1;
6702 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6703 mode->vsync_start = (vsync & 0xffff) + 1;
6704 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6705
6706 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006707
6708 return mode;
6709}
6710
Daniel Vetter3dec0092010-08-20 21:40:52 +02006711static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07006712{
6713 struct drm_device *dev = crtc->dev;
6714 drm_i915_private_t *dev_priv = dev->dev_private;
6715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6716 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006717 int dpll_reg = DPLL(pipe);
6718 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07006719
Eric Anholtbad720f2009-10-22 16:11:14 -07006720 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006721 return;
6722
6723 if (!dev_priv->lvds_downclock_avail)
6724 return;
6725
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006726 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006727 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08006728 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006729
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006730 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006731
6732 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6733 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006734 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006735
Jesse Barnes652c3932009-08-17 13:31:43 -07006736 dpll = I915_READ(dpll_reg);
6737 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08006738 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006739 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006740}
6741
6742static void intel_decrease_pllclock(struct drm_crtc *crtc)
6743{
6744 struct drm_device *dev = crtc->dev;
6745 drm_i915_private_t *dev_priv = dev->dev_private;
6746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006747
Eric Anholtbad720f2009-10-22 16:11:14 -07006748 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006749 return;
6750
6751 if (!dev_priv->lvds_downclock_avail)
6752 return;
6753
6754 /*
6755 * Since this is called by a timer, we should never get here in
6756 * the manual case.
6757 */
6758 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01006759 int pipe = intel_crtc->pipe;
6760 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02006761 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01006762
Zhao Yakui44d98a62009-10-09 11:39:40 +08006763 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006764
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006765 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006766
Chris Wilson074b5e12012-05-02 12:07:06 +01006767 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006768 dpll |= DISPLAY_RATE_SELECT_FPA1;
6769 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006770 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006771 dpll = I915_READ(dpll_reg);
6772 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08006773 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006774 }
6775
6776}
6777
Chris Wilsonf047e392012-07-21 12:31:41 +01006778void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006779{
Chris Wilsonf047e392012-07-21 12:31:41 +01006780 i915_update_gfx_val(dev->dev_private);
6781}
6782
6783void intel_mark_idle(struct drm_device *dev)
6784{
Chris Wilson725a5b52013-01-08 11:02:57 +00006785 struct drm_crtc *crtc;
6786
6787 if (!i915_powersave)
6788 return;
6789
6790 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6791 if (!crtc->fb)
6792 continue;
6793
6794 intel_decrease_pllclock(crtc);
6795 }
Chris Wilsonf047e392012-07-21 12:31:41 +01006796}
6797
6798void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
6799{
6800 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07006801 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006802
6803 if (!i915_powersave)
6804 return;
6805
Jesse Barnes652c3932009-08-17 13:31:43 -07006806 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006807 if (!crtc->fb)
6808 continue;
6809
Chris Wilsonf047e392012-07-21 12:31:41 +01006810 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6811 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006812 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006813}
6814
Jesse Barnes79e53942008-11-07 14:24:08 -08006815static void intel_crtc_destroy(struct drm_crtc *crtc)
6816{
6817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006818 struct drm_device *dev = crtc->dev;
6819 struct intel_unpin_work *work;
6820 unsigned long flags;
6821
6822 spin_lock_irqsave(&dev->event_lock, flags);
6823 work = intel_crtc->unpin_work;
6824 intel_crtc->unpin_work = NULL;
6825 spin_unlock_irqrestore(&dev->event_lock, flags);
6826
6827 if (work) {
6828 cancel_work_sync(&work->work);
6829 kfree(work);
6830 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006831
6832 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006833
Jesse Barnes79e53942008-11-07 14:24:08 -08006834 kfree(intel_crtc);
6835}
6836
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006837static void intel_unpin_work_fn(struct work_struct *__work)
6838{
6839 struct intel_unpin_work *work =
6840 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006841 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006842
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006843 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01006844 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00006845 drm_gem_object_unreference(&work->pending_flip_obj->base);
6846 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00006847
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006848 intel_update_fbc(dev);
6849 mutex_unlock(&dev->struct_mutex);
6850
6851 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
6852 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
6853
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006854 kfree(work);
6855}
6856
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006857static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01006858 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006859{
6860 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6862 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00006863 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006864 unsigned long flags;
6865
6866 /* Ignore early vblank irqs */
6867 if (intel_crtc == NULL)
6868 return;
6869
6870 spin_lock_irqsave(&dev->event_lock, flags);
6871 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00006872
6873 /* Ensure we don't miss a work->pending update ... */
6874 smp_rmb();
6875
6876 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006877 spin_unlock_irqrestore(&dev->event_lock, flags);
6878 return;
6879 }
6880
Chris Wilsone7d841c2012-12-03 11:36:30 +00006881 /* and that the unpin work is consistent wrt ->pending. */
6882 smp_rmb();
6883
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006884 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006885
Rob Clark45a066e2012-10-08 14:50:40 -05006886 if (work->event)
6887 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006888
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01006889 drm_vblank_put(dev, intel_crtc->pipe);
6890
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006891 spin_unlock_irqrestore(&dev->event_lock, flags);
6892
Chris Wilson05394f32010-11-08 19:18:58 +00006893 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00006894
Daniel Vetter2c10d572012-12-20 21:24:07 +01006895 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006896
6897 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07006898
6899 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006900}
6901
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006902void intel_finish_page_flip(struct drm_device *dev, int pipe)
6903{
6904 drm_i915_private_t *dev_priv = dev->dev_private;
6905 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
6906
Mario Kleiner49b14a52010-12-09 07:00:07 +01006907 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006908}
6909
6910void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
6911{
6912 drm_i915_private_t *dev_priv = dev->dev_private;
6913 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
6914
Mario Kleiner49b14a52010-12-09 07:00:07 +01006915 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006916}
6917
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006918void intel_prepare_page_flip(struct drm_device *dev, int plane)
6919{
6920 drm_i915_private_t *dev_priv = dev->dev_private;
6921 struct intel_crtc *intel_crtc =
6922 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
6923 unsigned long flags;
6924
Chris Wilsone7d841c2012-12-03 11:36:30 +00006925 /* NB: An MMIO update of the plane base pointer will also
6926 * generate a page-flip completion irq, i.e. every modeset
6927 * is also accompanied by a spurious intel_prepare_page_flip().
6928 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006929 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00006930 if (intel_crtc->unpin_work)
6931 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006932 spin_unlock_irqrestore(&dev->event_lock, flags);
6933}
6934
Chris Wilsone7d841c2012-12-03 11:36:30 +00006935inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
6936{
6937 /* Ensure that the work item is consistent when activating it ... */
6938 smp_wmb();
6939 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
6940 /* and that it is marked active as soon as the irq could fire. */
6941 smp_wmb();
6942}
6943
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006944static int intel_gen2_queue_flip(struct drm_device *dev,
6945 struct drm_crtc *crtc,
6946 struct drm_framebuffer *fb,
6947 struct drm_i915_gem_object *obj)
6948{
6949 struct drm_i915_private *dev_priv = dev->dev_private;
6950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006951 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006952 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006953 int ret;
6954
Daniel Vetter6d90c952012-04-26 23:28:05 +02006955 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006956 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006957 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006958
Daniel Vetter6d90c952012-04-26 23:28:05 +02006959 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006960 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006961 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006962
6963 /* Can't queue multiple flips, so wait for the previous
6964 * one to finish before executing the next.
6965 */
6966 if (intel_crtc->plane)
6967 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
6968 else
6969 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006970 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
6971 intel_ring_emit(ring, MI_NOOP);
6972 intel_ring_emit(ring, MI_DISPLAY_FLIP |
6973 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
6974 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02006975 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006976 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00006977
6978 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02006979 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006980 return 0;
6981
6982err_unpin:
6983 intel_unpin_fb_obj(obj);
6984err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006985 return ret;
6986}
6987
6988static int intel_gen3_queue_flip(struct drm_device *dev,
6989 struct drm_crtc *crtc,
6990 struct drm_framebuffer *fb,
6991 struct drm_i915_gem_object *obj)
6992{
6993 struct drm_i915_private *dev_priv = dev->dev_private;
6994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006995 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02006996 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006997 int ret;
6998
Daniel Vetter6d90c952012-04-26 23:28:05 +02006999 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007000 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007001 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007002
Daniel Vetter6d90c952012-04-26 23:28:05 +02007003 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007004 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007005 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007006
7007 if (intel_crtc->plane)
7008 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7009 else
7010 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007011 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7012 intel_ring_emit(ring, MI_NOOP);
7013 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7014 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7015 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007016 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007017 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007018
Chris Wilsone7d841c2012-12-03 11:36:30 +00007019 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007020 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007021 return 0;
7022
7023err_unpin:
7024 intel_unpin_fb_obj(obj);
7025err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007026 return ret;
7027}
7028
7029static int intel_gen4_queue_flip(struct drm_device *dev,
7030 struct drm_crtc *crtc,
7031 struct drm_framebuffer *fb,
7032 struct drm_i915_gem_object *obj)
7033{
7034 struct drm_i915_private *dev_priv = dev->dev_private;
7035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7036 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007037 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007038 int ret;
7039
Daniel Vetter6d90c952012-04-26 23:28:05 +02007040 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007041 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007042 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007043
Daniel Vetter6d90c952012-04-26 23:28:05 +02007044 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007045 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007046 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007047
7048 /* i965+ uses the linear or tiled offsets from the
7049 * Display Registers (which do not change across a page-flip)
7050 * so we need only reprogram the base address.
7051 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007052 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7053 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7054 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007055 intel_ring_emit(ring,
7056 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
7057 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007058
7059 /* XXX Enabling the panel-fitter across page-flip is so far
7060 * untested on non-native modes, so ignore it for now.
7061 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7062 */
7063 pf = 0;
7064 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007065 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007066
7067 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007068 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007069 return 0;
7070
7071err_unpin:
7072 intel_unpin_fb_obj(obj);
7073err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007074 return ret;
7075}
7076
7077static int intel_gen6_queue_flip(struct drm_device *dev,
7078 struct drm_crtc *crtc,
7079 struct drm_framebuffer *fb,
7080 struct drm_i915_gem_object *obj)
7081{
7082 struct drm_i915_private *dev_priv = dev->dev_private;
7083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007084 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007085 uint32_t pf, pipesrc;
7086 int ret;
7087
Daniel Vetter6d90c952012-04-26 23:28:05 +02007088 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007089 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007090 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007091
Daniel Vetter6d90c952012-04-26 23:28:05 +02007092 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007093 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007094 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007095
Daniel Vetter6d90c952012-04-26 23:28:05 +02007096 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7097 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7098 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007099 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007100
Chris Wilson99d9acd2012-04-17 20:37:00 +01007101 /* Contrary to the suggestions in the documentation,
7102 * "Enable Panel Fitter" does not seem to be required when page
7103 * flipping with a non-native mode, and worse causes a normal
7104 * modeset to fail.
7105 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7106 */
7107 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007108 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007109 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007110
7111 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007112 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007113 return 0;
7114
7115err_unpin:
7116 intel_unpin_fb_obj(obj);
7117err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007118 return ret;
7119}
7120
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007121/*
7122 * On gen7 we currently use the blit ring because (in early silicon at least)
7123 * the render ring doesn't give us interrpts for page flip completion, which
7124 * means clients will hang after the first flip is queued. Fortunately the
7125 * blit ring generates interrupts properly, so use it instead.
7126 */
7127static int intel_gen7_queue_flip(struct drm_device *dev,
7128 struct drm_crtc *crtc,
7129 struct drm_framebuffer *fb,
7130 struct drm_i915_gem_object *obj)
7131{
7132 struct drm_i915_private *dev_priv = dev->dev_private;
7133 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7134 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007135 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007136 int ret;
7137
7138 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7139 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007140 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007141
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007142 switch(intel_crtc->plane) {
7143 case PLANE_A:
7144 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7145 break;
7146 case PLANE_B:
7147 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7148 break;
7149 case PLANE_C:
7150 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7151 break;
7152 default:
7153 WARN_ONCE(1, "unknown plane in flip command\n");
7154 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007155 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007156 }
7157
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007158 ret = intel_ring_begin(ring, 4);
7159 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007160 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007161
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007162 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007163 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Daniel Vetterc2c75132012-07-05 12:17:30 +02007164 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007165 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007166
7167 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007168 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007169 return 0;
7170
7171err_unpin:
7172 intel_unpin_fb_obj(obj);
7173err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007174 return ret;
7175}
7176
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007177static int intel_default_queue_flip(struct drm_device *dev,
7178 struct drm_crtc *crtc,
7179 struct drm_framebuffer *fb,
7180 struct drm_i915_gem_object *obj)
7181{
7182 return -ENODEV;
7183}
7184
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007185static int intel_crtc_page_flip(struct drm_crtc *crtc,
7186 struct drm_framebuffer *fb,
7187 struct drm_pending_vblank_event *event)
7188{
7189 struct drm_device *dev = crtc->dev;
7190 struct drm_i915_private *dev_priv = dev->dev_private;
7191 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007192 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7194 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007195 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007196 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007197
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007198 /* Can't change pixel format via MI display flips. */
7199 if (fb->pixel_format != crtc->fb->pixel_format)
7200 return -EINVAL;
7201
7202 /*
7203 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7204 * Note that pitch changes could also affect these register.
7205 */
7206 if (INTEL_INFO(dev)->gen > 3 &&
7207 (fb->offsets[0] != crtc->fb->offsets[0] ||
7208 fb->pitches[0] != crtc->fb->pitches[0]))
7209 return -EINVAL;
7210
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007211 work = kzalloc(sizeof *work, GFP_KERNEL);
7212 if (work == NULL)
7213 return -ENOMEM;
7214
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007215 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007216 work->crtc = crtc;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007217 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08007218 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007219 INIT_WORK(&work->work, intel_unpin_work_fn);
7220
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007221 ret = drm_vblank_get(dev, intel_crtc->pipe);
7222 if (ret)
7223 goto free_work;
7224
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007225 /* We borrow the event spin lock for protecting unpin_work */
7226 spin_lock_irqsave(&dev->event_lock, flags);
7227 if (intel_crtc->unpin_work) {
7228 spin_unlock_irqrestore(&dev->event_lock, flags);
7229 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007230 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007231
7232 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007233 return -EBUSY;
7234 }
7235 intel_crtc->unpin_work = work;
7236 spin_unlock_irqrestore(&dev->event_lock, flags);
7237
7238 intel_fb = to_intel_framebuffer(fb);
7239 obj = intel_fb->obj;
7240
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007241 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7242 flush_workqueue(dev_priv->wq);
7243
Chris Wilson79158102012-05-23 11:13:58 +01007244 ret = i915_mutex_lock_interruptible(dev);
7245 if (ret)
7246 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007247
Jesse Barnes75dfca82010-02-10 15:09:44 -08007248 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007249 drm_gem_object_reference(&work->old_fb_obj->base);
7250 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007251
7252 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007253
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007254 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007255
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007256 work->enable_stall_check = true;
7257
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007258 atomic_inc(&intel_crtc->unpin_work_count);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007259
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007260 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7261 if (ret)
7262 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007263
Chris Wilson7782de32011-07-08 12:22:41 +01007264 intel_disable_fbc(dev);
Chris Wilsonf047e392012-07-21 12:31:41 +01007265 intel_mark_fb_busy(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007266 mutex_unlock(&dev->struct_mutex);
7267
Jesse Barnese5510fa2010-07-01 16:48:37 -07007268 trace_i915_flip_request(intel_crtc->plane, obj);
7269
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007270 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007271
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007272cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007273 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson05394f32010-11-08 19:18:58 +00007274 drm_gem_object_unreference(&work->old_fb_obj->base);
7275 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007276 mutex_unlock(&dev->struct_mutex);
7277
Chris Wilson79158102012-05-23 11:13:58 +01007278cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007279 spin_lock_irqsave(&dev->event_lock, flags);
7280 intel_crtc->unpin_work = NULL;
7281 spin_unlock_irqrestore(&dev->event_lock, flags);
7282
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007283 drm_vblank_put(dev, intel_crtc->pipe);
7284free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007285 kfree(work);
7286
7287 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007288}
7289
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007290static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007291 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7292 .load_lut = intel_crtc_load_lut,
Daniel Vetter976f8a22012-07-08 22:34:21 +02007293 .disable = intel_crtc_noop,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007294};
7295
Daniel Vetter6ed0f792012-07-08 19:41:43 +02007296bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
7297{
7298 struct intel_encoder *other_encoder;
7299 struct drm_crtc *crtc = &encoder->new_crtc->base;
7300
7301 if (WARN_ON(!crtc))
7302 return false;
7303
7304 list_for_each_entry(other_encoder,
7305 &crtc->dev->mode_config.encoder_list,
7306 base.head) {
7307
7308 if (&other_encoder->new_crtc->base != crtc ||
7309 encoder == other_encoder)
7310 continue;
7311 else
7312 return true;
7313 }
7314
7315 return false;
7316}
7317
Daniel Vetter50f56112012-07-02 09:35:43 +02007318static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7319 struct drm_crtc *crtc)
7320{
7321 struct drm_device *dev;
7322 struct drm_crtc *tmp;
7323 int crtc_mask = 1;
7324
7325 WARN(!crtc, "checking null crtc?\n");
7326
7327 dev = crtc->dev;
7328
7329 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7330 if (tmp == crtc)
7331 break;
7332 crtc_mask <<= 1;
7333 }
7334
7335 if (encoder->possible_crtcs & crtc_mask)
7336 return true;
7337 return false;
7338}
7339
Daniel Vetter9a935852012-07-05 22:34:27 +02007340/**
7341 * intel_modeset_update_staged_output_state
7342 *
7343 * Updates the staged output configuration state, e.g. after we've read out the
7344 * current hw state.
7345 */
7346static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7347{
7348 struct intel_encoder *encoder;
7349 struct intel_connector *connector;
7350
7351 list_for_each_entry(connector, &dev->mode_config.connector_list,
7352 base.head) {
7353 connector->new_encoder =
7354 to_intel_encoder(connector->base.encoder);
7355 }
7356
7357 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7358 base.head) {
7359 encoder->new_crtc =
7360 to_intel_crtc(encoder->base.crtc);
7361 }
7362}
7363
7364/**
7365 * intel_modeset_commit_output_state
7366 *
7367 * This function copies the stage display pipe configuration to the real one.
7368 */
7369static void intel_modeset_commit_output_state(struct drm_device *dev)
7370{
7371 struct intel_encoder *encoder;
7372 struct intel_connector *connector;
7373
7374 list_for_each_entry(connector, &dev->mode_config.connector_list,
7375 base.head) {
7376 connector->base.encoder = &connector->new_encoder->base;
7377 }
7378
7379 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7380 base.head) {
7381 encoder->base.crtc = &encoder->new_crtc->base;
7382 }
7383}
7384
Daniel Vetter7758a112012-07-08 19:40:39 +02007385static struct drm_display_mode *
7386intel_modeset_adjusted_mode(struct drm_crtc *crtc,
7387 struct drm_display_mode *mode)
7388{
7389 struct drm_device *dev = crtc->dev;
7390 struct drm_display_mode *adjusted_mode;
7391 struct drm_encoder_helper_funcs *encoder_funcs;
7392 struct intel_encoder *encoder;
7393
7394 adjusted_mode = drm_mode_duplicate(dev, mode);
7395 if (!adjusted_mode)
7396 return ERR_PTR(-ENOMEM);
7397
7398 /* Pass our mode to the connectors and the CRTC to give them a chance to
7399 * adjust it according to limitations or connector properties, and also
7400 * a chance to reject the mode entirely.
7401 */
7402 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7403 base.head) {
7404
7405 if (&encoder->new_crtc->base != crtc)
7406 continue;
7407 encoder_funcs = encoder->base.helper_private;
7408 if (!(encoder_funcs->mode_fixup(&encoder->base, mode,
7409 adjusted_mode))) {
7410 DRM_DEBUG_KMS("Encoder fixup failed\n");
7411 goto fail;
7412 }
7413 }
7414
7415 if (!(intel_crtc_mode_fixup(crtc, mode, adjusted_mode))) {
7416 DRM_DEBUG_KMS("CRTC fixup failed\n");
7417 goto fail;
7418 }
7419 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
7420
7421 return adjusted_mode;
7422fail:
7423 drm_mode_destroy(dev, adjusted_mode);
7424 return ERR_PTR(-EINVAL);
7425}
7426
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007427/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7428 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7429static void
7430intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7431 unsigned *prepare_pipes, unsigned *disable_pipes)
7432{
7433 struct intel_crtc *intel_crtc;
7434 struct drm_device *dev = crtc->dev;
7435 struct intel_encoder *encoder;
7436 struct intel_connector *connector;
7437 struct drm_crtc *tmp_crtc;
7438
7439 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7440
7441 /* Check which crtcs have changed outputs connected to them, these need
7442 * to be part of the prepare_pipes mask. We don't (yet) support global
7443 * modeset across multiple crtcs, so modeset_pipes will only have one
7444 * bit set at most. */
7445 list_for_each_entry(connector, &dev->mode_config.connector_list,
7446 base.head) {
7447 if (connector->base.encoder == &connector->new_encoder->base)
7448 continue;
7449
7450 if (connector->base.encoder) {
7451 tmp_crtc = connector->base.encoder->crtc;
7452
7453 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7454 }
7455
7456 if (connector->new_encoder)
7457 *prepare_pipes |=
7458 1 << connector->new_encoder->new_crtc->pipe;
7459 }
7460
7461 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7462 base.head) {
7463 if (encoder->base.crtc == &encoder->new_crtc->base)
7464 continue;
7465
7466 if (encoder->base.crtc) {
7467 tmp_crtc = encoder->base.crtc;
7468
7469 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7470 }
7471
7472 if (encoder->new_crtc)
7473 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7474 }
7475
7476 /* Check for any pipes that will be fully disabled ... */
7477 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7478 base.head) {
7479 bool used = false;
7480
7481 /* Don't try to disable disabled crtcs. */
7482 if (!intel_crtc->base.enabled)
7483 continue;
7484
7485 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7486 base.head) {
7487 if (encoder->new_crtc == intel_crtc)
7488 used = true;
7489 }
7490
7491 if (!used)
7492 *disable_pipes |= 1 << intel_crtc->pipe;
7493 }
7494
7495
7496 /* set_mode is also used to update properties on life display pipes. */
7497 intel_crtc = to_intel_crtc(crtc);
7498 if (crtc->enabled)
7499 *prepare_pipes |= 1 << intel_crtc->pipe;
7500
7501 /* We only support modeset on one single crtc, hence we need to do that
7502 * only for the passed in crtc iff we change anything else than just
7503 * disable crtcs.
7504 *
7505 * This is actually not true, to be fully compatible with the old crtc
7506 * helper we automatically disable _any_ output (i.e. doesn't need to be
7507 * connected to the crtc we're modesetting on) if it's disconnected.
7508 * Which is a rather nutty api (since changed the output configuration
7509 * without userspace's explicit request can lead to confusion), but
7510 * alas. Hence we currently need to modeset on all pipes we prepare. */
7511 if (*prepare_pipes)
7512 *modeset_pipes = *prepare_pipes;
7513
7514 /* ... and mask these out. */
7515 *modeset_pipes &= ~(*disable_pipes);
7516 *prepare_pipes &= ~(*disable_pipes);
7517}
7518
Daniel Vetterea9d7582012-07-10 10:42:52 +02007519static bool intel_crtc_in_use(struct drm_crtc *crtc)
7520{
7521 struct drm_encoder *encoder;
7522 struct drm_device *dev = crtc->dev;
7523
7524 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
7525 if (encoder->crtc == crtc)
7526 return true;
7527
7528 return false;
7529}
7530
7531static void
7532intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
7533{
7534 struct intel_encoder *intel_encoder;
7535 struct intel_crtc *intel_crtc;
7536 struct drm_connector *connector;
7537
7538 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
7539 base.head) {
7540 if (!intel_encoder->base.crtc)
7541 continue;
7542
7543 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
7544
7545 if (prepare_pipes & (1 << intel_crtc->pipe))
7546 intel_encoder->connectors_active = false;
7547 }
7548
7549 intel_modeset_commit_output_state(dev);
7550
7551 /* Update computed state. */
7552 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7553 base.head) {
7554 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
7555 }
7556
7557 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7558 if (!connector->encoder || !connector->encoder->crtc)
7559 continue;
7560
7561 intel_crtc = to_intel_crtc(connector->encoder->crtc);
7562
7563 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02007564 struct drm_property *dpms_property =
7565 dev->mode_config.dpms_property;
7566
Daniel Vetterea9d7582012-07-10 10:42:52 +02007567 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05007568 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02007569 dpms_property,
7570 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02007571
7572 intel_encoder = to_intel_encoder(connector->encoder);
7573 intel_encoder->connectors_active = true;
7574 }
7575 }
7576
7577}
7578
Daniel Vetter25c5b262012-07-08 22:08:04 +02007579#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
7580 list_for_each_entry((intel_crtc), \
7581 &(dev)->mode_config.crtc_list, \
7582 base.head) \
7583 if (mask & (1 <<(intel_crtc)->pipe)) \
7584
Daniel Vetterb9805142012-08-31 17:37:33 +02007585void
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007586intel_modeset_check_state(struct drm_device *dev)
7587{
7588 struct intel_crtc *crtc;
7589 struct intel_encoder *encoder;
7590 struct intel_connector *connector;
7591
7592 list_for_each_entry(connector, &dev->mode_config.connector_list,
7593 base.head) {
7594 /* This also checks the encoder/connector hw state with the
7595 * ->get_hw_state callbacks. */
7596 intel_connector_check_state(connector);
7597
7598 WARN(&connector->new_encoder->base != connector->base.encoder,
7599 "connector's staged encoder doesn't match current encoder\n");
7600 }
7601
7602 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7603 base.head) {
7604 bool enabled = false;
7605 bool active = false;
7606 enum pipe pipe, tracked_pipe;
7607
7608 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
7609 encoder->base.base.id,
7610 drm_get_encoder_name(&encoder->base));
7611
7612 WARN(&encoder->new_crtc->base != encoder->base.crtc,
7613 "encoder's stage crtc doesn't match current crtc\n");
7614 WARN(encoder->connectors_active && !encoder->base.crtc,
7615 "encoder's active_connectors set, but no crtc\n");
7616
7617 list_for_each_entry(connector, &dev->mode_config.connector_list,
7618 base.head) {
7619 if (connector->base.encoder != &encoder->base)
7620 continue;
7621 enabled = true;
7622 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
7623 active = true;
7624 }
7625 WARN(!!encoder->base.crtc != enabled,
7626 "encoder's enabled state mismatch "
7627 "(expected %i, found %i)\n",
7628 !!encoder->base.crtc, enabled);
7629 WARN(active && !encoder->base.crtc,
7630 "active encoder with no crtc\n");
7631
7632 WARN(encoder->connectors_active != active,
7633 "encoder's computed active state doesn't match tracked active state "
7634 "(expected %i, found %i)\n", active, encoder->connectors_active);
7635
7636 active = encoder->get_hw_state(encoder, &pipe);
7637 WARN(active != encoder->connectors_active,
7638 "encoder's hw state doesn't match sw tracking "
7639 "(expected %i, found %i)\n",
7640 encoder->connectors_active, active);
7641
7642 if (!encoder->base.crtc)
7643 continue;
7644
7645 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
7646 WARN(active && pipe != tracked_pipe,
7647 "active encoder's pipe doesn't match"
7648 "(expected %i, found %i)\n",
7649 tracked_pipe, pipe);
7650
7651 }
7652
7653 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
7654 base.head) {
7655 bool enabled = false;
7656 bool active = false;
7657
7658 DRM_DEBUG_KMS("[CRTC:%d]\n",
7659 crtc->base.base.id);
7660
7661 WARN(crtc->active && !crtc->base.enabled,
7662 "active crtc, but not enabled in sw tracking\n");
7663
7664 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7665 base.head) {
7666 if (encoder->base.crtc != &crtc->base)
7667 continue;
7668 enabled = true;
7669 if (encoder->connectors_active)
7670 active = true;
7671 }
7672 WARN(active != crtc->active,
7673 "crtc's computed active state doesn't match tracked active state "
7674 "(expected %i, found %i)\n", active, crtc->active);
7675 WARN(enabled != crtc->base.enabled,
7676 "crtc's computed enabled state doesn't match tracked enabled state "
7677 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
7678
7679 assert_pipe(dev->dev_private, crtc->pipe, crtc->active);
7680 }
7681}
7682
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007683int intel_set_mode(struct drm_crtc *crtc,
7684 struct drm_display_mode *mode,
7685 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02007686{
7687 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02007688 drm_i915_private_t *dev_priv = dev->dev_private;
Tim Gardner3ac18232012-12-07 07:54:26 -07007689 struct drm_display_mode *adjusted_mode, *saved_mode, *saved_hwmode;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007690 struct intel_crtc *intel_crtc;
7691 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007692 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02007693
Tim Gardner3ac18232012-12-07 07:54:26 -07007694 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007695 if (!saved_mode)
7696 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07007697 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02007698
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007699 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02007700 &prepare_pipes, &disable_pipes);
7701
7702 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7703 modeset_pipes, prepare_pipes, disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007704
Daniel Vetter976f8a22012-07-08 22:34:21 +02007705 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
7706 intel_crtc_disable(&intel_crtc->base);
7707
Tim Gardner3ac18232012-12-07 07:54:26 -07007708 *saved_hwmode = crtc->hwmode;
7709 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007710
Daniel Vetter25c5b262012-07-08 22:08:04 +02007711 /* Hack: Because we don't (yet) support global modeset on multiple
7712 * crtcs, we don't keep track of the new mode for more than one crtc.
7713 * Hence simply check whether any bit is set in modeset_pipes in all the
7714 * pieces of code that are not yet converted to deal with mutliple crtcs
7715 * changing their mode at the same time. */
7716 adjusted_mode = NULL;
7717 if (modeset_pipes) {
7718 adjusted_mode = intel_modeset_adjusted_mode(crtc, mode);
7719 if (IS_ERR(adjusted_mode)) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007720 ret = PTR_ERR(adjusted_mode);
Tim Gardner3ac18232012-12-07 07:54:26 -07007721 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007722 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007723 }
7724
Daniel Vetterea9d7582012-07-10 10:42:52 +02007725 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
7726 if (intel_crtc->base.enabled)
7727 dev_priv->display.crtc_disable(&intel_crtc->base);
7728 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007729
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02007730 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
7731 * to set it here already despite that we pass it down the callchain.
7732 */
7733 if (modeset_pipes)
Daniel Vetter25c5b262012-07-08 22:08:04 +02007734 crtc->mode = *mode;
Daniel Vetter7758a112012-07-08 19:40:39 +02007735
Daniel Vetterea9d7582012-07-10 10:42:52 +02007736 /* Only after disabling all output pipelines that will be changed can we
7737 * update the the output configuration. */
7738 intel_modeset_update_state(dev, prepare_pipes);
7739
Daniel Vetter47fab732012-10-26 10:58:18 +02007740 if (dev_priv->display.modeset_global_resources)
7741 dev_priv->display.modeset_global_resources(dev);
7742
Daniel Vettera6778b32012-07-02 09:56:42 +02007743 /* Set up the DPLL and any encoders state that needs to adjust or depend
7744 * on the DPLL.
7745 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007746 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007747 ret = intel_crtc_mode_set(&intel_crtc->base,
7748 mode, adjusted_mode,
7749 x, y, fb);
7750 if (ret)
7751 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02007752 }
7753
7754 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007755 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
7756 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02007757
Daniel Vetter25c5b262012-07-08 22:08:04 +02007758 if (modeset_pipes) {
7759 /* Store real post-adjustment hardware mode. */
7760 crtc->hwmode = *adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007761
Daniel Vetter25c5b262012-07-08 22:08:04 +02007762 /* Calculate and store various constants which
7763 * are later needed by vblank and swap-completion
7764 * timestamping. They are derived from true hwmode.
7765 */
7766 drm_calc_timestamping_constants(crtc);
7767 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007768
7769 /* FIXME: add subpixel order */
7770done:
7771 drm_mode_destroy(dev, adjusted_mode);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007772 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07007773 crtc->hwmode = *saved_hwmode;
7774 crtc->mode = *saved_mode;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007775 } else {
7776 intel_modeset_check_state(dev);
Daniel Vettera6778b32012-07-02 09:56:42 +02007777 }
7778
Tim Gardner3ac18232012-12-07 07:54:26 -07007779out:
7780 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02007781 return ret;
7782}
7783
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007784void intel_crtc_restore_mode(struct drm_crtc *crtc)
7785{
7786 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
7787}
7788
Daniel Vetter25c5b262012-07-08 22:08:04 +02007789#undef for_each_intel_crtc_masked
7790
Daniel Vetterd9e55602012-07-04 22:16:09 +02007791static void intel_set_config_free(struct intel_set_config *config)
7792{
7793 if (!config)
7794 return;
7795
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007796 kfree(config->save_connector_encoders);
7797 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02007798 kfree(config);
7799}
7800
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007801static int intel_set_config_save_state(struct drm_device *dev,
7802 struct intel_set_config *config)
7803{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007804 struct drm_encoder *encoder;
7805 struct drm_connector *connector;
7806 int count;
7807
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007808 config->save_encoder_crtcs =
7809 kcalloc(dev->mode_config.num_encoder,
7810 sizeof(struct drm_crtc *), GFP_KERNEL);
7811 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007812 return -ENOMEM;
7813
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007814 config->save_connector_encoders =
7815 kcalloc(dev->mode_config.num_connector,
7816 sizeof(struct drm_encoder *), GFP_KERNEL);
7817 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007818 return -ENOMEM;
7819
7820 /* Copy data. Note that driver private data is not affected.
7821 * Should anything bad happen only the expected state is
7822 * restored, not the drivers personal bookkeeping.
7823 */
7824 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007825 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007826 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007827 }
7828
7829 count = 0;
7830 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007831 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007832 }
7833
7834 return 0;
7835}
7836
7837static void intel_set_config_restore_state(struct drm_device *dev,
7838 struct intel_set_config *config)
7839{
Daniel Vetter9a935852012-07-05 22:34:27 +02007840 struct intel_encoder *encoder;
7841 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007842 int count;
7843
7844 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007845 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7846 encoder->new_crtc =
7847 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007848 }
7849
7850 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007851 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
7852 connector->new_encoder =
7853 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007854 }
7855}
7856
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007857static void
7858intel_set_config_compute_mode_changes(struct drm_mode_set *set,
7859 struct intel_set_config *config)
7860{
7861
7862 /* We should be able to check here if the fb has the same properties
7863 * and then just flip_or_move it */
7864 if (set->crtc->fb != set->fb) {
7865 /* If we have no fb then treat it as a full mode set */
7866 if (set->crtc->fb == NULL) {
7867 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
7868 config->mode_changed = true;
7869 } else if (set->fb == NULL) {
7870 config->mode_changed = true;
7871 } else if (set->fb->depth != set->crtc->fb->depth) {
7872 config->mode_changed = true;
7873 } else if (set->fb->bits_per_pixel !=
7874 set->crtc->fb->bits_per_pixel) {
7875 config->mode_changed = true;
7876 } else
7877 config->fb_changed = true;
7878 }
7879
Daniel Vetter835c5872012-07-10 18:11:08 +02007880 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007881 config->fb_changed = true;
7882
7883 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
7884 DRM_DEBUG_KMS("modes are different, full mode set\n");
7885 drm_mode_debug_printmodeline(&set->crtc->mode);
7886 drm_mode_debug_printmodeline(set->mode);
7887 config->mode_changed = true;
7888 }
7889}
7890
Daniel Vetter2e431052012-07-04 22:42:15 +02007891static int
Daniel Vetter9a935852012-07-05 22:34:27 +02007892intel_modeset_stage_output_state(struct drm_device *dev,
7893 struct drm_mode_set *set,
7894 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02007895{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007896 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02007897 struct intel_connector *connector;
7898 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02007899 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02007900
Damien Lespiau9abdda72013-02-13 13:29:23 +00007901 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02007902 * of connectors. For paranoia, double-check this. */
7903 WARN_ON(!set->fb && (set->num_connectors != 0));
7904 WARN_ON(set->fb && (set->num_connectors == 0));
7905
Daniel Vetter50f56112012-07-02 09:35:43 +02007906 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007907 list_for_each_entry(connector, &dev->mode_config.connector_list,
7908 base.head) {
7909 /* Otherwise traverse passed in connector list and get encoders
7910 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007911 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02007912 if (set->connectors[ro] == &connector->base) {
7913 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02007914 break;
7915 }
7916 }
7917
Daniel Vetter9a935852012-07-05 22:34:27 +02007918 /* If we disable the crtc, disable all its connectors. Also, if
7919 * the connector is on the changing crtc but not on the new
7920 * connector list, disable it. */
7921 if ((!set->fb || ro == set->num_connectors) &&
7922 connector->base.encoder &&
7923 connector->base.encoder->crtc == set->crtc) {
7924 connector->new_encoder = NULL;
7925
7926 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
7927 connector->base.base.id,
7928 drm_get_connector_name(&connector->base));
7929 }
7930
7931
7932 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007933 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007934 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02007935 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007936 }
7937 /* connector->new_encoder is now updated for all connectors. */
7938
7939 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007940 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007941 list_for_each_entry(connector, &dev->mode_config.connector_list,
7942 base.head) {
7943 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02007944 continue;
7945
Daniel Vetter9a935852012-07-05 22:34:27 +02007946 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02007947
7948 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02007949 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02007950 new_crtc = set->crtc;
7951 }
7952
7953 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02007954 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
7955 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007956 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02007957 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007958 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
7959
7960 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
7961 connector->base.base.id,
7962 drm_get_connector_name(&connector->base),
7963 new_crtc->base.id);
7964 }
7965
7966 /* Check for any encoders that needs to be disabled. */
7967 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7968 base.head) {
7969 list_for_each_entry(connector,
7970 &dev->mode_config.connector_list,
7971 base.head) {
7972 if (connector->new_encoder == encoder) {
7973 WARN_ON(!connector->new_encoder->new_crtc);
7974
7975 goto next_encoder;
7976 }
7977 }
7978 encoder->new_crtc = NULL;
7979next_encoder:
7980 /* Only now check for crtc changes so we don't miss encoders
7981 * that will be disabled. */
7982 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02007983 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007984 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02007985 }
7986 }
Daniel Vetter9a935852012-07-05 22:34:27 +02007987 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02007988
Daniel Vetter2e431052012-07-04 22:42:15 +02007989 return 0;
7990}
7991
7992static int intel_crtc_set_config(struct drm_mode_set *set)
7993{
7994 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02007995 struct drm_mode_set save_set;
7996 struct intel_set_config *config;
7997 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02007998
Daniel Vetter8d3e3752012-07-05 16:09:09 +02007999 BUG_ON(!set);
8000 BUG_ON(!set->crtc);
8001 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02008002
8003 if (!set->mode)
8004 set->fb = NULL;
8005
Daniel Vetter431e50f2012-07-10 17:53:42 +02008006 /* The fb helper likes to play gross jokes with ->mode_set_config.
8007 * Unfortunately the crtc helper doesn't do much at all for this case,
8008 * so we have to cope with this madness until the fb helper is fixed up. */
8009 if (set->fb && set->num_connectors == 0)
8010 return 0;
8011
Daniel Vetter2e431052012-07-04 22:42:15 +02008012 if (set->fb) {
8013 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8014 set->crtc->base.id, set->fb->base.id,
8015 (int)set->num_connectors, set->x, set->y);
8016 } else {
8017 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02008018 }
8019
8020 dev = set->crtc->dev;
8021
8022 ret = -ENOMEM;
8023 config = kzalloc(sizeof(*config), GFP_KERNEL);
8024 if (!config)
8025 goto out_config;
8026
8027 ret = intel_set_config_save_state(dev, config);
8028 if (ret)
8029 goto out_config;
8030
8031 save_set.crtc = set->crtc;
8032 save_set.mode = &set->crtc->mode;
8033 save_set.x = set->crtc->x;
8034 save_set.y = set->crtc->y;
8035 save_set.fb = set->crtc->fb;
8036
8037 /* Compute whether we need a full modeset, only an fb base update or no
8038 * change at all. In the future we might also check whether only the
8039 * mode changed, e.g. for LVDS where we only change the panel fitter in
8040 * such cases. */
8041 intel_set_config_compute_mode_changes(set, config);
8042
Daniel Vetter9a935852012-07-05 22:34:27 +02008043 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02008044 if (ret)
8045 goto fail;
8046
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008047 if (config->mode_changed) {
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008048 if (set->mode) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008049 DRM_DEBUG_KMS("attempting to set mode from"
8050 " userspace\n");
8051 drm_mode_debug_printmodeline(set->mode);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008052 }
8053
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008054 ret = intel_set_mode(set->crtc, set->mode,
8055 set->x, set->y, set->fb);
8056 if (ret) {
8057 DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
8058 set->crtc->base.id, ret);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008059 goto fail;
8060 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008061 } else if (config->fb_changed) {
Daniel Vetter4f660f42012-07-02 09:47:37 +02008062 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008063 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008064 }
8065
Daniel Vetterd9e55602012-07-04 22:16:09 +02008066 intel_set_config_free(config);
8067
Daniel Vetter50f56112012-07-02 09:35:43 +02008068 return 0;
8069
8070fail:
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008071 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008072
8073 /* Try to restore the config */
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008074 if (config->mode_changed &&
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008075 intel_set_mode(save_set.crtc, save_set.mode,
8076 save_set.x, save_set.y, save_set.fb))
Daniel Vetter50f56112012-07-02 09:35:43 +02008077 DRM_ERROR("failed to restore config after modeset failure\n");
8078
Daniel Vetterd9e55602012-07-04 22:16:09 +02008079out_config:
8080 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008081 return ret;
8082}
8083
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008084static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008085 .cursor_set = intel_crtc_cursor_set,
8086 .cursor_move = intel_crtc_cursor_move,
8087 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008088 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008089 .destroy = intel_crtc_destroy,
8090 .page_flip = intel_crtc_page_flip,
8091};
8092
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008093static void intel_cpu_pll_init(struct drm_device *dev)
8094{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008095 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008096 intel_ddi_pll_init(dev);
8097}
8098
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008099static void intel_pch_pll_init(struct drm_device *dev)
8100{
8101 drm_i915_private_t *dev_priv = dev->dev_private;
8102 int i;
8103
8104 if (dev_priv->num_pch_pll == 0) {
8105 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
8106 return;
8107 }
8108
8109 for (i = 0; i < dev_priv->num_pch_pll; i++) {
8110 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
8111 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
8112 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
8113 }
8114}
8115
Hannes Ederb358d0a2008-12-18 21:18:47 +01008116static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08008117{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008118 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008119 struct intel_crtc *intel_crtc;
8120 int i;
8121
8122 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8123 if (intel_crtc == NULL)
8124 return;
8125
8126 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8127
8128 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08008129 for (i = 0; i < 256; i++) {
8130 intel_crtc->lut_r[i] = i;
8131 intel_crtc->lut_g[i] = i;
8132 intel_crtc->lut_b[i] = i;
8133 }
8134
Jesse Barnes80824002009-09-10 15:28:06 -07008135 /* Swap pipes & planes for FBC on pre-965 */
8136 intel_crtc->pipe = pipe;
8137 intel_crtc->plane = pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02008138 intel_crtc->cpu_transcoder = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01008139 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008140 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01008141 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07008142 }
8143
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008144 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8145 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8146 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8147 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8148
Jesse Barnes5a354202011-06-24 12:19:22 -07008149 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07008150
Jesse Barnes79e53942008-11-07 14:24:08 -08008151 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08008152}
8153
Carl Worth08d7b3d2009-04-29 14:43:54 -07008154int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00008155 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07008156{
Carl Worth08d7b3d2009-04-29 14:43:54 -07008157 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02008158 struct drm_mode_object *drmmode_obj;
8159 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008160
Daniel Vetter1cff8f62012-04-24 09:55:08 +02008161 if (!drm_core_check_feature(dev, DRIVER_MODESET))
8162 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008163
Daniel Vetterc05422d2009-08-11 16:05:30 +02008164 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8165 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07008166
Daniel Vetterc05422d2009-08-11 16:05:30 +02008167 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07008168 DRM_ERROR("no such CRTC id\n");
8169 return -EINVAL;
8170 }
8171
Daniel Vetterc05422d2009-08-11 16:05:30 +02008172 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8173 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008174
Daniel Vetterc05422d2009-08-11 16:05:30 +02008175 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008176}
8177
Daniel Vetter66a92782012-07-12 20:08:18 +02008178static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008179{
Daniel Vetter66a92782012-07-12 20:08:18 +02008180 struct drm_device *dev = encoder->base.dev;
8181 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008182 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008183 int entry = 0;
8184
Daniel Vetter66a92782012-07-12 20:08:18 +02008185 list_for_each_entry(source_encoder,
8186 &dev->mode_config.encoder_list, base.head) {
8187
8188 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008189 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02008190
8191 /* Intel hw has only one MUX where enocoders could be cloned. */
8192 if (encoder->cloneable && source_encoder->cloneable)
8193 index_mask |= (1 << entry);
8194
Jesse Barnes79e53942008-11-07 14:24:08 -08008195 entry++;
8196 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01008197
Jesse Barnes79e53942008-11-07 14:24:08 -08008198 return index_mask;
8199}
8200
Chris Wilson4d302442010-12-14 19:21:29 +00008201static bool has_edp_a(struct drm_device *dev)
8202{
8203 struct drm_i915_private *dev_priv = dev->dev_private;
8204
8205 if (!IS_MOBILE(dev))
8206 return false;
8207
8208 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8209 return false;
8210
8211 if (IS_GEN5(dev) &&
8212 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8213 return false;
8214
8215 return true;
8216}
8217
Jesse Barnes79e53942008-11-07 14:24:08 -08008218static void intel_setup_outputs(struct drm_device *dev)
8219{
Eric Anholt725e30a2009-01-22 13:01:02 -08008220 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008221 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008222 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008223 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08008224
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008225 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00008226 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8227 /* disable the panel fitter on everything but LVDS */
8228 I915_WRITE(PFIT_CONTROL, 0);
8229 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008230
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008231 if (!(HAS_DDI(dev) && (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02008232 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008233
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008234 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03008235 int found;
8236
8237 /* Haswell uses DDI functions to detect digital outputs */
8238 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
8239 /* DDI A only supports eDP */
8240 if (found)
8241 intel_ddi_init(dev, PORT_A);
8242
8243 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
8244 * register */
8245 found = I915_READ(SFUSE_STRAP);
8246
8247 if (found & SFUSE_STRAP_DDIB_DETECTED)
8248 intel_ddi_init(dev, PORT_B);
8249 if (found & SFUSE_STRAP_DDIC_DETECTED)
8250 intel_ddi_init(dev, PORT_C);
8251 if (found & SFUSE_STRAP_DDID_DETECTED)
8252 intel_ddi_init(dev, PORT_D);
8253 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008254 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02008255 dpd_is_edp = intel_dpd_is_edp(dev);
8256
8257 if (has_edp_a(dev))
8258 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008259
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008260 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08008261 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01008262 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008263 if (!found)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008264 intel_hdmi_init(dev, HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008265 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008266 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008267 }
8268
8269 if (I915_READ(HDMIC) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008270 intel_hdmi_init(dev, HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008271
Jesse Barnesb708a1d2012-06-11 14:39:56 -04008272 if (!dpd_is_edp && I915_READ(HDMID) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008273 intel_hdmi_init(dev, HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008274
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008275 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008276 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008277
Daniel Vetter270b3042012-10-27 15:52:05 +02008278 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008279 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008280 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05308281 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008282 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
8283 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05308284
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008285 if (I915_READ(VLV_DISPLAY_BASE + SDVOB) & PORT_DETECTED) {
8286 intel_hdmi_init(dev, VLV_DISPLAY_BASE + SDVOB, PORT_B);
8287 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
8288 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008289 }
8290
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008291 if (I915_READ(VLV_DISPLAY_BASE + SDVOC) & PORT_DETECTED)
8292 intel_hdmi_init(dev, VLV_DISPLAY_BASE + SDVOC, PORT_C);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008293
Zhenyu Wang103a1962009-11-27 11:44:36 +08008294 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008295 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008296
Eric Anholt725e30a2009-01-22 13:01:02 -08008297 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008298 DRM_DEBUG_KMS("probing SDVOB\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008299 found = intel_sdvo_init(dev, SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008300 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8301 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008302 intel_hdmi_init(dev, SDVOB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008303 }
Ma Ling27185ae2009-08-24 13:50:23 +08008304
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008305 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8306 DRM_DEBUG_KMS("probing DP_B\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008307 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008308 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008309 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008310
8311 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008312
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008313 if (I915_READ(SDVOB) & SDVO_DETECTED) {
8314 DRM_DEBUG_KMS("probing SDVOC\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008315 found = intel_sdvo_init(dev, SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008316 }
Ma Ling27185ae2009-08-24 13:50:23 +08008317
8318 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
8319
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008320 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8321 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008322 intel_hdmi_init(dev, SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008323 }
8324 if (SUPPORTS_INTEGRATED_DP(dev)) {
8325 DRM_DEBUG_KMS("probing DP_C\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008326 intel_dp_init(dev, DP_C, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008327 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008328 }
Ma Ling27185ae2009-08-24 13:50:23 +08008329
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008330 if (SUPPORTS_INTEGRATED_DP(dev) &&
8331 (I915_READ(DP_D) & DP_DETECTED)) {
8332 DRM_DEBUG_KMS("probing DP_D\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008333 intel_dp_init(dev, DP_D, PORT_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008334 }
Eric Anholtbad720f2009-10-22 16:11:14 -07008335 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008336 intel_dvo_init(dev);
8337
Zhenyu Wang103a1962009-11-27 11:44:36 +08008338 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008339 intel_tv_init(dev);
8340
Chris Wilson4ef69c72010-09-09 15:14:28 +01008341 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8342 encoder->base.possible_crtcs = encoder->crtc_mask;
8343 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02008344 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08008345 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008346
Paulo Zanonidde86e22012-12-01 12:04:25 -02008347 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02008348
8349 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008350}
8351
8352static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8353{
8354 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008355
8356 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008357 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008358
8359 kfree(intel_fb);
8360}
8361
8362static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008363 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008364 unsigned int *handle)
8365{
8366 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008367 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008368
Chris Wilson05394f32010-11-08 19:18:58 +00008369 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008370}
8371
8372static const struct drm_framebuffer_funcs intel_fb_funcs = {
8373 .destroy = intel_user_framebuffer_destroy,
8374 .create_handle = intel_user_framebuffer_create_handle,
8375};
8376
Dave Airlie38651672010-03-30 05:34:13 +00008377int intel_framebuffer_init(struct drm_device *dev,
8378 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008379 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008380 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008381{
Jesse Barnes79e53942008-11-07 14:24:08 -08008382 int ret;
8383
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008384 if (obj->tiling_mode == I915_TILING_Y) {
8385 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01008386 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008387 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008388
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008389 if (mode_cmd->pitches[0] & 63) {
8390 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
8391 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01008392 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008393 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008394
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008395 /* FIXME <= Gen4 stride limits are bit unclear */
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008396 if (mode_cmd->pitches[0] > 32768) {
8397 DRM_DEBUG("pitch (%d) must be at less than 32768\n",
8398 mode_cmd->pitches[0]);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008399 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008400 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008401
8402 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008403 mode_cmd->pitches[0] != obj->stride) {
8404 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
8405 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008406 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008407 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008408
Ville Syrjälä57779d02012-10-31 17:50:14 +02008409 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008410 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02008411 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008412 case DRM_FORMAT_RGB565:
8413 case DRM_FORMAT_XRGB8888:
8414 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008415 break;
8416 case DRM_FORMAT_XRGB1555:
8417 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008418 if (INTEL_INFO(dev)->gen > 3) {
8419 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008420 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008421 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02008422 break;
8423 case DRM_FORMAT_XBGR8888:
8424 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008425 case DRM_FORMAT_XRGB2101010:
8426 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008427 case DRM_FORMAT_XBGR2101010:
8428 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008429 if (INTEL_INFO(dev)->gen < 4) {
8430 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008431 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008432 }
Jesse Barnesb5626742011-06-24 12:19:27 -07008433 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02008434 case DRM_FORMAT_YUYV:
8435 case DRM_FORMAT_UYVY:
8436 case DRM_FORMAT_YVYU:
8437 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008438 if (INTEL_INFO(dev)->gen < 5) {
8439 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008440 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008441 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008442 break;
8443 default:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008444 DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01008445 return -EINVAL;
8446 }
8447
Ville Syrjälä90f9a332012-10-31 17:50:19 +02008448 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
8449 if (mode_cmd->offsets[0] != 0)
8450 return -EINVAL;
8451
Daniel Vetterc7d73f62012-12-13 23:38:38 +01008452 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
8453 intel_fb->obj = obj;
8454
Jesse Barnes79e53942008-11-07 14:24:08 -08008455 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8456 if (ret) {
8457 DRM_ERROR("framebuffer init failed %d\n", ret);
8458 return ret;
8459 }
8460
Jesse Barnes79e53942008-11-07 14:24:08 -08008461 return 0;
8462}
8463
Jesse Barnes79e53942008-11-07 14:24:08 -08008464static struct drm_framebuffer *
8465intel_user_framebuffer_create(struct drm_device *dev,
8466 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008467 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08008468{
Chris Wilson05394f32010-11-08 19:18:58 +00008469 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008470
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008471 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8472 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00008473 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01008474 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08008475
Chris Wilsond2dff872011-04-19 08:36:26 +01008476 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08008477}
8478
Jesse Barnes79e53942008-11-07 14:24:08 -08008479static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08008480 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00008481 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08008482};
8483
Jesse Barnese70236a2009-09-21 10:42:27 -07008484/* Set up chip specific display functions */
8485static void intel_init_display(struct drm_device *dev)
8486{
8487 struct drm_i915_private *dev_priv = dev->dev_private;
8488
8489 /* We always want a DPMS function */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008490 if (HAS_DDI(dev)) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008491 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02008492 dev_priv->display.crtc_enable = haswell_crtc_enable;
8493 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008494 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008495 dev_priv->display.update_plane = ironlake_update_plane;
8496 } else if (HAS_PCH_SPLIT(dev)) {
Eric Anholtf564048e2011-03-30 13:01:02 -07008497 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008498 dev_priv->display.crtc_enable = ironlake_crtc_enable;
8499 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008500 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008501 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008502 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07008503 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008504 dev_priv->display.crtc_enable = i9xx_crtc_enable;
8505 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008506 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008507 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008508 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008509
Jesse Barnese70236a2009-09-21 10:42:27 -07008510 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07008511 if (IS_VALLEYVIEW(dev))
8512 dev_priv->display.get_display_clock_speed =
8513 valleyview_get_display_clock_speed;
8514 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07008515 dev_priv->display.get_display_clock_speed =
8516 i945_get_display_clock_speed;
8517 else if (IS_I915G(dev))
8518 dev_priv->display.get_display_clock_speed =
8519 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008520 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008521 dev_priv->display.get_display_clock_speed =
8522 i9xx_misc_get_display_clock_speed;
8523 else if (IS_I915GM(dev))
8524 dev_priv->display.get_display_clock_speed =
8525 i915gm_get_display_clock_speed;
8526 else if (IS_I865G(dev))
8527 dev_priv->display.get_display_clock_speed =
8528 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02008529 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008530 dev_priv->display.get_display_clock_speed =
8531 i855_get_display_clock_speed;
8532 else /* 852, 830 */
8533 dev_priv->display.get_display_clock_speed =
8534 i830_get_display_clock_speed;
8535
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008536 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01008537 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008538 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008539 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08008540 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008541 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008542 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07008543 } else if (IS_IVYBRIDGE(dev)) {
8544 /* FIXME: detect B0+ stepping and use auto training */
8545 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008546 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02008547 dev_priv->display.modeset_global_resources =
8548 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03008549 } else if (IS_HASWELL(dev)) {
8550 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08008551 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02008552 dev_priv->display.modeset_global_resources =
8553 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02008554 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07008555 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08008556 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07008557 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008558
8559 /* Default just returns -ENODEV to indicate unsupported */
8560 dev_priv->display.queue_flip = intel_default_queue_flip;
8561
8562 switch (INTEL_INFO(dev)->gen) {
8563 case 2:
8564 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8565 break;
8566
8567 case 3:
8568 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8569 break;
8570
8571 case 4:
8572 case 5:
8573 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8574 break;
8575
8576 case 6:
8577 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8578 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008579 case 7:
8580 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8581 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008582 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008583}
8584
Jesse Barnesb690e962010-07-19 13:53:12 -07008585/*
8586 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8587 * resume, or other times. This quirk makes sure that's the case for
8588 * affected systems.
8589 */
Akshay Joshi0206e352011-08-16 15:34:10 -04008590static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07008591{
8592 struct drm_i915_private *dev_priv = dev->dev_private;
8593
8594 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008595 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008596}
8597
Keith Packard435793d2011-07-12 14:56:22 -07008598/*
8599 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8600 */
8601static void quirk_ssc_force_disable(struct drm_device *dev)
8602{
8603 struct drm_i915_private *dev_priv = dev->dev_private;
8604 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008605 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07008606}
8607
Carsten Emde4dca20e2012-03-15 15:56:26 +01008608/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01008609 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
8610 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01008611 */
8612static void quirk_invert_brightness(struct drm_device *dev)
8613{
8614 struct drm_i915_private *dev_priv = dev->dev_private;
8615 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008616 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008617}
8618
8619struct intel_quirk {
8620 int device;
8621 int subsystem_vendor;
8622 int subsystem_device;
8623 void (*hook)(struct drm_device *dev);
8624};
8625
Egbert Eich5f85f1762012-10-14 15:46:38 +02008626/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
8627struct intel_dmi_quirk {
8628 void (*hook)(struct drm_device *dev);
8629 const struct dmi_system_id (*dmi_id_list)[];
8630};
8631
8632static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
8633{
8634 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
8635 return 1;
8636}
8637
8638static const struct intel_dmi_quirk intel_dmi_quirks[] = {
8639 {
8640 .dmi_id_list = &(const struct dmi_system_id[]) {
8641 {
8642 .callback = intel_dmi_reverse_brightness,
8643 .ident = "NCR Corporation",
8644 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
8645 DMI_MATCH(DMI_PRODUCT_NAME, ""),
8646 },
8647 },
8648 { } /* terminating entry */
8649 },
8650 .hook = quirk_invert_brightness,
8651 },
8652};
8653
Ben Widawskyc43b5632012-04-16 14:07:40 -07008654static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07008655 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04008656 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07008657
Jesse Barnesb690e962010-07-19 13:53:12 -07008658 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
8659 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
8660
Jesse Barnesb690e962010-07-19 13:53:12 -07008661 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
8662 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
8663
Daniel Vetterccd0d362012-10-10 23:13:59 +02008664 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07008665 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02008666 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07008667
8668 /* Lenovo U160 cannot use SSC on LVDS */
8669 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02008670
8671 /* Sony Vaio Y cannot use SSC on LVDS */
8672 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01008673
8674 /* Acer Aspire 5734Z must invert backlight brightness */
8675 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02008676
8677 /* Acer/eMachines G725 */
8678 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02008679
8680 /* Acer/eMachines e725 */
8681 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02008682
8683 /* Acer/Packard Bell NCL20 */
8684 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07008685};
8686
8687static void intel_init_quirks(struct drm_device *dev)
8688{
8689 struct pci_dev *d = dev->pdev;
8690 int i;
8691
8692 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
8693 struct intel_quirk *q = &intel_quirks[i];
8694
8695 if (d->device == q->device &&
8696 (d->subsystem_vendor == q->subsystem_vendor ||
8697 q->subsystem_vendor == PCI_ANY_ID) &&
8698 (d->subsystem_device == q->subsystem_device ||
8699 q->subsystem_device == PCI_ANY_ID))
8700 q->hook(dev);
8701 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02008702 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
8703 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
8704 intel_dmi_quirks[i].hook(dev);
8705 }
Jesse Barnesb690e962010-07-19 13:53:12 -07008706}
8707
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008708/* Disable the VGA plane that we never use */
8709static void i915_disable_vga(struct drm_device *dev)
8710{
8711 struct drm_i915_private *dev_priv = dev->dev_private;
8712 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02008713 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008714
8715 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07008716 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008717 sr1 = inb(VGA_SR_DATA);
8718 outb(sr1 | 1<<5, VGA_SR_DATA);
8719 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
8720 udelay(300);
8721
8722 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
8723 POSTING_READ(vga_reg);
8724}
8725
Daniel Vetterf8175862012-04-10 15:50:11 +02008726void intel_modeset_init_hw(struct drm_device *dev)
8727{
Paulo Zanonifa42e232013-01-25 16:59:11 -02008728 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03008729
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03008730 intel_prepare_ddi(dev);
8731
Daniel Vetterf8175862012-04-10 15:50:11 +02008732 intel_init_clock_gating(dev);
8733
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008734 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008735 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008736 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02008737}
8738
Jesse Barnes79e53942008-11-07 14:24:08 -08008739void intel_modeset_init(struct drm_device *dev)
8740{
Jesse Barnes652c3932009-08-17 13:31:43 -07008741 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008742 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008743
8744 drm_mode_config_init(dev);
8745
8746 dev->mode_config.min_width = 0;
8747 dev->mode_config.min_height = 0;
8748
Dave Airlie019d96c2011-09-29 16:20:42 +01008749 dev->mode_config.preferred_depth = 24;
8750 dev->mode_config.prefer_shadow = 1;
8751
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02008752 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08008753
Jesse Barnesb690e962010-07-19 13:53:12 -07008754 intel_init_quirks(dev);
8755
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008756 intel_init_pm(dev);
8757
Jesse Barnese70236a2009-09-21 10:42:27 -07008758 intel_init_display(dev);
8759
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008760 if (IS_GEN2(dev)) {
8761 dev->mode_config.max_width = 2048;
8762 dev->mode_config.max_height = 2048;
8763 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07008764 dev->mode_config.max_width = 4096;
8765 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08008766 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008767 dev->mode_config.max_width = 8192;
8768 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08008769 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08008770 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008771
Zhao Yakui28c97732009-10-09 11:39:41 +08008772 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10008773 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08008774
Dave Airliea3524f12010-06-06 18:59:41 +10008775 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008776 intel_crtc_init(dev, i);
Jesse Barnes00c2064b2012-01-13 15:48:39 -08008777 ret = intel_plane_init(dev, i);
8778 if (ret)
8779 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08008780 }
8781
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008782 intel_cpu_pll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008783 intel_pch_pll_init(dev);
8784
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008785 /* Just disable it once at startup */
8786 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008787 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00008788
8789 /* Just in case the BIOS is doing something questionable. */
8790 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01008791}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008792
Daniel Vetter24929352012-07-02 20:28:59 +02008793static void
8794intel_connector_break_all_links(struct intel_connector *connector)
8795{
8796 connector->base.dpms = DRM_MODE_DPMS_OFF;
8797 connector->base.encoder = NULL;
8798 connector->encoder->connectors_active = false;
8799 connector->encoder->base.crtc = NULL;
8800}
8801
Daniel Vetter7fad7982012-07-04 17:51:47 +02008802static void intel_enable_pipe_a(struct drm_device *dev)
8803{
8804 struct intel_connector *connector;
8805 struct drm_connector *crt = NULL;
8806 struct intel_load_detect_pipe load_detect_temp;
8807
8808 /* We can't just switch on the pipe A, we need to set things up with a
8809 * proper mode and output configuration. As a gross hack, enable pipe A
8810 * by enabling the load detect pipe once. */
8811 list_for_each_entry(connector,
8812 &dev->mode_config.connector_list,
8813 base.head) {
8814 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
8815 crt = &connector->base;
8816 break;
8817 }
8818 }
8819
8820 if (!crt)
8821 return;
8822
8823 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
8824 intel_release_load_detect_pipe(crt, &load_detect_temp);
8825
8826
8827}
8828
Daniel Vetterfa555832012-10-10 23:14:00 +02008829static bool
8830intel_check_plane_mapping(struct intel_crtc *crtc)
8831{
8832 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8833 u32 reg, val;
8834
8835 if (dev_priv->num_pipe == 1)
8836 return true;
8837
8838 reg = DSPCNTR(!crtc->plane);
8839 val = I915_READ(reg);
8840
8841 if ((val & DISPLAY_PLANE_ENABLE) &&
8842 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
8843 return false;
8844
8845 return true;
8846}
8847
Daniel Vetter24929352012-07-02 20:28:59 +02008848static void intel_sanitize_crtc(struct intel_crtc *crtc)
8849{
8850 struct drm_device *dev = crtc->base.dev;
8851 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02008852 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02008853
Daniel Vetter24929352012-07-02 20:28:59 +02008854 /* Clear any frame start delays used for debugging left by the BIOS */
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008855 reg = PIPECONF(crtc->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02008856 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
8857
8858 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02008859 * disable the crtc (and hence change the state) if it is wrong. Note
8860 * that gen4+ has a fixed plane -> pipe mapping. */
8861 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02008862 struct intel_connector *connector;
8863 bool plane;
8864
Daniel Vetter24929352012-07-02 20:28:59 +02008865 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
8866 crtc->base.base.id);
8867
8868 /* Pipe has the wrong plane attached and the plane is active.
8869 * Temporarily change the plane mapping and disable everything
8870 * ... */
8871 plane = crtc->plane;
8872 crtc->plane = !plane;
8873 dev_priv->display.crtc_disable(&crtc->base);
8874 crtc->plane = plane;
8875
8876 /* ... and break all links. */
8877 list_for_each_entry(connector, &dev->mode_config.connector_list,
8878 base.head) {
8879 if (connector->encoder->base.crtc != &crtc->base)
8880 continue;
8881
8882 intel_connector_break_all_links(connector);
8883 }
8884
8885 WARN_ON(crtc->active);
8886 crtc->base.enabled = false;
8887 }
Daniel Vetter24929352012-07-02 20:28:59 +02008888
Daniel Vetter7fad7982012-07-04 17:51:47 +02008889 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
8890 crtc->pipe == PIPE_A && !crtc->active) {
8891 /* BIOS forgot to enable pipe A, this mostly happens after
8892 * resume. Force-enable the pipe to fix this, the update_dpms
8893 * call below we restore the pipe to the right state, but leave
8894 * the required bits on. */
8895 intel_enable_pipe_a(dev);
8896 }
8897
Daniel Vetter24929352012-07-02 20:28:59 +02008898 /* Adjust the state of the output pipe according to whether we
8899 * have active connectors/encoders. */
8900 intel_crtc_update_dpms(&crtc->base);
8901
8902 if (crtc->active != crtc->base.enabled) {
8903 struct intel_encoder *encoder;
8904
8905 /* This can happen either due to bugs in the get_hw_state
8906 * functions or because the pipe is force-enabled due to the
8907 * pipe A quirk. */
8908 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
8909 crtc->base.base.id,
8910 crtc->base.enabled ? "enabled" : "disabled",
8911 crtc->active ? "enabled" : "disabled");
8912
8913 crtc->base.enabled = crtc->active;
8914
8915 /* Because we only establish the connector -> encoder ->
8916 * crtc links if something is active, this means the
8917 * crtc is now deactivated. Break the links. connector
8918 * -> encoder links are only establish when things are
8919 * actually up, hence no need to break them. */
8920 WARN_ON(crtc->active);
8921
8922 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
8923 WARN_ON(encoder->connectors_active);
8924 encoder->base.crtc = NULL;
8925 }
8926 }
8927}
8928
8929static void intel_sanitize_encoder(struct intel_encoder *encoder)
8930{
8931 struct intel_connector *connector;
8932 struct drm_device *dev = encoder->base.dev;
8933
8934 /* We need to check both for a crtc link (meaning that the
8935 * encoder is active and trying to read from a pipe) and the
8936 * pipe itself being active. */
8937 bool has_active_crtc = encoder->base.crtc &&
8938 to_intel_crtc(encoder->base.crtc)->active;
8939
8940 if (encoder->connectors_active && !has_active_crtc) {
8941 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
8942 encoder->base.base.id,
8943 drm_get_encoder_name(&encoder->base));
8944
8945 /* Connector is active, but has no active pipe. This is
8946 * fallout from our resume register restoring. Disable
8947 * the encoder manually again. */
8948 if (encoder->base.crtc) {
8949 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
8950 encoder->base.base.id,
8951 drm_get_encoder_name(&encoder->base));
8952 encoder->disable(encoder);
8953 }
8954
8955 /* Inconsistent output/port/pipe state happens presumably due to
8956 * a bug in one of the get_hw_state functions. Or someplace else
8957 * in our code, like the register restore mess on resume. Clamp
8958 * things to off as a safer default. */
8959 list_for_each_entry(connector,
8960 &dev->mode_config.connector_list,
8961 base.head) {
8962 if (connector->encoder != encoder)
8963 continue;
8964
8965 intel_connector_break_all_links(connector);
8966 }
8967 }
8968 /* Enabled encoders without active connectors will be fixed in
8969 * the crtc fixup. */
8970}
8971
Daniel Vetter44cec742013-01-25 17:53:21 +01008972void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01008973{
8974 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02008975 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01008976
8977 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
8978 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +02008979 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01008980 }
8981}
8982
Daniel Vetter24929352012-07-02 20:28:59 +02008983/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
8984 * and i915 state tracking structures. */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01008985void intel_modeset_setup_hw_state(struct drm_device *dev,
8986 bool force_restore)
Daniel Vetter24929352012-07-02 20:28:59 +02008987{
8988 struct drm_i915_private *dev_priv = dev->dev_private;
8989 enum pipe pipe;
8990 u32 tmp;
8991 struct intel_crtc *crtc;
8992 struct intel_encoder *encoder;
8993 struct intel_connector *connector;
8994
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008995 if (HAS_DDI(dev)) {
Paulo Zanonie28d54c2012-10-24 16:09:25 -02008996 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8997
8998 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8999 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9000 case TRANS_DDI_EDP_INPUT_A_ON:
9001 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9002 pipe = PIPE_A;
9003 break;
9004 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9005 pipe = PIPE_B;
9006 break;
9007 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9008 pipe = PIPE_C;
9009 break;
9010 }
9011
9012 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9013 crtc->cpu_transcoder = TRANSCODER_EDP;
9014
9015 DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
9016 pipe_name(pipe));
9017 }
9018 }
9019
Daniel Vetter24929352012-07-02 20:28:59 +02009020 for_each_pipe(pipe) {
9021 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9022
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009023 tmp = I915_READ(PIPECONF(crtc->cpu_transcoder));
Daniel Vetter24929352012-07-02 20:28:59 +02009024 if (tmp & PIPECONF_ENABLE)
9025 crtc->active = true;
9026 else
9027 crtc->active = false;
9028
9029 crtc->base.enabled = crtc->active;
9030
9031 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9032 crtc->base.base.id,
9033 crtc->active ? "enabled" : "disabled");
9034 }
9035
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009036 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009037 intel_ddi_setup_hw_pll_state(dev);
9038
Daniel Vetter24929352012-07-02 20:28:59 +02009039 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9040 base.head) {
9041 pipe = 0;
9042
9043 if (encoder->get_hw_state(encoder, &pipe)) {
9044 encoder->base.crtc =
9045 dev_priv->pipe_to_crtc_mapping[pipe];
9046 } else {
9047 encoder->base.crtc = NULL;
9048 }
9049
9050 encoder->connectors_active = false;
9051 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9052 encoder->base.base.id,
9053 drm_get_encoder_name(&encoder->base),
9054 encoder->base.crtc ? "enabled" : "disabled",
9055 pipe);
9056 }
9057
9058 list_for_each_entry(connector, &dev->mode_config.connector_list,
9059 base.head) {
9060 if (connector->get_hw_state(connector)) {
9061 connector->base.dpms = DRM_MODE_DPMS_ON;
9062 connector->encoder->connectors_active = true;
9063 connector->base.encoder = &connector->encoder->base;
9064 } else {
9065 connector->base.dpms = DRM_MODE_DPMS_OFF;
9066 connector->base.encoder = NULL;
9067 }
9068 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9069 connector->base.base.id,
9070 drm_get_connector_name(&connector->base),
9071 connector->base.encoder ? "enabled" : "disabled");
9072 }
9073
9074 /* HW state is read out, now we need to sanitize this mess. */
9075 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9076 base.head) {
9077 intel_sanitize_encoder(encoder);
9078 }
9079
9080 for_each_pipe(pipe) {
9081 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9082 intel_sanitize_crtc(crtc);
9083 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009084
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009085 if (force_restore) {
9086 for_each_pipe(pipe) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009087 intel_crtc_restore_mode(dev_priv->pipe_to_crtc_mapping[pipe]);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009088 }
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009089
9090 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009091 } else {
9092 intel_modeset_update_staged_output_state(dev);
9093 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009094
9095 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +02009096
9097 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009098}
9099
9100void intel_modeset_gem_init(struct drm_device *dev)
9101{
Chris Wilson1833b132012-05-09 11:56:28 +01009102 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02009103
9104 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +02009105
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009106 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08009107}
9108
9109void intel_modeset_cleanup(struct drm_device *dev)
9110{
Jesse Barnes652c3932009-08-17 13:31:43 -07009111 struct drm_i915_private *dev_priv = dev->dev_private;
9112 struct drm_crtc *crtc;
9113 struct intel_crtc *intel_crtc;
9114
Keith Packardf87ea762010-10-03 19:36:26 -07009115 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009116 mutex_lock(&dev->struct_mutex);
9117
Jesse Barnes723bfd72010-10-07 16:01:13 -07009118 intel_unregister_dsm_handler();
9119
9120
Jesse Barnes652c3932009-08-17 13:31:43 -07009121 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9122 /* Skip inactive CRTCs */
9123 if (!crtc->fb)
9124 continue;
9125
9126 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02009127 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009128 }
9129
Chris Wilson973d04f2011-07-08 12:22:37 +01009130 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07009131
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009132 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00009133
Daniel Vetter930ebb42012-06-29 23:32:16 +02009134 ironlake_teardown_rc6(dev);
9135
Jesse Barnes57f350b2012-03-28 13:39:25 -07009136 if (IS_VALLEYVIEW(dev))
9137 vlv_init_dpio(dev);
9138
Kristian Høgsberg69341a52009-11-11 12:19:17 -05009139 mutex_unlock(&dev->struct_mutex);
9140
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009141 /* Disable the irq before mode object teardown, for the irq might
9142 * enqueue unpin/hotplug work. */
9143 drm_irq_uninstall(dev);
9144 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02009145 cancel_work_sync(&dev_priv->rps.work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009146
Chris Wilson1630fe72011-07-08 12:22:42 +01009147 /* flush any delayed tasks or pending work */
9148 flush_scheduled_work();
9149
Jesse Barnes79e53942008-11-07 14:24:08 -08009150 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +01009151
9152 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009153}
9154
Dave Airlie28d52042009-09-21 14:33:58 +10009155/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08009156 * Return which encoder is currently attached for connector.
9157 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01009158struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08009159{
Chris Wilsondf0e9242010-09-09 16:20:55 +01009160 return &intel_attached_encoder(connector)->base;
9161}
Jesse Barnes79e53942008-11-07 14:24:08 -08009162
Chris Wilsondf0e9242010-09-09 16:20:55 +01009163void intel_connector_attach_encoder(struct intel_connector *connector,
9164 struct intel_encoder *encoder)
9165{
9166 connector->encoder = encoder;
9167 drm_mode_connector_attach_encoder(&connector->base,
9168 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009169}
Dave Airlie28d52042009-09-21 14:33:58 +10009170
9171/*
9172 * set vga decode state - true == enable VGA decode
9173 */
9174int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9175{
9176 struct drm_i915_private *dev_priv = dev->dev_private;
9177 u16 gmch_ctrl;
9178
9179 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9180 if (state)
9181 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9182 else
9183 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9184 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9185 return 0;
9186}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009187
9188#ifdef CONFIG_DEBUG_FS
9189#include <linux/seq_file.h>
9190
9191struct intel_display_error_state {
9192 struct intel_cursor_error_state {
9193 u32 control;
9194 u32 position;
9195 u32 base;
9196 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +01009197 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009198
9199 struct intel_pipe_error_state {
9200 u32 conf;
9201 u32 source;
9202
9203 u32 htotal;
9204 u32 hblank;
9205 u32 hsync;
9206 u32 vtotal;
9207 u32 vblank;
9208 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +01009209 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009210
9211 struct intel_plane_error_state {
9212 u32 control;
9213 u32 stride;
9214 u32 size;
9215 u32 pos;
9216 u32 addr;
9217 u32 surface;
9218 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +01009219 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009220};
9221
9222struct intel_display_error_state *
9223intel_display_capture_error_state(struct drm_device *dev)
9224{
Akshay Joshi0206e352011-08-16 15:34:10 -04009225 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009226 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009227 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009228 int i;
9229
9230 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9231 if (error == NULL)
9232 return NULL;
9233
Damien Lespiau52331302012-08-15 19:23:25 +01009234 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009235 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
9236
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009237 error->cursor[i].control = I915_READ(CURCNTR(i));
9238 error->cursor[i].position = I915_READ(CURPOS(i));
9239 error->cursor[i].base = I915_READ(CURBASE(i));
9240
9241 error->plane[i].control = I915_READ(DSPCNTR(i));
9242 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
9243 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04009244 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009245 error->plane[i].addr = I915_READ(DSPADDR(i));
9246 if (INTEL_INFO(dev)->gen >= 4) {
9247 error->plane[i].surface = I915_READ(DSPSURF(i));
9248 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9249 }
9250
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009251 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009252 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009253 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
9254 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
9255 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
9256 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
9257 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
9258 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009259 }
9260
9261 return error;
9262}
9263
9264void
9265intel_display_print_error_state(struct seq_file *m,
9266 struct drm_device *dev,
9267 struct intel_display_error_state *error)
9268{
Damien Lespiau52331302012-08-15 19:23:25 +01009269 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009270 int i;
9271
Damien Lespiau52331302012-08-15 19:23:25 +01009272 seq_printf(m, "Num Pipes: %d\n", dev_priv->num_pipe);
9273 for_each_pipe(i) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009274 seq_printf(m, "Pipe [%d]:\n", i);
9275 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9276 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
9277 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9278 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9279 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9280 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9281 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9282 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9283
9284 seq_printf(m, "Plane [%d]:\n", i);
9285 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9286 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9287 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
9288 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9289 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9290 if (INTEL_INFO(dev)->gen >= 4) {
9291 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9292 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9293 }
9294
9295 seq_printf(m, "Cursor [%d]:\n", i);
9296 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9297 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9298 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9299 }
9300}
9301#endif