blob: 15ce99125c64d435baf49be6084b04009a26b762 [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 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080050} intel_range_t;
51
52typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040053 int dot_limit;
54 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080055} intel_p2_t;
56
57#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080058typedef struct intel_limit intel_limit_t;
59struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040060 intel_range_t dot, vco, n, m, m1, m2, p, p1;
61 intel_p2_t p2;
Ville Syrjäläf4808ab2013-02-28 19:19:44 +020062 /**
63 * find_pll() - Find the best values for the PLL
64 * @limit: limits for the PLL
65 * @crtc: current CRTC
66 * @target: target frequency in kHz
67 * @refclk: reference clock frequency in kHz
68 * @match_clock: if provided, @best_clock P divider must
69 * match the P divider from @match_clock
70 * used for LVDS downclocking
71 * @best_clock: best PLL values found
72 *
73 * Returns true on success, false on failure.
74 */
75 bool (*find_pll)(const intel_limit_t *limit,
76 struct drm_crtc *crtc,
77 int target, int refclk,
78 intel_clock_t *match_clock,
79 intel_clock_t *best_clock);
Ma Lingd4906092009-03-18 20:13:27 +080080};
Jesse Barnes79e53942008-11-07 14:24:08 -080081
Jesse Barnes2377b742010-07-07 14:06:43 -070082/* FDI */
83#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
84
Daniel Vetterd2acd212012-10-20 20:57:43 +020085int
86intel_pch_rawclk(struct drm_device *dev)
87{
88 struct drm_i915_private *dev_priv = dev->dev_private;
89
90 WARN_ON(!HAS_PCH_SPLIT(dev));
91
92 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
93}
94
Ma Lingd4906092009-03-18 20:13:27 +080095static bool
96intel_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);
Ma Lingd4906092009-03-18 20:13:27 +080099static bool
100intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800101 int target, int refclk, intel_clock_t *match_clock,
102 intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800103
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700104static bool
105intel_find_pll_g4x_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);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800108static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500109intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800110 int target, int refclk, intel_clock_t *match_clock,
111 intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700112
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700113static bool
114intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
115 int target, int refclk, intel_clock_t *match_clock,
116 intel_clock_t *best_clock);
117
Chris Wilson021357a2010-09-07 20:54:59 +0100118static inline u32 /* units of 100MHz */
119intel_fdi_link_freq(struct drm_device *dev)
120{
Chris Wilson8b99e682010-10-13 09:59:17 +0100121 if (IS_GEN5(dev)) {
122 struct drm_i915_private *dev_priv = dev->dev_private;
123 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
124 } else
125 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100126}
127
Keith Packarde4b36692009-06-05 19:22:17 -0700128static const intel_limit_t intel_limits_i8xx_dvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400129 .dot = { .min = 25000, .max = 350000 },
130 .vco = { .min = 930000, .max = 1400000 },
131 .n = { .min = 3, .max = 16 },
132 .m = { .min = 96, .max = 140 },
133 .m1 = { .min = 18, .max = 26 },
134 .m2 = { .min = 6, .max = 16 },
135 .p = { .min = 4, .max = 128 },
136 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700137 .p2 = { .dot_limit = 165000,
138 .p2_slow = 4, .p2_fast = 2 },
Ma Lingd4906092009-03-18 20:13:27 +0800139 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700140};
141
142static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400143 .dot = { .min = 25000, .max = 350000 },
144 .vco = { .min = 930000, .max = 1400000 },
145 .n = { .min = 3, .max = 16 },
146 .m = { .min = 96, .max = 140 },
147 .m1 = { .min = 18, .max = 26 },
148 .m2 = { .min = 6, .max = 16 },
149 .p = { .min = 4, .max = 128 },
150 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700151 .p2 = { .dot_limit = 165000,
152 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800153 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700154};
Eric Anholt273e27c2011-03-30 13:01:10 -0700155
Keith Packarde4b36692009-06-05 19:22:17 -0700156static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400157 .dot = { .min = 20000, .max = 400000 },
158 .vco = { .min = 1400000, .max = 2800000 },
159 .n = { .min = 1, .max = 6 },
160 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100161 .m1 = { .min = 8, .max = 18 },
162 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400163 .p = { .min = 5, .max = 80 },
164 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700165 .p2 = { .dot_limit = 200000,
166 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800167 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700168};
169
170static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400171 .dot = { .min = 20000, .max = 400000 },
172 .vco = { .min = 1400000, .max = 2800000 },
173 .n = { .min = 1, .max = 6 },
174 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100175 .m1 = { .min = 8, .max = 18 },
176 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400177 .p = { .min = 7, .max = 98 },
178 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700179 .p2 = { .dot_limit = 112000,
180 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800181 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700182};
183
Eric Anholt273e27c2011-03-30 13:01:10 -0700184
Keith Packarde4b36692009-06-05 19:22:17 -0700185static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700186 .dot = { .min = 25000, .max = 270000 },
187 .vco = { .min = 1750000, .max = 3500000},
188 .n = { .min = 1, .max = 4 },
189 .m = { .min = 104, .max = 138 },
190 .m1 = { .min = 17, .max = 23 },
191 .m2 = { .min = 5, .max = 11 },
192 .p = { .min = 10, .max = 30 },
193 .p1 = { .min = 1, .max = 3},
194 .p2 = { .dot_limit = 270000,
195 .p2_slow = 10,
196 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800197 },
Ma Lingd4906092009-03-18 20:13:27 +0800198 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700199};
200
201static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700202 .dot = { .min = 22000, .max = 400000 },
203 .vco = { .min = 1750000, .max = 3500000},
204 .n = { .min = 1, .max = 4 },
205 .m = { .min = 104, .max = 138 },
206 .m1 = { .min = 16, .max = 23 },
207 .m2 = { .min = 5, .max = 11 },
208 .p = { .min = 5, .max = 80 },
209 .p1 = { .min = 1, .max = 8},
210 .p2 = { .dot_limit = 165000,
211 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800212 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700213};
214
215static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700216 .dot = { .min = 20000, .max = 115000 },
217 .vco = { .min = 1750000, .max = 3500000 },
218 .n = { .min = 1, .max = 3 },
219 .m = { .min = 104, .max = 138 },
220 .m1 = { .min = 17, .max = 23 },
221 .m2 = { .min = 5, .max = 11 },
222 .p = { .min = 28, .max = 112 },
223 .p1 = { .min = 2, .max = 8 },
224 .p2 = { .dot_limit = 0,
225 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800226 },
Ma Lingd4906092009-03-18 20:13:27 +0800227 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700228};
229
230static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700231 .dot = { .min = 80000, .max = 224000 },
232 .vco = { .min = 1750000, .max = 3500000 },
233 .n = { .min = 1, .max = 3 },
234 .m = { .min = 104, .max = 138 },
235 .m1 = { .min = 17, .max = 23 },
236 .m2 = { .min = 5, .max = 11 },
237 .p = { .min = 14, .max = 42 },
238 .p1 = { .min = 2, .max = 6 },
239 .p2 = { .dot_limit = 0,
240 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800241 },
Ma Lingd4906092009-03-18 20:13:27 +0800242 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700243};
244
245static const intel_limit_t intel_limits_g4x_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400246 .dot = { .min = 161670, .max = 227000 },
247 .vco = { .min = 1750000, .max = 3500000},
248 .n = { .min = 1, .max = 2 },
249 .m = { .min = 97, .max = 108 },
250 .m1 = { .min = 0x10, .max = 0x12 },
251 .m2 = { .min = 0x05, .max = 0x06 },
252 .p = { .min = 10, .max = 20 },
253 .p1 = { .min = 1, .max = 2},
254 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700255 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400256 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700257};
258
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500259static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400260 .dot = { .min = 20000, .max = 400000},
261 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700262 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400263 .n = { .min = 3, .max = 6 },
264 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700265 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400266 .m1 = { .min = 0, .max = 0 },
267 .m2 = { .min = 0, .max = 254 },
268 .p = { .min = 5, .max = 80 },
269 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700270 .p2 = { .dot_limit = 200000,
271 .p2_slow = 10, .p2_fast = 5 },
Shaohua Li61157072009-04-03 15:24:43 +0800272 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700273};
274
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500275static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400276 .dot = { .min = 20000, .max = 400000 },
277 .vco = { .min = 1700000, .max = 3500000 },
278 .n = { .min = 3, .max = 6 },
279 .m = { .min = 2, .max = 256 },
280 .m1 = { .min = 0, .max = 0 },
281 .m2 = { .min = 0, .max = 254 },
282 .p = { .min = 7, .max = 112 },
283 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700284 .p2 = { .dot_limit = 112000,
285 .p2_slow = 14, .p2_fast = 14 },
Shaohua Li61157072009-04-03 15:24:43 +0800286 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700287};
288
Eric Anholt273e27c2011-03-30 13:01:10 -0700289/* Ironlake / Sandybridge
290 *
291 * We calculate clock using (register_value + 2) for N/M1/M2, so here
292 * the range value for them is (actual_value - 2).
293 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800294static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700295 .dot = { .min = 25000, .max = 350000 },
296 .vco = { .min = 1760000, .max = 3510000 },
297 .n = { .min = 1, .max = 5 },
298 .m = { .min = 79, .max = 127 },
299 .m1 = { .min = 12, .max = 22 },
300 .m2 = { .min = 5, .max = 9 },
301 .p = { .min = 5, .max = 80 },
302 .p1 = { .min = 1, .max = 8 },
303 .p2 = { .dot_limit = 225000,
304 .p2_slow = 10, .p2_fast = 5 },
Zhao Yakui45476682009-12-31 16:06:04 +0800305 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700306};
307
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800308static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700309 .dot = { .min = 25000, .max = 350000 },
310 .vco = { .min = 1760000, .max = 3510000 },
311 .n = { .min = 1, .max = 3 },
312 .m = { .min = 79, .max = 118 },
313 .m1 = { .min = 12, .max = 22 },
314 .m2 = { .min = 5, .max = 9 },
315 .p = { .min = 28, .max = 112 },
316 .p1 = { .min = 2, .max = 8 },
317 .p2 = { .dot_limit = 225000,
318 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800319 .find_pll = intel_g4x_find_best_PLL,
320};
321
322static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .dot = { .min = 25000, .max = 350000 },
324 .vco = { .min = 1760000, .max = 3510000 },
325 .n = { .min = 1, .max = 3 },
326 .m = { .min = 79, .max = 127 },
327 .m1 = { .min = 12, .max = 22 },
328 .m2 = { .min = 5, .max = 9 },
329 .p = { .min = 14, .max = 56 },
330 .p1 = { .min = 2, .max = 8 },
331 .p2 = { .dot_limit = 225000,
332 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800333 .find_pll = intel_g4x_find_best_PLL,
334};
335
Eric Anholt273e27c2011-03-30 13:01:10 -0700336/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800337static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700338 .dot = { .min = 25000, .max = 350000 },
339 .vco = { .min = 1760000, .max = 3510000 },
340 .n = { .min = 1, .max = 2 },
341 .m = { .min = 79, .max = 126 },
342 .m1 = { .min = 12, .max = 22 },
343 .m2 = { .min = 5, .max = 9 },
344 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400345 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700346 .p2 = { .dot_limit = 225000,
347 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800348 .find_pll = intel_g4x_find_best_PLL,
349};
350
351static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700352 .dot = { .min = 25000, .max = 350000 },
353 .vco = { .min = 1760000, .max = 3510000 },
354 .n = { .min = 1, .max = 3 },
355 .m = { .min = 79, .max = 126 },
356 .m1 = { .min = 12, .max = 22 },
357 .m2 = { .min = 5, .max = 9 },
358 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400359 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700360 .p2 = { .dot_limit = 225000,
361 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800362 .find_pll = intel_g4x_find_best_PLL,
363};
364
365static const intel_limit_t intel_limits_ironlake_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400366 .dot = { .min = 25000, .max = 350000 },
367 .vco = { .min = 1760000, .max = 3510000},
368 .n = { .min = 1, .max = 2 },
369 .m = { .min = 81, .max = 90 },
370 .m1 = { .min = 12, .max = 22 },
371 .m2 = { .min = 5, .max = 9 },
372 .p = { .min = 10, .max = 20 },
373 .p1 = { .min = 1, .max = 2},
374 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700375 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400376 .find_pll = intel_find_pll_ironlake_dp,
Jesse Barnes79e53942008-11-07 14:24:08 -0800377};
378
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700379static const intel_limit_t intel_limits_vlv_dac = {
380 .dot = { .min = 25000, .max = 270000 },
381 .vco = { .min = 4000000, .max = 6000000 },
382 .n = { .min = 1, .max = 7 },
383 .m = { .min = 22, .max = 450 }, /* guess */
384 .m1 = { .min = 2, .max = 3 },
385 .m2 = { .min = 11, .max = 156 },
386 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200387 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700388 .p2 = { .dot_limit = 270000,
389 .p2_slow = 2, .p2_fast = 20 },
390 .find_pll = intel_vlv_find_best_pll,
391};
392
393static const intel_limit_t intel_limits_vlv_hdmi = {
Daniel Vetter75e53982013-04-18 21:10:43 +0200394 .dot = { .min = 25000, .max = 270000 },
395 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700396 .n = { .min = 1, .max = 7 },
397 .m = { .min = 60, .max = 300 }, /* guess */
398 .m1 = { .min = 2, .max = 3 },
399 .m2 = { .min = 11, .max = 156 },
400 .p = { .min = 10, .max = 30 },
401 .p1 = { .min = 2, .max = 3 },
402 .p2 = { .dot_limit = 270000,
403 .p2_slow = 2, .p2_fast = 20 },
404 .find_pll = intel_vlv_find_best_pll,
405};
406
407static const intel_limit_t intel_limits_vlv_dp = {
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530408 .dot = { .min = 25000, .max = 270000 },
409 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700410 .n = { .min = 1, .max = 7 },
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530411 .m = { .min = 22, .max = 450 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700412 .m1 = { .min = 2, .max = 3 },
413 .m2 = { .min = 11, .max = 156 },
414 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200415 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700416 .p2 = { .dot_limit = 270000,
417 .p2_slow = 2, .p2_fast = 20 },
418 .find_pll = intel_vlv_find_best_pll,
419};
420
Jesse Barnes57f350b2012-03-28 13:39:25 -0700421u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
422{
Daniel Vetter09153002012-12-12 14:06:44 +0100423 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnes57f350b2012-03-28 13:39:25 -0700424
Jesse Barnes57f350b2012-03-28 13:39:25 -0700425 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
426 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100427 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700428 }
429
430 I915_WRITE(DPIO_REG, reg);
431 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
432 DPIO_BYTE);
433 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
434 DRM_ERROR("DPIO read wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100435 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700436 }
Jesse Barnes57f350b2012-03-28 13:39:25 -0700437
Daniel Vetter09153002012-12-12 14:06:44 +0100438 return I915_READ(DPIO_DATA);
Jesse Barnes57f350b2012-03-28 13:39:25 -0700439}
440
Pallavi Ge2fa6fb2013-04-18 14:44:28 -0700441void intel_dpio_write(struct drm_i915_private *dev_priv, int reg, u32 val)
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700442{
Daniel Vetter09153002012-12-12 14:06:44 +0100443 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700444
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700445 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
446 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100447 return;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700448 }
449
450 I915_WRITE(DPIO_DATA, val);
451 I915_WRITE(DPIO_REG, reg);
452 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
453 DPIO_BYTE);
454 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
455 DRM_ERROR("DPIO write wait timed out\n");
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700456}
457
Chris Wilson1b894b52010-12-14 20:04:54 +0000458static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
459 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800460{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800461 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800462 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800463
464 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100465 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000466 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800467 limit = &intel_limits_ironlake_dual_lvds_100m;
468 else
469 limit = &intel_limits_ironlake_dual_lvds;
470 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000471 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800472 limit = &intel_limits_ironlake_single_lvds_100m;
473 else
474 limit = &intel_limits_ironlake_single_lvds;
475 }
476 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Jani Nikula547dc042012-11-02 11:24:03 +0200477 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Zhao Yakui45476682009-12-31 16:06:04 +0800478 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800479 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800480 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800481
482 return limit;
483}
484
Ma Ling044c7c42009-03-18 20:13:23 +0800485static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
486{
487 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800488 const intel_limit_t *limit;
489
490 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100491 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700492 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800493 else
Keith Packarde4b36692009-06-05 19:22:17 -0700494 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800495 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
496 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700497 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800498 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700499 limit = &intel_limits_g4x_sdvo;
Akshay Joshi0206e352011-08-16 15:34:10 -0400500 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700501 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800502 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700503 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800504
505 return limit;
506}
507
Chris Wilson1b894b52010-12-14 20:04:54 +0000508static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800509{
510 struct drm_device *dev = crtc->dev;
511 const intel_limit_t *limit;
512
Eric Anholtbad720f2009-10-22 16:11:14 -0700513 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000514 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800515 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800516 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500517 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800518 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500519 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800520 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500521 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700522 } else if (IS_VALLEYVIEW(dev)) {
523 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
524 limit = &intel_limits_vlv_dac;
525 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
526 limit = &intel_limits_vlv_hdmi;
527 else
528 limit = &intel_limits_vlv_dp;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100529 } else if (!IS_GEN2(dev)) {
530 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
531 limit = &intel_limits_i9xx_lvds;
532 else
533 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800534 } else {
535 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700536 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800537 else
Keith Packarde4b36692009-06-05 19:22:17 -0700538 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800539 }
540 return limit;
541}
542
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500543/* m1 is reserved as 0 in Pineview, n is a ring counter */
544static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800545{
Shaohua Li21778322009-02-23 15:19:16 +0800546 clock->m = clock->m2 + 2;
547 clock->p = clock->p1 * clock->p2;
548 clock->vco = refclk * clock->m / clock->n;
549 clock->dot = clock->vco / clock->p;
550}
551
552static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
553{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500554 if (IS_PINEVIEW(dev)) {
555 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800556 return;
557 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800558 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
559 clock->p = clock->p1 * clock->p2;
560 clock->vco = refclk * clock->m / (clock->n + 2);
561 clock->dot = clock->vco / clock->p;
562}
563
Jesse Barnes79e53942008-11-07 14:24:08 -0800564/**
565 * Returns whether any output on the specified pipe is of the specified type
566 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100567bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800568{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100569 struct drm_device *dev = crtc->dev;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100570 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800571
Daniel Vetter6c2b7c122012-07-05 09:50:24 +0200572 for_each_encoder_on_crtc(dev, crtc, encoder)
573 if (encoder->type == type)
Chris Wilson4ef69c72010-09-09 15:14:28 +0100574 return true;
575
576 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800577}
578
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800579#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800580/**
581 * Returns whether the given set of divisors are valid for a given refclk with
582 * the given connectors.
583 */
584
Chris Wilson1b894b52010-12-14 20:04:54 +0000585static bool intel_PLL_is_valid(struct drm_device *dev,
586 const intel_limit_t *limit,
587 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800588{
Jesse Barnes79e53942008-11-07 14:24:08 -0800589 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400590 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800591 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400592 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800593 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400594 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800595 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400596 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500597 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400598 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800599 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400600 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800601 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400602 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800603 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400604 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800605 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
606 * connector, etc., rather than just a single range.
607 */
608 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400609 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610
611 return true;
612}
613
Ma Lingd4906092009-03-18 20:13:27 +0800614static bool
615intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800616 int target, int refclk, intel_clock_t *match_clock,
617 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800618
Jesse Barnes79e53942008-11-07 14:24:08 -0800619{
620 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800621 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800622 int err = target;
623
Daniel Vettera210b022012-11-26 17:22:08 +0100624 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800625 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100626 * For LVDS just rely on its current settings for dual-channel.
627 * We haven't figured out how to reliably set up different
628 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800629 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100630 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800631 clock.p2 = limit->p2.p2_fast;
632 else
633 clock.p2 = limit->p2.p2_slow;
634 } else {
635 if (target < limit->p2.dot_limit)
636 clock.p2 = limit->p2.p2_slow;
637 else
638 clock.p2 = limit->p2.p2_fast;
639 }
640
Akshay Joshi0206e352011-08-16 15:34:10 -0400641 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800642
Zhao Yakui42158662009-11-20 11:24:18 +0800643 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
644 clock.m1++) {
645 for (clock.m2 = limit->m2.min;
646 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500647 /* m1 is always 0 in Pineview */
648 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800649 break;
650 for (clock.n = limit->n.min;
651 clock.n <= limit->n.max; clock.n++) {
652 for (clock.p1 = limit->p1.min;
653 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 int this_err;
655
Shaohua Li21778322009-02-23 15:19:16 +0800656 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000657 if (!intel_PLL_is_valid(dev, limit,
658 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800660 if (match_clock &&
661 clock.p != match_clock->p)
662 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800663
664 this_err = abs(clock.dot - target);
665 if (this_err < err) {
666 *best_clock = clock;
667 err = this_err;
668 }
669 }
670 }
671 }
672 }
673
674 return (err != target);
675}
676
Ma Lingd4906092009-03-18 20:13:27 +0800677static bool
678intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800679 int target, int refclk, intel_clock_t *match_clock,
680 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800681{
682 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800683 intel_clock_t clock;
684 int max_n;
685 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400686 /* approximately equals target * 0.00585 */
687 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800688 found = false;
689
690 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800691 int lvds_reg;
692
Eric Anholtc619eed2010-01-28 16:45:52 -0800693 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800694 lvds_reg = PCH_LVDS;
695 else
696 lvds_reg = LVDS;
Daniel Vetter1974cad2012-11-26 17:22:09 +0100697 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800698 clock.p2 = limit->p2.p2_fast;
699 else
700 clock.p2 = limit->p2.p2_slow;
701 } else {
702 if (target < limit->p2.dot_limit)
703 clock.p2 = limit->p2.p2_slow;
704 else
705 clock.p2 = limit->p2.p2_fast;
706 }
707
708 memset(best_clock, 0, sizeof(*best_clock));
709 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200710 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800711 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200712 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800713 for (clock.m1 = limit->m1.max;
714 clock.m1 >= limit->m1.min; clock.m1--) {
715 for (clock.m2 = limit->m2.max;
716 clock.m2 >= limit->m2.min; clock.m2--) {
717 for (clock.p1 = limit->p1.max;
718 clock.p1 >= limit->p1.min; clock.p1--) {
719 int this_err;
720
Shaohua Li21778322009-02-23 15:19:16 +0800721 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000722 if (!intel_PLL_is_valid(dev, limit,
723 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800724 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800725 if (match_clock &&
726 clock.p != match_clock->p)
727 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000728
729 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800730 if (this_err < err_most) {
731 *best_clock = clock;
732 err_most = this_err;
733 max_n = clock.n;
734 found = true;
735 }
736 }
737 }
738 }
739 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800740 return found;
741}
Ma Lingd4906092009-03-18 20:13:27 +0800742
Zhenyu Wang2c072452009-06-05 15:38:42 +0800743static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500744intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800745 int target, int refclk, intel_clock_t *match_clock,
746 intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800747{
748 struct drm_device *dev = crtc->dev;
749 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800750
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800751 if (target < 200000) {
752 clock.n = 1;
753 clock.p1 = 2;
754 clock.p2 = 10;
755 clock.m1 = 12;
756 clock.m2 = 9;
757 } else {
758 clock.n = 2;
759 clock.p1 = 1;
760 clock.p2 = 10;
761 clock.m1 = 14;
762 clock.m2 = 8;
763 }
764 intel_clock(dev, refclk, &clock);
765 memcpy(best_clock, &clock, sizeof(intel_clock_t));
766 return true;
767}
768
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700769/* DisplayPort has only two frequencies, 162MHz and 270MHz */
770static bool
771intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800772 int target, int refclk, intel_clock_t *match_clock,
773 intel_clock_t *best_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700774{
Chris Wilson5eddb702010-09-11 13:48:45 +0100775 intel_clock_t clock;
776 if (target < 200000) {
777 clock.p1 = 2;
778 clock.p2 = 10;
779 clock.n = 2;
780 clock.m1 = 23;
781 clock.m2 = 8;
782 } else {
783 clock.p1 = 1;
784 clock.p2 = 10;
785 clock.n = 1;
786 clock.m1 = 14;
787 clock.m2 = 2;
788 }
789 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
790 clock.p = (clock.p1 * clock.p2);
791 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
792 clock.vco = 0;
793 memcpy(best_clock, &clock, sizeof(intel_clock_t));
794 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700795}
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700796static bool
797intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
798 int target, int refclk, intel_clock_t *match_clock,
799 intel_clock_t *best_clock)
800{
801 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
802 u32 m, n, fastclk;
803 u32 updrate, minupdate, fracbits, p;
804 unsigned long bestppm, ppm, absppm;
805 int dotclk, flag;
806
Alan Coxaf447bd2012-07-25 13:49:18 +0100807 flag = 0;
Jesse Barnesa0c4da22012-06-15 11:55:13 -0700808 dotclk = target * 1000;
809 bestppm = 1000000;
810 ppm = absppm = 0;
811 fastclk = dotclk / (2*100);
812 updrate = 0;
813 minupdate = 19200;
814 fracbits = 1;
815 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
816 bestm1 = bestm2 = bestp1 = bestp2 = 0;
817
818 /* based on hardware requirement, prefer smaller n to precision */
819 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
820 updrate = refclk / n;
821 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
822 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
823 if (p2 > 10)
824 p2 = p2 - 1;
825 p = p1 * p2;
826 /* based on hardware requirement, prefer bigger m1,m2 values */
827 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
828 m2 = (((2*(fastclk * p * n / m1 )) +
829 refclk) / (2*refclk));
830 m = m1 * m2;
831 vco = updrate * m;
832 if (vco >= limit->vco.min && vco < limit->vco.max) {
833 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
834 absppm = (ppm > 0) ? ppm : (-ppm);
835 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
836 bestppm = 0;
837 flag = 1;
838 }
839 if (absppm < bestppm - 10) {
840 bestppm = absppm;
841 flag = 1;
842 }
843 if (flag) {
844 bestn = n;
845 bestm1 = m1;
846 bestm2 = m2;
847 bestp1 = p1;
848 bestp2 = p2;
849 flag = 0;
850 }
851 }
852 }
853 }
854 }
855 }
856 best_clock->n = bestn;
857 best_clock->m1 = bestm1;
858 best_clock->m2 = bestm2;
859 best_clock->p1 = bestp1;
860 best_clock->p2 = bestp2;
861
862 return true;
863}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700864
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200865enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
866 enum pipe pipe)
867{
868 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
870
Daniel Vetter3b117c82013-04-17 20:15:07 +0200871 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200872}
873
Paulo Zanonia928d532012-05-04 17:18:15 -0300874static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
875{
876 struct drm_i915_private *dev_priv = dev->dev_private;
877 u32 frame, frame_reg = PIPEFRAME(pipe);
878
879 frame = I915_READ(frame_reg);
880
881 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
882 DRM_DEBUG_KMS("vblank wait timed out\n");
883}
884
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700885/**
886 * intel_wait_for_vblank - wait for vblank on a given pipe
887 * @dev: drm device
888 * @pipe: pipe to wait for
889 *
890 * Wait for vblank to occur on a given pipe. Needed for various bits of
891 * mode setting code.
892 */
893void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800894{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700895 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800896 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700897
Paulo Zanonia928d532012-05-04 17:18:15 -0300898 if (INTEL_INFO(dev)->gen >= 5) {
899 ironlake_wait_for_vblank(dev, pipe);
900 return;
901 }
902
Chris Wilson300387c2010-09-05 20:25:43 +0100903 /* Clear existing vblank status. Note this will clear any other
904 * sticky status fields as well.
905 *
906 * This races with i915_driver_irq_handler() with the result
907 * that either function could miss a vblank event. Here it is not
908 * fatal, as we will either wait upon the next vblank interrupt or
909 * timeout. Generally speaking intel_wait_for_vblank() is only
910 * called during modeset at which time the GPU should be idle and
911 * should *not* be performing page flips and thus not waiting on
912 * vblanks...
913 * Currently, the result of us stealing a vblank from the irq
914 * handler is that a single frame will be skipped during swapbuffers.
915 */
916 I915_WRITE(pipestat_reg,
917 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
918
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700919 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100920 if (wait_for(I915_READ(pipestat_reg) &
921 PIPE_VBLANK_INTERRUPT_STATUS,
922 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700923 DRM_DEBUG_KMS("vblank wait timed out\n");
924}
925
Keith Packardab7ad7f2010-10-03 00:33:06 -0700926/*
927 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700928 * @dev: drm device
929 * @pipe: pipe to wait for
930 *
931 * After disabling a pipe, we can't wait for vblank in the usual way,
932 * spinning on the vblank interrupt status bit, since we won't actually
933 * see an interrupt when the pipe is disabled.
934 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700935 * On Gen4 and above:
936 * wait for the pipe register state bit to turn off
937 *
938 * Otherwise:
939 * wait for the display line value to settle (it usually
940 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100941 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700942 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100943void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700944{
945 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200946 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
947 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700948
Keith Packardab7ad7f2010-10-03 00:33:06 -0700949 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200950 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700951
Keith Packardab7ad7f2010-10-03 00:33:06 -0700952 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100953 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
954 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200955 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700956 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300957 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100958 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700959 unsigned long timeout = jiffies + msecs_to_jiffies(100);
960
Paulo Zanoni837ba002012-05-04 17:18:14 -0300961 if (IS_GEN2(dev))
962 line_mask = DSL_LINEMASK_GEN2;
963 else
964 line_mask = DSL_LINEMASK_GEN3;
965
Keith Packardab7ad7f2010-10-03 00:33:06 -0700966 /* Wait for the display line to settle */
967 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300968 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700969 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300970 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700971 time_after(timeout, jiffies));
972 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200973 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700974 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800975}
976
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000977/*
978 * ibx_digital_port_connected - is the specified port connected?
979 * @dev_priv: i915 private structure
980 * @port: the port to test
981 *
982 * Returns true if @port is connected, false otherwise.
983 */
984bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
985 struct intel_digital_port *port)
986{
987 u32 bit;
988
Damien Lespiauc36346e2012-12-13 16:09:03 +0000989 if (HAS_PCH_IBX(dev_priv->dev)) {
990 switch(port->port) {
991 case PORT_B:
992 bit = SDE_PORTB_HOTPLUG;
993 break;
994 case PORT_C:
995 bit = SDE_PORTC_HOTPLUG;
996 break;
997 case PORT_D:
998 bit = SDE_PORTD_HOTPLUG;
999 break;
1000 default:
1001 return true;
1002 }
1003 } else {
1004 switch(port->port) {
1005 case PORT_B:
1006 bit = SDE_PORTB_HOTPLUG_CPT;
1007 break;
1008 case PORT_C:
1009 bit = SDE_PORTC_HOTPLUG_CPT;
1010 break;
1011 case PORT_D:
1012 bit = SDE_PORTD_HOTPLUG_CPT;
1013 break;
1014 default:
1015 return true;
1016 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001017 }
1018
1019 return I915_READ(SDEISR) & bit;
1020}
1021
Jesse Barnesb24e7172011-01-04 15:09:30 -08001022static const char *state_string(bool enabled)
1023{
1024 return enabled ? "on" : "off";
1025}
1026
1027/* Only for pre-ILK configs */
1028static void assert_pll(struct drm_i915_private *dev_priv,
1029 enum pipe pipe, bool state)
1030{
1031 int reg;
1032 u32 val;
1033 bool cur_state;
1034
1035 reg = DPLL(pipe);
1036 val = I915_READ(reg);
1037 cur_state = !!(val & DPLL_VCO_ENABLE);
1038 WARN(cur_state != state,
1039 "PLL state assertion failure (expected %s, current %s)\n",
1040 state_string(state), state_string(cur_state));
1041}
1042#define assert_pll_enabled(d, p) assert_pll(d, p, true)
1043#define assert_pll_disabled(d, p) assert_pll(d, p, false)
1044
Jesse Barnes040484a2011-01-03 12:14:26 -08001045/* For ILK+ */
1046static void assert_pch_pll(struct drm_i915_private *dev_priv,
Chris Wilson92b27b02012-05-20 18:10:50 +01001047 struct intel_pch_pll *pll,
1048 struct intel_crtc *crtc,
1049 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001050{
Jesse Barnes040484a2011-01-03 12:14:26 -08001051 u32 val;
1052 bool cur_state;
1053
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001054 if (HAS_PCH_LPT(dev_priv->dev)) {
1055 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1056 return;
1057 }
1058
Chris Wilson92b27b02012-05-20 18:10:50 +01001059 if (WARN (!pll,
1060 "asserting PCH PLL %s with no PLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001061 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001062
Chris Wilson92b27b02012-05-20 18:10:50 +01001063 val = I915_READ(pll->pll_reg);
1064 cur_state = !!(val & DPLL_VCO_ENABLE);
1065 WARN(cur_state != state,
1066 "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
1067 pll->pll_reg, state_string(state), state_string(cur_state), val);
1068
1069 /* Make sure the selected PLL is correctly attached to the transcoder */
1070 if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001071 u32 pch_dpll;
1072
1073 pch_dpll = I915_READ(PCH_DPLL_SEL);
Chris Wilson92b27b02012-05-20 18:10:50 +01001074 cur_state = pll->pll_reg == _PCH_DPLL_B;
1075 if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001076 "PLL[%d] not attached to this transcoder %c: %08x\n",
1077 cur_state, pipe_name(crtc->pipe), pch_dpll)) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001078 cur_state = !!(val >> (4*crtc->pipe + 3));
1079 WARN(cur_state != state,
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001080 "PLL[%d] not %s on this transcoder %c: %08x\n",
Chris Wilson92b27b02012-05-20 18:10:50 +01001081 pll->pll_reg == _PCH_DPLL_B,
1082 state_string(state),
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001083 pipe_name(crtc->pipe),
Chris Wilson92b27b02012-05-20 18:10:50 +01001084 val);
1085 }
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001086 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001087}
Chris Wilson92b27b02012-05-20 18:10:50 +01001088#define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
1089#define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
Jesse Barnes040484a2011-01-03 12:14:26 -08001090
1091static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1092 enum pipe pipe, bool state)
1093{
1094 int reg;
1095 u32 val;
1096 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001097 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1098 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001099
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001100 if (HAS_DDI(dev_priv->dev)) {
1101 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001102 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001103 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001104 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001105 } else {
1106 reg = FDI_TX_CTL(pipe);
1107 val = I915_READ(reg);
1108 cur_state = !!(val & FDI_TX_ENABLE);
1109 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001110 WARN(cur_state != state,
1111 "FDI TX state assertion failure (expected %s, current %s)\n",
1112 state_string(state), state_string(cur_state));
1113}
1114#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1115#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1116
1117static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1118 enum pipe pipe, bool state)
1119{
1120 int reg;
1121 u32 val;
1122 bool cur_state;
1123
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001124 reg = FDI_RX_CTL(pipe);
1125 val = I915_READ(reg);
1126 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001127 WARN(cur_state != state,
1128 "FDI RX state assertion failure (expected %s, current %s)\n",
1129 state_string(state), state_string(cur_state));
1130}
1131#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1132#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1133
1134static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1135 enum pipe pipe)
1136{
1137 int reg;
1138 u32 val;
1139
1140 /* ILK FDI PLL is always enabled */
1141 if (dev_priv->info->gen == 5)
1142 return;
1143
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001144 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001145 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001146 return;
1147
Jesse Barnes040484a2011-01-03 12:14:26 -08001148 reg = FDI_TX_CTL(pipe);
1149 val = I915_READ(reg);
1150 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1151}
1152
1153static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1154 enum pipe pipe)
1155{
1156 int reg;
1157 u32 val;
1158
1159 reg = FDI_RX_CTL(pipe);
1160 val = I915_READ(reg);
1161 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1162}
1163
Jesse Barnesea0760c2011-01-04 15:09:32 -08001164static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1165 enum pipe pipe)
1166{
1167 int pp_reg, lvds_reg;
1168 u32 val;
1169 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001170 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001171
1172 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1173 pp_reg = PCH_PP_CONTROL;
1174 lvds_reg = PCH_LVDS;
1175 } else {
1176 pp_reg = PP_CONTROL;
1177 lvds_reg = LVDS;
1178 }
1179
1180 val = I915_READ(pp_reg);
1181 if (!(val & PANEL_POWER_ON) ||
1182 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1183 locked = false;
1184
1185 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1186 panel_pipe = PIPE_B;
1187
1188 WARN(panel_pipe == pipe && locked,
1189 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001190 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001191}
1192
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001193void assert_pipe(struct drm_i915_private *dev_priv,
1194 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001195{
1196 int reg;
1197 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001198 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001199 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1200 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001201
Daniel Vetter8e636782012-01-22 01:36:48 +01001202 /* if we need the pipe A quirk it must be always on */
1203 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1204 state = true;
1205
Paulo Zanoni15d199e2013-03-22 14:14:13 -03001206 if (!intel_using_power_well(dev_priv->dev) &&
1207 cpu_transcoder != TRANSCODER_EDP) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001208 cur_state = false;
1209 } else {
1210 reg = PIPECONF(cpu_transcoder);
1211 val = I915_READ(reg);
1212 cur_state = !!(val & PIPECONF_ENABLE);
1213 }
1214
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001215 WARN(cur_state != state,
1216 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001217 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001218}
1219
Chris Wilson931872f2012-01-16 23:01:13 +00001220static void assert_plane(struct drm_i915_private *dev_priv,
1221 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001222{
1223 int reg;
1224 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001225 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001226
1227 reg = DSPCNTR(plane);
1228 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001229 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1230 WARN(cur_state != state,
1231 "plane %c assertion failure (expected %s, current %s)\n",
1232 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001233}
1234
Chris Wilson931872f2012-01-16 23:01:13 +00001235#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1236#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1237
Jesse Barnesb24e7172011-01-04 15:09:30 -08001238static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1239 enum pipe pipe)
1240{
1241 int reg, i;
1242 u32 val;
1243 int cur_pipe;
1244
Jesse Barnes19ec1352011-02-02 12:28:02 -08001245 /* Planes are fixed to pipes on ILK+ */
Jesse Barnesda6ecc52013-03-08 10:46:00 -08001246 if (HAS_PCH_SPLIT(dev_priv->dev) || IS_VALLEYVIEW(dev_priv->dev)) {
Adam Jackson28c057942011-10-07 14:38:42 -04001247 reg = DSPCNTR(pipe);
1248 val = I915_READ(reg);
1249 WARN((val & DISPLAY_PLANE_ENABLE),
1250 "plane %c assertion failure, should be disabled but not\n",
1251 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001252 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001253 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001254
Jesse Barnesb24e7172011-01-04 15:09:30 -08001255 /* Need to check both planes against the pipe */
1256 for (i = 0; i < 2; i++) {
1257 reg = DSPCNTR(i);
1258 val = I915_READ(reg);
1259 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1260 DISPPLANE_SEL_PIPE_SHIFT;
1261 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001262 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1263 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001264 }
1265}
1266
Jesse Barnes19332d72013-03-28 09:55:38 -07001267static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1268 enum pipe pipe)
1269{
1270 int reg, i;
1271 u32 val;
1272
1273 if (!IS_VALLEYVIEW(dev_priv->dev))
1274 return;
1275
1276 /* Need to check both planes against the pipe */
1277 for (i = 0; i < dev_priv->num_plane; i++) {
1278 reg = SPCNTR(pipe, i);
1279 val = I915_READ(reg);
1280 WARN((val & SP_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001281 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1282 sprite_name(pipe, i), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001283 }
1284}
1285
Jesse Barnes92f25842011-01-04 15:09:34 -08001286static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1287{
1288 u32 val;
1289 bool enabled;
1290
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001291 if (HAS_PCH_LPT(dev_priv->dev)) {
1292 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1293 return;
1294 }
1295
Jesse Barnes92f25842011-01-04 15:09:34 -08001296 val = I915_READ(PCH_DREF_CONTROL);
1297 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1298 DREF_SUPERSPREAD_SOURCE_MASK));
1299 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1300}
1301
1302static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1303 enum pipe pipe)
1304{
1305 int reg;
1306 u32 val;
1307 bool enabled;
1308
1309 reg = TRANSCONF(pipe);
1310 val = I915_READ(reg);
1311 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001312 WARN(enabled,
1313 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1314 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001315}
1316
Keith Packard4e634382011-08-06 10:39:45 -07001317static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1318 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001319{
1320 if ((val & DP_PORT_EN) == 0)
1321 return false;
1322
1323 if (HAS_PCH_CPT(dev_priv->dev)) {
1324 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1325 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1326 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1327 return false;
1328 } else {
1329 if ((val & DP_PIPE_MASK) != (pipe << 30))
1330 return false;
1331 }
1332 return true;
1333}
1334
Keith Packard1519b992011-08-06 10:35:34 -07001335static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1336 enum pipe pipe, u32 val)
1337{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001338 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001339 return false;
1340
1341 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001342 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001343 return false;
1344 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001345 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001346 return false;
1347 }
1348 return true;
1349}
1350
1351static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1352 enum pipe pipe, u32 val)
1353{
1354 if ((val & LVDS_PORT_EN) == 0)
1355 return false;
1356
1357 if (HAS_PCH_CPT(dev_priv->dev)) {
1358 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1359 return false;
1360 } else {
1361 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1362 return false;
1363 }
1364 return true;
1365}
1366
1367static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1368 enum pipe pipe, u32 val)
1369{
1370 if ((val & ADPA_DAC_ENABLE) == 0)
1371 return false;
1372 if (HAS_PCH_CPT(dev_priv->dev)) {
1373 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1374 return false;
1375 } else {
1376 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1377 return false;
1378 }
1379 return true;
1380}
1381
Jesse Barnes291906f2011-02-02 12:28:03 -08001382static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001383 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001384{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001385 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001386 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001387 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001388 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001389
Daniel Vetter75c5da22012-09-10 21:58:29 +02001390 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1391 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001392 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001393}
1394
1395static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1396 enum pipe pipe, int reg)
1397{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001398 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001399 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001400 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001401 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001402
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001403 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001404 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001405 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001406}
1407
1408static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1409 enum pipe pipe)
1410{
1411 int reg;
1412 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001413
Keith Packardf0575e92011-07-25 22:12:43 -07001414 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1415 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1416 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001417
1418 reg = PCH_ADPA;
1419 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001420 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001421 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001422 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001423
1424 reg = PCH_LVDS;
1425 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001426 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001427 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001428 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001429
Paulo Zanonie2debe92013-02-18 19:00:27 -03001430 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1431 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1432 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001433}
1434
Jesse Barnesb24e7172011-01-04 15:09:30 -08001435/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001436 * intel_enable_pll - enable a PLL
1437 * @dev_priv: i915 private structure
1438 * @pipe: pipe PLL to enable
1439 *
1440 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1441 * make sure the PLL reg is writable first though, since the panel write
1442 * protect mechanism may be enabled.
1443 *
1444 * Note! This is for pre-ILK only.
Thomas Richter7434a252012-07-18 19:22:30 +02001445 *
1446 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001447 */
1448static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1449{
1450 int reg;
1451 u32 val;
1452
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001453 assert_pipe_disabled(dev_priv, pipe);
1454
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001455 /* No really, not for ILK+ */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07001456 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001457
1458 /* PLL is protected by panel, make sure we can write it */
1459 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1460 assert_panel_unlocked(dev_priv, pipe);
1461
1462 reg = DPLL(pipe);
1463 val = I915_READ(reg);
1464 val |= DPLL_VCO_ENABLE;
1465
1466 /* We do this three times for luck */
1467 I915_WRITE(reg, val);
1468 POSTING_READ(reg);
1469 udelay(150); /* wait for warmup */
1470 I915_WRITE(reg, val);
1471 POSTING_READ(reg);
1472 udelay(150); /* wait for warmup */
1473 I915_WRITE(reg, val);
1474 POSTING_READ(reg);
1475 udelay(150); /* wait for warmup */
1476}
1477
1478/**
1479 * intel_disable_pll - disable a PLL
1480 * @dev_priv: i915 private structure
1481 * @pipe: pipe PLL to disable
1482 *
1483 * Disable the PLL for @pipe, making sure the pipe is off first.
1484 *
1485 * Note! This is for pre-ILK only.
1486 */
1487static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1488{
1489 int reg;
1490 u32 val;
1491
1492 /* Don't disable pipe A or pipe A PLLs if needed */
1493 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1494 return;
1495
1496 /* Make sure the pipe isn't still relying on us */
1497 assert_pipe_disabled(dev_priv, pipe);
1498
1499 reg = DPLL(pipe);
1500 val = I915_READ(reg);
1501 val &= ~DPLL_VCO_ENABLE;
1502 I915_WRITE(reg, val);
1503 POSTING_READ(reg);
1504}
1505
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001506/* SBI access */
1507static void
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001508intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value,
1509 enum intel_sbi_destination destination)
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001510{
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001511 u32 tmp;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001512
Daniel Vetter09153002012-12-12 14:06:44 +01001513 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001514
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001515 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001516 100)) {
1517 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001518 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001519 }
1520
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001521 I915_WRITE(SBI_ADDR, (reg << 16));
1522 I915_WRITE(SBI_DATA, value);
1523
1524 if (destination == SBI_ICLK)
1525 tmp = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRWR;
1526 else
1527 tmp = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IOWR;
1528 I915_WRITE(SBI_CTL_STAT, SBI_BUSY | tmp);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001529
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001530 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001531 100)) {
1532 DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001533 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001534 }
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001535}
1536
1537static u32
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001538intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg,
1539 enum intel_sbi_destination destination)
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001540{
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001541 u32 value = 0;
Daniel Vetter09153002012-12-12 14:06:44 +01001542 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001543
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001544 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001545 100)) {
1546 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001547 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001548 }
1549
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001550 I915_WRITE(SBI_ADDR, (reg << 16));
1551
1552 if (destination == SBI_ICLK)
1553 value = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRRD;
1554 else
1555 value = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IORD;
1556 I915_WRITE(SBI_CTL_STAT, value | SBI_BUSY);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001557
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001558 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001559 100)) {
1560 DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001561 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001562 }
1563
Daniel Vetter09153002012-12-12 14:06:44 +01001564 return I915_READ(SBI_DATA);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001565}
1566
Jesse Barnes89b667f2013-04-18 14:51:36 -07001567void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1568{
1569 u32 port_mask;
1570
1571 if (!port)
1572 port_mask = DPLL_PORTB_READY_MASK;
1573 else
1574 port_mask = DPLL_PORTC_READY_MASK;
1575
1576 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1577 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1578 'B' + port, I915_READ(DPLL(0)));
1579}
1580
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001581/**
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001582 * ironlake_enable_pch_pll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001583 * @dev_priv: i915 private structure
1584 * @pipe: pipe PLL to enable
1585 *
1586 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1587 * drives the transcoder clock.
1588 */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001589static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001590{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001591 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Chris Wilson48da64a2012-05-13 20:16:12 +01001592 struct intel_pch_pll *pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001593 int reg;
1594 u32 val;
1595
Chris Wilson48da64a2012-05-13 20:16:12 +01001596 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001597 BUG_ON(dev_priv->info->gen < 5);
Chris Wilson48da64a2012-05-13 20:16:12 +01001598 pll = intel_crtc->pch_pll;
1599 if (pll == NULL)
1600 return;
1601
1602 if (WARN_ON(pll->refcount == 0))
1603 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001604
1605 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1606 pll->pll_reg, pll->active, pll->on,
1607 intel_crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001608
1609 /* PCH refclock must be enabled first */
1610 assert_pch_refclk_enabled(dev_priv);
1611
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001612 if (pll->active++ && pll->on) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001613 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001614 return;
1615 }
1616
1617 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1618
1619 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001620 val = I915_READ(reg);
1621 val |= DPLL_VCO_ENABLE;
1622 I915_WRITE(reg, val);
1623 POSTING_READ(reg);
1624 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001625
1626 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001627}
1628
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001629static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001630{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001631 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1632 struct intel_pch_pll *pll = intel_crtc->pch_pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001633 int reg;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001634 u32 val;
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001635
Jesse Barnes92f25842011-01-04 15:09:34 -08001636 /* PCH only available on ILK+ */
1637 BUG_ON(dev_priv->info->gen < 5);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001638 if (pll == NULL)
1639 return;
1640
Chris Wilson48da64a2012-05-13 20:16:12 +01001641 if (WARN_ON(pll->refcount == 0))
1642 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001643
1644 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1645 pll->pll_reg, pll->active, pll->on,
1646 intel_crtc->base.base.id);
1647
Chris Wilson48da64a2012-05-13 20:16:12 +01001648 if (WARN_ON(pll->active == 0)) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001649 assert_pch_pll_disabled(dev_priv, pll, NULL);
Chris Wilson48da64a2012-05-13 20:16:12 +01001650 return;
1651 }
1652
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001653 if (--pll->active) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001654 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001655 return;
1656 }
1657
1658 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
Jesse Barnes92f25842011-01-04 15:09:34 -08001659
1660 /* Make sure transcoder isn't still depending on us */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001661 assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001662
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001663 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001664 val = I915_READ(reg);
1665 val &= ~DPLL_VCO_ENABLE;
1666 I915_WRITE(reg, val);
1667 POSTING_READ(reg);
1668 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001669
1670 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001671}
1672
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001673static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1674 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001675{
Daniel Vetter23670b322012-11-01 09:15:30 +01001676 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001677 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetter23670b322012-11-01 09:15:30 +01001678 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001679
1680 /* PCH only available on ILK+ */
1681 BUG_ON(dev_priv->info->gen < 5);
1682
1683 /* Make sure PCH DPLL is enabled */
Chris Wilson92b27b02012-05-20 18:10:50 +01001684 assert_pch_pll_enabled(dev_priv,
1685 to_intel_crtc(crtc)->pch_pll,
1686 to_intel_crtc(crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001687
1688 /* FDI must be feeding us bits for PCH ports */
1689 assert_fdi_tx_enabled(dev_priv, pipe);
1690 assert_fdi_rx_enabled(dev_priv, pipe);
1691
Daniel Vetter23670b322012-11-01 09:15:30 +01001692 if (HAS_PCH_CPT(dev)) {
1693 /* Workaround: Set the timing override bit before enabling the
1694 * pch transcoder. */
1695 reg = TRANS_CHICKEN2(pipe);
1696 val = I915_READ(reg);
1697 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1698 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001699 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001700
Jesse Barnes040484a2011-01-03 12:14:26 -08001701 reg = TRANSCONF(pipe);
1702 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001703 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001704
1705 if (HAS_PCH_IBX(dev_priv->dev)) {
1706 /*
1707 * make the BPC in transcoder be consistent with
1708 * that in pipeconf reg.
1709 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001710 val &= ~PIPECONF_BPC_MASK;
1711 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001712 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001713
1714 val &= ~TRANS_INTERLACE_MASK;
1715 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001716 if (HAS_PCH_IBX(dev_priv->dev) &&
1717 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1718 val |= TRANS_LEGACY_INTERLACED_ILK;
1719 else
1720 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001721 else
1722 val |= TRANS_PROGRESSIVE;
1723
Jesse Barnes040484a2011-01-03 12:14:26 -08001724 I915_WRITE(reg, val | TRANS_ENABLE);
1725 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001726 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001727}
1728
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001729static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001730 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001731{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001732 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001733
1734 /* PCH only available on ILK+ */
1735 BUG_ON(dev_priv->info->gen < 5);
1736
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001737 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001738 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001739 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001740
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001741 /* Workaround: set timing override bit. */
1742 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001743 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001744 I915_WRITE(_TRANSA_CHICKEN2, val);
1745
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001746 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001747 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001748
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001749 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1750 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001751 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001752 else
1753 val |= TRANS_PROGRESSIVE;
1754
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001755 I915_WRITE(TRANSCONF(TRANSCODER_A), val);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001756 if (wait_for(I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE, 100))
1757 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001758}
1759
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001760static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1761 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001762{
Daniel Vetter23670b322012-11-01 09:15:30 +01001763 struct drm_device *dev = dev_priv->dev;
1764 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001765
1766 /* FDI relies on the transcoder */
1767 assert_fdi_tx_disabled(dev_priv, pipe);
1768 assert_fdi_rx_disabled(dev_priv, pipe);
1769
Jesse Barnes291906f2011-02-02 12:28:03 -08001770 /* Ports must be off as well */
1771 assert_pch_ports_disabled(dev_priv, pipe);
1772
Jesse Barnes040484a2011-01-03 12:14:26 -08001773 reg = TRANSCONF(pipe);
1774 val = I915_READ(reg);
1775 val &= ~TRANS_ENABLE;
1776 I915_WRITE(reg, val);
1777 /* wait for PCH transcoder off, transcoder state */
1778 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001779 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001780
1781 if (!HAS_PCH_IBX(dev)) {
1782 /* Workaround: Clear the timing override chicken bit again. */
1783 reg = TRANS_CHICKEN2(pipe);
1784 val = I915_READ(reg);
1785 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1786 I915_WRITE(reg, val);
1787 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001788}
1789
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001790static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001791{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001792 u32 val;
1793
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001794 val = I915_READ(_TRANSACONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001795 val &= ~TRANS_ENABLE;
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001796 I915_WRITE(_TRANSACONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001797 /* wait for PCH transcoder off, transcoder state */
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001798 if (wait_for((I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE) == 0, 50))
1799 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001800
1801 /* Workaround: clear timing override bit. */
1802 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001803 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001804 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001805}
1806
1807/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001808 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001809 * @dev_priv: i915 private structure
1810 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001811 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001812 *
1813 * Enable @pipe, making sure that various hardware specific requirements
1814 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1815 *
1816 * @pipe should be %PIPE_A or %PIPE_B.
1817 *
1818 * Will wait until the pipe is actually running (i.e. first vblank) before
1819 * returning.
1820 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001821static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1822 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001823{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001824 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1825 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001826 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001827 int reg;
1828 u32 val;
1829
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001830 assert_planes_disabled(dev_priv, pipe);
1831 assert_sprites_disabled(dev_priv, pipe);
1832
Paulo Zanoni681e5812012-12-06 11:12:38 -02001833 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001834 pch_transcoder = TRANSCODER_A;
1835 else
1836 pch_transcoder = pipe;
1837
Jesse Barnesb24e7172011-01-04 15:09:30 -08001838 /*
1839 * A pipe without a PLL won't actually be able to drive bits from
1840 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1841 * need the check.
1842 */
1843 if (!HAS_PCH_SPLIT(dev_priv->dev))
1844 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001845 else {
1846 if (pch_port) {
1847 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001848 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001849 assert_fdi_tx_pll_enabled(dev_priv,
1850 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001851 }
1852 /* FIXME: assert CPU port conditions for SNB+ */
1853 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001854
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001855 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001856 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001857 if (val & PIPECONF_ENABLE)
1858 return;
1859
1860 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001861 intel_wait_for_vblank(dev_priv->dev, pipe);
1862}
1863
1864/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001865 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001866 * @dev_priv: i915 private structure
1867 * @pipe: pipe to disable
1868 *
1869 * Disable @pipe, making sure that various hardware specific requirements
1870 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1871 *
1872 * @pipe should be %PIPE_A or %PIPE_B.
1873 *
1874 * Will wait until the pipe has shut down before returning.
1875 */
1876static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1877 enum pipe pipe)
1878{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001879 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1880 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001881 int reg;
1882 u32 val;
1883
1884 /*
1885 * Make sure planes won't keep trying to pump pixels to us,
1886 * or we might hang the display.
1887 */
1888 assert_planes_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001889 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001890
1891 /* Don't disable pipe A or pipe A PLLs if needed */
1892 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1893 return;
1894
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001895 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001896 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001897 if ((val & PIPECONF_ENABLE) == 0)
1898 return;
1899
1900 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001901 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1902}
1903
Keith Packardd74362c2011-07-28 14:47:14 -07001904/*
1905 * Plane regs are double buffered, going from enabled->disabled needs a
1906 * trigger in order to latch. The display address reg provides this.
1907 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001908void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001909 enum plane plane)
1910{
Damien Lespiau14f86142012-10-29 15:24:49 +00001911 if (dev_priv->info->gen >= 4)
1912 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1913 else
1914 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001915}
1916
Jesse Barnesb24e7172011-01-04 15:09:30 -08001917/**
1918 * intel_enable_plane - enable a display plane on a given pipe
1919 * @dev_priv: i915 private structure
1920 * @plane: plane to enable
1921 * @pipe: pipe being fed
1922 *
1923 * Enable @plane on @pipe, making sure that @pipe is running first.
1924 */
1925static void intel_enable_plane(struct drm_i915_private *dev_priv,
1926 enum plane plane, enum pipe pipe)
1927{
1928 int reg;
1929 u32 val;
1930
1931 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1932 assert_pipe_enabled(dev_priv, pipe);
1933
1934 reg = DSPCNTR(plane);
1935 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001936 if (val & DISPLAY_PLANE_ENABLE)
1937 return;
1938
1939 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001940 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001941 intel_wait_for_vblank(dev_priv->dev, pipe);
1942}
1943
Jesse Barnesb24e7172011-01-04 15:09:30 -08001944/**
1945 * intel_disable_plane - disable a display plane
1946 * @dev_priv: i915 private structure
1947 * @plane: plane to disable
1948 * @pipe: pipe consuming the data
1949 *
1950 * Disable @plane; should be an independent operation.
1951 */
1952static void intel_disable_plane(struct drm_i915_private *dev_priv,
1953 enum plane plane, enum pipe pipe)
1954{
1955 int reg;
1956 u32 val;
1957
1958 reg = DSPCNTR(plane);
1959 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001960 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1961 return;
1962
1963 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001964 intel_flush_display_plane(dev_priv, plane);
1965 intel_wait_for_vblank(dev_priv->dev, pipe);
1966}
1967
Chris Wilson693db182013-03-05 14:52:39 +00001968static bool need_vtd_wa(struct drm_device *dev)
1969{
1970#ifdef CONFIG_INTEL_IOMMU
1971 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1972 return true;
1973#endif
1974 return false;
1975}
1976
Chris Wilson127bd2a2010-07-23 23:32:05 +01001977int
Chris Wilson48b956c2010-09-14 12:50:34 +01001978intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001979 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001980 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001981{
Chris Wilsonce453d82011-02-21 14:43:56 +00001982 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001983 u32 alignment;
1984 int ret;
1985
Chris Wilson05394f32010-11-08 19:18:58 +00001986 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001987 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001988 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1989 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001990 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001991 alignment = 4 * 1024;
1992 else
1993 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001994 break;
1995 case I915_TILING_X:
1996 /* pin() will align the object as required by fence */
1997 alignment = 0;
1998 break;
1999 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02002000 /* Despite that we check this in framebuffer_init userspace can
2001 * screw us over and change the tiling after the fact. Only
2002 * pinned buffers can't change their tiling. */
2003 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002004 return -EINVAL;
2005 default:
2006 BUG();
2007 }
2008
Chris Wilson693db182013-03-05 14:52:39 +00002009 /* Note that the w/a also requires 64 PTE of padding following the
2010 * bo. We currently fill all unused PTE with the shadow page and so
2011 * we should always have valid PTE following the scanout preventing
2012 * the VT-d warning.
2013 */
2014 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2015 alignment = 256 * 1024;
2016
Chris Wilsonce453d82011-02-21 14:43:56 +00002017 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002018 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002019 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002020 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002021
2022 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2023 * fence, whereas 965+ only requires a fence if using
2024 * framebuffer compression. For simplicity, we always install
2025 * a fence as the cost is not that onerous.
2026 */
Chris Wilson06d98132012-04-17 15:31:24 +01002027 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002028 if (ret)
2029 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002030
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002031 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002032
Chris Wilsonce453d82011-02-21 14:43:56 +00002033 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002034 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002035
2036err_unpin:
2037 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002038err_interruptible:
2039 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01002040 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002041}
2042
Chris Wilson1690e1e2011-12-14 13:57:08 +01002043void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2044{
2045 i915_gem_object_unpin_fence(obj);
2046 i915_gem_object_unpin(obj);
2047}
2048
Daniel Vetterc2c75132012-07-05 12:17:30 +02002049/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2050 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002051unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2052 unsigned int tiling_mode,
2053 unsigned int cpp,
2054 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002055{
Chris Wilsonbc752862013-02-21 20:04:31 +00002056 if (tiling_mode != I915_TILING_NONE) {
2057 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002058
Chris Wilsonbc752862013-02-21 20:04:31 +00002059 tile_rows = *y / 8;
2060 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002061
Chris Wilsonbc752862013-02-21 20:04:31 +00002062 tiles = *x / (512/cpp);
2063 *x %= 512/cpp;
2064
2065 return tile_rows * pitch * 8 + tiles * 4096;
2066 } else {
2067 unsigned int offset;
2068
2069 offset = *y * pitch + *x * cpp;
2070 *y = 0;
2071 *x = (offset & 4095) / cpp;
2072 return offset & -4096;
2073 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002074}
2075
Jesse Barnes17638cd2011-06-24 12:19:23 -07002076static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2077 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002078{
2079 struct drm_device *dev = crtc->dev;
2080 struct drm_i915_private *dev_priv = dev->dev_private;
2081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2082 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002083 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002084 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002085 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002086 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002087 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002088
2089 switch (plane) {
2090 case 0:
2091 case 1:
2092 break;
2093 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002094 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07002095 return -EINVAL;
2096 }
2097
2098 intel_fb = to_intel_framebuffer(fb);
2099 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002100
Chris Wilson5eddb702010-09-11 13:48:45 +01002101 reg = DSPCNTR(plane);
2102 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002103 /* Mask out pixel format bits in case we change it */
2104 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002105 switch (fb->pixel_format) {
2106 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002107 dspcntr |= DISPPLANE_8BPP;
2108 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002109 case DRM_FORMAT_XRGB1555:
2110 case DRM_FORMAT_ARGB1555:
2111 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002112 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002113 case DRM_FORMAT_RGB565:
2114 dspcntr |= DISPPLANE_BGRX565;
2115 break;
2116 case DRM_FORMAT_XRGB8888:
2117 case DRM_FORMAT_ARGB8888:
2118 dspcntr |= DISPPLANE_BGRX888;
2119 break;
2120 case DRM_FORMAT_XBGR8888:
2121 case DRM_FORMAT_ABGR8888:
2122 dspcntr |= DISPPLANE_RGBX888;
2123 break;
2124 case DRM_FORMAT_XRGB2101010:
2125 case DRM_FORMAT_ARGB2101010:
2126 dspcntr |= DISPPLANE_BGRX101010;
2127 break;
2128 case DRM_FORMAT_XBGR2101010:
2129 case DRM_FORMAT_ABGR2101010:
2130 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002131 break;
2132 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002133 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002134 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002135
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002136 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002137 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002138 dspcntr |= DISPPLANE_TILED;
2139 else
2140 dspcntr &= ~DISPPLANE_TILED;
2141 }
2142
Chris Wilson5eddb702010-09-11 13:48:45 +01002143 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002144
Daniel Vettere506a0c2012-07-05 12:17:29 +02002145 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002146
Daniel Vetterc2c75132012-07-05 12:17:30 +02002147 if (INTEL_INFO(dev)->gen >= 4) {
2148 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002149 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2150 fb->bits_per_pixel / 8,
2151 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002152 linear_offset -= intel_crtc->dspaddr_offset;
2153 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002154 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002155 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002156
2157 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2158 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002159 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002160 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002161 I915_MODIFY_DISPBASE(DSPSURF(plane),
2162 obj->gtt_offset + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002163 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002164 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002165 } else
Daniel Vettere506a0c2012-07-05 12:17:29 +02002166 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002167 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002168
Jesse Barnes17638cd2011-06-24 12:19:23 -07002169 return 0;
2170}
2171
2172static int ironlake_update_plane(struct drm_crtc *crtc,
2173 struct drm_framebuffer *fb, int x, int y)
2174{
2175 struct drm_device *dev = crtc->dev;
2176 struct drm_i915_private *dev_priv = dev->dev_private;
2177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2178 struct intel_framebuffer *intel_fb;
2179 struct drm_i915_gem_object *obj;
2180 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002181 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002182 u32 dspcntr;
2183 u32 reg;
2184
2185 switch (plane) {
2186 case 0:
2187 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002188 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002189 break;
2190 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002191 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002192 return -EINVAL;
2193 }
2194
2195 intel_fb = to_intel_framebuffer(fb);
2196 obj = intel_fb->obj;
2197
2198 reg = DSPCNTR(plane);
2199 dspcntr = I915_READ(reg);
2200 /* Mask out pixel format bits in case we change it */
2201 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002202 switch (fb->pixel_format) {
2203 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002204 dspcntr |= DISPPLANE_8BPP;
2205 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002206 case DRM_FORMAT_RGB565:
2207 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002208 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002209 case DRM_FORMAT_XRGB8888:
2210 case DRM_FORMAT_ARGB8888:
2211 dspcntr |= DISPPLANE_BGRX888;
2212 break;
2213 case DRM_FORMAT_XBGR8888:
2214 case DRM_FORMAT_ABGR8888:
2215 dspcntr |= DISPPLANE_RGBX888;
2216 break;
2217 case DRM_FORMAT_XRGB2101010:
2218 case DRM_FORMAT_ARGB2101010:
2219 dspcntr |= DISPPLANE_BGRX101010;
2220 break;
2221 case DRM_FORMAT_XBGR2101010:
2222 case DRM_FORMAT_ABGR2101010:
2223 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002224 break;
2225 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002226 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002227 }
2228
2229 if (obj->tiling_mode != I915_TILING_NONE)
2230 dspcntr |= DISPPLANE_TILED;
2231 else
2232 dspcntr &= ~DISPPLANE_TILED;
2233
2234 /* must disable */
2235 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2236
2237 I915_WRITE(reg, dspcntr);
2238
Daniel Vettere506a0c2012-07-05 12:17:29 +02002239 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002240 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002241 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2242 fb->bits_per_pixel / 8,
2243 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002244 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002245
Daniel Vettere506a0c2012-07-05 12:17:29 +02002246 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2247 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002248 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002249 I915_MODIFY_DISPBASE(DSPSURF(plane),
2250 obj->gtt_offset + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002251 if (IS_HASWELL(dev)) {
2252 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2253 } else {
2254 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2255 I915_WRITE(DSPLINOFF(plane), linear_offset);
2256 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002257 POSTING_READ(reg);
2258
2259 return 0;
2260}
2261
2262/* Assume fb object is pinned & idle & fenced and just update base pointers */
2263static int
2264intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2265 int x, int y, enum mode_set_atomic state)
2266{
2267 struct drm_device *dev = crtc->dev;
2268 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002269
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002270 if (dev_priv->display.disable_fbc)
2271 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002272 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002273
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002274 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002275}
2276
Ville Syrjälä96a02912013-02-18 19:08:49 +02002277void intel_display_handle_reset(struct drm_device *dev)
2278{
2279 struct drm_i915_private *dev_priv = dev->dev_private;
2280 struct drm_crtc *crtc;
2281
2282 /*
2283 * Flips in the rings have been nuked by the reset,
2284 * so complete all pending flips so that user space
2285 * will get its events and not get stuck.
2286 *
2287 * Also update the base address of all primary
2288 * planes to the the last fb to make sure we're
2289 * showing the correct fb after a reset.
2290 *
2291 * Need to make two loops over the crtcs so that we
2292 * don't try to grab a crtc mutex before the
2293 * pending_flip_queue really got woken up.
2294 */
2295
2296 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2298 enum plane plane = intel_crtc->plane;
2299
2300 intel_prepare_page_flip(dev, plane);
2301 intel_finish_page_flip_plane(dev, plane);
2302 }
2303
2304 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2306
2307 mutex_lock(&crtc->mutex);
2308 if (intel_crtc->active)
2309 dev_priv->display.update_plane(crtc, crtc->fb,
2310 crtc->x, crtc->y);
2311 mutex_unlock(&crtc->mutex);
2312 }
2313}
2314
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002315static int
Chris Wilson14667a42012-04-03 17:58:35 +01002316intel_finish_fb(struct drm_framebuffer *old_fb)
2317{
2318 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2319 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2320 bool was_interruptible = dev_priv->mm.interruptible;
2321 int ret;
2322
Chris Wilson14667a42012-04-03 17:58:35 +01002323 /* Big Hammer, we also need to ensure that any pending
2324 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2325 * current scanout is retired before unpinning the old
2326 * framebuffer.
2327 *
2328 * This should only fail upon a hung GPU, in which case we
2329 * can safely continue.
2330 */
2331 dev_priv->mm.interruptible = false;
2332 ret = i915_gem_object_finish_gpu(obj);
2333 dev_priv->mm.interruptible = was_interruptible;
2334
2335 return ret;
2336}
2337
Ville Syrjälä198598d2012-10-31 17:50:24 +02002338static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2339{
2340 struct drm_device *dev = crtc->dev;
2341 struct drm_i915_master_private *master_priv;
2342 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2343
2344 if (!dev->primary->master)
2345 return;
2346
2347 master_priv = dev->primary->master->driver_priv;
2348 if (!master_priv->sarea_priv)
2349 return;
2350
2351 switch (intel_crtc->pipe) {
2352 case 0:
2353 master_priv->sarea_priv->pipeA_x = x;
2354 master_priv->sarea_priv->pipeA_y = y;
2355 break;
2356 case 1:
2357 master_priv->sarea_priv->pipeB_x = x;
2358 master_priv->sarea_priv->pipeB_y = y;
2359 break;
2360 default:
2361 break;
2362 }
2363}
2364
Chris Wilson14667a42012-04-03 17:58:35 +01002365static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002366intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002367 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002368{
2369 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002370 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002371 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002372 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002373 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002374
2375 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002376 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002377 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002378 return 0;
2379 }
2380
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002381 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002382 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2383 plane_name(intel_crtc->plane),
2384 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002385 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002386 }
2387
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002388 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002389 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002390 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002391 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002392 if (ret != 0) {
2393 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002394 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002395 return ret;
2396 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002397
Daniel Vetter94352cf2012-07-05 22:51:56 +02002398 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002399 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002400 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002401 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002402 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002403 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002404 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002405
Daniel Vetter94352cf2012-07-05 22:51:56 +02002406 old_fb = crtc->fb;
2407 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002408 crtc->x = x;
2409 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002410
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002411 if (old_fb) {
2412 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002413 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002414 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002415
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002416 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002417 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002418
Ville Syrjälä198598d2012-10-31 17:50:24 +02002419 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002420
2421 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002422}
2423
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002424static void intel_fdi_normal_train(struct drm_crtc *crtc)
2425{
2426 struct drm_device *dev = crtc->dev;
2427 struct drm_i915_private *dev_priv = dev->dev_private;
2428 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2429 int pipe = intel_crtc->pipe;
2430 u32 reg, temp;
2431
2432 /* enable normal train */
2433 reg = FDI_TX_CTL(pipe);
2434 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002435 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002436 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2437 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002438 } else {
2439 temp &= ~FDI_LINK_TRAIN_NONE;
2440 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002441 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002442 I915_WRITE(reg, temp);
2443
2444 reg = FDI_RX_CTL(pipe);
2445 temp = I915_READ(reg);
2446 if (HAS_PCH_CPT(dev)) {
2447 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2448 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2449 } else {
2450 temp &= ~FDI_LINK_TRAIN_NONE;
2451 temp |= FDI_LINK_TRAIN_NONE;
2452 }
2453 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2454
2455 /* wait one idle pattern time */
2456 POSTING_READ(reg);
2457 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002458
2459 /* IVB wants error correction enabled */
2460 if (IS_IVYBRIDGE(dev))
2461 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2462 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002463}
2464
Daniel Vetter01a415f2012-10-27 15:58:40 +02002465static void ivb_modeset_global_resources(struct drm_device *dev)
2466{
2467 struct drm_i915_private *dev_priv = dev->dev_private;
2468 struct intel_crtc *pipe_B_crtc =
2469 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2470 struct intel_crtc *pipe_C_crtc =
2471 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2472 uint32_t temp;
2473
2474 /* When everything is off disable fdi C so that we could enable fdi B
2475 * with all lanes. XXX: This misses the case where a pipe is not using
2476 * any pch resources and so doesn't need any fdi lanes. */
2477 if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
2478 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2479 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2480
2481 temp = I915_READ(SOUTH_CHICKEN1);
2482 temp &= ~FDI_BC_BIFURCATION_SELECT;
2483 DRM_DEBUG_KMS("disabling fdi C rx\n");
2484 I915_WRITE(SOUTH_CHICKEN1, temp);
2485 }
2486}
2487
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002488/* The FDI link training functions for ILK/Ibexpeak. */
2489static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2490{
2491 struct drm_device *dev = crtc->dev;
2492 struct drm_i915_private *dev_priv = dev->dev_private;
2493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2494 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002495 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002496 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002497
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002498 /* FDI needs bits from pipe & plane first */
2499 assert_pipe_enabled(dev_priv, pipe);
2500 assert_plane_enabled(dev_priv, plane);
2501
Adam Jacksone1a44742010-06-25 15:32:14 -04002502 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2503 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002504 reg = FDI_RX_IMR(pipe);
2505 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002506 temp &= ~FDI_RX_SYMBOL_LOCK;
2507 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002508 I915_WRITE(reg, temp);
2509 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002510 udelay(150);
2511
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002512 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002513 reg = FDI_TX_CTL(pipe);
2514 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002515 temp &= ~(7 << 19);
2516 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002517 temp &= ~FDI_LINK_TRAIN_NONE;
2518 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002519 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002520
Chris Wilson5eddb702010-09-11 13:48:45 +01002521 reg = FDI_RX_CTL(pipe);
2522 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002523 temp &= ~FDI_LINK_TRAIN_NONE;
2524 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002525 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2526
2527 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002528 udelay(150);
2529
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002530 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002531 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2532 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2533 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002534
Chris Wilson5eddb702010-09-11 13:48:45 +01002535 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002536 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002537 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002538 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2539
2540 if ((temp & FDI_RX_BIT_LOCK)) {
2541 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002542 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002543 break;
2544 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002545 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002546 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002547 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002548
2549 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002550 reg = FDI_TX_CTL(pipe);
2551 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002552 temp &= ~FDI_LINK_TRAIN_NONE;
2553 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002554 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002555
Chris Wilson5eddb702010-09-11 13:48:45 +01002556 reg = FDI_RX_CTL(pipe);
2557 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002558 temp &= ~FDI_LINK_TRAIN_NONE;
2559 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002560 I915_WRITE(reg, temp);
2561
2562 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002563 udelay(150);
2564
Chris Wilson5eddb702010-09-11 13:48:45 +01002565 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002566 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002567 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002568 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2569
2570 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002571 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002572 DRM_DEBUG_KMS("FDI train 2 done.\n");
2573 break;
2574 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002575 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002576 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002577 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002578
2579 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002580
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002581}
2582
Akshay Joshi0206e352011-08-16 15:34:10 -04002583static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002584 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2585 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2586 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2587 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2588};
2589
2590/* The FDI link training functions for SNB/Cougarpoint. */
2591static void gen6_fdi_link_train(struct drm_crtc *crtc)
2592{
2593 struct drm_device *dev = crtc->dev;
2594 struct drm_i915_private *dev_priv = dev->dev_private;
2595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2596 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002597 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002598
Adam Jacksone1a44742010-06-25 15:32:14 -04002599 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2600 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002601 reg = FDI_RX_IMR(pipe);
2602 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002603 temp &= ~FDI_RX_SYMBOL_LOCK;
2604 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002605 I915_WRITE(reg, temp);
2606
2607 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002608 udelay(150);
2609
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002610 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002611 reg = FDI_TX_CTL(pipe);
2612 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002613 temp &= ~(7 << 19);
2614 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002615 temp &= ~FDI_LINK_TRAIN_NONE;
2616 temp |= FDI_LINK_TRAIN_PATTERN_1;
2617 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2618 /* SNB-B */
2619 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002620 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002621
Daniel Vetterd74cf322012-10-26 10:58:13 +02002622 I915_WRITE(FDI_RX_MISC(pipe),
2623 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2624
Chris Wilson5eddb702010-09-11 13:48:45 +01002625 reg = FDI_RX_CTL(pipe);
2626 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002627 if (HAS_PCH_CPT(dev)) {
2628 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2629 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2630 } else {
2631 temp &= ~FDI_LINK_TRAIN_NONE;
2632 temp |= FDI_LINK_TRAIN_PATTERN_1;
2633 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002634 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2635
2636 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002637 udelay(150);
2638
Akshay Joshi0206e352011-08-16 15:34:10 -04002639 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002640 reg = FDI_TX_CTL(pipe);
2641 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002642 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2643 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002644 I915_WRITE(reg, temp);
2645
2646 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002647 udelay(500);
2648
Sean Paulfa37d392012-03-02 12:53:39 -05002649 for (retry = 0; retry < 5; retry++) {
2650 reg = FDI_RX_IIR(pipe);
2651 temp = I915_READ(reg);
2652 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2653 if (temp & FDI_RX_BIT_LOCK) {
2654 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2655 DRM_DEBUG_KMS("FDI train 1 done.\n");
2656 break;
2657 }
2658 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002659 }
Sean Paulfa37d392012-03-02 12:53:39 -05002660 if (retry < 5)
2661 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002662 }
2663 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002664 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002665
2666 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002667 reg = FDI_TX_CTL(pipe);
2668 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002669 temp &= ~FDI_LINK_TRAIN_NONE;
2670 temp |= FDI_LINK_TRAIN_PATTERN_2;
2671 if (IS_GEN6(dev)) {
2672 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2673 /* SNB-B */
2674 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2675 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002676 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002677
Chris Wilson5eddb702010-09-11 13:48:45 +01002678 reg = FDI_RX_CTL(pipe);
2679 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002680 if (HAS_PCH_CPT(dev)) {
2681 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2682 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2683 } else {
2684 temp &= ~FDI_LINK_TRAIN_NONE;
2685 temp |= FDI_LINK_TRAIN_PATTERN_2;
2686 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002687 I915_WRITE(reg, temp);
2688
2689 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002690 udelay(150);
2691
Akshay Joshi0206e352011-08-16 15:34:10 -04002692 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002693 reg = FDI_TX_CTL(pipe);
2694 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002695 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2696 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002697 I915_WRITE(reg, temp);
2698
2699 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002700 udelay(500);
2701
Sean Paulfa37d392012-03-02 12:53:39 -05002702 for (retry = 0; retry < 5; retry++) {
2703 reg = FDI_RX_IIR(pipe);
2704 temp = I915_READ(reg);
2705 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2706 if (temp & FDI_RX_SYMBOL_LOCK) {
2707 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2708 DRM_DEBUG_KMS("FDI train 2 done.\n");
2709 break;
2710 }
2711 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002712 }
Sean Paulfa37d392012-03-02 12:53:39 -05002713 if (retry < 5)
2714 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002715 }
2716 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002717 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002718
2719 DRM_DEBUG_KMS("FDI train done.\n");
2720}
2721
Jesse Barnes357555c2011-04-28 15:09:55 -07002722/* Manual link training for Ivy Bridge A0 parts */
2723static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2724{
2725 struct drm_device *dev = crtc->dev;
2726 struct drm_i915_private *dev_priv = dev->dev_private;
2727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2728 int pipe = intel_crtc->pipe;
2729 u32 reg, temp, i;
2730
2731 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2732 for train result */
2733 reg = FDI_RX_IMR(pipe);
2734 temp = I915_READ(reg);
2735 temp &= ~FDI_RX_SYMBOL_LOCK;
2736 temp &= ~FDI_RX_BIT_LOCK;
2737 I915_WRITE(reg, temp);
2738
2739 POSTING_READ(reg);
2740 udelay(150);
2741
Daniel Vetter01a415f2012-10-27 15:58:40 +02002742 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2743 I915_READ(FDI_RX_IIR(pipe)));
2744
Jesse Barnes357555c2011-04-28 15:09:55 -07002745 /* enable CPU FDI TX and PCH FDI RX */
2746 reg = FDI_TX_CTL(pipe);
2747 temp = I915_READ(reg);
2748 temp &= ~(7 << 19);
2749 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2750 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2751 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2752 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2753 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002754 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002755 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2756
Daniel Vetterd74cf322012-10-26 10:58:13 +02002757 I915_WRITE(FDI_RX_MISC(pipe),
2758 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2759
Jesse Barnes357555c2011-04-28 15:09:55 -07002760 reg = FDI_RX_CTL(pipe);
2761 temp = I915_READ(reg);
2762 temp &= ~FDI_LINK_TRAIN_AUTO;
2763 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2764 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002765 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002766 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2767
2768 POSTING_READ(reg);
2769 udelay(150);
2770
Akshay Joshi0206e352011-08-16 15:34:10 -04002771 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002772 reg = FDI_TX_CTL(pipe);
2773 temp = I915_READ(reg);
2774 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2775 temp |= snb_b_fdi_train_param[i];
2776 I915_WRITE(reg, temp);
2777
2778 POSTING_READ(reg);
2779 udelay(500);
2780
2781 reg = FDI_RX_IIR(pipe);
2782 temp = I915_READ(reg);
2783 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2784
2785 if (temp & FDI_RX_BIT_LOCK ||
2786 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2787 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002788 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002789 break;
2790 }
2791 }
2792 if (i == 4)
2793 DRM_ERROR("FDI train 1 fail!\n");
2794
2795 /* Train 2 */
2796 reg = FDI_TX_CTL(pipe);
2797 temp = I915_READ(reg);
2798 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2799 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2800 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2801 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2802 I915_WRITE(reg, temp);
2803
2804 reg = FDI_RX_CTL(pipe);
2805 temp = I915_READ(reg);
2806 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2807 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2808 I915_WRITE(reg, temp);
2809
2810 POSTING_READ(reg);
2811 udelay(150);
2812
Akshay Joshi0206e352011-08-16 15:34:10 -04002813 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002814 reg = FDI_TX_CTL(pipe);
2815 temp = I915_READ(reg);
2816 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2817 temp |= snb_b_fdi_train_param[i];
2818 I915_WRITE(reg, temp);
2819
2820 POSTING_READ(reg);
2821 udelay(500);
2822
2823 reg = FDI_RX_IIR(pipe);
2824 temp = I915_READ(reg);
2825 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2826
2827 if (temp & FDI_RX_SYMBOL_LOCK) {
2828 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002829 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002830 break;
2831 }
2832 }
2833 if (i == 4)
2834 DRM_ERROR("FDI train 2 fail!\n");
2835
2836 DRM_DEBUG_KMS("FDI train done.\n");
2837}
2838
Daniel Vetter88cefb62012-08-12 19:27:14 +02002839static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002840{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002841 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002842 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002843 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002844 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002845
Jesse Barnesc64e3112010-09-10 11:27:03 -07002846
Jesse Barnes0e23b992010-09-10 11:10:00 -07002847 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002848 reg = FDI_RX_CTL(pipe);
2849 temp = I915_READ(reg);
2850 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002851 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002852 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002853 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2854
2855 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002856 udelay(200);
2857
2858 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002859 temp = I915_READ(reg);
2860 I915_WRITE(reg, temp | FDI_PCDCLK);
2861
2862 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002863 udelay(200);
2864
Paulo Zanoni20749732012-11-23 15:30:38 -02002865 /* Enable CPU FDI TX PLL, always on for Ironlake */
2866 reg = FDI_TX_CTL(pipe);
2867 temp = I915_READ(reg);
2868 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2869 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002870
Paulo Zanoni20749732012-11-23 15:30:38 -02002871 POSTING_READ(reg);
2872 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002873 }
2874}
2875
Daniel Vetter88cefb62012-08-12 19:27:14 +02002876static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2877{
2878 struct drm_device *dev = intel_crtc->base.dev;
2879 struct drm_i915_private *dev_priv = dev->dev_private;
2880 int pipe = intel_crtc->pipe;
2881 u32 reg, temp;
2882
2883 /* Switch from PCDclk to Rawclk */
2884 reg = FDI_RX_CTL(pipe);
2885 temp = I915_READ(reg);
2886 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2887
2888 /* Disable CPU FDI TX PLL */
2889 reg = FDI_TX_CTL(pipe);
2890 temp = I915_READ(reg);
2891 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2892
2893 POSTING_READ(reg);
2894 udelay(100);
2895
2896 reg = FDI_RX_CTL(pipe);
2897 temp = I915_READ(reg);
2898 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2899
2900 /* Wait for the clocks to turn off. */
2901 POSTING_READ(reg);
2902 udelay(100);
2903}
2904
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002905static void ironlake_fdi_disable(struct drm_crtc *crtc)
2906{
2907 struct drm_device *dev = crtc->dev;
2908 struct drm_i915_private *dev_priv = dev->dev_private;
2909 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2910 int pipe = intel_crtc->pipe;
2911 u32 reg, temp;
2912
2913 /* disable CPU FDI tx and PCH FDI rx */
2914 reg = FDI_TX_CTL(pipe);
2915 temp = I915_READ(reg);
2916 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2917 POSTING_READ(reg);
2918
2919 reg = FDI_RX_CTL(pipe);
2920 temp = I915_READ(reg);
2921 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002922 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002923 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2924
2925 POSTING_READ(reg);
2926 udelay(100);
2927
2928 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002929 if (HAS_PCH_IBX(dev)) {
2930 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002931 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002932
2933 /* still set train pattern 1 */
2934 reg = FDI_TX_CTL(pipe);
2935 temp = I915_READ(reg);
2936 temp &= ~FDI_LINK_TRAIN_NONE;
2937 temp |= FDI_LINK_TRAIN_PATTERN_1;
2938 I915_WRITE(reg, temp);
2939
2940 reg = FDI_RX_CTL(pipe);
2941 temp = I915_READ(reg);
2942 if (HAS_PCH_CPT(dev)) {
2943 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2944 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2945 } else {
2946 temp &= ~FDI_LINK_TRAIN_NONE;
2947 temp |= FDI_LINK_TRAIN_PATTERN_1;
2948 }
2949 /* BPC in FDI rx is consistent with that in PIPECONF */
2950 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002951 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002952 I915_WRITE(reg, temp);
2953
2954 POSTING_READ(reg);
2955 udelay(100);
2956}
2957
Chris Wilson5bb61642012-09-27 21:25:58 +01002958static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2959{
2960 struct drm_device *dev = crtc->dev;
2961 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002963 unsigned long flags;
2964 bool pending;
2965
Ville Syrjälä10d83732013-01-29 18:13:34 +02002966 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2967 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002968 return false;
2969
2970 spin_lock_irqsave(&dev->event_lock, flags);
2971 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2972 spin_unlock_irqrestore(&dev->event_lock, flags);
2973
2974 return pending;
2975}
2976
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002977static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2978{
Chris Wilson0f911282012-04-17 10:05:38 +01002979 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002980 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002981
2982 if (crtc->fb == NULL)
2983 return;
2984
Daniel Vetter2c10d572012-12-20 21:24:07 +01002985 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2986
Chris Wilson5bb61642012-09-27 21:25:58 +01002987 wait_event(dev_priv->pending_flip_queue,
2988 !intel_crtc_has_pending_flip(crtc));
2989
Chris Wilson0f911282012-04-17 10:05:38 +01002990 mutex_lock(&dev->struct_mutex);
2991 intel_finish_fb(crtc->fb);
2992 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002993}
2994
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002995/* Program iCLKIP clock to the desired frequency */
2996static void lpt_program_iclkip(struct drm_crtc *crtc)
2997{
2998 struct drm_device *dev = crtc->dev;
2999 struct drm_i915_private *dev_priv = dev->dev_private;
3000 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3001 u32 temp;
3002
Daniel Vetter09153002012-12-12 14:06:44 +01003003 mutex_lock(&dev_priv->dpio_lock);
3004
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003005 /* It is necessary to ungate the pixclk gate prior to programming
3006 * the divisors, and gate it back when it is done.
3007 */
3008 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3009
3010 /* Disable SSCCTL */
3011 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003012 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3013 SBI_SSCCTL_DISABLE,
3014 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003015
3016 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
3017 if (crtc->mode.clock == 20000) {
3018 auxdiv = 1;
3019 divsel = 0x41;
3020 phaseinc = 0x20;
3021 } else {
3022 /* The iCLK virtual clock root frequency is in MHz,
3023 * but the crtc->mode.clock in in KHz. To get the divisors,
3024 * it is necessary to divide one by another, so we
3025 * convert the virtual clock precision to KHz here for higher
3026 * precision.
3027 */
3028 u32 iclk_virtual_root_freq = 172800 * 1000;
3029 u32 iclk_pi_range = 64;
3030 u32 desired_divisor, msb_divisor_value, pi_value;
3031
3032 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
3033 msb_divisor_value = desired_divisor / iclk_pi_range;
3034 pi_value = desired_divisor % iclk_pi_range;
3035
3036 auxdiv = 0;
3037 divsel = msb_divisor_value - 2;
3038 phaseinc = pi_value;
3039 }
3040
3041 /* This should not happen with any sane values */
3042 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3043 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3044 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3045 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3046
3047 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
3048 crtc->mode.clock,
3049 auxdiv,
3050 divsel,
3051 phasedir,
3052 phaseinc);
3053
3054 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003055 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003056 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3057 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3058 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3059 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3060 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3061 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003062 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003063
3064 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003065 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003066 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3067 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003068 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003069
3070 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003071 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003072 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003073 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003074
3075 /* Wait for initialization time */
3076 udelay(24);
3077
3078 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003079
3080 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003081}
3082
Jesse Barnesf67a5592011-01-05 10:31:48 -08003083/*
3084 * Enable PCH resources required for PCH ports:
3085 * - PCH PLLs
3086 * - FDI training & RX/TX
3087 * - update transcoder timings
3088 * - DP transcoding bits
3089 * - transcoder
3090 */
3091static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003092{
3093 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003094 struct drm_i915_private *dev_priv = dev->dev_private;
3095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3096 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003097 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003098
Chris Wilsone7e164d2012-05-11 09:21:25 +01003099 assert_transcoder_disabled(dev_priv, pipe);
3100
Daniel Vettercd986ab2012-10-26 10:58:12 +02003101 /* Write the TU size bits before fdi link training, so that error
3102 * detection works. */
3103 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3104 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3105
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003106 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003107 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003108
Daniel Vetter572deb32012-10-27 18:46:14 +02003109 /* XXX: pch pll's can be enabled any time before we enable the PCH
3110 * transcoder, and we actually should do this to not upset any PCH
3111 * transcoder that already use the clock when we share it.
3112 *
3113 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
3114 * unconditionally resets the pll - we need that to have the right LVDS
3115 * enable sequence. */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02003116 ironlake_enable_pch_pll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01003117
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003118 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003119 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003120
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003121 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003122 switch (pipe) {
3123 default:
3124 case 0:
3125 temp |= TRANSA_DPLL_ENABLE;
3126 sel = TRANSA_DPLLB_SEL;
3127 break;
3128 case 1:
3129 temp |= TRANSB_DPLL_ENABLE;
3130 sel = TRANSB_DPLLB_SEL;
3131 break;
3132 case 2:
3133 temp |= TRANSC_DPLL_ENABLE;
3134 sel = TRANSC_DPLLB_SEL;
3135 break;
Jesse Barnesd64311a2011-10-12 15:01:33 -07003136 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003137 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3138 temp |= sel;
3139 else
3140 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003141 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003142 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003143
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003144 /* set transcoder timing, panel must allow it */
3145 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003146 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3147 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3148 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3149
3150 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3151 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3152 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003153 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003154
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003155 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003156
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003157 /* For PCH DP, enable TRANS_DP_CTL */
3158 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003159 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3160 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003161 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003162 reg = TRANS_DP_CTL(pipe);
3163 temp = I915_READ(reg);
3164 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003165 TRANS_DP_SYNC_MASK |
3166 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003167 temp |= (TRANS_DP_OUTPUT_ENABLE |
3168 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003169 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003170
3171 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003172 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003173 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003174 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003175
3176 switch (intel_trans_dp_port_sel(crtc)) {
3177 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003178 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003179 break;
3180 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003181 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003182 break;
3183 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003184 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003185 break;
3186 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003187 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003188 }
3189
Chris Wilson5eddb702010-09-11 13:48:45 +01003190 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003191 }
3192
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003193 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003194}
3195
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003196static void lpt_pch_enable(struct drm_crtc *crtc)
3197{
3198 struct drm_device *dev = crtc->dev;
3199 struct drm_i915_private *dev_priv = dev->dev_private;
3200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003201 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003202
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003203 assert_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003204
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003205 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003206
Paulo Zanoni0540e482012-10-31 18:12:40 -02003207 /* Set transcoder timing. */
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003208 I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
3209 I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
3210 I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003211
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003212 I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
3213 I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
3214 I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
3215 I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003216
Paulo Zanoni937bb612012-10-31 18:12:47 -02003217 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003218}
3219
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003220static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3221{
3222 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3223
3224 if (pll == NULL)
3225 return;
3226
3227 if (pll->refcount == 0) {
3228 WARN(1, "bad PCH PLL refcount\n");
3229 return;
3230 }
3231
3232 --pll->refcount;
3233 intel_crtc->pch_pll = NULL;
3234}
3235
3236static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3237{
3238 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3239 struct intel_pch_pll *pll;
3240 int i;
3241
3242 pll = intel_crtc->pch_pll;
3243 if (pll) {
3244 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3245 intel_crtc->base.base.id, pll->pll_reg);
3246 goto prepare;
3247 }
3248
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003249 if (HAS_PCH_IBX(dev_priv->dev)) {
3250 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3251 i = intel_crtc->pipe;
3252 pll = &dev_priv->pch_plls[i];
3253
3254 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3255 intel_crtc->base.base.id, pll->pll_reg);
3256
3257 goto found;
3258 }
3259
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003260 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3261 pll = &dev_priv->pch_plls[i];
3262
3263 /* Only want to check enabled timings first */
3264 if (pll->refcount == 0)
3265 continue;
3266
3267 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3268 fp == I915_READ(pll->fp0_reg)) {
3269 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3270 intel_crtc->base.base.id,
3271 pll->pll_reg, pll->refcount, pll->active);
3272
3273 goto found;
3274 }
3275 }
3276
3277 /* Ok no matching timings, maybe there's a free one? */
3278 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3279 pll = &dev_priv->pch_plls[i];
3280 if (pll->refcount == 0) {
3281 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3282 intel_crtc->base.base.id, pll->pll_reg);
3283 goto found;
3284 }
3285 }
3286
3287 return NULL;
3288
3289found:
3290 intel_crtc->pch_pll = pll;
3291 pll->refcount++;
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003292 DRM_DEBUG_DRIVER("using pll %d for pipe %c\n", i, pipe_name(intel_crtc->pipe));
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003293prepare: /* separate function? */
3294 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003295
Chris Wilsone04c7352012-05-02 20:43:56 +01003296 /* Wait for the clocks to stabilize before rewriting the regs */
3297 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003298 POSTING_READ(pll->pll_reg);
3299 udelay(150);
Chris Wilsone04c7352012-05-02 20:43:56 +01003300
3301 I915_WRITE(pll->fp0_reg, fp);
3302 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003303 pll->on = false;
3304 return pll;
3305}
3306
Jesse Barnesd4270e52011-10-11 10:43:02 -07003307void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3308{
3309 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003310 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003311 u32 temp;
3312
3313 temp = I915_READ(dslreg);
3314 udelay(500);
3315 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003316 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003317 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003318 }
3319}
3320
Jesse Barnesf67a5592011-01-05 10:31:48 -08003321static void ironlake_crtc_enable(struct drm_crtc *crtc)
3322{
3323 struct drm_device *dev = crtc->dev;
3324 struct drm_i915_private *dev_priv = dev->dev_private;
3325 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003326 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003327 int pipe = intel_crtc->pipe;
3328 int plane = intel_crtc->plane;
3329 u32 temp;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003330
Daniel Vetter08a48462012-07-02 11:43:47 +02003331 WARN_ON(!crtc->enabled);
3332
Jesse Barnesf67a5592011-01-05 10:31:48 -08003333 if (intel_crtc->active)
3334 return;
3335
3336 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003337
3338 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3339 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3340
Jesse Barnesf67a5592011-01-05 10:31:48 -08003341 intel_update_watermarks(dev);
3342
3343 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3344 temp = I915_READ(PCH_LVDS);
3345 if ((temp & LVDS_PORT_EN) == 0)
3346 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3347 }
3348
Jesse Barnesf67a5592011-01-05 10:31:48 -08003349
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003350 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003351 /* Note: FDI PLL enabling _must_ be done before we enable the
3352 * cpu pipes, hence this is separate from all the other fdi/pch
3353 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003354 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003355 } else {
3356 assert_fdi_tx_disabled(dev_priv, pipe);
3357 assert_fdi_rx_disabled(dev_priv, pipe);
3358 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003359
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003360 for_each_encoder_on_crtc(dev, crtc, encoder)
3361 if (encoder->pre_enable)
3362 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003363
3364 /* Enable panel fitting for LVDS */
3365 if (dev_priv->pch_pf_size &&
Jani Nikula547dc042012-11-02 11:24:03 +02003366 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3367 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnesf67a5592011-01-05 10:31:48 -08003368 /* Force use of hard-coded filter coefficients
3369 * as some pre-programmed values are broken,
3370 * e.g. x201.
3371 */
Paulo Zanoni13888d72012-11-20 13:27:41 -02003372 if (IS_IVYBRIDGE(dev))
3373 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3374 PF_PIPE_SEL_IVB(pipe));
3375 else
3376 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003377 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3378 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003379 }
3380
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003381 /*
3382 * On ILK+ LUT must be loaded before the pipe is running but with
3383 * clocks enabled
3384 */
3385 intel_crtc_load_lut(crtc);
3386
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003387 intel_enable_pipe(dev_priv, pipe,
3388 intel_crtc->config.has_pch_encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003389 intel_enable_plane(dev_priv, plane, pipe);
3390
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003391 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003392 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003393
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003394 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003395 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003396 mutex_unlock(&dev->struct_mutex);
3397
Chris Wilson6b383a72010-09-13 13:54:26 +01003398 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003399
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003400 for_each_encoder_on_crtc(dev, crtc, encoder)
3401 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003402
3403 if (HAS_PCH_CPT(dev))
3404 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003405
3406 /*
3407 * There seems to be a race in PCH platform hw (at least on some
3408 * outputs) where an enabled pipe still completes any pageflip right
3409 * away (as if the pipe is off) instead of waiting for vblank. As soon
3410 * as the first vblank happend, everything works as expected. Hence just
3411 * wait for one vblank before returning to avoid strange things
3412 * happening.
3413 */
3414 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003415}
3416
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003417static void haswell_crtc_enable(struct drm_crtc *crtc)
3418{
3419 struct drm_device *dev = crtc->dev;
3420 struct drm_i915_private *dev_priv = dev->dev_private;
3421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3422 struct intel_encoder *encoder;
3423 int pipe = intel_crtc->pipe;
3424 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003425
3426 WARN_ON(!crtc->enabled);
3427
3428 if (intel_crtc->active)
3429 return;
3430
3431 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003432
3433 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3434 if (intel_crtc->config.has_pch_encoder)
3435 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3436
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003437 intel_update_watermarks(dev);
3438
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003439 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003440 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003441
3442 for_each_encoder_on_crtc(dev, crtc, encoder)
3443 if (encoder->pre_enable)
3444 encoder->pre_enable(encoder);
3445
Paulo Zanoni1f544382012-10-24 11:32:00 -02003446 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003447
Paulo Zanoni1f544382012-10-24 11:32:00 -02003448 /* Enable panel fitting for eDP */
Jani Nikula547dc042012-11-02 11:24:03 +02003449 if (dev_priv->pch_pf_size &&
3450 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003451 /* Force use of hard-coded filter coefficients
3452 * as some pre-programmed values are broken,
3453 * e.g. x201.
3454 */
Paulo Zanoni54075a72012-11-20 13:27:42 -02003455 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3456 PF_PIPE_SEL_IVB(pipe));
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003457 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3458 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3459 }
3460
3461 /*
3462 * On ILK+ LUT must be loaded before the pipe is running but with
3463 * clocks enabled
3464 */
3465 intel_crtc_load_lut(crtc);
3466
Paulo Zanoni1f544382012-10-24 11:32:00 -02003467 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003468 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003469
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003470 intel_enable_pipe(dev_priv, pipe,
3471 intel_crtc->config.has_pch_encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003472 intel_enable_plane(dev_priv, plane, pipe);
3473
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003474 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003475 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003476
3477 mutex_lock(&dev->struct_mutex);
3478 intel_update_fbc(dev);
3479 mutex_unlock(&dev->struct_mutex);
3480
3481 intel_crtc_update_cursor(crtc, true);
3482
3483 for_each_encoder_on_crtc(dev, crtc, encoder)
3484 encoder->enable(encoder);
3485
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003486 /*
3487 * There seems to be a race in PCH platform hw (at least on some
3488 * outputs) where an enabled pipe still completes any pageflip right
3489 * away (as if the pipe is off) instead of waiting for vblank. As soon
3490 * as the first vblank happend, everything works as expected. Hence just
3491 * wait for one vblank before returning to avoid strange things
3492 * happening.
3493 */
3494 intel_wait_for_vblank(dev, intel_crtc->pipe);
3495}
3496
Jesse Barnes6be4a602010-09-10 10:26:01 -07003497static void ironlake_crtc_disable(struct drm_crtc *crtc)
3498{
3499 struct drm_device *dev = crtc->dev;
3500 struct drm_i915_private *dev_priv = dev->dev_private;
3501 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003502 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003503 int pipe = intel_crtc->pipe;
3504 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003506
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003507
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003508 if (!intel_crtc->active)
3509 return;
3510
Daniel Vetterea9d7582012-07-10 10:42:52 +02003511 for_each_encoder_on_crtc(dev, crtc, encoder)
3512 encoder->disable(encoder);
3513
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003514 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003515 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003516 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003517
Jesse Barnesb24e7172011-01-04 15:09:30 -08003518 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003519
Chris Wilson973d04f2011-07-08 12:22:37 +01003520 if (dev_priv->cfb_plane == plane)
3521 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003522
Paulo Zanoni86642812013-04-12 17:57:57 -03003523 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003524 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003525
Jesse Barnes6be4a602010-09-10 10:26:01 -07003526 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003527 I915_WRITE(PF_CTL(pipe), 0);
3528 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003529
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003530 for_each_encoder_on_crtc(dev, crtc, encoder)
3531 if (encoder->post_disable)
3532 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003533
Chris Wilson5eddb702010-09-11 13:48:45 +01003534 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003535
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003536 ironlake_disable_pch_transcoder(dev_priv, pipe);
Paulo Zanoni86642812013-04-12 17:57:57 -03003537 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003538
3539 if (HAS_PCH_CPT(dev)) {
3540 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003541 reg = TRANS_DP_CTL(pipe);
3542 temp = I915_READ(reg);
3543 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003544 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003545 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003546
3547 /* disable DPLL_SEL */
3548 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003549 switch (pipe) {
3550 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003551 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003552 break;
3553 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003554 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003555 break;
3556 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003557 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003558 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003559 break;
3560 default:
3561 BUG(); /* wtf */
3562 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003563 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003564 }
3565
3566 /* disable PCH DPLL */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003567 intel_disable_pch_pll(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003568
Daniel Vetter88cefb62012-08-12 19:27:14 +02003569 ironlake_fdi_pll_disable(intel_crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +01003570
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003571 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003572 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003573
3574 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003575 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003576 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003577}
3578
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003579static void haswell_crtc_disable(struct drm_crtc *crtc)
3580{
3581 struct drm_device *dev = crtc->dev;
3582 struct drm_i915_private *dev_priv = dev->dev_private;
3583 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3584 struct intel_encoder *encoder;
3585 int pipe = intel_crtc->pipe;
3586 int plane = intel_crtc->plane;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003587 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003588
3589 if (!intel_crtc->active)
3590 return;
3591
3592 for_each_encoder_on_crtc(dev, crtc, encoder)
3593 encoder->disable(encoder);
3594
3595 intel_crtc_wait_for_pending_flips(crtc);
3596 drm_vblank_off(dev, pipe);
3597 intel_crtc_update_cursor(crtc, false);
3598
3599 intel_disable_plane(dev_priv, plane, pipe);
3600
3601 if (dev_priv->cfb_plane == plane)
3602 intel_disable_fbc(dev);
3603
Paulo Zanoni86642812013-04-12 17:57:57 -03003604 if (intel_crtc->config.has_pch_encoder)
3605 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003606 intel_disable_pipe(dev_priv, pipe);
3607
Paulo Zanoniad80a812012-10-24 16:06:19 -02003608 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003609
Paulo Zanonif7708f72013-03-22 14:16:38 -03003610 /* XXX: Once we have proper panel fitter state tracking implemented with
3611 * hardware state read/check support we should switch to only disable
3612 * the panel fitter when we know it's used. */
3613 if (intel_using_power_well(dev)) {
3614 I915_WRITE(PF_CTL(pipe), 0);
3615 I915_WRITE(PF_WIN_SZ(pipe), 0);
3616 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003617
Paulo Zanoni1f544382012-10-24 11:32:00 -02003618 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003619
3620 for_each_encoder_on_crtc(dev, crtc, encoder)
3621 if (encoder->post_disable)
3622 encoder->post_disable(encoder);
3623
Daniel Vetter88adfff2013-03-28 10:42:01 +01003624 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003625 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003626 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003627 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003628 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003629
3630 intel_crtc->active = false;
3631 intel_update_watermarks(dev);
3632
3633 mutex_lock(&dev->struct_mutex);
3634 intel_update_fbc(dev);
3635 mutex_unlock(&dev->struct_mutex);
3636}
3637
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003638static void ironlake_crtc_off(struct drm_crtc *crtc)
3639{
3640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3641 intel_put_pch_pll(intel_crtc);
3642}
3643
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003644static void haswell_crtc_off(struct drm_crtc *crtc)
3645{
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3647
3648 /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
3649 * start using it. */
Daniel Vetter3b117c82013-04-17 20:15:07 +02003650 intel_crtc->config.cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003651
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003652 intel_ddi_put_crtc_pll(crtc);
3653}
3654
Daniel Vetter02e792f2009-09-15 22:57:34 +02003655static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3656{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003657 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003658 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003659 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003660
Chris Wilson23f09ce2010-08-12 13:53:37 +01003661 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003662 dev_priv->mm.interruptible = false;
3663 (void) intel_overlay_switch_off(intel_crtc->overlay);
3664 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003665 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003666 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003667
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003668 /* Let userspace switch the overlay on again. In most cases userspace
3669 * has to recompute where to put it anyway.
3670 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003671}
3672
Egbert Eich61bc95c2013-03-04 09:24:38 -05003673/**
3674 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3675 * cursor plane briefly if not already running after enabling the display
3676 * plane.
3677 * This workaround avoids occasional blank screens when self refresh is
3678 * enabled.
3679 */
3680static void
3681g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3682{
3683 u32 cntl = I915_READ(CURCNTR(pipe));
3684
3685 if ((cntl & CURSOR_MODE) == 0) {
3686 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3687
3688 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3689 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3690 intel_wait_for_vblank(dev_priv->dev, pipe);
3691 I915_WRITE(CURCNTR(pipe), cntl);
3692 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3693 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3694 }
3695}
3696
Jesse Barnes89b667f2013-04-18 14:51:36 -07003697static void valleyview_crtc_enable(struct drm_crtc *crtc)
3698{
3699 struct drm_device *dev = crtc->dev;
3700 struct drm_i915_private *dev_priv = dev->dev_private;
3701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3702 struct intel_encoder *encoder;
3703 int pipe = intel_crtc->pipe;
3704 int plane = intel_crtc->plane;
3705
3706 WARN_ON(!crtc->enabled);
3707
3708 if (intel_crtc->active)
3709 return;
3710
3711 intel_crtc->active = true;
3712 intel_update_watermarks(dev);
3713
3714 mutex_lock(&dev_priv->dpio_lock);
3715
3716 for_each_encoder_on_crtc(dev, crtc, encoder)
3717 if (encoder->pre_pll_enable)
3718 encoder->pre_pll_enable(encoder);
3719
3720 intel_enable_pll(dev_priv, pipe);
3721
3722 for_each_encoder_on_crtc(dev, crtc, encoder)
3723 if (encoder->pre_enable)
3724 encoder->pre_enable(encoder);
3725
3726 /* VLV wants encoder enabling _before_ the pipe is up. */
3727 for_each_encoder_on_crtc(dev, crtc, encoder)
3728 encoder->enable(encoder);
3729
3730 intel_enable_pipe(dev_priv, pipe, false);
3731 intel_enable_plane(dev_priv, plane, pipe);
3732
3733 intel_crtc_load_lut(crtc);
3734 intel_update_fbc(dev);
3735
3736 /* Give the overlay scaler a chance to enable if it's on this pipe */
3737 intel_crtc_dpms_overlay(intel_crtc, true);
3738 intel_crtc_update_cursor(crtc, true);
3739
3740 mutex_unlock(&dev_priv->dpio_lock);
3741}
3742
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003743static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003744{
3745 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003746 struct drm_i915_private *dev_priv = dev->dev_private;
3747 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003748 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003749 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003750 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003751
Daniel Vetter08a48462012-07-02 11:43:47 +02003752 WARN_ON(!crtc->enabled);
3753
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003754 if (intel_crtc->active)
3755 return;
3756
3757 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003758 intel_update_watermarks(dev);
3759
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003760 intel_enable_pll(dev_priv, pipe);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003761
3762 for_each_encoder_on_crtc(dev, crtc, encoder)
3763 if (encoder->pre_enable)
3764 encoder->pre_enable(encoder);
3765
Jesse Barnes040484a2011-01-03 12:14:26 -08003766 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003767 intel_enable_plane(dev_priv, plane, pipe);
Egbert Eich61bc95c2013-03-04 09:24:38 -05003768 if (IS_G4X(dev))
3769 g4x_fixup_plane(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003770
3771 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003772 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003773
3774 /* Give the overlay scaler a chance to enable if it's on this pipe */
3775 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003776 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003777
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003778 for_each_encoder_on_crtc(dev, crtc, encoder)
3779 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003780}
3781
Daniel Vetter87476d62013-04-11 16:29:06 +02003782static void i9xx_pfit_disable(struct intel_crtc *crtc)
3783{
3784 struct drm_device *dev = crtc->base.dev;
3785 struct drm_i915_private *dev_priv = dev->dev_private;
3786 enum pipe pipe;
3787 uint32_t pctl = I915_READ(PFIT_CONTROL);
3788
3789 assert_pipe_disabled(dev_priv, crtc->pipe);
3790
3791 if (INTEL_INFO(dev)->gen >= 4)
3792 pipe = (pctl & PFIT_PIPE_MASK) >> PFIT_PIPE_SHIFT;
3793 else
3794 pipe = PIPE_B;
3795
3796 if (pipe == crtc->pipe) {
3797 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n", pctl);
3798 I915_WRITE(PFIT_CONTROL, 0);
3799 }
3800}
3801
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003802static void i9xx_crtc_disable(struct drm_crtc *crtc)
3803{
3804 struct drm_device *dev = crtc->dev;
3805 struct drm_i915_private *dev_priv = dev->dev_private;
3806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003807 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003808 int pipe = intel_crtc->pipe;
3809 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003810
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003811 if (!intel_crtc->active)
3812 return;
3813
Daniel Vetterea9d7582012-07-10 10:42:52 +02003814 for_each_encoder_on_crtc(dev, crtc, encoder)
3815 encoder->disable(encoder);
3816
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003817 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003818 intel_crtc_wait_for_pending_flips(crtc);
3819 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003820 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003821 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003822
Chris Wilson973d04f2011-07-08 12:22:37 +01003823 if (dev_priv->cfb_plane == plane)
3824 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003825
Jesse Barnesb24e7172011-01-04 15:09:30 -08003826 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003827 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003828
Daniel Vetter87476d62013-04-11 16:29:06 +02003829 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003830
Jesse Barnes89b667f2013-04-18 14:51:36 -07003831 for_each_encoder_on_crtc(dev, crtc, encoder)
3832 if (encoder->post_disable)
3833 encoder->post_disable(encoder);
3834
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003835 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003836
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003837 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003838 intel_update_fbc(dev);
3839 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003840}
3841
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003842static void i9xx_crtc_off(struct drm_crtc *crtc)
3843{
3844}
3845
Daniel Vetter976f8a22012-07-08 22:34:21 +02003846static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3847 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003848{
3849 struct drm_device *dev = crtc->dev;
3850 struct drm_i915_master_private *master_priv;
3851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3852 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003853
3854 if (!dev->primary->master)
3855 return;
3856
3857 master_priv = dev->primary->master->driver_priv;
3858 if (!master_priv->sarea_priv)
3859 return;
3860
Jesse Barnes79e53942008-11-07 14:24:08 -08003861 switch (pipe) {
3862 case 0:
3863 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3864 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3865 break;
3866 case 1:
3867 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3868 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3869 break;
3870 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003871 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003872 break;
3873 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003874}
3875
Daniel Vetter976f8a22012-07-08 22:34:21 +02003876/**
3877 * Sets the power management mode of the pipe and plane.
3878 */
3879void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003880{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003881 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003882 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003883 struct intel_encoder *intel_encoder;
3884 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003885
Daniel Vetter976f8a22012-07-08 22:34:21 +02003886 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3887 enable |= intel_encoder->connectors_active;
3888
3889 if (enable)
3890 dev_priv->display.crtc_enable(crtc);
3891 else
3892 dev_priv->display.crtc_disable(crtc);
3893
3894 intel_crtc_update_sarea(crtc, enable);
3895}
3896
Daniel Vetter976f8a22012-07-08 22:34:21 +02003897static void intel_crtc_disable(struct drm_crtc *crtc)
3898{
3899 struct drm_device *dev = crtc->dev;
3900 struct drm_connector *connector;
3901 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003902 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003903
3904 /* crtc should still be enabled when we disable it. */
3905 WARN_ON(!crtc->enabled);
3906
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003907 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003908 dev_priv->display.crtc_disable(crtc);
3909 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003910 dev_priv->display.off(crtc);
3911
Chris Wilson931872f2012-01-16 23:01:13 +00003912 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3913 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003914
3915 if (crtc->fb) {
3916 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003917 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003918 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003919 crtc->fb = NULL;
3920 }
3921
3922 /* Update computed state. */
3923 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3924 if (!connector->encoder || !connector->encoder->crtc)
3925 continue;
3926
3927 if (connector->encoder->crtc != crtc)
3928 continue;
3929
3930 connector->dpms = DRM_MODE_DPMS_OFF;
3931 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003932 }
3933}
3934
Daniel Vettera261b242012-07-26 19:21:47 +02003935void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003936{
Daniel Vettera261b242012-07-26 19:21:47 +02003937 struct drm_crtc *crtc;
3938
3939 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3940 if (crtc->enabled)
3941 intel_crtc_disable(crtc);
3942 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003943}
3944
Chris Wilsonea5b2132010-08-04 13:50:23 +01003945void intel_encoder_destroy(struct drm_encoder *encoder)
3946{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003947 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003948
Chris Wilsonea5b2132010-08-04 13:50:23 +01003949 drm_encoder_cleanup(encoder);
3950 kfree(intel_encoder);
3951}
3952
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003953/* Simple dpms helper for encodres with just one connector, no cloning and only
3954 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3955 * state of the entire output pipe. */
3956void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3957{
3958 if (mode == DRM_MODE_DPMS_ON) {
3959 encoder->connectors_active = true;
3960
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003961 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003962 } else {
3963 encoder->connectors_active = false;
3964
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003965 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003966 }
3967}
3968
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003969/* Cross check the actual hw state with our own modeset state tracking (and it's
3970 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003971static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003972{
3973 if (connector->get_hw_state(connector)) {
3974 struct intel_encoder *encoder = connector->encoder;
3975 struct drm_crtc *crtc;
3976 bool encoder_enabled;
3977 enum pipe pipe;
3978
3979 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3980 connector->base.base.id,
3981 drm_get_connector_name(&connector->base));
3982
3983 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3984 "wrong connector dpms state\n");
3985 WARN(connector->base.encoder != &encoder->base,
3986 "active connector not linked to encoder\n");
3987 WARN(!encoder->connectors_active,
3988 "encoder->connectors_active not set\n");
3989
3990 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3991 WARN(!encoder_enabled, "encoder not enabled\n");
3992 if (WARN_ON(!encoder->base.crtc))
3993 return;
3994
3995 crtc = encoder->base.crtc;
3996
3997 WARN(!crtc->enabled, "crtc not enabled\n");
3998 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3999 WARN(pipe != to_intel_crtc(crtc)->pipe,
4000 "encoder active on the wrong pipe\n");
4001 }
4002}
4003
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004004/* Even simpler default implementation, if there's really no special case to
4005 * consider. */
4006void intel_connector_dpms(struct drm_connector *connector, int mode)
4007{
4008 struct intel_encoder *encoder = intel_attached_encoder(connector);
4009
4010 /* All the simple cases only support two dpms states. */
4011 if (mode != DRM_MODE_DPMS_ON)
4012 mode = DRM_MODE_DPMS_OFF;
4013
4014 if (mode == connector->dpms)
4015 return;
4016
4017 connector->dpms = mode;
4018
4019 /* Only need to change hw state when actually enabled */
4020 if (encoder->base.crtc)
4021 intel_encoder_dpms(encoder, mode);
4022 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02004023 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004024
Daniel Vetterb9805142012-08-31 17:37:33 +02004025 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004026}
4027
Daniel Vetterf0947c32012-07-02 13:10:34 +02004028/* Simple connector->get_hw_state implementation for encoders that support only
4029 * one connector and no cloning and hence the encoder state determines the state
4030 * of the connector. */
4031bool intel_connector_get_hw_state(struct intel_connector *connector)
4032{
Daniel Vetter24929352012-07-02 20:28:59 +02004033 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02004034 struct intel_encoder *encoder = connector->encoder;
4035
4036 return encoder->get_hw_state(encoder, &pipe);
4037}
4038
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004039static bool intel_crtc_compute_config(struct drm_crtc *crtc,
4040 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08004041{
Zhenyu Wang2c072452009-06-05 15:38:42 +08004042 struct drm_device *dev = crtc->dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004043 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01004044
Eric Anholtbad720f2009-10-22 16:11:14 -07004045 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08004046 /* FDI link clock is fixed at 2.7G */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004047 if (pipe_config->requested_mode.clock * 3
4048 > IRONLAKE_FDI_FREQ * 4)
Jesse Barnes2377b742010-07-07 14:06:43 -07004049 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004050 }
Chris Wilson89749352010-09-12 18:25:19 +01004051
Daniel Vetterf9bef082012-04-15 19:53:19 +02004052 /* All interlaced capable intel hw wants timings in frames. Note though
4053 * that intel_lvds_mode_fixup does some funny tricks with the crtc
4054 * timings, so we need to be careful not to clobber these.*/
Daniel Vetter7ae89232013-03-27 00:44:52 +01004055 if (!pipe_config->timings_set)
Daniel Vetterf9bef082012-04-15 19:53:19 +02004056 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01004057
Chris Wilson44f46b422012-06-21 13:19:59 +03004058 /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
4059 * with a hsync front porch of 0.
4060 */
4061 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4062 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
4063 return false;
4064
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004065 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004066 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02004067 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01004068 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4069 * for lvds. */
4070 pipe_config->pipe_bpp = 8*3;
4071 }
4072
Jesse Barnes79e53942008-11-07 14:24:08 -08004073 return true;
4074}
4075
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07004076static int valleyview_get_display_clock_speed(struct drm_device *dev)
4077{
4078 return 400000; /* FIXME */
4079}
4080
Jesse Barnese70236a2009-09-21 10:42:27 -07004081static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08004082{
Jesse Barnese70236a2009-09-21 10:42:27 -07004083 return 400000;
4084}
Jesse Barnes79e53942008-11-07 14:24:08 -08004085
Jesse Barnese70236a2009-09-21 10:42:27 -07004086static int i915_get_display_clock_speed(struct drm_device *dev)
4087{
4088 return 333000;
4089}
Jesse Barnes79e53942008-11-07 14:24:08 -08004090
Jesse Barnese70236a2009-09-21 10:42:27 -07004091static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4092{
4093 return 200000;
4094}
Jesse Barnes79e53942008-11-07 14:24:08 -08004095
Jesse Barnese70236a2009-09-21 10:42:27 -07004096static int i915gm_get_display_clock_speed(struct drm_device *dev)
4097{
4098 u16 gcfgc = 0;
4099
4100 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4101
4102 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004103 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004104 else {
4105 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4106 case GC_DISPLAY_CLOCK_333_MHZ:
4107 return 333000;
4108 default:
4109 case GC_DISPLAY_CLOCK_190_200_MHZ:
4110 return 190000;
4111 }
4112 }
4113}
Jesse Barnes79e53942008-11-07 14:24:08 -08004114
Jesse Barnese70236a2009-09-21 10:42:27 -07004115static int i865_get_display_clock_speed(struct drm_device *dev)
4116{
4117 return 266000;
4118}
4119
4120static int i855_get_display_clock_speed(struct drm_device *dev)
4121{
4122 u16 hpllcc = 0;
4123 /* Assume that the hardware is in the high speed state. This
4124 * should be the default.
4125 */
4126 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4127 case GC_CLOCK_133_200:
4128 case GC_CLOCK_100_200:
4129 return 200000;
4130 case GC_CLOCK_166_250:
4131 return 250000;
4132 case GC_CLOCK_100_133:
4133 return 133000;
4134 }
4135
4136 /* Shouldn't happen */
4137 return 0;
4138}
4139
4140static int i830_get_display_clock_speed(struct drm_device *dev)
4141{
4142 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004143}
4144
Zhenyu Wang2c072452009-06-05 15:38:42 +08004145static void
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004146intel_reduce_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004147{
4148 while (*num > 0xffffff || *den > 0xffffff) {
4149 *num >>= 1;
4150 *den >>= 1;
4151 }
4152}
4153
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004154void
4155intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4156 int pixel_clock, int link_clock,
4157 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004158{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004159 m_n->tu = 64;
Chris Wilson22ed1112010-12-04 01:01:29 +00004160 m_n->gmch_m = bits_per_pixel * pixel_clock;
4161 m_n->gmch_n = link_clock * nlanes * 8;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004162 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
Chris Wilson22ed1112010-12-04 01:01:29 +00004163 m_n->link_m = pixel_clock;
4164 m_n->link_n = link_clock;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004165 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004166}
4167
Chris Wilsona7615032011-01-12 17:04:08 +00004168static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4169{
Keith Packard72bbe582011-09-26 16:09:45 -07004170 if (i915_panel_use_ssc >= 0)
4171 return i915_panel_use_ssc != 0;
4172 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004173 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004174}
4175
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004176static int vlv_get_refclk(struct drm_crtc *crtc)
4177{
4178 struct drm_device *dev = crtc->dev;
4179 struct drm_i915_private *dev_priv = dev->dev_private;
4180 int refclk = 27000; /* for DP & HDMI */
4181
4182 return 100000; /* only one validated so far */
4183
4184 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4185 refclk = 96000;
4186 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4187 if (intel_panel_use_ssc(dev_priv))
4188 refclk = 100000;
4189 else
4190 refclk = 96000;
4191 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4192 refclk = 100000;
4193 }
4194
4195 return refclk;
4196}
4197
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004198static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4199{
4200 struct drm_device *dev = crtc->dev;
4201 struct drm_i915_private *dev_priv = dev->dev_private;
4202 int refclk;
4203
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004204 if (IS_VALLEYVIEW(dev)) {
4205 refclk = vlv_get_refclk(crtc);
4206 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004207 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4208 refclk = dev_priv->lvds_ssc_freq * 1000;
4209 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4210 refclk / 1000);
4211 } else if (!IS_GEN2(dev)) {
4212 refclk = 96000;
4213 } else {
4214 refclk = 48000;
4215 }
4216
4217 return refclk;
4218}
4219
Daniel Vetterf47709a2013-03-28 10:42:02 +01004220static void i9xx_adjust_sdvo_tv_clock(struct intel_crtc *crtc)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004221{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004222 unsigned dotclock = crtc->config.adjusted_mode.clock;
4223 struct dpll *clock = &crtc->config.dpll;
4224
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004225 /* SDVO TV has fixed PLL values depend on its clock range,
4226 this mirrors vbios setting. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01004227 if (dotclock >= 100000 && dotclock < 140500) {
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004228 clock->p1 = 2;
4229 clock->p2 = 10;
4230 clock->n = 3;
4231 clock->m1 = 16;
4232 clock->m2 = 8;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004233 } else if (dotclock >= 140500 && dotclock <= 200000) {
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004234 clock->p1 = 1;
4235 clock->p2 = 10;
4236 clock->n = 6;
4237 clock->m1 = 12;
4238 clock->m2 = 8;
4239 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004240
4241 crtc->config.clock_set = true;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004242}
4243
Daniel Vetterf47709a2013-03-28 10:42:02 +01004244static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004245 intel_clock_t *reduced_clock)
4246{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004247 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004248 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004249 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004250 u32 fp, fp2 = 0;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004251 struct dpll *clock = &crtc->config.dpll;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004252
4253 if (IS_PINEVIEW(dev)) {
4254 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
4255 if (reduced_clock)
4256 fp2 = (1 << reduced_clock->n) << 16 |
4257 reduced_clock->m1 << 8 | reduced_clock->m2;
4258 } else {
4259 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4260 if (reduced_clock)
4261 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4262 reduced_clock->m2;
4263 }
4264
4265 I915_WRITE(FP0(pipe), fp);
4266
Daniel Vetterf47709a2013-03-28 10:42:02 +01004267 crtc->lowfreq_avail = false;
4268 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004269 reduced_clock && i915_powersave) {
4270 I915_WRITE(FP1(pipe), fp2);
Daniel Vetterf47709a2013-03-28 10:42:02 +01004271 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004272 } else {
4273 I915_WRITE(FP1(pipe), fp);
4274 }
4275}
4276
Jesse Barnes89b667f2013-04-18 14:51:36 -07004277static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4278{
4279 u32 reg_val;
4280
4281 /*
4282 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4283 * and set it to a reasonable value instead.
4284 */
4285 reg_val = intel_dpio_read(dev_priv, DPIO_IREF(1));
4286 reg_val &= 0xffffff00;
4287 reg_val |= 0x00000030;
4288 intel_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4289
4290 reg_val = intel_dpio_read(dev_priv, DPIO_CALIBRATION);
4291 reg_val &= 0x8cffffff;
4292 reg_val = 0x8c000000;
4293 intel_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4294
4295 reg_val = intel_dpio_read(dev_priv, DPIO_IREF(1));
4296 reg_val &= 0xffffff00;
4297 intel_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4298
4299 reg_val = intel_dpio_read(dev_priv, DPIO_CALIBRATION);
4300 reg_val &= 0x00ffffff;
4301 reg_val |= 0xb0000000;
4302 intel_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4303}
4304
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004305static void intel_dp_set_m_n(struct intel_crtc *crtc)
4306{
4307 if (crtc->config.has_pch_encoder)
4308 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4309 else
4310 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4311}
4312
Daniel Vetterf47709a2013-03-28 10:42:02 +01004313static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004314{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004315 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004316 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004317 struct drm_display_mode *adjusted_mode =
4318 &crtc->config.adjusted_mode;
4319 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004320 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004321 u32 dpll, mdiv;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004322 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004323 bool is_hdmi;
4324 u32 coreclk, reg_val, temp;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004325
Daniel Vetter09153002012-12-12 14:06:44 +01004326 mutex_lock(&dev_priv->dpio_lock);
4327
Jesse Barnes89b667f2013-04-18 14:51:36 -07004328 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004329
Daniel Vetterf47709a2013-03-28 10:42:02 +01004330 bestn = crtc->config.dpll.n;
4331 bestm1 = crtc->config.dpll.m1;
4332 bestm2 = crtc->config.dpll.m2;
4333 bestp1 = crtc->config.dpll.p1;
4334 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004335
Jesse Barnes89b667f2013-04-18 14:51:36 -07004336 /* See eDP HDMI DPIO driver vbios notes doc */
4337
4338 /* PLL B needs special handling */
4339 if (pipe)
4340 vlv_pllb_recal_opamp(dev_priv);
4341
4342 /* Set up Tx target for periodic Rcomp update */
4343 intel_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
4344
4345 /* Disable target IRef on PLL */
4346 reg_val = intel_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
4347 reg_val &= 0x00ffffff;
4348 intel_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
4349
4350 /* Disable fast lock */
4351 intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
4352
4353 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004354 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4355 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4356 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004357 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004358 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI) ||
4359 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4360 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4361 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
4362 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4363
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004364 mdiv |= DPIO_ENABLE_CALIBRATION;
4365 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4366
Jesse Barnes89b667f2013-04-18 14:51:36 -07004367 /* Set HBR and RBR LPF coefficients */
4368 if (adjusted_mode->clock == 162000 ||
4369 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
4370 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
4371 0x005f0021);
4372 else
4373 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
4374 0x00d0000f);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004375
Jesse Barnes89b667f2013-04-18 14:51:36 -07004376 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4377 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4378 /* Use SSC source */
4379 if (!pipe)
4380 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4381 0x0df40000);
4382 else
4383 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4384 0x0df70000);
4385 } else { /* HDMI or VGA */
4386 /* Use bend source */
4387 if (!pipe)
4388 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4389 0x0df70000);
4390 else
4391 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4392 0x0df40000);
4393 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004394
Jesse Barnes89b667f2013-04-18 14:51:36 -07004395 coreclk = intel_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
4396 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4397 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4398 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4399 coreclk |= 0x01000000;
4400 intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
4401
4402 intel_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
4403
4404 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4405 if (encoder->pre_pll_enable)
4406 encoder->pre_pll_enable(encoder);
4407
4408 /* Enable DPIO clock input */
4409 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4410 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4411 if (pipe)
4412 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004413
4414 dpll |= DPLL_VCO_ENABLE;
4415 I915_WRITE(DPLL(pipe), dpll);
4416 POSTING_READ(DPLL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004417 udelay(150);
4418
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004419 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4420 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4421
Jesse Barnes89b667f2013-04-18 14:51:36 -07004422 if (is_hdmi) {
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004423 temp = 0;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004424 if (crtc->config.pixel_multiplier > 1) {
4425 temp = (crtc->config.pixel_multiplier - 1)
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004426 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4427 }
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004428
Jesse Barnes89b667f2013-04-18 14:51:36 -07004429 I915_WRITE(DPLL_MD(pipe), temp);
4430 POSTING_READ(DPLL_MD(pipe));
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304431 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004432
Jesse Barnes89b667f2013-04-18 14:51:36 -07004433 if (crtc->config.has_dp_encoder)
4434 intel_dp_set_m_n(crtc);
Daniel Vetter09153002012-12-12 14:06:44 +01004435
4436 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004437}
4438
Daniel Vetterf47709a2013-03-28 10:42:02 +01004439static void i9xx_update_pll(struct intel_crtc *crtc,
4440 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004441 int num_connectors)
4442{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004443 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004444 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterdafd2262012-11-26 17:22:07 +01004445 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004446 int pipe = crtc->pipe;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004447 u32 dpll;
4448 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004449 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004450
Daniel Vetterf47709a2013-03-28 10:42:02 +01004451 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304452
Daniel Vetterf47709a2013-03-28 10:42:02 +01004453 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4454 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004455
4456 dpll = DPLL_VGA_MODE_DIS;
4457
Daniel Vetterf47709a2013-03-28 10:42:02 +01004458 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004459 dpll |= DPLLB_MODE_LVDS;
4460 else
4461 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004462
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004463 if (is_sdvo) {
Daniel Vetterf47709a2013-03-28 10:42:02 +01004464 if ((crtc->config.pixel_multiplier > 1) &&
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004465 (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))) {
Daniel Vetterf47709a2013-03-28 10:42:02 +01004466 dpll |= (crtc->config.pixel_multiplier - 1)
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004467 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004468 }
4469 dpll |= DPLL_DVO_HIGH_SPEED;
4470 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004471 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004472 dpll |= DPLL_DVO_HIGH_SPEED;
4473
4474 /* compute bitmask from p1 value */
4475 if (IS_PINEVIEW(dev))
4476 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4477 else {
4478 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4479 if (IS_G4X(dev) && reduced_clock)
4480 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4481 }
4482 switch (clock->p2) {
4483 case 5:
4484 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4485 break;
4486 case 7:
4487 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4488 break;
4489 case 10:
4490 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4491 break;
4492 case 14:
4493 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4494 break;
4495 }
4496 if (INTEL_INFO(dev)->gen >= 4)
4497 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4498
Daniel Vetterf47709a2013-03-28 10:42:02 +01004499 if (is_sdvo && intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_TVOUT))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004500 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004501 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_TVOUT))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004502 /* XXX: just matching BIOS for now */
4503 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4504 dpll |= 3;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004505 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004506 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4507 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4508 else
4509 dpll |= PLL_REF_INPUT_DREFCLK;
4510
4511 dpll |= DPLL_VCO_ENABLE;
4512 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4513 POSTING_READ(DPLL(pipe));
4514 udelay(150);
4515
Daniel Vetterf47709a2013-03-28 10:42:02 +01004516 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetterdafd2262012-11-26 17:22:07 +01004517 if (encoder->pre_pll_enable)
4518 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004519
Daniel Vetterf47709a2013-03-28 10:42:02 +01004520 if (crtc->config.has_dp_encoder)
4521 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004522
4523 I915_WRITE(DPLL(pipe), dpll);
4524
4525 /* Wait for the clocks to stabilize. */
4526 POSTING_READ(DPLL(pipe));
4527 udelay(150);
4528
4529 if (INTEL_INFO(dev)->gen >= 4) {
4530 u32 temp = 0;
4531 if (is_sdvo) {
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004532 temp = 0;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004533 if (crtc->config.pixel_multiplier > 1) {
4534 temp = (crtc->config.pixel_multiplier - 1)
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004535 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4536 }
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004537 }
4538 I915_WRITE(DPLL_MD(pipe), temp);
4539 } else {
4540 /* The pixel multiplier can only be updated once the
4541 * DPLL is enabled and the clocks are stable.
4542 *
4543 * So write it again.
4544 */
4545 I915_WRITE(DPLL(pipe), dpll);
4546 }
4547}
4548
Daniel Vetterf47709a2013-03-28 10:42:02 +01004549static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004550 struct drm_display_mode *adjusted_mode,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004551 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004552 int num_connectors)
4553{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004554 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004555 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterdafd2262012-11-26 17:22:07 +01004556 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004557 int pipe = crtc->pipe;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004558 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004559 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004560
Daniel Vetterf47709a2013-03-28 10:42:02 +01004561 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304562
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004563 dpll = DPLL_VGA_MODE_DIS;
4564
Daniel Vetterf47709a2013-03-28 10:42:02 +01004565 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004566 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4567 } else {
4568 if (clock->p1 == 2)
4569 dpll |= PLL_P1_DIVIDE_BY_TWO;
4570 else
4571 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4572 if (clock->p2 == 4)
4573 dpll |= PLL_P2_DIVIDE_BY_4;
4574 }
4575
Daniel Vetterf47709a2013-03-28 10:42:02 +01004576 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004577 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4578 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4579 else
4580 dpll |= PLL_REF_INPUT_DREFCLK;
4581
4582 dpll |= DPLL_VCO_ENABLE;
4583 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4584 POSTING_READ(DPLL(pipe));
4585 udelay(150);
4586
Daniel Vetterf47709a2013-03-28 10:42:02 +01004587 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetterdafd2262012-11-26 17:22:07 +01004588 if (encoder->pre_pll_enable)
4589 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004590
Daniel Vetter5b5896e2012-09-11 12:37:55 +02004591 I915_WRITE(DPLL(pipe), dpll);
4592
4593 /* Wait for the clocks to stabilize. */
4594 POSTING_READ(DPLL(pipe));
4595 udelay(150);
4596
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004597 /* The pixel multiplier can only be updated once the
4598 * DPLL is enabled and the clocks are stable.
4599 *
4600 * So write it again.
4601 */
4602 I915_WRITE(DPLL(pipe), dpll);
4603}
4604
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004605static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
4606 struct drm_display_mode *mode,
4607 struct drm_display_mode *adjusted_mode)
4608{
4609 struct drm_device *dev = intel_crtc->base.dev;
4610 struct drm_i915_private *dev_priv = dev->dev_private;
4611 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004612 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004613 uint32_t vsyncshift;
4614
4615 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4616 /* the chip adds 2 halflines automatically */
4617 adjusted_mode->crtc_vtotal -= 1;
4618 adjusted_mode->crtc_vblank_end -= 1;
4619 vsyncshift = adjusted_mode->crtc_hsync_start
4620 - adjusted_mode->crtc_htotal / 2;
4621 } else {
4622 vsyncshift = 0;
4623 }
4624
4625 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004626 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004627
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004628 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004629 (adjusted_mode->crtc_hdisplay - 1) |
4630 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004631 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004632 (adjusted_mode->crtc_hblank_start - 1) |
4633 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004634 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004635 (adjusted_mode->crtc_hsync_start - 1) |
4636 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4637
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004638 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004639 (adjusted_mode->crtc_vdisplay - 1) |
4640 ((adjusted_mode->crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004641 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004642 (adjusted_mode->crtc_vblank_start - 1) |
4643 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004644 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004645 (adjusted_mode->crtc_vsync_start - 1) |
4646 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4647
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004648 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4649 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4650 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4651 * bits. */
4652 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4653 (pipe == PIPE_B || pipe == PIPE_C))
4654 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4655
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004656 /* pipesrc controls the size that is scaled from, which should
4657 * always be the user's requested size.
4658 */
4659 I915_WRITE(PIPESRC(pipe),
4660 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4661}
4662
Daniel Vetter84b046f2013-02-19 18:48:54 +01004663static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4664{
4665 struct drm_device *dev = intel_crtc->base.dev;
4666 struct drm_i915_private *dev_priv = dev->dev_private;
4667 uint32_t pipeconf;
4668
4669 pipeconf = I915_READ(PIPECONF(intel_crtc->pipe));
4670
4671 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4672 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4673 * core speed.
4674 *
4675 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4676 * pipe == 0 check?
4677 */
4678 if (intel_crtc->config.requested_mode.clock >
4679 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4680 pipeconf |= PIPECONF_DOUBLE_WIDE;
4681 else
4682 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4683 }
4684
4685 /* default to 8bpc */
4686 pipeconf &= ~(PIPECONF_BPC_MASK | PIPECONF_DITHER_EN);
4687 if (intel_crtc->config.has_dp_encoder) {
4688 if (intel_crtc->config.dither) {
4689 pipeconf |= PIPECONF_6BPC |
4690 PIPECONF_DITHER_EN |
4691 PIPECONF_DITHER_TYPE_SP;
4692 }
4693 }
4694
4695 if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(&intel_crtc->base,
4696 INTEL_OUTPUT_EDP)) {
4697 if (intel_crtc->config.dither) {
4698 pipeconf |= PIPECONF_6BPC |
4699 PIPECONF_ENABLE |
4700 I965_PIPECONF_ACTIVE;
4701 }
4702 }
4703
4704 if (HAS_PIPE_CXSR(dev)) {
4705 if (intel_crtc->lowfreq_avail) {
4706 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4707 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4708 } else {
4709 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4710 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4711 }
4712 }
4713
4714 pipeconf &= ~PIPECONF_INTERLACE_MASK;
4715 if (!IS_GEN2(dev) &&
4716 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4717 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4718 else
4719 pipeconf |= PIPECONF_PROGRESSIVE;
4720
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03004721 if (IS_VALLEYVIEW(dev)) {
4722 if (intel_crtc->config.limited_color_range)
4723 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
4724 else
4725 pipeconf &= ~PIPECONF_COLOR_RANGE_SELECT;
4726 }
4727
Daniel Vetter84b046f2013-02-19 18:48:54 +01004728 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4729 POSTING_READ(PIPECONF(intel_crtc->pipe));
4730}
4731
Eric Anholtf564048e2011-03-30 13:01:02 -07004732static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004733 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004734 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004735{
4736 struct drm_device *dev = crtc->dev;
4737 struct drm_i915_private *dev_priv = dev->dev_private;
4738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004739 struct drm_display_mode *adjusted_mode =
4740 &intel_crtc->config.adjusted_mode;
4741 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08004742 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004743 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004744 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004745 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004746 u32 dspcntr;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004747 bool ok, has_reduced_clock = false, is_sdvo = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01004748 bool is_lvds = false, is_tv = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004749 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004750 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004751 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004752
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02004753 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004754 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004755 case INTEL_OUTPUT_LVDS:
4756 is_lvds = true;
4757 break;
4758 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08004759 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08004760 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01004761 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004762 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004763 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004764 case INTEL_OUTPUT_TVOUT:
4765 is_tv = true;
4766 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004767 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004768
Eric Anholtc751ce42010-03-25 11:48:48 -07004769 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004770 }
4771
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004772 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004773
Ma Lingd4906092009-03-18 20:13:27 +08004774 /*
4775 * Returns a set of divisors for the desired target clock with the given
4776 * refclk, or FALSE. The returned values represent the clock equation:
4777 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4778 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004779 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08004780 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4781 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004782 if (!ok) {
4783 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004784 return -EINVAL;
4785 }
4786
4787 /* Ensure that the cursor is valid for the new mode before changing... */
4788 intel_crtc_update_cursor(crtc, true);
4789
4790 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004791 /*
4792 * Ensure we match the reduced clock's P to the target clock.
4793 * If the clocks don't match, we can't switch the display clock
4794 * by using the FP0/FP1. In such case we will disable the LVDS
4795 * downclock feature.
4796 */
Eric Anholtf564048e2011-03-30 13:01:02 -07004797 has_reduced_clock = limit->find_pll(limit, crtc,
4798 dev_priv->lvds_downclock,
4799 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08004800 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004801 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004802 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004803 /* Compat-code for transition, will disappear. */
4804 if (!intel_crtc->config.clock_set) {
4805 intel_crtc->config.dpll.n = clock.n;
4806 intel_crtc->config.dpll.m1 = clock.m1;
4807 intel_crtc->config.dpll.m2 = clock.m2;
4808 intel_crtc->config.dpll.p1 = clock.p1;
4809 intel_crtc->config.dpll.p2 = clock.p2;
4810 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004811
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004812 if (is_sdvo && is_tv)
Daniel Vetterf47709a2013-03-28 10:42:02 +01004813 i9xx_adjust_sdvo_tv_clock(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004814
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004815 if (IS_GEN2(dev))
Daniel Vetterf47709a2013-03-28 10:42:02 +01004816 i8xx_update_pll(intel_crtc, adjusted_mode,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304817 has_reduced_clock ? &reduced_clock : NULL,
4818 num_connectors);
Jesse Barnesa0c4da22012-06-15 11:55:13 -07004819 else if (IS_VALLEYVIEW(dev))
Daniel Vetterf47709a2013-03-28 10:42:02 +01004820 vlv_update_pll(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004821 else
Daniel Vetterf47709a2013-03-28 10:42:02 +01004822 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004823 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07004824 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004825
Eric Anholtf564048e2011-03-30 13:01:02 -07004826 /* Set up the display plane register */
4827 dspcntr = DISPPLANE_GAMMA_ENABLE;
4828
Jesse Barnesda6ecc52013-03-08 10:46:00 -08004829 if (!IS_VALLEYVIEW(dev)) {
4830 if (pipe == 0)
4831 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4832 else
4833 dspcntr |= DISPPLANE_SEL_PIPE_B;
4834 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004835
Ville Syrjälä2582a852013-04-17 17:48:47 +03004836 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe_name(pipe));
Eric Anholtf564048e2011-03-30 13:01:02 -07004837 drm_mode_debug_printmodeline(mode);
4838
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004839 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Eric Anholtf564048e2011-03-30 13:01:02 -07004840
4841 /* pipesrc and dspsize control the size that is scaled from,
4842 * which should always be the user's requested size.
4843 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004844 I915_WRITE(DSPSIZE(plane),
4845 ((mode->vdisplay - 1) << 16) |
4846 (mode->hdisplay - 1));
4847 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004848
Daniel Vetter84b046f2013-02-19 18:48:54 +01004849 i9xx_set_pipeconf(intel_crtc);
4850
Eric Anholtf564048e2011-03-30 13:01:02 -07004851 I915_WRITE(DSPCNTR(plane), dspcntr);
4852 POSTING_READ(DSPCNTR(plane));
4853
Daniel Vetter94352cf2012-07-05 22:51:56 +02004854 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004855
4856 intel_update_watermarks(dev);
4857
Eric Anholtf564048e2011-03-30 13:01:02 -07004858 return ret;
4859}
4860
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004861static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4862 struct intel_crtc_config *pipe_config)
4863{
4864 struct drm_device *dev = crtc->base.dev;
4865 struct drm_i915_private *dev_priv = dev->dev_private;
4866 uint32_t tmp;
4867
4868 tmp = I915_READ(PIPECONF(crtc->pipe));
4869 if (!(tmp & PIPECONF_ENABLE))
4870 return false;
4871
4872 return true;
4873}
4874
Paulo Zanonidde86e22012-12-01 12:04:25 -02004875static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07004876{
4877 struct drm_i915_private *dev_priv = dev->dev_private;
4878 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004879 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004880 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004881 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004882 bool has_cpu_edp = false;
4883 bool has_pch_edp = false;
4884 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07004885 bool has_ck505 = false;
4886 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004887
4888 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07004889 list_for_each_entry(encoder, &mode_config->encoder_list,
4890 base.head) {
4891 switch (encoder->type) {
4892 case INTEL_OUTPUT_LVDS:
4893 has_panel = true;
4894 has_lvds = true;
4895 break;
4896 case INTEL_OUTPUT_EDP:
4897 has_panel = true;
4898 if (intel_encoder_is_pch_edp(&encoder->base))
4899 has_pch_edp = true;
4900 else
4901 has_cpu_edp = true;
4902 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004903 }
4904 }
4905
Keith Packard99eb6a02011-09-26 14:29:12 -07004906 if (HAS_PCH_IBX(dev)) {
4907 has_ck505 = dev_priv->display_clock_mode;
4908 can_ssc = has_ck505;
4909 } else {
4910 has_ck505 = false;
4911 can_ssc = true;
4912 }
4913
4914 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4915 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4916 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004917
4918 /* Ironlake: try to setup display ref clock before DPLL
4919 * enabling. This is only under driver's control after
4920 * PCH B stepping, previous chipset stepping should be
4921 * ignoring this setting.
4922 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004923 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004924
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004925 /* As we must carefully and slowly disable/enable each source in turn,
4926 * compute the final state we want first and check if we need to
4927 * make any changes at all.
4928 */
4929 final = val;
4930 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07004931 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004932 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07004933 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004934 final |= DREF_NONSPREAD_SOURCE_ENABLE;
4935
4936 final &= ~DREF_SSC_SOURCE_MASK;
4937 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4938 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004939
Keith Packard199e5d72011-09-22 12:01:57 -07004940 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004941 final |= DREF_SSC_SOURCE_ENABLE;
4942
4943 if (intel_panel_use_ssc(dev_priv) && can_ssc)
4944 final |= DREF_SSC1_ENABLE;
4945
4946 if (has_cpu_edp) {
4947 if (intel_panel_use_ssc(dev_priv) && can_ssc)
4948 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4949 else
4950 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
4951 } else
4952 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4953 } else {
4954 final |= DREF_SSC_SOURCE_DISABLE;
4955 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4956 }
4957
4958 if (final == val)
4959 return;
4960
4961 /* Always enable nonspread source */
4962 val &= ~DREF_NONSPREAD_SOURCE_MASK;
4963
4964 if (has_ck505)
4965 val |= DREF_NONSPREAD_CK505_ENABLE;
4966 else
4967 val |= DREF_NONSPREAD_SOURCE_ENABLE;
4968
4969 if (has_panel) {
4970 val &= ~DREF_SSC_SOURCE_MASK;
4971 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004972
Keith Packard199e5d72011-09-22 12:01:57 -07004973 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07004974 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004975 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004976 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02004977 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004978 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004979
4980 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004981 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07004982 POSTING_READ(PCH_DREF_CONTROL);
4983 udelay(200);
4984
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004985 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004986
4987 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07004988 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07004989 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004990 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004991 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004992 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07004993 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004994 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004995 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004996 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004997
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004998 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07004999 POSTING_READ(PCH_DREF_CONTROL);
5000 udelay(200);
5001 } else {
5002 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5003
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005004 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07005005
5006 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005007 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005008
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005009 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07005010 POSTING_READ(PCH_DREF_CONTROL);
5011 udelay(200);
5012
5013 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005014 val &= ~DREF_SSC_SOURCE_MASK;
5015 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005016
5017 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005018 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07005019
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005020 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005021 POSTING_READ(PCH_DREF_CONTROL);
5022 udelay(200);
5023 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07005024
5025 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005026}
5027
Paulo Zanonidde86e22012-12-01 12:04:25 -02005028/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
5029static void lpt_init_pch_refclk(struct drm_device *dev)
5030{
5031 struct drm_i915_private *dev_priv = dev->dev_private;
5032 struct drm_mode_config *mode_config = &dev->mode_config;
5033 struct intel_encoder *encoder;
5034 bool has_vga = false;
5035 bool is_sdv = false;
5036 u32 tmp;
5037
5038 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5039 switch (encoder->type) {
5040 case INTEL_OUTPUT_ANALOG:
5041 has_vga = true;
5042 break;
5043 }
5044 }
5045
5046 if (!has_vga)
5047 return;
5048
Daniel Vetterc00db242013-01-22 15:33:27 +01005049 mutex_lock(&dev_priv->dpio_lock);
5050
Paulo Zanonidde86e22012-12-01 12:04:25 -02005051 /* XXX: Rip out SDV support once Haswell ships for real. */
5052 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
5053 is_sdv = true;
5054
5055 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5056 tmp &= ~SBI_SSCCTL_DISABLE;
5057 tmp |= SBI_SSCCTL_PATHALT;
5058 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5059
5060 udelay(24);
5061
5062 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5063 tmp &= ~SBI_SSCCTL_PATHALT;
5064 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5065
5066 if (!is_sdv) {
5067 tmp = I915_READ(SOUTH_CHICKEN2);
5068 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5069 I915_WRITE(SOUTH_CHICKEN2, tmp);
5070
5071 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
5072 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5073 DRM_ERROR("FDI mPHY reset assert timeout\n");
5074
5075 tmp = I915_READ(SOUTH_CHICKEN2);
5076 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5077 I915_WRITE(SOUTH_CHICKEN2, tmp);
5078
5079 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
5080 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
5081 100))
5082 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5083 }
5084
5085 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5086 tmp &= ~(0xFF << 24);
5087 tmp |= (0x12 << 24);
5088 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5089
Paulo Zanonidde86e22012-12-01 12:04:25 -02005090 if (is_sdv) {
5091 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5092 tmp |= 0x7FFF;
5093 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5094 }
5095
5096 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5097 tmp |= (1 << 11);
5098 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5099
5100 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5101 tmp |= (1 << 11);
5102 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5103
5104 if (is_sdv) {
5105 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5106 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5107 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5108
5109 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5110 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5111 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5112
5113 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5114 tmp |= (0x3F << 8);
5115 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5116
5117 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5118 tmp |= (0x3F << 8);
5119 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5120 }
5121
5122 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5123 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5124 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5125
5126 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5127 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5128 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5129
5130 if (!is_sdv) {
5131 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5132 tmp &= ~(7 << 13);
5133 tmp |= (5 << 13);
5134 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5135
5136 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5137 tmp &= ~(7 << 13);
5138 tmp |= (5 << 13);
5139 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5140 }
5141
5142 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5143 tmp &= ~0xFF;
5144 tmp |= 0x1C;
5145 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5146
5147 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5148 tmp &= ~0xFF;
5149 tmp |= 0x1C;
5150 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5151
5152 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5153 tmp &= ~(0xFF << 16);
5154 tmp |= (0x1C << 16);
5155 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5156
5157 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5158 tmp &= ~(0xFF << 16);
5159 tmp |= (0x1C << 16);
5160 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5161
5162 if (!is_sdv) {
5163 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5164 tmp |= (1 << 27);
5165 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5166
5167 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5168 tmp |= (1 << 27);
5169 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5170
5171 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5172 tmp &= ~(0xF << 28);
5173 tmp |= (4 << 28);
5174 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5175
5176 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5177 tmp &= ~(0xF << 28);
5178 tmp |= (4 << 28);
5179 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5180 }
5181
5182 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5183 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5184 tmp |= SBI_DBUFF0_ENABLE;
5185 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005186
5187 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005188}
5189
5190/*
5191 * Initialize reference clocks when the driver loads
5192 */
5193void intel_init_pch_refclk(struct drm_device *dev)
5194{
5195 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5196 ironlake_init_pch_refclk(dev);
5197 else if (HAS_PCH_LPT(dev))
5198 lpt_init_pch_refclk(dev);
5199}
5200
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005201static int ironlake_get_refclk(struct drm_crtc *crtc)
5202{
5203 struct drm_device *dev = crtc->dev;
5204 struct drm_i915_private *dev_priv = dev->dev_private;
5205 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005206 struct intel_encoder *edp_encoder = NULL;
5207 int num_connectors = 0;
5208 bool is_lvds = false;
5209
Daniel Vetter6c2b7c122012-07-05 09:50:24 +02005210 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005211 switch (encoder->type) {
5212 case INTEL_OUTPUT_LVDS:
5213 is_lvds = true;
5214 break;
5215 case INTEL_OUTPUT_EDP:
5216 edp_encoder = encoder;
5217 break;
5218 }
5219 num_connectors++;
5220 }
5221
5222 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5223 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5224 dev_priv->lvds_ssc_freq);
5225 return dev_priv->lvds_ssc_freq * 1000;
5226 }
5227
5228 return 120000;
5229}
5230
Paulo Zanonic8203562012-09-12 10:06:29 -03005231static void ironlake_set_pipeconf(struct drm_crtc *crtc,
5232 struct drm_display_mode *adjusted_mode,
5233 bool dither)
5234{
5235 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5237 int pipe = intel_crtc->pipe;
5238 uint32_t val;
5239
5240 val = I915_READ(PIPECONF(pipe));
5241
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005242 val &= ~PIPECONF_BPC_MASK;
Daniel Vetter965e0c42013-03-27 00:44:57 +01005243 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005244 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005245 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005246 break;
5247 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005248 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005249 break;
5250 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005251 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005252 break;
5253 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005254 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005255 break;
5256 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005257 /* Case prevented by intel_choose_pipe_bpp_dither. */
5258 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005259 }
5260
5261 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5262 if (dither)
5263 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5264
5265 val &= ~PIPECONF_INTERLACE_MASK;
5266 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5267 val |= PIPECONF_INTERLACED_ILK;
5268 else
5269 val |= PIPECONF_PROGRESSIVE;
5270
Daniel Vetter50f3b012013-03-27 00:44:56 +01005271 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005272 val |= PIPECONF_COLOR_RANGE_SELECT;
5273 else
5274 val &= ~PIPECONF_COLOR_RANGE_SELECT;
5275
Paulo Zanonic8203562012-09-12 10:06:29 -03005276 I915_WRITE(PIPECONF(pipe), val);
5277 POSTING_READ(PIPECONF(pipe));
5278}
5279
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005280/*
5281 * Set up the pipe CSC unit.
5282 *
5283 * Currently only full range RGB to limited range RGB conversion
5284 * is supported, but eventually this should handle various
5285 * RGB<->YCbCr scenarios as well.
5286 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005287static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005288{
5289 struct drm_device *dev = crtc->dev;
5290 struct drm_i915_private *dev_priv = dev->dev_private;
5291 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5292 int pipe = intel_crtc->pipe;
5293 uint16_t coeff = 0x7800; /* 1.0 */
5294
5295 /*
5296 * TODO: Check what kind of values actually come out of the pipe
5297 * with these coeff/postoff values and adjust to get the best
5298 * accuracy. Perhaps we even need to take the bpc value into
5299 * consideration.
5300 */
5301
Daniel Vetter50f3b012013-03-27 00:44:56 +01005302 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005303 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5304
5305 /*
5306 * GY/GU and RY/RU should be the other way around according
5307 * to BSpec, but reality doesn't agree. Just set them up in
5308 * a way that results in the correct picture.
5309 */
5310 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5311 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5312
5313 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5314 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5315
5316 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5317 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5318
5319 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5320 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5321 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5322
5323 if (INTEL_INFO(dev)->gen > 6) {
5324 uint16_t postoff = 0;
5325
Daniel Vetter50f3b012013-03-27 00:44:56 +01005326 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005327 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5328
5329 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5330 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5331 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5332
5333 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5334 } else {
5335 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5336
Daniel Vetter50f3b012013-03-27 00:44:56 +01005337 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005338 mode |= CSC_BLACK_SCREEN_OFFSET;
5339
5340 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5341 }
5342}
5343
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005344static void haswell_set_pipeconf(struct drm_crtc *crtc,
5345 struct drm_display_mode *adjusted_mode,
5346 bool dither)
5347{
5348 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005350 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005351 uint32_t val;
5352
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005353 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005354
5355 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5356 if (dither)
5357 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5358
5359 val &= ~PIPECONF_INTERLACE_MASK_HSW;
5360 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5361 val |= PIPECONF_INTERLACED_ILK;
5362 else
5363 val |= PIPECONF_PROGRESSIVE;
5364
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005365 I915_WRITE(PIPECONF(cpu_transcoder), val);
5366 POSTING_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005367}
5368
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005369static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5370 struct drm_display_mode *adjusted_mode,
5371 intel_clock_t *clock,
5372 bool *has_reduced_clock,
5373 intel_clock_t *reduced_clock)
5374{
5375 struct drm_device *dev = crtc->dev;
5376 struct drm_i915_private *dev_priv = dev->dev_private;
5377 struct intel_encoder *intel_encoder;
5378 int refclk;
5379 const intel_limit_t *limit;
5380 bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
5381
5382 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5383 switch (intel_encoder->type) {
5384 case INTEL_OUTPUT_LVDS:
5385 is_lvds = true;
5386 break;
5387 case INTEL_OUTPUT_SDVO:
5388 case INTEL_OUTPUT_HDMI:
5389 is_sdvo = true;
5390 if (intel_encoder->needs_tv_clock)
5391 is_tv = true;
5392 break;
5393 case INTEL_OUTPUT_TVOUT:
5394 is_tv = true;
5395 break;
5396 }
5397 }
5398
5399 refclk = ironlake_get_refclk(crtc);
5400
5401 /*
5402 * Returns a set of divisors for the desired target clock with the given
5403 * refclk, or FALSE. The returned values represent the clock equation:
5404 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5405 */
5406 limit = intel_limit(crtc, refclk);
5407 ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5408 clock);
5409 if (!ret)
5410 return false;
5411
5412 if (is_lvds && dev_priv->lvds_downclock_avail) {
5413 /*
5414 * Ensure we match the reduced clock's P to the target clock.
5415 * If the clocks don't match, we can't switch the display clock
5416 * by using the FP0/FP1. In such case we will disable the LVDS
5417 * downclock feature.
5418 */
5419 *has_reduced_clock = limit->find_pll(limit, crtc,
5420 dev_priv->lvds_downclock,
5421 refclk,
5422 clock,
5423 reduced_clock);
5424 }
5425
5426 if (is_sdvo && is_tv)
Daniel Vetterf47709a2013-03-28 10:42:02 +01005427 i9xx_adjust_sdvo_tv_clock(to_intel_crtc(crtc));
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005428
5429 return true;
5430}
5431
Daniel Vetter01a415f2012-10-27 15:58:40 +02005432static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5433{
5434 struct drm_i915_private *dev_priv = dev->dev_private;
5435 uint32_t temp;
5436
5437 temp = I915_READ(SOUTH_CHICKEN1);
5438 if (temp & FDI_BC_BIFURCATION_SELECT)
5439 return;
5440
5441 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5442 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5443
5444 temp |= FDI_BC_BIFURCATION_SELECT;
5445 DRM_DEBUG_KMS("enabling fdi C rx\n");
5446 I915_WRITE(SOUTH_CHICKEN1, temp);
5447 POSTING_READ(SOUTH_CHICKEN1);
5448}
5449
5450static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
5451{
5452 struct drm_device *dev = intel_crtc->base.dev;
5453 struct drm_i915_private *dev_priv = dev->dev_private;
5454 struct intel_crtc *pipe_B_crtc =
5455 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5456
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005457 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5458 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
Daniel Vetter01a415f2012-10-27 15:58:40 +02005459 if (intel_crtc->fdi_lanes > 4) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005460 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5461 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
Daniel Vetter01a415f2012-10-27 15:58:40 +02005462 /* Clamp lanes to avoid programming the hw with bogus values. */
5463 intel_crtc->fdi_lanes = 4;
5464
5465 return false;
5466 }
5467
Ben Widawsky7eb552a2013-03-13 14:05:41 -07005468 if (INTEL_INFO(dev)->num_pipes == 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005469 return true;
5470
5471 switch (intel_crtc->pipe) {
5472 case PIPE_A:
5473 return true;
5474 case PIPE_B:
5475 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5476 intel_crtc->fdi_lanes > 2) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005477 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5478 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
Daniel Vetter01a415f2012-10-27 15:58:40 +02005479 /* Clamp lanes to avoid programming the hw with bogus values. */
5480 intel_crtc->fdi_lanes = 2;
5481
5482 return false;
5483 }
5484
5485 if (intel_crtc->fdi_lanes > 2)
5486 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5487 else
5488 cpt_enable_fdi_bc_bifurcation(dev);
5489
5490 return true;
5491 case PIPE_C:
5492 if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
5493 if (intel_crtc->fdi_lanes > 2) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005494 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5495 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
Daniel Vetter01a415f2012-10-27 15:58:40 +02005496 /* Clamp lanes to avoid programming the hw with bogus values. */
5497 intel_crtc->fdi_lanes = 2;
5498
5499 return false;
5500 }
5501 } else {
5502 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5503 return false;
5504 }
5505
5506 cpt_enable_fdi_bc_bifurcation(dev);
5507
5508 return true;
5509 default:
5510 BUG();
5511 }
5512}
5513
Paulo Zanonid4b19312012-11-29 11:29:32 -02005514int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5515{
5516 /*
5517 * Account for spread spectrum to avoid
5518 * oversubscribing the link. Max center spread
5519 * is 2.5%; use 5% for safety's sake.
5520 */
5521 u32 bps = target_clock * bpp * 21 / 20;
5522 return bps / (link_bw * 8) + 1;
5523}
5524
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005525void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5526 struct intel_link_m_n *m_n)
5527{
5528 struct drm_device *dev = crtc->base.dev;
5529 struct drm_i915_private *dev_priv = dev->dev_private;
5530 int pipe = crtc->pipe;
5531
5532 I915_WRITE(TRANSDATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5533 I915_WRITE(TRANSDATA_N1(pipe), m_n->gmch_n);
5534 I915_WRITE(TRANSDPLINK_M1(pipe), m_n->link_m);
5535 I915_WRITE(TRANSDPLINK_N1(pipe), m_n->link_n);
5536}
5537
5538void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5539 struct intel_link_m_n *m_n)
5540{
5541 struct drm_device *dev = crtc->base.dev;
5542 struct drm_i915_private *dev_priv = dev->dev_private;
5543 int pipe = crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005544 enum transcoder transcoder = crtc->config.cpu_transcoder;
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005545
5546 if (INTEL_INFO(dev)->gen >= 5) {
5547 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5548 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5549 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5550 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5551 } else {
5552 I915_WRITE(PIPE_GMCH_DATA_M(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5553 I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n->gmch_n);
5554 I915_WRITE(PIPE_DP_LINK_M(pipe), m_n->link_m);
5555 I915_WRITE(PIPE_DP_LINK_N(pipe), m_n->link_n);
5556 }
5557}
5558
5559static void ironlake_fdi_set_m_n(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08005560{
5561 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08005562 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005563 struct drm_display_mode *adjusted_mode =
5564 &intel_crtc->config.adjusted_mode;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005565 struct intel_link_m_n m_n = {0};
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005566 int target_clock, lane, link_bw;
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005567
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005568 /* FDI is a binary signal running at ~2.7GHz, encoding
5569 * each output octet as 10 bits. The actual frequency
5570 * is stored as a divider into a 100MHz clock, and the
5571 * mode pixel clock is stored in units of 1KHz.
5572 * Hence the bw of each lane in terms of the mode signal
5573 * is:
5574 */
5575 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005576
Daniel Vetterdf92b1e2013-03-28 10:41:58 +01005577 if (intel_crtc->config.pixel_target_clock)
5578 target_clock = intel_crtc->config.pixel_target_clock;
Daniel Vetter94bf2ce2012-06-04 18:39:19 +02005579 else
5580 target_clock = adjusted_mode->clock;
5581
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005582 lane = ironlake_get_lanes_required(target_clock, link_bw,
5583 intel_crtc->config.pipe_bpp);
Eric Anholt8febb292011-03-30 13:01:07 -07005584
5585 intel_crtc->fdi_lanes = lane;
5586
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005587 if (intel_crtc->config.pixel_multiplier > 1)
5588 link_bw *= intel_crtc->config.pixel_multiplier;
Daniel Vetter965e0c42013-03-27 00:44:57 +01005589 intel_link_compute_m_n(intel_crtc->config.pipe_bpp, lane, target_clock,
5590 link_bw, &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07005591
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005592 intel_cpu_transcoder_set_m_n(intel_crtc, &m_n);
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005593}
5594
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005595static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005596 intel_clock_t *clock, u32 *fp,
5597 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005598{
5599 struct drm_crtc *crtc = &intel_crtc->base;
5600 struct drm_device *dev = crtc->dev;
5601 struct drm_i915_private *dev_priv = dev->dev_private;
5602 struct intel_encoder *intel_encoder;
5603 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005604 int factor, num_connectors = 0;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005605 bool is_lvds = false, is_sdvo = false, is_tv = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005606
5607 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5608 switch (intel_encoder->type) {
5609 case INTEL_OUTPUT_LVDS:
5610 is_lvds = true;
5611 break;
5612 case INTEL_OUTPUT_SDVO:
5613 case INTEL_OUTPUT_HDMI:
5614 is_sdvo = true;
5615 if (intel_encoder->needs_tv_clock)
5616 is_tv = true;
5617 break;
5618 case INTEL_OUTPUT_TVOUT:
5619 is_tv = true;
5620 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005621 }
5622
5623 num_connectors++;
5624 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005625
Chris Wilsonc1858122010-12-03 21:35:48 +00005626 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005627 factor = 21;
5628 if (is_lvds) {
5629 if ((intel_panel_use_ssc(dev_priv) &&
5630 dev_priv->lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005631 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005632 factor = 25;
5633 } else if (is_sdvo && is_tv)
5634 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005635
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005636 if (clock->m < factor * clock->n)
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005637 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005638
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005639 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5640 *fp2 |= FP_CB_TUNE;
5641
Chris Wilson5eddb702010-09-11 13:48:45 +01005642 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005643
Eric Anholta07d6782011-03-30 13:01:08 -07005644 if (is_lvds)
5645 dpll |= DPLLB_MODE_LVDS;
5646 else
5647 dpll |= DPLLB_MODE_DAC_SERIAL;
5648 if (is_sdvo) {
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005649 if (intel_crtc->config.pixel_multiplier > 1) {
5650 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5651 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08005652 }
Eric Anholta07d6782011-03-30 13:01:08 -07005653 dpll |= DPLL_DVO_HIGH_SPEED;
5654 }
Daniel Vetter8b470472013-03-28 10:41:59 +01005655 if (intel_crtc->config.has_dp_encoder &&
5656 intel_crtc->config.has_pch_encoder)
Eric Anholta07d6782011-03-30 13:01:08 -07005657 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005658
Eric Anholta07d6782011-03-30 13:01:08 -07005659 /* compute bitmask from p1 value */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005660 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005661 /* also FPA1 */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005662 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005663
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005664 switch (clock->p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005665 case 5:
5666 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5667 break;
5668 case 7:
5669 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5670 break;
5671 case 10:
5672 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5673 break;
5674 case 14:
5675 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5676 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005677 }
5678
5679 if (is_sdvo && is_tv)
5680 dpll |= PLL_REF_INPUT_TVCLKINBC;
5681 else if (is_tv)
5682 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005683 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08005684 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00005685 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005686 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005687 else
5688 dpll |= PLL_REF_INPUT_DREFCLK;
5689
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005690 return dpll;
5691}
5692
Jesse Barnes79e53942008-11-07 14:24:08 -08005693static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005694 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005695 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005696{
5697 struct drm_device *dev = crtc->dev;
5698 struct drm_i915_private *dev_priv = dev->dev_private;
5699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005700 struct drm_display_mode *adjusted_mode =
5701 &intel_crtc->config.adjusted_mode;
5702 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08005703 int pipe = intel_crtc->pipe;
5704 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005705 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005706 intel_clock_t clock, reduced_clock;
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005707 u32 dpll, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005708 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005709 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005710 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005711 int ret;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005712 bool dither, fdi_config_ok;
Jesse Barnes79e53942008-11-07 14:24:08 -08005713
5714 for_each_encoder_on_crtc(dev, crtc, encoder) {
5715 switch (encoder->type) {
5716 case INTEL_OUTPUT_LVDS:
5717 is_lvds = true;
5718 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005719 }
5720
5721 num_connectors++;
5722 }
5723
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005724 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5725 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5726
Daniel Vetter3b117c82013-04-17 20:15:07 +02005727 intel_crtc->config.cpu_transcoder = pipe;
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005728
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005729 ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
5730 &has_reduced_clock, &reduced_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005731 if (!ok) {
5732 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5733 return -EINVAL;
5734 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01005735 /* Compat-code for transition, will disappear. */
5736 if (!intel_crtc->config.clock_set) {
5737 intel_crtc->config.dpll.n = clock.n;
5738 intel_crtc->config.dpll.m1 = clock.m1;
5739 intel_crtc->config.dpll.m2 = clock.m2;
5740 intel_crtc->config.dpll.p1 = clock.p1;
5741 intel_crtc->config.dpll.p2 = clock.p2;
5742 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005743
5744 /* Ensure that the cursor is valid for the new mode before changing... */
5745 intel_crtc_update_cursor(crtc, true);
5746
Jesse Barnes79e53942008-11-07 14:24:08 -08005747 /* determine panel color depth */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01005748 dither = intel_crtc->config.dither;
Paulo Zanonic8203562012-09-12 10:06:29 -03005749 if (is_lvds && dev_priv->lvds_dither)
5750 dither = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005751
Jesse Barnes79e53942008-11-07 14:24:08 -08005752 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5753 if (has_reduced_clock)
5754 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5755 reduced_clock.m2;
5756
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005757 dpll = ironlake_compute_dpll(intel_crtc, &clock, &fp, &reduced_clock,
5758 has_reduced_clock ? &fp2 : NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08005759
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005760 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08005761 drm_mode_debug_printmodeline(mode);
5762
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005763 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01005764 if (intel_crtc->config.has_pch_encoder) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005765 struct intel_pch_pll *pll;
Chris Wilson5eddb702010-09-11 13:48:45 +01005766
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005767 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5768 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005769 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5770 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07005771 return -EINVAL;
5772 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005773 } else
5774 intel_put_pch_pll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005775
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005776 if (intel_crtc->config.has_dp_encoder)
5777 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005778
Daniel Vetterdafd2262012-11-26 17:22:07 +01005779 for_each_encoder_on_crtc(dev, crtc, encoder)
5780 if (encoder->pre_pll_enable)
5781 encoder->pre_pll_enable(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08005782
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005783 if (intel_crtc->pch_pll) {
5784 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005785
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005786 /* Wait for the clocks to stabilize. */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005787 POSTING_READ(intel_crtc->pch_pll->pll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005788 udelay(150);
5789
Eric Anholt8febb292011-03-30 13:01:07 -07005790 /* The pixel multiplier can only be updated once the
5791 * DPLL is enabled and the clocks are stable.
5792 *
5793 * So write it again.
5794 */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005795 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08005796 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005797
Chris Wilson5eddb702010-09-11 13:48:45 +01005798 intel_crtc->lowfreq_avail = false;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005799 if (intel_crtc->pch_pll) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07005800 if (is_lvds && has_reduced_clock && i915_powersave) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005801 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005802 intel_crtc->lowfreq_avail = true;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005803 } else {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005804 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07005805 }
5806 }
5807
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005808 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005809
Daniel Vetter01a415f2012-10-27 15:58:40 +02005810 /* Note, this also computes intel_crtc->fdi_lanes which is used below in
5811 * ironlake_check_fdi_lanes. */
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005812 intel_crtc->fdi_lanes = 0;
5813 if (intel_crtc->config.has_pch_encoder)
5814 ironlake_fdi_set_m_n(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01005815
Daniel Vetter01a415f2012-10-27 15:58:40 +02005816 fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005817
Paulo Zanonic8203562012-09-12 10:06:29 -03005818 ironlake_set_pipeconf(crtc, adjusted_mode, dither);
Jesse Barnes79e53942008-11-07 14:24:08 -08005819
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005820 /* Set up the display plane register */
5821 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005822 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005823
Daniel Vetter94352cf2012-07-05 22:51:56 +02005824 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005825
5826 intel_update_watermarks(dev);
5827
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03005828 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5829
Daniel Vetter01a415f2012-10-27 15:58:40 +02005830 return fdi_config_ok ? ret : -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005831}
5832
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005833static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5834 struct intel_crtc_config *pipe_config)
5835{
5836 struct drm_device *dev = crtc->base.dev;
5837 struct drm_i915_private *dev_priv = dev->dev_private;
5838 uint32_t tmp;
5839
5840 tmp = I915_READ(PIPECONF(crtc->pipe));
5841 if (!(tmp & PIPECONF_ENABLE))
5842 return false;
5843
Daniel Vetter88adfff2013-03-28 10:42:01 +01005844 if (I915_READ(TRANSCONF(crtc->pipe)) & TRANS_ENABLE)
5845 pipe_config->has_pch_encoder = true;
5846
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005847 return true;
5848}
5849
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005850static void haswell_modeset_global_resources(struct drm_device *dev)
5851{
5852 struct drm_i915_private *dev_priv = dev->dev_private;
5853 bool enable = false;
5854 struct intel_crtc *crtc;
5855 struct intel_encoder *encoder;
5856
5857 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
5858 if (crtc->pipe != PIPE_A && crtc->base.enabled)
5859 enable = true;
5860 /* XXX: Should check for edp transcoder here, but thanks to init
5861 * sequence that's not yet available. Just in case desktop eDP
5862 * on PORT D is possible on haswell, too. */
5863 }
5864
5865 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
5866 base.head) {
5867 if (encoder->type != INTEL_OUTPUT_EDP &&
5868 encoder->connectors_active)
5869 enable = true;
5870 }
5871
5872 /* Even the eDP panel fitter is outside the always-on well. */
5873 if (dev_priv->pch_pf_size)
5874 enable = true;
5875
5876 intel_set_power_well(dev, enable);
5877}
5878
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005879static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005880 int x, int y,
5881 struct drm_framebuffer *fb)
5882{
5883 struct drm_device *dev = crtc->dev;
5884 struct drm_i915_private *dev_priv = dev->dev_private;
5885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005886 struct drm_display_mode *adjusted_mode =
5887 &intel_crtc->config.adjusted_mode;
5888 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005889 int pipe = intel_crtc->pipe;
5890 int plane = intel_crtc->plane;
5891 int num_connectors = 0;
Daniel Vetter8b470472013-03-28 10:41:59 +01005892 bool is_cpu_edp = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005893 struct intel_encoder *encoder;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005894 int ret;
5895 bool dither;
5896
5897 for_each_encoder_on_crtc(dev, crtc, encoder) {
5898 switch (encoder->type) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005899 case INTEL_OUTPUT_EDP:
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005900 if (!intel_encoder_is_pch_edp(&encoder->base))
5901 is_cpu_edp = true;
5902 break;
5903 }
5904
5905 num_connectors++;
5906 }
5907
Daniel Vetterbba21812013-03-22 10:53:40 +01005908 if (is_cpu_edp)
Daniel Vetter3b117c82013-04-17 20:15:07 +02005909 intel_crtc->config.cpu_transcoder = TRANSCODER_EDP;
Daniel Vetterbba21812013-03-22 10:53:40 +01005910 else
Daniel Vetter3b117c82013-04-17 20:15:07 +02005911 intel_crtc->config.cpu_transcoder = pipe;
Daniel Vetterbba21812013-03-22 10:53:40 +01005912
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005913 /* We are not sure yet this won't happen. */
5914 WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
5915 INTEL_PCH_TYPE(dev));
5916
5917 WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
5918 num_connectors, pipe_name(pipe));
5919
Daniel Vetter3b117c82013-04-17 20:15:07 +02005920 WARN_ON(I915_READ(PIPECONF(intel_crtc->config.cpu_transcoder)) &
Paulo Zanoni1ce42922012-10-05 12:06:01 -03005921 (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
5922
5923 WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
5924
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005925 if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
5926 return -EINVAL;
5927
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005928 /* Ensure that the cursor is valid for the new mode before changing... */
5929 intel_crtc_update_cursor(crtc, true);
5930
5931 /* determine panel color depth */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01005932 dither = intel_crtc->config.dither;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005933
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005934 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe_name(pipe));
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005935 drm_mode_debug_printmodeline(mode);
5936
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005937 if (intel_crtc->config.has_dp_encoder)
5938 intel_dp_set_m_n(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005939
5940 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005941
5942 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5943
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005944 if (intel_crtc->config.has_pch_encoder)
5945 ironlake_fdi_set_m_n(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005946
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005947 haswell_set_pipeconf(crtc, adjusted_mode, dither);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005948
Daniel Vetter50f3b012013-03-27 00:44:56 +01005949 intel_set_pipe_csc(crtc);
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005950
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005951 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005952 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005953 POSTING_READ(DSPCNTR(plane));
5954
5955 ret = intel_pipe_set_base(crtc, x, y, fb);
5956
5957 intel_update_watermarks(dev);
5958
5959 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5960
Jesse Barnes79e53942008-11-07 14:24:08 -08005961 return ret;
5962}
5963
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005964static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5965 struct intel_crtc_config *pipe_config)
5966{
5967 struct drm_device *dev = crtc->base.dev;
5968 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2bfce952013-04-18 16:35:40 -03005969 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005970 uint32_t tmp;
5971
Paulo Zanoni2bfce952013-04-18 16:35:40 -03005972 if (!intel_using_power_well(dev_priv->dev) &&
5973 cpu_transcoder != TRANSCODER_EDP)
5974 return false;
5975
5976 tmp = I915_READ(PIPECONF(cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005977 if (!(tmp & PIPECONF_ENABLE))
5978 return false;
5979
Daniel Vetter88adfff2013-03-28 10:42:01 +01005980 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03005981 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01005982 * DDI E. So just check whether this pipe is wired to DDI E and whether
5983 * the PCH transcoder is on.
5984 */
Paulo Zanonif196e6b2013-04-18 16:35:41 -03005985 tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01005986 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
5987 I915_READ(TRANSCONF(PIPE_A)) & TRANS_ENABLE)
5988 pipe_config->has_pch_encoder = true;
5989
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005990 return true;
5991}
5992
Eric Anholtf564048e2011-03-30 13:01:02 -07005993static int intel_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005994 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005995 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07005996{
5997 struct drm_device *dev = crtc->dev;
5998 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01005999 struct drm_encoder_helper_funcs *encoder_funcs;
6000 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07006001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006002 struct drm_display_mode *adjusted_mode =
6003 &intel_crtc->config.adjusted_mode;
6004 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07006005 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006006 int ret;
6007
Eric Anholt0b701d22011-03-30 13:01:03 -07006008 drm_vblank_pre_modeset(dev, pipe);
6009
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01006010 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
6011
Jesse Barnes79e53942008-11-07 14:24:08 -08006012 drm_vblank_post_modeset(dev, pipe);
6013
Daniel Vetter9256aa12012-10-31 19:26:13 +01006014 if (ret != 0)
6015 return ret;
6016
6017 for_each_encoder_on_crtc(dev, crtc, encoder) {
6018 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
6019 encoder->base.base.id,
6020 drm_get_encoder_name(&encoder->base),
6021 mode->base.id, mode->name);
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006022 if (encoder->mode_set) {
6023 encoder->mode_set(encoder);
6024 } else {
6025 encoder_funcs = encoder->base.helper_private;
6026 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
6027 }
Daniel Vetter9256aa12012-10-31 19:26:13 +01006028 }
6029
6030 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006031}
6032
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006033static bool intel_eld_uptodate(struct drm_connector *connector,
6034 int reg_eldv, uint32_t bits_eldv,
6035 int reg_elda, uint32_t bits_elda,
6036 int reg_edid)
6037{
6038 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6039 uint8_t *eld = connector->eld;
6040 uint32_t i;
6041
6042 i = I915_READ(reg_eldv);
6043 i &= bits_eldv;
6044
6045 if (!eld[0])
6046 return !i;
6047
6048 if (!i)
6049 return false;
6050
6051 i = I915_READ(reg_elda);
6052 i &= ~bits_elda;
6053 I915_WRITE(reg_elda, i);
6054
6055 for (i = 0; i < eld[2]; i++)
6056 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6057 return false;
6058
6059 return true;
6060}
6061
Wu Fengguange0dac652011-09-05 14:25:34 +08006062static void g4x_write_eld(struct drm_connector *connector,
6063 struct drm_crtc *crtc)
6064{
6065 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6066 uint8_t *eld = connector->eld;
6067 uint32_t eldv;
6068 uint32_t len;
6069 uint32_t i;
6070
6071 i = I915_READ(G4X_AUD_VID_DID);
6072
6073 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6074 eldv = G4X_ELDV_DEVCL_DEVBLC;
6075 else
6076 eldv = G4X_ELDV_DEVCTG;
6077
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006078 if (intel_eld_uptodate(connector,
6079 G4X_AUD_CNTL_ST, eldv,
6080 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6081 G4X_HDMIW_HDMIEDID))
6082 return;
6083
Wu Fengguange0dac652011-09-05 14:25:34 +08006084 i = I915_READ(G4X_AUD_CNTL_ST);
6085 i &= ~(eldv | G4X_ELD_ADDR);
6086 len = (i >> 9) & 0x1f; /* ELD buffer size */
6087 I915_WRITE(G4X_AUD_CNTL_ST, i);
6088
6089 if (!eld[0])
6090 return;
6091
6092 len = min_t(uint8_t, eld[2], len);
6093 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6094 for (i = 0; i < len; i++)
6095 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6096
6097 i = I915_READ(G4X_AUD_CNTL_ST);
6098 i |= eldv;
6099 I915_WRITE(G4X_AUD_CNTL_ST, i);
6100}
6101
Wang Xingchao83358c852012-08-16 22:43:37 +08006102static void haswell_write_eld(struct drm_connector *connector,
6103 struct drm_crtc *crtc)
6104{
6105 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6106 uint8_t *eld = connector->eld;
6107 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006109 uint32_t eldv;
6110 uint32_t i;
6111 int len;
6112 int pipe = to_intel_crtc(crtc)->pipe;
6113 int tmp;
6114
6115 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6116 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6117 int aud_config = HSW_AUD_CFG(pipe);
6118 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6119
6120
6121 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6122
6123 /* Audio output enable */
6124 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6125 tmp = I915_READ(aud_cntrl_st2);
6126 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6127 I915_WRITE(aud_cntrl_st2, tmp);
6128
6129 /* Wait for 1 vertical blank */
6130 intel_wait_for_vblank(dev, pipe);
6131
6132 /* Set ELD valid state */
6133 tmp = I915_READ(aud_cntrl_st2);
6134 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6135 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6136 I915_WRITE(aud_cntrl_st2, tmp);
6137 tmp = I915_READ(aud_cntrl_st2);
6138 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6139
6140 /* Enable HDMI mode */
6141 tmp = I915_READ(aud_config);
6142 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6143 /* clear N_programing_enable and N_value_index */
6144 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6145 I915_WRITE(aud_config, tmp);
6146
6147 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6148
6149 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006150 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006151
6152 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6153 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6154 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6155 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6156 } else
6157 I915_WRITE(aud_config, 0);
6158
6159 if (intel_eld_uptodate(connector,
6160 aud_cntrl_st2, eldv,
6161 aud_cntl_st, IBX_ELD_ADDRESS,
6162 hdmiw_hdmiedid))
6163 return;
6164
6165 i = I915_READ(aud_cntrl_st2);
6166 i &= ~eldv;
6167 I915_WRITE(aud_cntrl_st2, i);
6168
6169 if (!eld[0])
6170 return;
6171
6172 i = I915_READ(aud_cntl_st);
6173 i &= ~IBX_ELD_ADDRESS;
6174 I915_WRITE(aud_cntl_st, i);
6175 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6176 DRM_DEBUG_DRIVER("port num:%d\n", i);
6177
6178 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6179 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6180 for (i = 0; i < len; i++)
6181 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6182
6183 i = I915_READ(aud_cntrl_st2);
6184 i |= eldv;
6185 I915_WRITE(aud_cntrl_st2, i);
6186
6187}
6188
Wu Fengguange0dac652011-09-05 14:25:34 +08006189static void ironlake_write_eld(struct drm_connector *connector,
6190 struct drm_crtc *crtc)
6191{
6192 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6193 uint8_t *eld = connector->eld;
6194 uint32_t eldv;
6195 uint32_t i;
6196 int len;
6197 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006198 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006199 int aud_cntl_st;
6200 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006201 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006202
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006203 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006204 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6205 aud_config = IBX_AUD_CFG(pipe);
6206 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006207 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006208 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006209 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6210 aud_config = CPT_AUD_CFG(pipe);
6211 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006212 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006213 }
6214
Wang Xingchao9b138a82012-08-09 16:52:18 +08006215 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006216
6217 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006218 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006219 if (!i) {
6220 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6221 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006222 eldv = IBX_ELD_VALIDB;
6223 eldv |= IBX_ELD_VALIDB << 4;
6224 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006225 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006226 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006227 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006228 }
6229
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006230 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6231 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6232 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006233 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6234 } else
6235 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006236
6237 if (intel_eld_uptodate(connector,
6238 aud_cntrl_st2, eldv,
6239 aud_cntl_st, IBX_ELD_ADDRESS,
6240 hdmiw_hdmiedid))
6241 return;
6242
Wu Fengguange0dac652011-09-05 14:25:34 +08006243 i = I915_READ(aud_cntrl_st2);
6244 i &= ~eldv;
6245 I915_WRITE(aud_cntrl_st2, i);
6246
6247 if (!eld[0])
6248 return;
6249
Wu Fengguange0dac652011-09-05 14:25:34 +08006250 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006251 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006252 I915_WRITE(aud_cntl_st, i);
6253
6254 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6255 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6256 for (i = 0; i < len; i++)
6257 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6258
6259 i = I915_READ(aud_cntrl_st2);
6260 i |= eldv;
6261 I915_WRITE(aud_cntrl_st2, i);
6262}
6263
6264void intel_write_eld(struct drm_encoder *encoder,
6265 struct drm_display_mode *mode)
6266{
6267 struct drm_crtc *crtc = encoder->crtc;
6268 struct drm_connector *connector;
6269 struct drm_device *dev = encoder->dev;
6270 struct drm_i915_private *dev_priv = dev->dev_private;
6271
6272 connector = drm_select_eld(encoder, mode);
6273 if (!connector)
6274 return;
6275
6276 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6277 connector->base.id,
6278 drm_get_connector_name(connector),
6279 connector->encoder->base.id,
6280 drm_get_encoder_name(connector->encoder));
6281
6282 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6283
6284 if (dev_priv->display.write_eld)
6285 dev_priv->display.write_eld(connector, crtc);
6286}
6287
Jesse Barnes79e53942008-11-07 14:24:08 -08006288/** Loads the palette/gamma unit for the CRTC with the prepared values */
6289void intel_crtc_load_lut(struct drm_crtc *crtc)
6290{
6291 struct drm_device *dev = crtc->dev;
6292 struct drm_i915_private *dev_priv = dev->dev_private;
6293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006294 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006295 int i;
6296
6297 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006298 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006299 return;
6300
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006301 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006302 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006303 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006304
Jesse Barnes79e53942008-11-07 14:24:08 -08006305 for (i = 0; i < 256; i++) {
6306 I915_WRITE(palreg + 4 * i,
6307 (intel_crtc->lut_r[i] << 16) |
6308 (intel_crtc->lut_g[i] << 8) |
6309 intel_crtc->lut_b[i]);
6310 }
6311}
6312
Chris Wilson560b85b2010-08-07 11:01:38 +01006313static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6314{
6315 struct drm_device *dev = crtc->dev;
6316 struct drm_i915_private *dev_priv = dev->dev_private;
6317 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6318 bool visible = base != 0;
6319 u32 cntl;
6320
6321 if (intel_crtc->cursor_visible == visible)
6322 return;
6323
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006324 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006325 if (visible) {
6326 /* On these chipsets we can only modify the base whilst
6327 * the cursor is disabled.
6328 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006329 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006330
6331 cntl &= ~(CURSOR_FORMAT_MASK);
6332 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6333 cntl |= CURSOR_ENABLE |
6334 CURSOR_GAMMA_ENABLE |
6335 CURSOR_FORMAT_ARGB;
6336 } else
6337 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006338 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006339
6340 intel_crtc->cursor_visible = visible;
6341}
6342
6343static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6344{
6345 struct drm_device *dev = crtc->dev;
6346 struct drm_i915_private *dev_priv = dev->dev_private;
6347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6348 int pipe = intel_crtc->pipe;
6349 bool visible = base != 0;
6350
6351 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006352 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006353 if (base) {
6354 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6355 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6356 cntl |= pipe << 28; /* Connect to correct pipe */
6357 } else {
6358 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6359 cntl |= CURSOR_MODE_DISABLE;
6360 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006361 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006362
6363 intel_crtc->cursor_visible = visible;
6364 }
6365 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006366 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006367}
6368
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006369static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6370{
6371 struct drm_device *dev = crtc->dev;
6372 struct drm_i915_private *dev_priv = dev->dev_private;
6373 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6374 int pipe = intel_crtc->pipe;
6375 bool visible = base != 0;
6376
6377 if (intel_crtc->cursor_visible != visible) {
6378 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6379 if (base) {
6380 cntl &= ~CURSOR_MODE;
6381 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6382 } else {
6383 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6384 cntl |= CURSOR_MODE_DISABLE;
6385 }
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006386 if (IS_HASWELL(dev))
6387 cntl |= CURSOR_PIPE_CSC_ENABLE;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006388 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6389
6390 intel_crtc->cursor_visible = visible;
6391 }
6392 /* and commit changes on next vblank */
6393 I915_WRITE(CURBASE_IVB(pipe), base);
6394}
6395
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006396/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006397static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6398 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006399{
6400 struct drm_device *dev = crtc->dev;
6401 struct drm_i915_private *dev_priv = dev->dev_private;
6402 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6403 int pipe = intel_crtc->pipe;
6404 int x = intel_crtc->cursor_x;
6405 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006406 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006407 bool visible;
6408
6409 pos = 0;
6410
Chris Wilson6b383a72010-09-13 13:54:26 +01006411 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006412 base = intel_crtc->cursor_addr;
6413 if (x > (int) crtc->fb->width)
6414 base = 0;
6415
6416 if (y > (int) crtc->fb->height)
6417 base = 0;
6418 } else
6419 base = 0;
6420
6421 if (x < 0) {
6422 if (x + intel_crtc->cursor_width < 0)
6423 base = 0;
6424
6425 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6426 x = -x;
6427 }
6428 pos |= x << CURSOR_X_SHIFT;
6429
6430 if (y < 0) {
6431 if (y + intel_crtc->cursor_height < 0)
6432 base = 0;
6433
6434 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6435 y = -y;
6436 }
6437 pos |= y << CURSOR_Y_SHIFT;
6438
6439 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006440 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006441 return;
6442
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006443 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006444 I915_WRITE(CURPOS_IVB(pipe), pos);
6445 ivb_update_cursor(crtc, base);
6446 } else {
6447 I915_WRITE(CURPOS(pipe), pos);
6448 if (IS_845G(dev) || IS_I865G(dev))
6449 i845_update_cursor(crtc, base);
6450 else
6451 i9xx_update_cursor(crtc, base);
6452 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006453}
6454
Jesse Barnes79e53942008-11-07 14:24:08 -08006455static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006456 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006457 uint32_t handle,
6458 uint32_t width, uint32_t height)
6459{
6460 struct drm_device *dev = crtc->dev;
6461 struct drm_i915_private *dev_priv = dev->dev_private;
6462 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006463 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006464 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006465 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006466
Jesse Barnes79e53942008-11-07 14:24:08 -08006467 /* if we want to turn off the cursor ignore width and height */
6468 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006469 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006470 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006471 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006472 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006473 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006474 }
6475
6476 /* Currently we only support 64x64 cursors */
6477 if (width != 64 || height != 64) {
6478 DRM_ERROR("we currently only support 64x64 cursors\n");
6479 return -EINVAL;
6480 }
6481
Chris Wilson05394f32010-11-08 19:18:58 +00006482 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006483 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006484 return -ENOENT;
6485
Chris Wilson05394f32010-11-08 19:18:58 +00006486 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006487 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006488 ret = -ENOMEM;
6489 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006490 }
6491
Dave Airlie71acb5e2008-12-30 20:31:46 +10006492 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006493 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006494 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00006495 unsigned alignment;
6496
Chris Wilsond9e86c02010-11-10 16:40:20 +00006497 if (obj->tiling_mode) {
6498 DRM_ERROR("cursor cannot be tiled\n");
6499 ret = -EINVAL;
6500 goto fail_locked;
6501 }
6502
Chris Wilson693db182013-03-05 14:52:39 +00006503 /* Note that the w/a also requires 2 PTE of padding following
6504 * the bo. We currently fill all unused PTE with the shadow
6505 * page and so we should always have valid PTE following the
6506 * cursor preventing the VT-d warning.
6507 */
6508 alignment = 0;
6509 if (need_vtd_wa(dev))
6510 alignment = 64*1024;
6511
6512 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006513 if (ret) {
6514 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006515 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006516 }
6517
Chris Wilsond9e86c02010-11-10 16:40:20 +00006518 ret = i915_gem_object_put_fence(obj);
6519 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006520 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006521 goto fail_unpin;
6522 }
6523
Chris Wilson05394f32010-11-08 19:18:58 +00006524 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006525 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006526 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006527 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006528 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6529 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006530 if (ret) {
6531 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006532 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006533 }
Chris Wilson05394f32010-11-08 19:18:58 +00006534 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006535 }
6536
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006537 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006538 I915_WRITE(CURSIZE, (height << 12) | width);
6539
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006540 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006541 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006542 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006543 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006544 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6545 } else
6546 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006547 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006548 }
Jesse Barnes80824002009-09-10 15:28:06 -07006549
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006550 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006551
6552 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006553 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006554 intel_crtc->cursor_width = width;
6555 intel_crtc->cursor_height = height;
6556
Chris Wilson6b383a72010-09-13 13:54:26 +01006557 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006558
Jesse Barnes79e53942008-11-07 14:24:08 -08006559 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006560fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006561 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006562fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006563 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006564fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006565 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006566 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006567}
6568
6569static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6570{
Jesse Barnes79e53942008-11-07 14:24:08 -08006571 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006572
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006573 intel_crtc->cursor_x = x;
6574 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006575
Chris Wilson6b383a72010-09-13 13:54:26 +01006576 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006577
6578 return 0;
6579}
6580
6581/** Sets the color ramps on behalf of RandR */
6582void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6583 u16 blue, int regno)
6584{
6585 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6586
6587 intel_crtc->lut_r[regno] = red >> 8;
6588 intel_crtc->lut_g[regno] = green >> 8;
6589 intel_crtc->lut_b[regno] = blue >> 8;
6590}
6591
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006592void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6593 u16 *blue, int regno)
6594{
6595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6596
6597 *red = intel_crtc->lut_r[regno] << 8;
6598 *green = intel_crtc->lut_g[regno] << 8;
6599 *blue = intel_crtc->lut_b[regno] << 8;
6600}
6601
Jesse Barnes79e53942008-11-07 14:24:08 -08006602static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006603 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006604{
James Simmons72034252010-08-03 01:33:19 +01006605 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006606 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006607
James Simmons72034252010-08-03 01:33:19 +01006608 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006609 intel_crtc->lut_r[i] = red[i] >> 8;
6610 intel_crtc->lut_g[i] = green[i] >> 8;
6611 intel_crtc->lut_b[i] = blue[i] >> 8;
6612 }
6613
6614 intel_crtc_load_lut(crtc);
6615}
6616
Jesse Barnes79e53942008-11-07 14:24:08 -08006617/* VESA 640x480x72Hz mode to set on the pipe */
6618static struct drm_display_mode load_detect_mode = {
6619 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6620 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6621};
6622
Chris Wilsond2dff872011-04-19 08:36:26 +01006623static struct drm_framebuffer *
6624intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006625 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006626 struct drm_i915_gem_object *obj)
6627{
6628 struct intel_framebuffer *intel_fb;
6629 int ret;
6630
6631 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6632 if (!intel_fb) {
6633 drm_gem_object_unreference_unlocked(&obj->base);
6634 return ERR_PTR(-ENOMEM);
6635 }
6636
6637 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6638 if (ret) {
6639 drm_gem_object_unreference_unlocked(&obj->base);
6640 kfree(intel_fb);
6641 return ERR_PTR(ret);
6642 }
6643
6644 return &intel_fb->base;
6645}
6646
6647static u32
6648intel_framebuffer_pitch_for_width(int width, int bpp)
6649{
6650 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6651 return ALIGN(pitch, 64);
6652}
6653
6654static u32
6655intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6656{
6657 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6658 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6659}
6660
6661static struct drm_framebuffer *
6662intel_framebuffer_create_for_mode(struct drm_device *dev,
6663 struct drm_display_mode *mode,
6664 int depth, int bpp)
6665{
6666 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006667 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006668
6669 obj = i915_gem_alloc_object(dev,
6670 intel_framebuffer_size_for_mode(mode, bpp));
6671 if (obj == NULL)
6672 return ERR_PTR(-ENOMEM);
6673
6674 mode_cmd.width = mode->hdisplay;
6675 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006676 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6677 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006678 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006679
6680 return intel_framebuffer_create(dev, &mode_cmd, obj);
6681}
6682
6683static struct drm_framebuffer *
6684mode_fits_in_fbdev(struct drm_device *dev,
6685 struct drm_display_mode *mode)
6686{
6687 struct drm_i915_private *dev_priv = dev->dev_private;
6688 struct drm_i915_gem_object *obj;
6689 struct drm_framebuffer *fb;
6690
6691 if (dev_priv->fbdev == NULL)
6692 return NULL;
6693
6694 obj = dev_priv->fbdev->ifb.obj;
6695 if (obj == NULL)
6696 return NULL;
6697
6698 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006699 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6700 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006701 return NULL;
6702
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006703 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006704 return NULL;
6705
6706 return fb;
6707}
6708
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006709bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006710 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006711 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006712{
6713 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006714 struct intel_encoder *intel_encoder =
6715 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006716 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006717 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006718 struct drm_crtc *crtc = NULL;
6719 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006720 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006721 int i = -1;
6722
Chris Wilsond2dff872011-04-19 08:36:26 +01006723 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6724 connector->base.id, drm_get_connector_name(connector),
6725 encoder->base.id, drm_get_encoder_name(encoder));
6726
Jesse Barnes79e53942008-11-07 14:24:08 -08006727 /*
6728 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006729 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006730 * - if the connector already has an assigned crtc, use it (but make
6731 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006732 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006733 * - try to find the first unused crtc that can drive this connector,
6734 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006735 */
6736
6737 /* See if we already have a CRTC for this connector */
6738 if (encoder->crtc) {
6739 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006740
Daniel Vetter7b240562012-12-12 00:35:33 +01006741 mutex_lock(&crtc->mutex);
6742
Daniel Vetter24218aa2012-08-12 19:27:11 +02006743 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006744 old->load_detect_temp = false;
6745
6746 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006747 if (connector->dpms != DRM_MODE_DPMS_ON)
6748 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006749
Chris Wilson71731882011-04-19 23:10:58 +01006750 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006751 }
6752
6753 /* Find an unused one (if possible) */
6754 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6755 i++;
6756 if (!(encoder->possible_crtcs & (1 << i)))
6757 continue;
6758 if (!possible_crtc->enabled) {
6759 crtc = possible_crtc;
6760 break;
6761 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006762 }
6763
6764 /*
6765 * If we didn't find an unused CRTC, don't use any.
6766 */
6767 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006768 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6769 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006770 }
6771
Daniel Vetter7b240562012-12-12 00:35:33 +01006772 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02006773 intel_encoder->new_crtc = to_intel_crtc(crtc);
6774 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006775
6776 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006777 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006778 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006779 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006780
Chris Wilson64927112011-04-20 07:25:26 +01006781 if (!mode)
6782 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006783
Chris Wilsond2dff872011-04-19 08:36:26 +01006784 /* We need a framebuffer large enough to accommodate all accesses
6785 * that the plane may generate whilst we perform load detection.
6786 * We can not rely on the fbcon either being present (we get called
6787 * during its initialisation to detect all boot displays, or it may
6788 * not even exist) or that it is large enough to satisfy the
6789 * requested mode.
6790 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006791 fb = mode_fits_in_fbdev(dev, mode);
6792 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006793 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006794 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6795 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006796 } else
6797 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006798 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006799 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01006800 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006801 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006802 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006803
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006804 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006805 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006806 if (old->release_fb)
6807 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01006808 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006809 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006810 }
Chris Wilson71731882011-04-19 23:10:58 +01006811
Jesse Barnes79e53942008-11-07 14:24:08 -08006812 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006813 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006814 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006815}
6816
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006817void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006818 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006819{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006820 struct intel_encoder *intel_encoder =
6821 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006822 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01006823 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08006824
Chris Wilsond2dff872011-04-19 08:36:26 +01006825 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6826 connector->base.id, drm_get_connector_name(connector),
6827 encoder->base.id, drm_get_encoder_name(encoder));
6828
Chris Wilson8261b192011-04-19 23:18:09 +01006829 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006830 to_intel_connector(connector)->new_encoder = NULL;
6831 intel_encoder->new_crtc = NULL;
6832 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006833
Daniel Vetter36206362012-12-10 20:42:17 +01006834 if (old->release_fb) {
6835 drm_framebuffer_unregister_private(old->release_fb);
6836 drm_framebuffer_unreference(old->release_fb);
6837 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006838
Daniel Vetter67c96402013-01-23 16:25:09 +00006839 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01006840 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006841 }
6842
Eric Anholtc751ce42010-03-25 11:48:48 -07006843 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006844 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6845 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01006846
6847 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08006848}
6849
6850/* Returns the clock of the currently programmed mode of the given pipe. */
6851static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6852{
6853 struct drm_i915_private *dev_priv = dev->dev_private;
6854 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6855 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006856 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006857 u32 fp;
6858 intel_clock_t clock;
6859
6860 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006861 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006862 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006863 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006864
6865 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006866 if (IS_PINEVIEW(dev)) {
6867 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6868 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006869 } else {
6870 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6871 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6872 }
6873
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006874 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006875 if (IS_PINEVIEW(dev))
6876 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6877 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006878 else
6879 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006880 DPLL_FPA01_P1_POST_DIV_SHIFT);
6881
6882 switch (dpll & DPLL_MODE_MASK) {
6883 case DPLLB_MODE_DAC_SERIAL:
6884 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6885 5 : 10;
6886 break;
6887 case DPLLB_MODE_LVDS:
6888 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6889 7 : 14;
6890 break;
6891 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006892 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006893 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6894 return 0;
6895 }
6896
6897 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08006898 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006899 } else {
6900 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6901
6902 if (is_lvds) {
6903 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6904 DPLL_FPA01_P1_POST_DIV_SHIFT);
6905 clock.p2 = 14;
6906
6907 if ((dpll & PLL_REF_INPUT_MASK) ==
6908 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6909 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08006910 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006911 } else
Shaohua Li21778322009-02-23 15:19:16 +08006912 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006913 } else {
6914 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6915 clock.p1 = 2;
6916 else {
6917 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6918 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6919 }
6920 if (dpll & PLL_P2_DIVIDE_BY_4)
6921 clock.p2 = 4;
6922 else
6923 clock.p2 = 2;
6924
Shaohua Li21778322009-02-23 15:19:16 +08006925 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006926 }
6927 }
6928
6929 /* XXX: It would be nice to validate the clocks, but we can't reuse
6930 * i830PllIsValid() because it relies on the xf86_config connector
6931 * configuration being accurate, which it isn't necessarily.
6932 */
6933
6934 return clock.dot;
6935}
6936
6937/** Returns the currently programmed mode of the given pipe. */
6938struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6939 struct drm_crtc *crtc)
6940{
Jesse Barnes548f2452011-02-17 10:40:53 -08006941 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02006943 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006944 struct drm_display_mode *mode;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006945 int htot = I915_READ(HTOTAL(cpu_transcoder));
6946 int hsync = I915_READ(HSYNC(cpu_transcoder));
6947 int vtot = I915_READ(VTOTAL(cpu_transcoder));
6948 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08006949
6950 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6951 if (!mode)
6952 return NULL;
6953
6954 mode->clock = intel_crtc_clock_get(dev, crtc);
6955 mode->hdisplay = (htot & 0xffff) + 1;
6956 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6957 mode->hsync_start = (hsync & 0xffff) + 1;
6958 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6959 mode->vdisplay = (vtot & 0xffff) + 1;
6960 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6961 mode->vsync_start = (vsync & 0xffff) + 1;
6962 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6963
6964 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006965
6966 return mode;
6967}
6968
Daniel Vetter3dec0092010-08-20 21:40:52 +02006969static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07006970{
6971 struct drm_device *dev = crtc->dev;
6972 drm_i915_private_t *dev_priv = dev->dev_private;
6973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6974 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006975 int dpll_reg = DPLL(pipe);
6976 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07006977
Eric Anholtbad720f2009-10-22 16:11:14 -07006978 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006979 return;
6980
6981 if (!dev_priv->lvds_downclock_avail)
6982 return;
6983
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006984 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006985 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08006986 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006987
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006988 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006989
6990 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6991 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006992 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006993
Jesse Barnes652c3932009-08-17 13:31:43 -07006994 dpll = I915_READ(dpll_reg);
6995 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08006996 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006997 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006998}
6999
7000static void intel_decrease_pllclock(struct drm_crtc *crtc)
7001{
7002 struct drm_device *dev = crtc->dev;
7003 drm_i915_private_t *dev_priv = dev->dev_private;
7004 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007005
Eric Anholtbad720f2009-10-22 16:11:14 -07007006 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007007 return;
7008
7009 if (!dev_priv->lvds_downclock_avail)
7010 return;
7011
7012 /*
7013 * Since this is called by a timer, we should never get here in
7014 * the manual case.
7015 */
7016 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01007017 int pipe = intel_crtc->pipe;
7018 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02007019 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01007020
Zhao Yakui44d98a62009-10-09 11:39:40 +08007021 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007022
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007023 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007024
Chris Wilson074b5e12012-05-02 12:07:06 +01007025 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007026 dpll |= DISPLAY_RATE_SELECT_FPA1;
7027 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007028 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007029 dpll = I915_READ(dpll_reg);
7030 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007031 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007032 }
7033
7034}
7035
Chris Wilsonf047e392012-07-21 12:31:41 +01007036void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07007037{
Chris Wilsonf047e392012-07-21 12:31:41 +01007038 i915_update_gfx_val(dev->dev_private);
7039}
7040
7041void intel_mark_idle(struct drm_device *dev)
7042{
Chris Wilson725a5b52013-01-08 11:02:57 +00007043 struct drm_crtc *crtc;
7044
7045 if (!i915_powersave)
7046 return;
7047
7048 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7049 if (!crtc->fb)
7050 continue;
7051
7052 intel_decrease_pllclock(crtc);
7053 }
Chris Wilsonf047e392012-07-21 12:31:41 +01007054}
7055
7056void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
7057{
7058 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07007059 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07007060
7061 if (!i915_powersave)
7062 return;
7063
Jesse Barnes652c3932009-08-17 13:31:43 -07007064 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07007065 if (!crtc->fb)
7066 continue;
7067
Chris Wilsonf047e392012-07-21 12:31:41 +01007068 if (to_intel_framebuffer(crtc->fb)->obj == obj)
7069 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007070 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007071}
7072
Jesse Barnes79e53942008-11-07 14:24:08 -08007073static void intel_crtc_destroy(struct drm_crtc *crtc)
7074{
7075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007076 struct drm_device *dev = crtc->dev;
7077 struct intel_unpin_work *work;
7078 unsigned long flags;
7079
7080 spin_lock_irqsave(&dev->event_lock, flags);
7081 work = intel_crtc->unpin_work;
7082 intel_crtc->unpin_work = NULL;
7083 spin_unlock_irqrestore(&dev->event_lock, flags);
7084
7085 if (work) {
7086 cancel_work_sync(&work->work);
7087 kfree(work);
7088 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007089
7090 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007091
Jesse Barnes79e53942008-11-07 14:24:08 -08007092 kfree(intel_crtc);
7093}
7094
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007095static void intel_unpin_work_fn(struct work_struct *__work)
7096{
7097 struct intel_unpin_work *work =
7098 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007099 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007100
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007101 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007102 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007103 drm_gem_object_unreference(&work->pending_flip_obj->base);
7104 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007105
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007106 intel_update_fbc(dev);
7107 mutex_unlock(&dev->struct_mutex);
7108
7109 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7110 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7111
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007112 kfree(work);
7113}
7114
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007115static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007116 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007117{
7118 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7120 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007121 unsigned long flags;
7122
7123 /* Ignore early vblank irqs */
7124 if (intel_crtc == NULL)
7125 return;
7126
7127 spin_lock_irqsave(&dev->event_lock, flags);
7128 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007129
7130 /* Ensure we don't miss a work->pending update ... */
7131 smp_rmb();
7132
7133 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007134 spin_unlock_irqrestore(&dev->event_lock, flags);
7135 return;
7136 }
7137
Chris Wilsone7d841c2012-12-03 11:36:30 +00007138 /* and that the unpin work is consistent wrt ->pending. */
7139 smp_rmb();
7140
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007141 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007142
Rob Clark45a066e2012-10-08 14:50:40 -05007143 if (work->event)
7144 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007145
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007146 drm_vblank_put(dev, intel_crtc->pipe);
7147
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007148 spin_unlock_irqrestore(&dev->event_lock, flags);
7149
Daniel Vetter2c10d572012-12-20 21:24:07 +01007150 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007151
7152 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007153
7154 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007155}
7156
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007157void intel_finish_page_flip(struct drm_device *dev, int pipe)
7158{
7159 drm_i915_private_t *dev_priv = dev->dev_private;
7160 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7161
Mario Kleiner49b14a52010-12-09 07:00:07 +01007162 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007163}
7164
7165void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7166{
7167 drm_i915_private_t *dev_priv = dev->dev_private;
7168 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7169
Mario Kleiner49b14a52010-12-09 07:00:07 +01007170 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007171}
7172
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007173void intel_prepare_page_flip(struct drm_device *dev, int plane)
7174{
7175 drm_i915_private_t *dev_priv = dev->dev_private;
7176 struct intel_crtc *intel_crtc =
7177 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7178 unsigned long flags;
7179
Chris Wilsone7d841c2012-12-03 11:36:30 +00007180 /* NB: An MMIO update of the plane base pointer will also
7181 * generate a page-flip completion irq, i.e. every modeset
7182 * is also accompanied by a spurious intel_prepare_page_flip().
7183 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007184 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007185 if (intel_crtc->unpin_work)
7186 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007187 spin_unlock_irqrestore(&dev->event_lock, flags);
7188}
7189
Chris Wilsone7d841c2012-12-03 11:36:30 +00007190inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7191{
7192 /* Ensure that the work item is consistent when activating it ... */
7193 smp_wmb();
7194 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7195 /* and that it is marked active as soon as the irq could fire. */
7196 smp_wmb();
7197}
7198
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007199static int intel_gen2_queue_flip(struct drm_device *dev,
7200 struct drm_crtc *crtc,
7201 struct drm_framebuffer *fb,
7202 struct drm_i915_gem_object *obj)
7203{
7204 struct drm_i915_private *dev_priv = dev->dev_private;
7205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007206 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007207 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007208 int ret;
7209
Daniel Vetter6d90c952012-04-26 23:28:05 +02007210 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007211 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007212 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007213
Daniel Vetter6d90c952012-04-26 23:28:05 +02007214 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007215 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007216 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007217
7218 /* Can't queue multiple flips, so wait for the previous
7219 * one to finish before executing the next.
7220 */
7221 if (intel_crtc->plane)
7222 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7223 else
7224 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007225 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7226 intel_ring_emit(ring, MI_NOOP);
7227 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7228 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7229 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007230 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007231 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007232
7233 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007234 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007235 return 0;
7236
7237err_unpin:
7238 intel_unpin_fb_obj(obj);
7239err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007240 return ret;
7241}
7242
7243static int intel_gen3_queue_flip(struct drm_device *dev,
7244 struct drm_crtc *crtc,
7245 struct drm_framebuffer *fb,
7246 struct drm_i915_gem_object *obj)
7247{
7248 struct drm_i915_private *dev_priv = dev->dev_private;
7249 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007250 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007251 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007252 int ret;
7253
Daniel Vetter6d90c952012-04-26 23:28:05 +02007254 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007255 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007256 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007257
Daniel Vetter6d90c952012-04-26 23:28:05 +02007258 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007259 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007260 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007261
7262 if (intel_crtc->plane)
7263 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7264 else
7265 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007266 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7267 intel_ring_emit(ring, MI_NOOP);
7268 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7269 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7270 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007271 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007272 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007273
Chris Wilsone7d841c2012-12-03 11:36:30 +00007274 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007275 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007276 return 0;
7277
7278err_unpin:
7279 intel_unpin_fb_obj(obj);
7280err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007281 return ret;
7282}
7283
7284static int intel_gen4_queue_flip(struct drm_device *dev,
7285 struct drm_crtc *crtc,
7286 struct drm_framebuffer *fb,
7287 struct drm_i915_gem_object *obj)
7288{
7289 struct drm_i915_private *dev_priv = dev->dev_private;
7290 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7291 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007292 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007293 int ret;
7294
Daniel Vetter6d90c952012-04-26 23:28:05 +02007295 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007296 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007297 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007298
Daniel Vetter6d90c952012-04-26 23:28:05 +02007299 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007300 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007301 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007302
7303 /* i965+ uses the linear or tiled offsets from the
7304 * Display Registers (which do not change across a page-flip)
7305 * so we need only reprogram the base address.
7306 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007307 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7308 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7309 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007310 intel_ring_emit(ring,
7311 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
7312 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007313
7314 /* XXX Enabling the panel-fitter across page-flip is so far
7315 * untested on non-native modes, so ignore it for now.
7316 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7317 */
7318 pf = 0;
7319 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007320 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007321
7322 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007323 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007324 return 0;
7325
7326err_unpin:
7327 intel_unpin_fb_obj(obj);
7328err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007329 return ret;
7330}
7331
7332static int intel_gen6_queue_flip(struct drm_device *dev,
7333 struct drm_crtc *crtc,
7334 struct drm_framebuffer *fb,
7335 struct drm_i915_gem_object *obj)
7336{
7337 struct drm_i915_private *dev_priv = dev->dev_private;
7338 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007339 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007340 uint32_t pf, pipesrc;
7341 int ret;
7342
Daniel Vetter6d90c952012-04-26 23:28:05 +02007343 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007344 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007345 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007346
Daniel Vetter6d90c952012-04-26 23:28:05 +02007347 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007348 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007349 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007350
Daniel Vetter6d90c952012-04-26 23:28:05 +02007351 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7352 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7353 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007354 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007355
Chris Wilson99d9acd2012-04-17 20:37:00 +01007356 /* Contrary to the suggestions in the documentation,
7357 * "Enable Panel Fitter" does not seem to be required when page
7358 * flipping with a non-native mode, and worse causes a normal
7359 * modeset to fail.
7360 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7361 */
7362 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007363 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007364 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007365
7366 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007367 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007368 return 0;
7369
7370err_unpin:
7371 intel_unpin_fb_obj(obj);
7372err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007373 return ret;
7374}
7375
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007376/*
7377 * On gen7 we currently use the blit ring because (in early silicon at least)
7378 * the render ring doesn't give us interrpts for page flip completion, which
7379 * means clients will hang after the first flip is queued. Fortunately the
7380 * blit ring generates interrupts properly, so use it instead.
7381 */
7382static int intel_gen7_queue_flip(struct drm_device *dev,
7383 struct drm_crtc *crtc,
7384 struct drm_framebuffer *fb,
7385 struct drm_i915_gem_object *obj)
7386{
7387 struct drm_i915_private *dev_priv = dev->dev_private;
7388 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7389 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007390 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007391 int ret;
7392
7393 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7394 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007395 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007396
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007397 switch(intel_crtc->plane) {
7398 case PLANE_A:
7399 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7400 break;
7401 case PLANE_B:
7402 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7403 break;
7404 case PLANE_C:
7405 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7406 break;
7407 default:
7408 WARN_ONCE(1, "unknown plane in flip command\n");
7409 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007410 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007411 }
7412
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007413 ret = intel_ring_begin(ring, 4);
7414 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007415 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007416
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007417 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007418 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Daniel Vetterc2c75132012-07-05 12:17:30 +02007419 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007420 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007421
7422 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007423 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007424 return 0;
7425
7426err_unpin:
7427 intel_unpin_fb_obj(obj);
7428err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007429 return ret;
7430}
7431
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007432static int intel_default_queue_flip(struct drm_device *dev,
7433 struct drm_crtc *crtc,
7434 struct drm_framebuffer *fb,
7435 struct drm_i915_gem_object *obj)
7436{
7437 return -ENODEV;
7438}
7439
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007440static int intel_crtc_page_flip(struct drm_crtc *crtc,
7441 struct drm_framebuffer *fb,
7442 struct drm_pending_vblank_event *event)
7443{
7444 struct drm_device *dev = crtc->dev;
7445 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007446 struct drm_framebuffer *old_fb = crtc->fb;
7447 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7449 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007450 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007451 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007452
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007453 /* Can't change pixel format via MI display flips. */
7454 if (fb->pixel_format != crtc->fb->pixel_format)
7455 return -EINVAL;
7456
7457 /*
7458 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7459 * Note that pitch changes could also affect these register.
7460 */
7461 if (INTEL_INFO(dev)->gen > 3 &&
7462 (fb->offsets[0] != crtc->fb->offsets[0] ||
7463 fb->pitches[0] != crtc->fb->pitches[0]))
7464 return -EINVAL;
7465
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007466 work = kzalloc(sizeof *work, GFP_KERNEL);
7467 if (work == NULL)
7468 return -ENOMEM;
7469
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007470 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007471 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007472 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007473 INIT_WORK(&work->work, intel_unpin_work_fn);
7474
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007475 ret = drm_vblank_get(dev, intel_crtc->pipe);
7476 if (ret)
7477 goto free_work;
7478
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007479 /* We borrow the event spin lock for protecting unpin_work */
7480 spin_lock_irqsave(&dev->event_lock, flags);
7481 if (intel_crtc->unpin_work) {
7482 spin_unlock_irqrestore(&dev->event_lock, flags);
7483 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007484 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007485
7486 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007487 return -EBUSY;
7488 }
7489 intel_crtc->unpin_work = work;
7490 spin_unlock_irqrestore(&dev->event_lock, flags);
7491
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007492 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7493 flush_workqueue(dev_priv->wq);
7494
Chris Wilson79158102012-05-23 11:13:58 +01007495 ret = i915_mutex_lock_interruptible(dev);
7496 if (ret)
7497 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007498
Jesse Barnes75dfca82010-02-10 15:09:44 -08007499 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007500 drm_gem_object_reference(&work->old_fb_obj->base);
7501 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007502
7503 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007504
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007505 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007506
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007507 work->enable_stall_check = true;
7508
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007509 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02007510 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007511
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007512 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7513 if (ret)
7514 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007515
Chris Wilson7782de32011-07-08 12:22:41 +01007516 intel_disable_fbc(dev);
Chris Wilsonf047e392012-07-21 12:31:41 +01007517 intel_mark_fb_busy(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007518 mutex_unlock(&dev->struct_mutex);
7519
Jesse Barnese5510fa2010-07-01 16:48:37 -07007520 trace_i915_flip_request(intel_crtc->plane, obj);
7521
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007522 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007523
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007524cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007525 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007526 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007527 drm_gem_object_unreference(&work->old_fb_obj->base);
7528 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007529 mutex_unlock(&dev->struct_mutex);
7530
Chris Wilson79158102012-05-23 11:13:58 +01007531cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007532 spin_lock_irqsave(&dev->event_lock, flags);
7533 intel_crtc->unpin_work = NULL;
7534 spin_unlock_irqrestore(&dev->event_lock, flags);
7535
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007536 drm_vblank_put(dev, intel_crtc->pipe);
7537free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007538 kfree(work);
7539
7540 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007541}
7542
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007543static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007544 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7545 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007546};
7547
Daniel Vetter6ed0f792012-07-08 19:41:43 +02007548bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
7549{
7550 struct intel_encoder *other_encoder;
7551 struct drm_crtc *crtc = &encoder->new_crtc->base;
7552
7553 if (WARN_ON(!crtc))
7554 return false;
7555
7556 list_for_each_entry(other_encoder,
7557 &crtc->dev->mode_config.encoder_list,
7558 base.head) {
7559
7560 if (&other_encoder->new_crtc->base != crtc ||
7561 encoder == other_encoder)
7562 continue;
7563 else
7564 return true;
7565 }
7566
7567 return false;
7568}
7569
Daniel Vetter50f56112012-07-02 09:35:43 +02007570static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7571 struct drm_crtc *crtc)
7572{
7573 struct drm_device *dev;
7574 struct drm_crtc *tmp;
7575 int crtc_mask = 1;
7576
7577 WARN(!crtc, "checking null crtc?\n");
7578
7579 dev = crtc->dev;
7580
7581 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7582 if (tmp == crtc)
7583 break;
7584 crtc_mask <<= 1;
7585 }
7586
7587 if (encoder->possible_crtcs & crtc_mask)
7588 return true;
7589 return false;
7590}
7591
Daniel Vetter9a935852012-07-05 22:34:27 +02007592/**
7593 * intel_modeset_update_staged_output_state
7594 *
7595 * Updates the staged output configuration state, e.g. after we've read out the
7596 * current hw state.
7597 */
7598static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7599{
7600 struct intel_encoder *encoder;
7601 struct intel_connector *connector;
7602
7603 list_for_each_entry(connector, &dev->mode_config.connector_list,
7604 base.head) {
7605 connector->new_encoder =
7606 to_intel_encoder(connector->base.encoder);
7607 }
7608
7609 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7610 base.head) {
7611 encoder->new_crtc =
7612 to_intel_crtc(encoder->base.crtc);
7613 }
7614}
7615
7616/**
7617 * intel_modeset_commit_output_state
7618 *
7619 * This function copies the stage display pipe configuration to the real one.
7620 */
7621static void intel_modeset_commit_output_state(struct drm_device *dev)
7622{
7623 struct intel_encoder *encoder;
7624 struct intel_connector *connector;
7625
7626 list_for_each_entry(connector, &dev->mode_config.connector_list,
7627 base.head) {
7628 connector->base.encoder = &connector->new_encoder->base;
7629 }
7630
7631 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7632 base.head) {
7633 encoder->base.crtc = &encoder->new_crtc->base;
7634 }
7635}
7636
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007637static int
7638pipe_config_set_bpp(struct drm_crtc *crtc,
7639 struct drm_framebuffer *fb,
7640 struct intel_crtc_config *pipe_config)
7641{
7642 struct drm_device *dev = crtc->dev;
7643 struct drm_connector *connector;
7644 int bpp;
7645
Daniel Vetterd42264b2013-03-28 16:38:08 +01007646 switch (fb->pixel_format) {
7647 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007648 bpp = 8*3; /* since we go through a colormap */
7649 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007650 case DRM_FORMAT_XRGB1555:
7651 case DRM_FORMAT_ARGB1555:
7652 /* checked in intel_framebuffer_init already */
7653 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7654 return -EINVAL;
7655 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007656 bpp = 6*3; /* min is 18bpp */
7657 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007658 case DRM_FORMAT_XBGR8888:
7659 case DRM_FORMAT_ABGR8888:
7660 /* checked in intel_framebuffer_init already */
7661 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7662 return -EINVAL;
7663 case DRM_FORMAT_XRGB8888:
7664 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007665 bpp = 8*3;
7666 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007667 case DRM_FORMAT_XRGB2101010:
7668 case DRM_FORMAT_ARGB2101010:
7669 case DRM_FORMAT_XBGR2101010:
7670 case DRM_FORMAT_ABGR2101010:
7671 /* checked in intel_framebuffer_init already */
7672 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01007673 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007674 bpp = 10*3;
7675 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01007676 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007677 default:
7678 DRM_DEBUG_KMS("unsupported depth\n");
7679 return -EINVAL;
7680 }
7681
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007682 pipe_config->pipe_bpp = bpp;
7683
7684 /* Clamp display bpp to EDID value */
7685 list_for_each_entry(connector, &dev->mode_config.connector_list,
7686 head) {
7687 if (connector->encoder && connector->encoder->crtc != crtc)
7688 continue;
7689
7690 /* Don't use an invalid EDID bpc value */
7691 if (connector->display_info.bpc &&
7692 connector->display_info.bpc * 3 < bpp) {
7693 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7694 bpp, connector->display_info.bpc*3);
7695 pipe_config->pipe_bpp = connector->display_info.bpc*3;
7696 }
Daniel Vetter996a2232013-04-19 11:24:34 +02007697
7698 /* Clamp bpp to 8 on screens without EDID 1.4 */
7699 if (connector->display_info.bpc == 0 && bpp > 24) {
7700 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7701 bpp);
7702 pipe_config->pipe_bpp = 24;
7703 }
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007704 }
7705
7706 return bpp;
7707}
7708
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007709static struct intel_crtc_config *
7710intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007711 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007712 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02007713{
7714 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02007715 struct drm_encoder_helper_funcs *encoder_funcs;
7716 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007717 struct intel_crtc_config *pipe_config;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007718 int plane_bpp;
Daniel Vetter7758a112012-07-08 19:40:39 +02007719
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007720 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7721 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02007722 return ERR_PTR(-ENOMEM);
7723
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007724 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7725 drm_mode_copy(&pipe_config->requested_mode, mode);
7726
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007727 plane_bpp = pipe_config_set_bpp(crtc, fb, pipe_config);
7728 if (plane_bpp < 0)
7729 goto fail;
7730
Daniel Vetter7758a112012-07-08 19:40:39 +02007731 /* Pass our mode to the connectors and the CRTC to give them a chance to
7732 * adjust it according to limitations or connector properties, and also
7733 * a chance to reject the mode entirely.
7734 */
7735 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7736 base.head) {
7737
7738 if (&encoder->new_crtc->base != crtc)
7739 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01007740
7741 if (encoder->compute_config) {
7742 if (!(encoder->compute_config(encoder, pipe_config))) {
7743 DRM_DEBUG_KMS("Encoder config failure\n");
7744 goto fail;
7745 }
7746
7747 continue;
7748 }
7749
Daniel Vetter7758a112012-07-08 19:40:39 +02007750 encoder_funcs = encoder->base.helper_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007751 if (!(encoder_funcs->mode_fixup(&encoder->base,
7752 &pipe_config->requested_mode,
7753 &pipe_config->adjusted_mode))) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007754 DRM_DEBUG_KMS("Encoder fixup failed\n");
7755 goto fail;
7756 }
7757 }
7758
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007759 if (!(intel_crtc_compute_config(crtc, pipe_config))) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007760 DRM_DEBUG_KMS("CRTC fixup failed\n");
7761 goto fail;
7762 }
7763 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
7764
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007765 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7766 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7767 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7768
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007769 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02007770fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007771 kfree(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +02007772 return ERR_PTR(-EINVAL);
7773}
7774
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007775/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7776 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7777static void
7778intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7779 unsigned *prepare_pipes, unsigned *disable_pipes)
7780{
7781 struct intel_crtc *intel_crtc;
7782 struct drm_device *dev = crtc->dev;
7783 struct intel_encoder *encoder;
7784 struct intel_connector *connector;
7785 struct drm_crtc *tmp_crtc;
7786
7787 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7788
7789 /* Check which crtcs have changed outputs connected to them, these need
7790 * to be part of the prepare_pipes mask. We don't (yet) support global
7791 * modeset across multiple crtcs, so modeset_pipes will only have one
7792 * bit set at most. */
7793 list_for_each_entry(connector, &dev->mode_config.connector_list,
7794 base.head) {
7795 if (connector->base.encoder == &connector->new_encoder->base)
7796 continue;
7797
7798 if (connector->base.encoder) {
7799 tmp_crtc = connector->base.encoder->crtc;
7800
7801 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7802 }
7803
7804 if (connector->new_encoder)
7805 *prepare_pipes |=
7806 1 << connector->new_encoder->new_crtc->pipe;
7807 }
7808
7809 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7810 base.head) {
7811 if (encoder->base.crtc == &encoder->new_crtc->base)
7812 continue;
7813
7814 if (encoder->base.crtc) {
7815 tmp_crtc = encoder->base.crtc;
7816
7817 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7818 }
7819
7820 if (encoder->new_crtc)
7821 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7822 }
7823
7824 /* Check for any pipes that will be fully disabled ... */
7825 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7826 base.head) {
7827 bool used = false;
7828
7829 /* Don't try to disable disabled crtcs. */
7830 if (!intel_crtc->base.enabled)
7831 continue;
7832
7833 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7834 base.head) {
7835 if (encoder->new_crtc == intel_crtc)
7836 used = true;
7837 }
7838
7839 if (!used)
7840 *disable_pipes |= 1 << intel_crtc->pipe;
7841 }
7842
7843
7844 /* set_mode is also used to update properties on life display pipes. */
7845 intel_crtc = to_intel_crtc(crtc);
7846 if (crtc->enabled)
7847 *prepare_pipes |= 1 << intel_crtc->pipe;
7848
Daniel Vetterb6c51642013-04-12 18:48:43 +02007849 /*
7850 * For simplicity do a full modeset on any pipe where the output routing
7851 * changed. We could be more clever, but that would require us to be
7852 * more careful with calling the relevant encoder->mode_set functions.
7853 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007854 if (*prepare_pipes)
7855 *modeset_pipes = *prepare_pipes;
7856
7857 /* ... and mask these out. */
7858 *modeset_pipes &= ~(*disable_pipes);
7859 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02007860
7861 /*
7862 * HACK: We don't (yet) fully support global modesets. intel_set_config
7863 * obies this rule, but the modeset restore mode of
7864 * intel_modeset_setup_hw_state does not.
7865 */
7866 *modeset_pipes &= 1 << intel_crtc->pipe;
7867 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02007868
7869 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7870 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007871}
7872
Daniel Vetterea9d7582012-07-10 10:42:52 +02007873static bool intel_crtc_in_use(struct drm_crtc *crtc)
7874{
7875 struct drm_encoder *encoder;
7876 struct drm_device *dev = crtc->dev;
7877
7878 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
7879 if (encoder->crtc == crtc)
7880 return true;
7881
7882 return false;
7883}
7884
7885static void
7886intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
7887{
7888 struct intel_encoder *intel_encoder;
7889 struct intel_crtc *intel_crtc;
7890 struct drm_connector *connector;
7891
7892 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
7893 base.head) {
7894 if (!intel_encoder->base.crtc)
7895 continue;
7896
7897 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
7898
7899 if (prepare_pipes & (1 << intel_crtc->pipe))
7900 intel_encoder->connectors_active = false;
7901 }
7902
7903 intel_modeset_commit_output_state(dev);
7904
7905 /* Update computed state. */
7906 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7907 base.head) {
7908 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
7909 }
7910
7911 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7912 if (!connector->encoder || !connector->encoder->crtc)
7913 continue;
7914
7915 intel_crtc = to_intel_crtc(connector->encoder->crtc);
7916
7917 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02007918 struct drm_property *dpms_property =
7919 dev->mode_config.dpms_property;
7920
Daniel Vetterea9d7582012-07-10 10:42:52 +02007921 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05007922 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02007923 dpms_property,
7924 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02007925
7926 intel_encoder = to_intel_encoder(connector->encoder);
7927 intel_encoder->connectors_active = true;
7928 }
7929 }
7930
7931}
7932
Daniel Vetter25c5b262012-07-08 22:08:04 +02007933#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
7934 list_for_each_entry((intel_crtc), \
7935 &(dev)->mode_config.crtc_list, \
7936 base.head) \
7937 if (mask & (1 <<(intel_crtc)->pipe)) \
7938
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007939static bool
7940intel_pipe_config_compare(struct intel_crtc_config *current_config,
7941 struct intel_crtc_config *pipe_config)
7942{
Daniel Vetter88adfff2013-03-28 10:42:01 +01007943 if (current_config->has_pch_encoder != pipe_config->has_pch_encoder) {
7944 DRM_ERROR("mismatch in has_pch_encoder "
7945 "(expected %i, found %i)\n",
7946 current_config->has_pch_encoder,
7947 pipe_config->has_pch_encoder);
7948 return false;
7949 }
7950
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007951 return true;
7952}
7953
Daniel Vetterb9805142012-08-31 17:37:33 +02007954void
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007955intel_modeset_check_state(struct drm_device *dev)
7956{
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007957 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007958 struct intel_crtc *crtc;
7959 struct intel_encoder *encoder;
7960 struct intel_connector *connector;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007961 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007962
7963 list_for_each_entry(connector, &dev->mode_config.connector_list,
7964 base.head) {
7965 /* This also checks the encoder/connector hw state with the
7966 * ->get_hw_state callbacks. */
7967 intel_connector_check_state(connector);
7968
7969 WARN(&connector->new_encoder->base != connector->base.encoder,
7970 "connector's staged encoder doesn't match current encoder\n");
7971 }
7972
7973 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7974 base.head) {
7975 bool enabled = false;
7976 bool active = false;
7977 enum pipe pipe, tracked_pipe;
7978
7979 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
7980 encoder->base.base.id,
7981 drm_get_encoder_name(&encoder->base));
7982
7983 WARN(&encoder->new_crtc->base != encoder->base.crtc,
7984 "encoder's stage crtc doesn't match current crtc\n");
7985 WARN(encoder->connectors_active && !encoder->base.crtc,
7986 "encoder's active_connectors set, but no crtc\n");
7987
7988 list_for_each_entry(connector, &dev->mode_config.connector_list,
7989 base.head) {
7990 if (connector->base.encoder != &encoder->base)
7991 continue;
7992 enabled = true;
7993 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
7994 active = true;
7995 }
7996 WARN(!!encoder->base.crtc != enabled,
7997 "encoder's enabled state mismatch "
7998 "(expected %i, found %i)\n",
7999 !!encoder->base.crtc, enabled);
8000 WARN(active && !encoder->base.crtc,
8001 "active encoder with no crtc\n");
8002
8003 WARN(encoder->connectors_active != active,
8004 "encoder's computed active state doesn't match tracked active state "
8005 "(expected %i, found %i)\n", active, encoder->connectors_active);
8006
8007 active = encoder->get_hw_state(encoder, &pipe);
8008 WARN(active != encoder->connectors_active,
8009 "encoder's hw state doesn't match sw tracking "
8010 "(expected %i, found %i)\n",
8011 encoder->connectors_active, active);
8012
8013 if (!encoder->base.crtc)
8014 continue;
8015
8016 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
8017 WARN(active && pipe != tracked_pipe,
8018 "active encoder's pipe doesn't match"
8019 "(expected %i, found %i)\n",
8020 tracked_pipe, pipe);
8021
8022 }
8023
8024 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
8025 base.head) {
8026 bool enabled = false;
8027 bool active = false;
8028
8029 DRM_DEBUG_KMS("[CRTC:%d]\n",
8030 crtc->base.base.id);
8031
8032 WARN(crtc->active && !crtc->base.enabled,
8033 "active crtc, but not enabled in sw tracking\n");
8034
8035 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8036 base.head) {
8037 if (encoder->base.crtc != &crtc->base)
8038 continue;
8039 enabled = true;
8040 if (encoder->connectors_active)
8041 active = true;
8042 }
8043 WARN(active != crtc->active,
8044 "crtc's computed active state doesn't match tracked active state "
8045 "(expected %i, found %i)\n", active, crtc->active);
8046 WARN(enabled != crtc->base.enabled,
8047 "crtc's computed enabled state doesn't match tracked enabled state "
8048 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
8049
Daniel Vetter88adfff2013-03-28 10:42:01 +01008050 memset(&pipe_config, 0, sizeof(pipe_config));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008051 active = dev_priv->display.get_pipe_config(crtc,
8052 &pipe_config);
8053 WARN(crtc->active != active,
8054 "crtc active state doesn't match with hw state "
8055 "(expected %i, found %i)\n", crtc->active, active);
8056
8057 WARN(active &&
8058 !intel_pipe_config_compare(&crtc->config, &pipe_config),
8059 "pipe state doesn't match!\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +02008060 }
8061}
8062
Daniel Vetterf30da182013-04-11 20:22:50 +02008063static int __intel_set_mode(struct drm_crtc *crtc,
8064 struct drm_display_mode *mode,
8065 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02008066{
8067 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02008068 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008069 struct drm_display_mode *saved_mode, *saved_hwmode;
8070 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008071 struct intel_crtc *intel_crtc;
8072 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008073 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02008074
Tim Gardner3ac18232012-12-07 07:54:26 -07008075 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008076 if (!saved_mode)
8077 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07008078 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02008079
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008080 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02008081 &prepare_pipes, &disable_pipes);
8082
Tim Gardner3ac18232012-12-07 07:54:26 -07008083 *saved_hwmode = crtc->hwmode;
8084 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008085
Daniel Vetter25c5b262012-07-08 22:08:04 +02008086 /* Hack: Because we don't (yet) support global modeset on multiple
8087 * crtcs, we don't keep track of the new mode for more than one crtc.
8088 * Hence simply check whether any bit is set in modeset_pipes in all the
8089 * pieces of code that are not yet converted to deal with mutliple crtcs
8090 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008091 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008092 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008093 if (IS_ERR(pipe_config)) {
8094 ret = PTR_ERR(pipe_config);
8095 pipe_config = NULL;
8096
Tim Gardner3ac18232012-12-07 07:54:26 -07008097 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008098 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008099 }
8100
Daniel Vetter460da9162013-03-27 00:44:51 +01008101 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8102 intel_crtc_disable(&intel_crtc->base);
8103
Daniel Vetterea9d7582012-07-10 10:42:52 +02008104 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8105 if (intel_crtc->base.enabled)
8106 dev_priv->display.crtc_disable(&intel_crtc->base);
8107 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008108
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02008109 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8110 * to set it here already despite that we pass it down the callchain.
8111 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008112 if (modeset_pipes) {
Daniel Vetter3b117c82013-04-17 20:15:07 +02008113 enum transcoder tmp = to_intel_crtc(crtc)->config.cpu_transcoder;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008114 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008115 /* mode_set/enable/disable functions rely on a correct pipe
8116 * config. */
8117 to_intel_crtc(crtc)->config = *pipe_config;
Daniel Vetter3b117c82013-04-17 20:15:07 +02008118 to_intel_crtc(crtc)->config.cpu_transcoder = tmp;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008119 }
Daniel Vetter7758a112012-07-08 19:40:39 +02008120
Daniel Vetterea9d7582012-07-10 10:42:52 +02008121 /* Only after disabling all output pipelines that will be changed can we
8122 * update the the output configuration. */
8123 intel_modeset_update_state(dev, prepare_pipes);
8124
Daniel Vetter47fab732012-10-26 10:58:18 +02008125 if (dev_priv->display.modeset_global_resources)
8126 dev_priv->display.modeset_global_resources(dev);
8127
Daniel Vettera6778b32012-07-02 09:56:42 +02008128 /* Set up the DPLL and any encoders state that needs to adjust or depend
8129 * on the DPLL.
8130 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008131 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008132 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008133 x, y, fb);
8134 if (ret)
8135 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02008136 }
8137
8138 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008139 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8140 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02008141
Daniel Vetter25c5b262012-07-08 22:08:04 +02008142 if (modeset_pipes) {
8143 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008144 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008145
Daniel Vetter25c5b262012-07-08 22:08:04 +02008146 /* Calculate and store various constants which
8147 * are later needed by vblank and swap-completion
8148 * timestamping. They are derived from true hwmode.
8149 */
8150 drm_calc_timestamping_constants(crtc);
8151 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008152
8153 /* FIXME: add subpixel order */
8154done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008155 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07008156 crtc->hwmode = *saved_hwmode;
8157 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008158 }
8159
Tim Gardner3ac18232012-12-07 07:54:26 -07008160out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008161 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07008162 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02008163 return ret;
8164}
8165
Daniel Vetterf30da182013-04-11 20:22:50 +02008166int intel_set_mode(struct drm_crtc *crtc,
8167 struct drm_display_mode *mode,
8168 int x, int y, struct drm_framebuffer *fb)
8169{
8170 int ret;
8171
8172 ret = __intel_set_mode(crtc, mode, x, y, fb);
8173
8174 if (ret == 0)
8175 intel_modeset_check_state(crtc->dev);
8176
8177 return ret;
8178}
8179
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008180void intel_crtc_restore_mode(struct drm_crtc *crtc)
8181{
8182 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8183}
8184
Daniel Vetter25c5b262012-07-08 22:08:04 +02008185#undef for_each_intel_crtc_masked
8186
Daniel Vetterd9e55602012-07-04 22:16:09 +02008187static void intel_set_config_free(struct intel_set_config *config)
8188{
8189 if (!config)
8190 return;
8191
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008192 kfree(config->save_connector_encoders);
8193 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02008194 kfree(config);
8195}
8196
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008197static int intel_set_config_save_state(struct drm_device *dev,
8198 struct intel_set_config *config)
8199{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008200 struct drm_encoder *encoder;
8201 struct drm_connector *connector;
8202 int count;
8203
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008204 config->save_encoder_crtcs =
8205 kcalloc(dev->mode_config.num_encoder,
8206 sizeof(struct drm_crtc *), GFP_KERNEL);
8207 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008208 return -ENOMEM;
8209
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008210 config->save_connector_encoders =
8211 kcalloc(dev->mode_config.num_connector,
8212 sizeof(struct drm_encoder *), GFP_KERNEL);
8213 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008214 return -ENOMEM;
8215
8216 /* Copy data. Note that driver private data is not affected.
8217 * Should anything bad happen only the expected state is
8218 * restored, not the drivers personal bookkeeping.
8219 */
8220 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008221 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008222 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008223 }
8224
8225 count = 0;
8226 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008227 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008228 }
8229
8230 return 0;
8231}
8232
8233static void intel_set_config_restore_state(struct drm_device *dev,
8234 struct intel_set_config *config)
8235{
Daniel Vetter9a935852012-07-05 22:34:27 +02008236 struct intel_encoder *encoder;
8237 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008238 int count;
8239
8240 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008241 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8242 encoder->new_crtc =
8243 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008244 }
8245
8246 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008247 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8248 connector->new_encoder =
8249 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008250 }
8251}
8252
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008253static void
8254intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8255 struct intel_set_config *config)
8256{
8257
8258 /* We should be able to check here if the fb has the same properties
8259 * and then just flip_or_move it */
8260 if (set->crtc->fb != set->fb) {
8261 /* If we have no fb then treat it as a full mode set */
8262 if (set->crtc->fb == NULL) {
8263 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
8264 config->mode_changed = true;
8265 } else if (set->fb == NULL) {
8266 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01008267 } else if (set->fb->pixel_format !=
8268 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008269 config->mode_changed = true;
8270 } else
8271 config->fb_changed = true;
8272 }
8273
Daniel Vetter835c5872012-07-10 18:11:08 +02008274 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008275 config->fb_changed = true;
8276
8277 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8278 DRM_DEBUG_KMS("modes are different, full mode set\n");
8279 drm_mode_debug_printmodeline(&set->crtc->mode);
8280 drm_mode_debug_printmodeline(set->mode);
8281 config->mode_changed = true;
8282 }
8283}
8284
Daniel Vetter2e431052012-07-04 22:42:15 +02008285static int
Daniel Vetter9a935852012-07-05 22:34:27 +02008286intel_modeset_stage_output_state(struct drm_device *dev,
8287 struct drm_mode_set *set,
8288 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02008289{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008290 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008291 struct intel_connector *connector;
8292 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02008293 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02008294
Damien Lespiau9abdda72013-02-13 13:29:23 +00008295 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02008296 * of connectors. For paranoia, double-check this. */
8297 WARN_ON(!set->fb && (set->num_connectors != 0));
8298 WARN_ON(set->fb && (set->num_connectors == 0));
8299
Daniel Vetter50f56112012-07-02 09:35:43 +02008300 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008301 list_for_each_entry(connector, &dev->mode_config.connector_list,
8302 base.head) {
8303 /* Otherwise traverse passed in connector list and get encoders
8304 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008305 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008306 if (set->connectors[ro] == &connector->base) {
8307 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02008308 break;
8309 }
8310 }
8311
Daniel Vetter9a935852012-07-05 22:34:27 +02008312 /* If we disable the crtc, disable all its connectors. Also, if
8313 * the connector is on the changing crtc but not on the new
8314 * connector list, disable it. */
8315 if ((!set->fb || ro == set->num_connectors) &&
8316 connector->base.encoder &&
8317 connector->base.encoder->crtc == set->crtc) {
8318 connector->new_encoder = NULL;
8319
8320 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8321 connector->base.base.id,
8322 drm_get_connector_name(&connector->base));
8323 }
8324
8325
8326 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008327 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008328 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008329 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008330 }
8331 /* connector->new_encoder is now updated for all connectors. */
8332
8333 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008334 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008335 list_for_each_entry(connector, &dev->mode_config.connector_list,
8336 base.head) {
8337 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02008338 continue;
8339
Daniel Vetter9a935852012-07-05 22:34:27 +02008340 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02008341
8342 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008343 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02008344 new_crtc = set->crtc;
8345 }
8346
8347 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02008348 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8349 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008350 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02008351 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008352 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8353
8354 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8355 connector->base.base.id,
8356 drm_get_connector_name(&connector->base),
8357 new_crtc->base.id);
8358 }
8359
8360 /* Check for any encoders that needs to be disabled. */
8361 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8362 base.head) {
8363 list_for_each_entry(connector,
8364 &dev->mode_config.connector_list,
8365 base.head) {
8366 if (connector->new_encoder == encoder) {
8367 WARN_ON(!connector->new_encoder->new_crtc);
8368
8369 goto next_encoder;
8370 }
8371 }
8372 encoder->new_crtc = NULL;
8373next_encoder:
8374 /* Only now check for crtc changes so we don't miss encoders
8375 * that will be disabled. */
8376 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008377 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008378 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008379 }
8380 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008381 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008382
Daniel Vetter2e431052012-07-04 22:42:15 +02008383 return 0;
8384}
8385
8386static int intel_crtc_set_config(struct drm_mode_set *set)
8387{
8388 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02008389 struct drm_mode_set save_set;
8390 struct intel_set_config *config;
8391 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02008392
Daniel Vetter8d3e3752012-07-05 16:09:09 +02008393 BUG_ON(!set);
8394 BUG_ON(!set->crtc);
8395 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02008396
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01008397 /* Enforce sane interface api - has been abused by the fb helper. */
8398 BUG_ON(!set->mode && set->fb);
8399 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02008400
Daniel Vetter2e431052012-07-04 22:42:15 +02008401 if (set->fb) {
8402 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8403 set->crtc->base.id, set->fb->base.id,
8404 (int)set->num_connectors, set->x, set->y);
8405 } else {
8406 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02008407 }
8408
8409 dev = set->crtc->dev;
8410
8411 ret = -ENOMEM;
8412 config = kzalloc(sizeof(*config), GFP_KERNEL);
8413 if (!config)
8414 goto out_config;
8415
8416 ret = intel_set_config_save_state(dev, config);
8417 if (ret)
8418 goto out_config;
8419
8420 save_set.crtc = set->crtc;
8421 save_set.mode = &set->crtc->mode;
8422 save_set.x = set->crtc->x;
8423 save_set.y = set->crtc->y;
8424 save_set.fb = set->crtc->fb;
8425
8426 /* Compute whether we need a full modeset, only an fb base update or no
8427 * change at all. In the future we might also check whether only the
8428 * mode changed, e.g. for LVDS where we only change the panel fitter in
8429 * such cases. */
8430 intel_set_config_compute_mode_changes(set, config);
8431
Daniel Vetter9a935852012-07-05 22:34:27 +02008432 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02008433 if (ret)
8434 goto fail;
8435
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008436 if (config->mode_changed) {
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008437 if (set->mode) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008438 DRM_DEBUG_KMS("attempting to set mode from"
8439 " userspace\n");
8440 drm_mode_debug_printmodeline(set->mode);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008441 }
8442
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008443 ret = intel_set_mode(set->crtc, set->mode,
8444 set->x, set->y, set->fb);
8445 if (ret) {
8446 DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
8447 set->crtc->base.id, ret);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008448 goto fail;
8449 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008450 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02008451 intel_crtc_wait_for_pending_flips(set->crtc);
8452
Daniel Vetter4f660f42012-07-02 09:47:37 +02008453 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008454 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008455 }
8456
Daniel Vetterd9e55602012-07-04 22:16:09 +02008457 intel_set_config_free(config);
8458
Daniel Vetter50f56112012-07-02 09:35:43 +02008459 return 0;
8460
8461fail:
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008462 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008463
8464 /* Try to restore the config */
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008465 if (config->mode_changed &&
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008466 intel_set_mode(save_set.crtc, save_set.mode,
8467 save_set.x, save_set.y, save_set.fb))
Daniel Vetter50f56112012-07-02 09:35:43 +02008468 DRM_ERROR("failed to restore config after modeset failure\n");
8469
Daniel Vetterd9e55602012-07-04 22:16:09 +02008470out_config:
8471 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008472 return ret;
8473}
8474
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008475static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008476 .cursor_set = intel_crtc_cursor_set,
8477 .cursor_move = intel_crtc_cursor_move,
8478 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008479 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008480 .destroy = intel_crtc_destroy,
8481 .page_flip = intel_crtc_page_flip,
8482};
8483
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008484static void intel_cpu_pll_init(struct drm_device *dev)
8485{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008486 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008487 intel_ddi_pll_init(dev);
8488}
8489
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008490static void intel_pch_pll_init(struct drm_device *dev)
8491{
8492 drm_i915_private_t *dev_priv = dev->dev_private;
8493 int i;
8494
8495 if (dev_priv->num_pch_pll == 0) {
8496 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
8497 return;
8498 }
8499
8500 for (i = 0; i < dev_priv->num_pch_pll; i++) {
8501 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
8502 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
8503 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
8504 }
8505}
8506
Hannes Ederb358d0a2008-12-18 21:18:47 +01008507static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08008508{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008509 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008510 struct intel_crtc *intel_crtc;
8511 int i;
8512
8513 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8514 if (intel_crtc == NULL)
8515 return;
8516
8517 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8518
8519 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08008520 for (i = 0; i < 256; i++) {
8521 intel_crtc->lut_r[i] = i;
8522 intel_crtc->lut_g[i] = i;
8523 intel_crtc->lut_b[i] = i;
8524 }
8525
Jesse Barnes80824002009-09-10 15:28:06 -07008526 /* Swap pipes & planes for FBC on pre-965 */
8527 intel_crtc->pipe = pipe;
8528 intel_crtc->plane = pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02008529 intel_crtc->config.cpu_transcoder = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01008530 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008531 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01008532 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07008533 }
8534
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008535 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8536 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8537 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8538 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8539
Jesse Barnes79e53942008-11-07 14:24:08 -08008540 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08008541}
8542
Carl Worth08d7b3d2009-04-29 14:43:54 -07008543int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00008544 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07008545{
Carl Worth08d7b3d2009-04-29 14:43:54 -07008546 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02008547 struct drm_mode_object *drmmode_obj;
8548 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008549
Daniel Vetter1cff8f62012-04-24 09:55:08 +02008550 if (!drm_core_check_feature(dev, DRIVER_MODESET))
8551 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008552
Daniel Vetterc05422d2009-08-11 16:05:30 +02008553 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8554 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07008555
Daniel Vetterc05422d2009-08-11 16:05:30 +02008556 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07008557 DRM_ERROR("no such CRTC id\n");
8558 return -EINVAL;
8559 }
8560
Daniel Vetterc05422d2009-08-11 16:05:30 +02008561 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8562 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008563
Daniel Vetterc05422d2009-08-11 16:05:30 +02008564 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008565}
8566
Daniel Vetter66a92782012-07-12 20:08:18 +02008567static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008568{
Daniel Vetter66a92782012-07-12 20:08:18 +02008569 struct drm_device *dev = encoder->base.dev;
8570 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008571 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008572 int entry = 0;
8573
Daniel Vetter66a92782012-07-12 20:08:18 +02008574 list_for_each_entry(source_encoder,
8575 &dev->mode_config.encoder_list, base.head) {
8576
8577 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008578 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02008579
8580 /* Intel hw has only one MUX where enocoders could be cloned. */
8581 if (encoder->cloneable && source_encoder->cloneable)
8582 index_mask |= (1 << entry);
8583
Jesse Barnes79e53942008-11-07 14:24:08 -08008584 entry++;
8585 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01008586
Jesse Barnes79e53942008-11-07 14:24:08 -08008587 return index_mask;
8588}
8589
Chris Wilson4d302442010-12-14 19:21:29 +00008590static bool has_edp_a(struct drm_device *dev)
8591{
8592 struct drm_i915_private *dev_priv = dev->dev_private;
8593
8594 if (!IS_MOBILE(dev))
8595 return false;
8596
8597 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8598 return false;
8599
8600 if (IS_GEN5(dev) &&
8601 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8602 return false;
8603
8604 return true;
8605}
8606
Jesse Barnes79e53942008-11-07 14:24:08 -08008607static void intel_setup_outputs(struct drm_device *dev)
8608{
Eric Anholt725e30a2009-01-22 13:01:02 -08008609 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008610 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008611 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008612 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08008613
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008614 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00008615 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8616 /* disable the panel fitter on everything but LVDS */
8617 I915_WRITE(PFIT_CONTROL, 0);
8618 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008619
Paulo Zanonic40c0f52013-04-12 18:16:53 -03008620 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02008621 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008622
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008623 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03008624 int found;
8625
8626 /* Haswell uses DDI functions to detect digital outputs */
8627 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
8628 /* DDI A only supports eDP */
8629 if (found)
8630 intel_ddi_init(dev, PORT_A);
8631
8632 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
8633 * register */
8634 found = I915_READ(SFUSE_STRAP);
8635
8636 if (found & SFUSE_STRAP_DDIB_DETECTED)
8637 intel_ddi_init(dev, PORT_B);
8638 if (found & SFUSE_STRAP_DDIC_DETECTED)
8639 intel_ddi_init(dev, PORT_C);
8640 if (found & SFUSE_STRAP_DDID_DETECTED)
8641 intel_ddi_init(dev, PORT_D);
8642 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008643 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02008644 dpd_is_edp = intel_dpd_is_edp(dev);
8645
8646 if (has_edp_a(dev))
8647 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008648
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008649 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08008650 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01008651 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008652 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03008653 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008654 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008655 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008656 }
8657
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008658 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03008659 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008660
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008661 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03008662 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008663
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008664 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008665 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008666
Daniel Vetter270b3042012-10-27 15:52:05 +02008667 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008668 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008669 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05308670 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008671 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
8672 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05308673
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008674 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03008675 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
8676 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008677 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
8678 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008679 }
Zhenyu Wang103a1962009-11-27 11:44:36 +08008680 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008681 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008682
Paulo Zanonie2debe92013-02-18 19:00:27 -03008683 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008684 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008685 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008686 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8687 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008688 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008689 }
Ma Ling27185ae2009-08-24 13:50:23 +08008690
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008691 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8692 DRM_DEBUG_KMS("probing DP_B\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008693 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008694 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008695 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008696
8697 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008698
Paulo Zanonie2debe92013-02-18 19:00:27 -03008699 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008700 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008701 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008702 }
Ma Ling27185ae2009-08-24 13:50:23 +08008703
Paulo Zanonie2debe92013-02-18 19:00:27 -03008704 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008705
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008706 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8707 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008708 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008709 }
8710 if (SUPPORTS_INTEGRATED_DP(dev)) {
8711 DRM_DEBUG_KMS("probing DP_C\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008712 intel_dp_init(dev, DP_C, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008713 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008714 }
Ma Ling27185ae2009-08-24 13:50:23 +08008715
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008716 if (SUPPORTS_INTEGRATED_DP(dev) &&
8717 (I915_READ(DP_D) & DP_DETECTED)) {
8718 DRM_DEBUG_KMS("probing DP_D\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008719 intel_dp_init(dev, DP_D, PORT_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008720 }
Eric Anholtbad720f2009-10-22 16:11:14 -07008721 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008722 intel_dvo_init(dev);
8723
Zhenyu Wang103a1962009-11-27 11:44:36 +08008724 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008725 intel_tv_init(dev);
8726
Chris Wilson4ef69c72010-09-09 15:14:28 +01008727 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8728 encoder->base.possible_crtcs = encoder->crtc_mask;
8729 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02008730 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08008731 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008732
Paulo Zanonidde86e22012-12-01 12:04:25 -02008733 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02008734
8735 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008736}
8737
8738static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8739{
8740 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008741
8742 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008743 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008744
8745 kfree(intel_fb);
8746}
8747
8748static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008749 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008750 unsigned int *handle)
8751{
8752 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008753 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008754
Chris Wilson05394f32010-11-08 19:18:58 +00008755 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008756}
8757
8758static const struct drm_framebuffer_funcs intel_fb_funcs = {
8759 .destroy = intel_user_framebuffer_destroy,
8760 .create_handle = intel_user_framebuffer_create_handle,
8761};
8762
Dave Airlie38651672010-03-30 05:34:13 +00008763int intel_framebuffer_init(struct drm_device *dev,
8764 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008765 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008766 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008767{
Jesse Barnes79e53942008-11-07 14:24:08 -08008768 int ret;
8769
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008770 if (obj->tiling_mode == I915_TILING_Y) {
8771 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01008772 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008773 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008774
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008775 if (mode_cmd->pitches[0] & 63) {
8776 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
8777 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01008778 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008779 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008780
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008781 /* FIXME <= Gen4 stride limits are bit unclear */
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008782 if (mode_cmd->pitches[0] > 32768) {
8783 DRM_DEBUG("pitch (%d) must be at less than 32768\n",
8784 mode_cmd->pitches[0]);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008785 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008786 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008787
8788 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008789 mode_cmd->pitches[0] != obj->stride) {
8790 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
8791 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008792 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008793 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008794
Ville Syrjälä57779d02012-10-31 17:50:14 +02008795 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008796 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02008797 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008798 case DRM_FORMAT_RGB565:
8799 case DRM_FORMAT_XRGB8888:
8800 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008801 break;
8802 case DRM_FORMAT_XRGB1555:
8803 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008804 if (INTEL_INFO(dev)->gen > 3) {
8805 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008806 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008807 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02008808 break;
8809 case DRM_FORMAT_XBGR8888:
8810 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008811 case DRM_FORMAT_XRGB2101010:
8812 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008813 case DRM_FORMAT_XBGR2101010:
8814 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008815 if (INTEL_INFO(dev)->gen < 4) {
8816 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008817 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008818 }
Jesse Barnesb5626742011-06-24 12:19:27 -07008819 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02008820 case DRM_FORMAT_YUYV:
8821 case DRM_FORMAT_UYVY:
8822 case DRM_FORMAT_YVYU:
8823 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008824 if (INTEL_INFO(dev)->gen < 5) {
8825 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008826 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008827 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008828 break;
8829 default:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008830 DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01008831 return -EINVAL;
8832 }
8833
Ville Syrjälä90f9a332012-10-31 17:50:19 +02008834 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
8835 if (mode_cmd->offsets[0] != 0)
8836 return -EINVAL;
8837
Daniel Vetterc7d73f62012-12-13 23:38:38 +01008838 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
8839 intel_fb->obj = obj;
8840
Jesse Barnes79e53942008-11-07 14:24:08 -08008841 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8842 if (ret) {
8843 DRM_ERROR("framebuffer init failed %d\n", ret);
8844 return ret;
8845 }
8846
Jesse Barnes79e53942008-11-07 14:24:08 -08008847 return 0;
8848}
8849
Jesse Barnes79e53942008-11-07 14:24:08 -08008850static struct drm_framebuffer *
8851intel_user_framebuffer_create(struct drm_device *dev,
8852 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008853 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08008854{
Chris Wilson05394f32010-11-08 19:18:58 +00008855 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008856
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008857 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8858 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00008859 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01008860 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08008861
Chris Wilsond2dff872011-04-19 08:36:26 +01008862 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08008863}
8864
Jesse Barnes79e53942008-11-07 14:24:08 -08008865static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08008866 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00008867 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08008868};
8869
Jesse Barnese70236a2009-09-21 10:42:27 -07008870/* Set up chip specific display functions */
8871static void intel_init_display(struct drm_device *dev)
8872{
8873 struct drm_i915_private *dev_priv = dev->dev_private;
8874
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008875 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008876 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008877 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02008878 dev_priv->display.crtc_enable = haswell_crtc_enable;
8879 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008880 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008881 dev_priv->display.update_plane = ironlake_update_plane;
8882 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008883 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -07008884 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008885 dev_priv->display.crtc_enable = ironlake_crtc_enable;
8886 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008887 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008888 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -07008889 } else if (IS_VALLEYVIEW(dev)) {
8890 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
8891 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
8892 dev_priv->display.crtc_enable = valleyview_crtc_enable;
8893 dev_priv->display.crtc_disable = i9xx_crtc_disable;
8894 dev_priv->display.off = i9xx_crtc_off;
8895 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008896 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008897 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -07008898 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008899 dev_priv->display.crtc_enable = i9xx_crtc_enable;
8900 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008901 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008902 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008903 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008904
Jesse Barnese70236a2009-09-21 10:42:27 -07008905 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07008906 if (IS_VALLEYVIEW(dev))
8907 dev_priv->display.get_display_clock_speed =
8908 valleyview_get_display_clock_speed;
8909 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07008910 dev_priv->display.get_display_clock_speed =
8911 i945_get_display_clock_speed;
8912 else if (IS_I915G(dev))
8913 dev_priv->display.get_display_clock_speed =
8914 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008915 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008916 dev_priv->display.get_display_clock_speed =
8917 i9xx_misc_get_display_clock_speed;
8918 else if (IS_I915GM(dev))
8919 dev_priv->display.get_display_clock_speed =
8920 i915gm_get_display_clock_speed;
8921 else if (IS_I865G(dev))
8922 dev_priv->display.get_display_clock_speed =
8923 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02008924 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008925 dev_priv->display.get_display_clock_speed =
8926 i855_get_display_clock_speed;
8927 else /* 852, 830 */
8928 dev_priv->display.get_display_clock_speed =
8929 i830_get_display_clock_speed;
8930
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008931 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01008932 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008933 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008934 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08008935 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008936 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008937 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07008938 } else if (IS_IVYBRIDGE(dev)) {
8939 /* FIXME: detect B0+ stepping and use auto training */
8940 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008941 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02008942 dev_priv->display.modeset_global_resources =
8943 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03008944 } else if (IS_HASWELL(dev)) {
8945 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08008946 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02008947 dev_priv->display.modeset_global_resources =
8948 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02008949 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07008950 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08008951 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07008952 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008953
8954 /* Default just returns -ENODEV to indicate unsupported */
8955 dev_priv->display.queue_flip = intel_default_queue_flip;
8956
8957 switch (INTEL_INFO(dev)->gen) {
8958 case 2:
8959 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8960 break;
8961
8962 case 3:
8963 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8964 break;
8965
8966 case 4:
8967 case 5:
8968 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8969 break;
8970
8971 case 6:
8972 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8973 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008974 case 7:
8975 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8976 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008977 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008978}
8979
Jesse Barnesb690e962010-07-19 13:53:12 -07008980/*
8981 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8982 * resume, or other times. This quirk makes sure that's the case for
8983 * affected systems.
8984 */
Akshay Joshi0206e352011-08-16 15:34:10 -04008985static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07008986{
8987 struct drm_i915_private *dev_priv = dev->dev_private;
8988
8989 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008990 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008991}
8992
Keith Packard435793d2011-07-12 14:56:22 -07008993/*
8994 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8995 */
8996static void quirk_ssc_force_disable(struct drm_device *dev)
8997{
8998 struct drm_i915_private *dev_priv = dev->dev_private;
8999 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009000 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07009001}
9002
Carsten Emde4dca20e2012-03-15 15:56:26 +01009003/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01009004 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9005 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01009006 */
9007static void quirk_invert_brightness(struct drm_device *dev)
9008{
9009 struct drm_i915_private *dev_priv = dev->dev_private;
9010 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02009011 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07009012}
9013
9014struct intel_quirk {
9015 int device;
9016 int subsystem_vendor;
9017 int subsystem_device;
9018 void (*hook)(struct drm_device *dev);
9019};
9020
Egbert Eich5f85f1762012-10-14 15:46:38 +02009021/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
9022struct intel_dmi_quirk {
9023 void (*hook)(struct drm_device *dev);
9024 const struct dmi_system_id (*dmi_id_list)[];
9025};
9026
9027static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
9028{
9029 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
9030 return 1;
9031}
9032
9033static const struct intel_dmi_quirk intel_dmi_quirks[] = {
9034 {
9035 .dmi_id_list = &(const struct dmi_system_id[]) {
9036 {
9037 .callback = intel_dmi_reverse_brightness,
9038 .ident = "NCR Corporation",
9039 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
9040 DMI_MATCH(DMI_PRODUCT_NAME, ""),
9041 },
9042 },
9043 { } /* terminating entry */
9044 },
9045 .hook = quirk_invert_brightness,
9046 },
9047};
9048
Ben Widawskyc43b5632012-04-16 14:07:40 -07009049static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07009050 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04009051 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07009052
Jesse Barnesb690e962010-07-19 13:53:12 -07009053 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9054 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9055
Jesse Barnesb690e962010-07-19 13:53:12 -07009056 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9057 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9058
Daniel Vetterccd0d362012-10-10 23:13:59 +02009059 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07009060 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02009061 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07009062
9063 /* Lenovo U160 cannot use SSC on LVDS */
9064 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02009065
9066 /* Sony Vaio Y cannot use SSC on LVDS */
9067 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01009068
9069 /* Acer Aspire 5734Z must invert backlight brightness */
9070 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02009071
9072 /* Acer/eMachines G725 */
9073 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02009074
9075 /* Acer/eMachines e725 */
9076 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02009077
9078 /* Acer/Packard Bell NCL20 */
9079 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Daniel Vetterac4199e2013-02-15 18:35:30 +01009080
9081 /* Acer Aspire 4736Z */
9082 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07009083};
9084
9085static void intel_init_quirks(struct drm_device *dev)
9086{
9087 struct pci_dev *d = dev->pdev;
9088 int i;
9089
9090 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9091 struct intel_quirk *q = &intel_quirks[i];
9092
9093 if (d->device == q->device &&
9094 (d->subsystem_vendor == q->subsystem_vendor ||
9095 q->subsystem_vendor == PCI_ANY_ID) &&
9096 (d->subsystem_device == q->subsystem_device ||
9097 q->subsystem_device == PCI_ANY_ID))
9098 q->hook(dev);
9099 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02009100 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9101 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9102 intel_dmi_quirks[i].hook(dev);
9103 }
Jesse Barnesb690e962010-07-19 13:53:12 -07009104}
9105
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009106/* Disable the VGA plane that we never use */
9107static void i915_disable_vga(struct drm_device *dev)
9108{
9109 struct drm_i915_private *dev_priv = dev->dev_private;
9110 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009111 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009112
9113 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07009114 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009115 sr1 = inb(VGA_SR_DATA);
9116 outb(sr1 | 1<<5, VGA_SR_DATA);
9117 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9118 udelay(300);
9119
9120 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9121 POSTING_READ(vga_reg);
9122}
9123
Daniel Vetterf8175862012-04-10 15:50:11 +02009124void intel_modeset_init_hw(struct drm_device *dev)
9125{
Paulo Zanonifa42e232013-01-25 16:59:11 -02009126 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03009127
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03009128 intel_prepare_ddi(dev);
9129
Daniel Vetterf8175862012-04-10 15:50:11 +02009130 intel_init_clock_gating(dev);
9131
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009132 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009133 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009134 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02009135}
9136
Jesse Barnes79e53942008-11-07 14:24:08 -08009137void intel_modeset_init(struct drm_device *dev)
9138{
Jesse Barnes652c3932009-08-17 13:31:43 -07009139 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009140 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009141
9142 drm_mode_config_init(dev);
9143
9144 dev->mode_config.min_width = 0;
9145 dev->mode_config.min_height = 0;
9146
Dave Airlie019d96c2011-09-29 16:20:42 +01009147 dev->mode_config.preferred_depth = 24;
9148 dev->mode_config.prefer_shadow = 1;
9149
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02009150 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08009151
Jesse Barnesb690e962010-07-19 13:53:12 -07009152 intel_init_quirks(dev);
9153
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009154 intel_init_pm(dev);
9155
Ben Widawskye3c74752013-04-05 13:12:39 -07009156 if (INTEL_INFO(dev)->num_pipes == 0)
9157 return;
9158
Jesse Barnese70236a2009-09-21 10:42:27 -07009159 intel_init_display(dev);
9160
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009161 if (IS_GEN2(dev)) {
9162 dev->mode_config.max_width = 2048;
9163 dev->mode_config.max_height = 2048;
9164 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009165 dev->mode_config.max_width = 4096;
9166 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009167 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009168 dev->mode_config.max_width = 8192;
9169 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009170 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08009171 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009172
Zhao Yakui28c97732009-10-09 11:39:41 +08009173 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009174 INTEL_INFO(dev)->num_pipes,
9175 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009176
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009177 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009178 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009179 for (j = 0; j < dev_priv->num_plane; j++) {
9180 ret = intel_plane_init(dev, i, j);
9181 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +03009182 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9183 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009184 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009185 }
9186
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009187 intel_cpu_pll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009188 intel_pch_pll_init(dev);
9189
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009190 /* Just disable it once at startup */
9191 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009192 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00009193
9194 /* Just in case the BIOS is doing something questionable. */
9195 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009196}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009197
Daniel Vetter24929352012-07-02 20:28:59 +02009198static void
9199intel_connector_break_all_links(struct intel_connector *connector)
9200{
9201 connector->base.dpms = DRM_MODE_DPMS_OFF;
9202 connector->base.encoder = NULL;
9203 connector->encoder->connectors_active = false;
9204 connector->encoder->base.crtc = NULL;
9205}
9206
Daniel Vetter7fad7982012-07-04 17:51:47 +02009207static void intel_enable_pipe_a(struct drm_device *dev)
9208{
9209 struct intel_connector *connector;
9210 struct drm_connector *crt = NULL;
9211 struct intel_load_detect_pipe load_detect_temp;
9212
9213 /* We can't just switch on the pipe A, we need to set things up with a
9214 * proper mode and output configuration. As a gross hack, enable pipe A
9215 * by enabling the load detect pipe once. */
9216 list_for_each_entry(connector,
9217 &dev->mode_config.connector_list,
9218 base.head) {
9219 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9220 crt = &connector->base;
9221 break;
9222 }
9223 }
9224
9225 if (!crt)
9226 return;
9227
9228 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9229 intel_release_load_detect_pipe(crt, &load_detect_temp);
9230
9231
9232}
9233
Daniel Vetterfa555832012-10-10 23:14:00 +02009234static bool
9235intel_check_plane_mapping(struct intel_crtc *crtc)
9236{
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009237 struct drm_device *dev = crtc->base.dev;
9238 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009239 u32 reg, val;
9240
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009241 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +02009242 return true;
9243
9244 reg = DSPCNTR(!crtc->plane);
9245 val = I915_READ(reg);
9246
9247 if ((val & DISPLAY_PLANE_ENABLE) &&
9248 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9249 return false;
9250
9251 return true;
9252}
9253
Daniel Vetter24929352012-07-02 20:28:59 +02009254static void intel_sanitize_crtc(struct intel_crtc *crtc)
9255{
9256 struct drm_device *dev = crtc->base.dev;
9257 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009258 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02009259
Daniel Vetter24929352012-07-02 20:28:59 +02009260 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +02009261 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02009262 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9263
9264 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02009265 * disable the crtc (and hence change the state) if it is wrong. Note
9266 * that gen4+ has a fixed plane -> pipe mapping. */
9267 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02009268 struct intel_connector *connector;
9269 bool plane;
9270
Daniel Vetter24929352012-07-02 20:28:59 +02009271 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9272 crtc->base.base.id);
9273
9274 /* Pipe has the wrong plane attached and the plane is active.
9275 * Temporarily change the plane mapping and disable everything
9276 * ... */
9277 plane = crtc->plane;
9278 crtc->plane = !plane;
9279 dev_priv->display.crtc_disable(&crtc->base);
9280 crtc->plane = plane;
9281
9282 /* ... and break all links. */
9283 list_for_each_entry(connector, &dev->mode_config.connector_list,
9284 base.head) {
9285 if (connector->encoder->base.crtc != &crtc->base)
9286 continue;
9287
9288 intel_connector_break_all_links(connector);
9289 }
9290
9291 WARN_ON(crtc->active);
9292 crtc->base.enabled = false;
9293 }
Daniel Vetter24929352012-07-02 20:28:59 +02009294
Daniel Vetter7fad7982012-07-04 17:51:47 +02009295 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9296 crtc->pipe == PIPE_A && !crtc->active) {
9297 /* BIOS forgot to enable pipe A, this mostly happens after
9298 * resume. Force-enable the pipe to fix this, the update_dpms
9299 * call below we restore the pipe to the right state, but leave
9300 * the required bits on. */
9301 intel_enable_pipe_a(dev);
9302 }
9303
Daniel Vetter24929352012-07-02 20:28:59 +02009304 /* Adjust the state of the output pipe according to whether we
9305 * have active connectors/encoders. */
9306 intel_crtc_update_dpms(&crtc->base);
9307
9308 if (crtc->active != crtc->base.enabled) {
9309 struct intel_encoder *encoder;
9310
9311 /* This can happen either due to bugs in the get_hw_state
9312 * functions or because the pipe is force-enabled due to the
9313 * pipe A quirk. */
9314 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9315 crtc->base.base.id,
9316 crtc->base.enabled ? "enabled" : "disabled",
9317 crtc->active ? "enabled" : "disabled");
9318
9319 crtc->base.enabled = crtc->active;
9320
9321 /* Because we only establish the connector -> encoder ->
9322 * crtc links if something is active, this means the
9323 * crtc is now deactivated. Break the links. connector
9324 * -> encoder links are only establish when things are
9325 * actually up, hence no need to break them. */
9326 WARN_ON(crtc->active);
9327
9328 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9329 WARN_ON(encoder->connectors_active);
9330 encoder->base.crtc = NULL;
9331 }
9332 }
9333}
9334
9335static void intel_sanitize_encoder(struct intel_encoder *encoder)
9336{
9337 struct intel_connector *connector;
9338 struct drm_device *dev = encoder->base.dev;
9339
9340 /* We need to check both for a crtc link (meaning that the
9341 * encoder is active and trying to read from a pipe) and the
9342 * pipe itself being active. */
9343 bool has_active_crtc = encoder->base.crtc &&
9344 to_intel_crtc(encoder->base.crtc)->active;
9345
9346 if (encoder->connectors_active && !has_active_crtc) {
9347 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9348 encoder->base.base.id,
9349 drm_get_encoder_name(&encoder->base));
9350
9351 /* Connector is active, but has no active pipe. This is
9352 * fallout from our resume register restoring. Disable
9353 * the encoder manually again. */
9354 if (encoder->base.crtc) {
9355 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9356 encoder->base.base.id,
9357 drm_get_encoder_name(&encoder->base));
9358 encoder->disable(encoder);
9359 }
9360
9361 /* Inconsistent output/port/pipe state happens presumably due to
9362 * a bug in one of the get_hw_state functions. Or someplace else
9363 * in our code, like the register restore mess on resume. Clamp
9364 * things to off as a safer default. */
9365 list_for_each_entry(connector,
9366 &dev->mode_config.connector_list,
9367 base.head) {
9368 if (connector->encoder != encoder)
9369 continue;
9370
9371 intel_connector_break_all_links(connector);
9372 }
9373 }
9374 /* Enabled encoders without active connectors will be fixed in
9375 * the crtc fixup. */
9376}
9377
Daniel Vetter44cec742013-01-25 17:53:21 +01009378void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009379{
9380 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009381 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009382
9383 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9384 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +02009385 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009386 }
9387}
9388
Daniel Vetter24929352012-07-02 20:28:59 +02009389/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
9390 * and i915 state tracking structures. */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009391void intel_modeset_setup_hw_state(struct drm_device *dev,
9392 bool force_restore)
Daniel Vetter24929352012-07-02 20:28:59 +02009393{
9394 struct drm_i915_private *dev_priv = dev->dev_private;
9395 enum pipe pipe;
9396 u32 tmp;
Jesse Barnesb5644d02013-03-26 13:25:27 -07009397 struct drm_plane *plane;
Daniel Vetter24929352012-07-02 20:28:59 +02009398 struct intel_crtc *crtc;
9399 struct intel_encoder *encoder;
9400 struct intel_connector *connector;
9401
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009402 if (HAS_DDI(dev)) {
Paulo Zanonie28d54c2012-10-24 16:09:25 -02009403 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9404
9405 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9406 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9407 case TRANS_DDI_EDP_INPUT_A_ON:
9408 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9409 pipe = PIPE_A;
9410 break;
9411 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9412 pipe = PIPE_B;
9413 break;
9414 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9415 pipe = PIPE_C;
9416 break;
Damien Lespiauaaa148e2013-03-07 15:30:26 +00009417 default:
9418 /* A bogus value has been programmed, disable
9419 * the transcoder */
9420 WARN(1, "Bogus eDP source %08x\n", tmp);
9421 intel_ddi_disable_transcoder_func(dev_priv,
9422 TRANSCODER_EDP);
9423 goto setup_pipes;
Paulo Zanonie28d54c2012-10-24 16:09:25 -02009424 }
9425
9426 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Daniel Vetter3b117c82013-04-17 20:15:07 +02009427 crtc->config.cpu_transcoder = TRANSCODER_EDP;
Paulo Zanonie28d54c2012-10-24 16:09:25 -02009428
9429 DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
9430 pipe_name(pipe));
9431 }
9432 }
9433
Damien Lespiauaaa148e2013-03-07 15:30:26 +00009434setup_pipes:
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009435 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9436 base.head) {
Daniel Vetter3b117c82013-04-17 20:15:07 +02009437 enum transcoder tmp = crtc->config.cpu_transcoder;
Daniel Vetter88adfff2013-03-28 10:42:01 +01009438 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +02009439 crtc->config.cpu_transcoder = tmp;
9440
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009441 crtc->active = dev_priv->display.get_pipe_config(crtc,
9442 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009443
9444 crtc->base.enabled = crtc->active;
9445
9446 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9447 crtc->base.base.id,
9448 crtc->active ? "enabled" : "disabled");
9449 }
9450
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009451 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009452 intel_ddi_setup_hw_pll_state(dev);
9453
Daniel Vetter24929352012-07-02 20:28:59 +02009454 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9455 base.head) {
9456 pipe = 0;
9457
9458 if (encoder->get_hw_state(encoder, &pipe)) {
9459 encoder->base.crtc =
9460 dev_priv->pipe_to_crtc_mapping[pipe];
9461 } else {
9462 encoder->base.crtc = NULL;
9463 }
9464
9465 encoder->connectors_active = false;
9466 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9467 encoder->base.base.id,
9468 drm_get_encoder_name(&encoder->base),
9469 encoder->base.crtc ? "enabled" : "disabled",
9470 pipe);
9471 }
9472
9473 list_for_each_entry(connector, &dev->mode_config.connector_list,
9474 base.head) {
9475 if (connector->get_hw_state(connector)) {
9476 connector->base.dpms = DRM_MODE_DPMS_ON;
9477 connector->encoder->connectors_active = true;
9478 connector->base.encoder = &connector->encoder->base;
9479 } else {
9480 connector->base.dpms = DRM_MODE_DPMS_OFF;
9481 connector->base.encoder = NULL;
9482 }
9483 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9484 connector->base.base.id,
9485 drm_get_connector_name(&connector->base),
9486 connector->base.encoder ? "enabled" : "disabled");
9487 }
9488
9489 /* HW state is read out, now we need to sanitize this mess. */
9490 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9491 base.head) {
9492 intel_sanitize_encoder(encoder);
9493 }
9494
9495 for_each_pipe(pipe) {
9496 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9497 intel_sanitize_crtc(crtc);
9498 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009499
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009500 if (force_restore) {
Daniel Vetterf30da182013-04-11 20:22:50 +02009501 /*
9502 * We need to use raw interfaces for restoring state to avoid
9503 * checking (bogus) intermediate states.
9504 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009505 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -07009506 struct drm_crtc *crtc =
9507 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +02009508
9509 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
9510 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009511 }
Jesse Barnesb5644d02013-03-26 13:25:27 -07009512 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
9513 intel_plane_restore(plane);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009514
9515 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009516 } else {
9517 intel_modeset_update_staged_output_state(dev);
9518 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009519
9520 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +02009521
9522 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009523}
9524
9525void intel_modeset_gem_init(struct drm_device *dev)
9526{
Chris Wilson1833b132012-05-09 11:56:28 +01009527 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02009528
9529 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +02009530
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009531 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08009532}
9533
9534void intel_modeset_cleanup(struct drm_device *dev)
9535{
Jesse Barnes652c3932009-08-17 13:31:43 -07009536 struct drm_i915_private *dev_priv = dev->dev_private;
9537 struct drm_crtc *crtc;
9538 struct intel_crtc *intel_crtc;
9539
Daniel Vetterfd0c0642013-04-24 11:13:35 +02009540 /*
9541 * Interrupts and polling as the first thing to avoid creating havoc.
9542 * Too much stuff here (turning of rps, connectors, ...) would
9543 * experience fancy races otherwise.
9544 */
9545 drm_irq_uninstall(dev);
9546 cancel_work_sync(&dev_priv->hotplug_work);
9547 /*
9548 * Due to the hpd irq storm handling the hotplug work can re-arm the
9549 * poll handlers. Hence disable polling after hpd handling is shut down.
9550 */
Keith Packardf87ea762010-10-03 19:36:26 -07009551 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +02009552
Jesse Barnes652c3932009-08-17 13:31:43 -07009553 mutex_lock(&dev->struct_mutex);
9554
Jesse Barnes723bfd72010-10-07 16:01:13 -07009555 intel_unregister_dsm_handler();
9556
Jesse Barnes652c3932009-08-17 13:31:43 -07009557 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9558 /* Skip inactive CRTCs */
9559 if (!crtc->fb)
9560 continue;
9561
9562 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02009563 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009564 }
9565
Chris Wilson973d04f2011-07-08 12:22:37 +01009566 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07009567
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009568 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00009569
Daniel Vetter930ebb42012-06-29 23:32:16 +02009570 ironlake_teardown_rc6(dev);
9571
Kristian Høgsberg69341a52009-11-11 12:19:17 -05009572 mutex_unlock(&dev->struct_mutex);
9573
Chris Wilson1630fe72011-07-08 12:22:42 +01009574 /* flush any delayed tasks or pending work */
9575 flush_scheduled_work();
9576
Jani Nikuladc652f92013-04-12 15:18:38 +03009577 /* destroy backlight, if any, before the connectors */
9578 intel_panel_destroy_backlight(dev);
9579
Jesse Barnes79e53942008-11-07 14:24:08 -08009580 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +01009581
9582 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009583}
9584
Dave Airlie28d52042009-09-21 14:33:58 +10009585/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08009586 * Return which encoder is currently attached for connector.
9587 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01009588struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08009589{
Chris Wilsondf0e9242010-09-09 16:20:55 +01009590 return &intel_attached_encoder(connector)->base;
9591}
Jesse Barnes79e53942008-11-07 14:24:08 -08009592
Chris Wilsondf0e9242010-09-09 16:20:55 +01009593void intel_connector_attach_encoder(struct intel_connector *connector,
9594 struct intel_encoder *encoder)
9595{
9596 connector->encoder = encoder;
9597 drm_mode_connector_attach_encoder(&connector->base,
9598 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009599}
Dave Airlie28d52042009-09-21 14:33:58 +10009600
9601/*
9602 * set vga decode state - true == enable VGA decode
9603 */
9604int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9605{
9606 struct drm_i915_private *dev_priv = dev->dev_private;
9607 u16 gmch_ctrl;
9608
9609 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9610 if (state)
9611 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9612 else
9613 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9614 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9615 return 0;
9616}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009617
9618#ifdef CONFIG_DEBUG_FS
9619#include <linux/seq_file.h>
9620
9621struct intel_display_error_state {
9622 struct intel_cursor_error_state {
9623 u32 control;
9624 u32 position;
9625 u32 base;
9626 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +01009627 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009628
9629 struct intel_pipe_error_state {
9630 u32 conf;
9631 u32 source;
9632
9633 u32 htotal;
9634 u32 hblank;
9635 u32 hsync;
9636 u32 vtotal;
9637 u32 vblank;
9638 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +01009639 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009640
9641 struct intel_plane_error_state {
9642 u32 control;
9643 u32 stride;
9644 u32 size;
9645 u32 pos;
9646 u32 addr;
9647 u32 surface;
9648 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +01009649 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009650};
9651
9652struct intel_display_error_state *
9653intel_display_capture_error_state(struct drm_device *dev)
9654{
Akshay Joshi0206e352011-08-16 15:34:10 -04009655 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009656 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009657 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009658 int i;
9659
9660 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9661 if (error == NULL)
9662 return NULL;
9663
Damien Lespiau52331302012-08-15 19:23:25 +01009664 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009665 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
9666
Paulo Zanonia18c4c32013-03-06 20:03:12 -03009667 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
9668 error->cursor[i].control = I915_READ(CURCNTR(i));
9669 error->cursor[i].position = I915_READ(CURPOS(i));
9670 error->cursor[i].base = I915_READ(CURBASE(i));
9671 } else {
9672 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
9673 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
9674 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
9675 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009676
9677 error->plane[i].control = I915_READ(DSPCNTR(i));
9678 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009679 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -03009680 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009681 error->plane[i].pos = I915_READ(DSPPOS(i));
9682 }
Paulo Zanonica291362013-03-06 20:03:14 -03009683 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
9684 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009685 if (INTEL_INFO(dev)->gen >= 4) {
9686 error->plane[i].surface = I915_READ(DSPSURF(i));
9687 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9688 }
9689
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009690 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009691 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009692 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
9693 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
9694 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
9695 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
9696 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
9697 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009698 }
9699
9700 return error;
9701}
9702
9703void
9704intel_display_print_error_state(struct seq_file *m,
9705 struct drm_device *dev,
9706 struct intel_display_error_state *error)
9707{
9708 int i;
9709
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009710 seq_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Damien Lespiau52331302012-08-15 19:23:25 +01009711 for_each_pipe(i) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009712 seq_printf(m, "Pipe [%d]:\n", i);
9713 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9714 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
9715 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9716 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9717 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9718 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9719 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9720 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9721
9722 seq_printf(m, "Plane [%d]:\n", i);
9723 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9724 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009725 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -03009726 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009727 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9728 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -03009729 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Paulo Zanonica291362013-03-06 20:03:14 -03009730 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009731 if (INTEL_INFO(dev)->gen >= 4) {
9732 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9733 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9734 }
9735
9736 seq_printf(m, "Cursor [%d]:\n", i);
9737 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9738 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9739 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9740 }
9741}
9742#endif