blob: 26acc421f0c60db26e9327a104e810322e56ffff [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
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700105intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
106 int target, int refclk, intel_clock_t *match_clock,
107 intel_clock_t *best_clock);
108
Chris Wilson021357a2010-09-07 20:54:59 +0100109static inline u32 /* units of 100MHz */
110intel_fdi_link_freq(struct drm_device *dev)
111{
Chris Wilson8b99e682010-10-13 09:59:17 +0100112 if (IS_GEN5(dev)) {
113 struct drm_i915_private *dev_priv = dev->dev_private;
114 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
115 } else
116 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100117}
118
Keith Packarde4b36692009-06-05 19:22:17 -0700119static const intel_limit_t intel_limits_i8xx_dvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400120 .dot = { .min = 25000, .max = 350000 },
121 .vco = { .min = 930000, .max = 1400000 },
122 .n = { .min = 3, .max = 16 },
123 .m = { .min = 96, .max = 140 },
124 .m1 = { .min = 18, .max = 26 },
125 .m2 = { .min = 6, .max = 16 },
126 .p = { .min = 4, .max = 128 },
127 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700128 .p2 = { .dot_limit = 165000,
129 .p2_slow = 4, .p2_fast = 2 },
Ma Lingd4906092009-03-18 20:13:27 +0800130 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700131};
132
133static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400134 .dot = { .min = 25000, .max = 350000 },
135 .vco = { .min = 930000, .max = 1400000 },
136 .n = { .min = 3, .max = 16 },
137 .m = { .min = 96, .max = 140 },
138 .m1 = { .min = 18, .max = 26 },
139 .m2 = { .min = 6, .max = 16 },
140 .p = { .min = 4, .max = 128 },
141 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700142 .p2 = { .dot_limit = 165000,
143 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800144 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700145};
Eric Anholt273e27c2011-03-30 13:01:10 -0700146
Keith Packarde4b36692009-06-05 19:22:17 -0700147static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400148 .dot = { .min = 20000, .max = 400000 },
149 .vco = { .min = 1400000, .max = 2800000 },
150 .n = { .min = 1, .max = 6 },
151 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100152 .m1 = { .min = 8, .max = 18 },
153 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400154 .p = { .min = 5, .max = 80 },
155 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700156 .p2 = { .dot_limit = 200000,
157 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800158 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700159};
160
161static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400162 .dot = { .min = 20000, .max = 400000 },
163 .vco = { .min = 1400000, .max = 2800000 },
164 .n = { .min = 1, .max = 6 },
165 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100166 .m1 = { .min = 8, .max = 18 },
167 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400168 .p = { .min = 7, .max = 98 },
169 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700170 .p2 = { .dot_limit = 112000,
171 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800172 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700173};
174
Eric Anholt273e27c2011-03-30 13:01:10 -0700175
Keith Packarde4b36692009-06-05 19:22:17 -0700176static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700177 .dot = { .min = 25000, .max = 270000 },
178 .vco = { .min = 1750000, .max = 3500000},
179 .n = { .min = 1, .max = 4 },
180 .m = { .min = 104, .max = 138 },
181 .m1 = { .min = 17, .max = 23 },
182 .m2 = { .min = 5, .max = 11 },
183 .p = { .min = 10, .max = 30 },
184 .p1 = { .min = 1, .max = 3},
185 .p2 = { .dot_limit = 270000,
186 .p2_slow = 10,
187 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800188 },
Ma Lingd4906092009-03-18 20:13:27 +0800189 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700190};
191
192static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700193 .dot = { .min = 22000, .max = 400000 },
194 .vco = { .min = 1750000, .max = 3500000},
195 .n = { .min = 1, .max = 4 },
196 .m = { .min = 104, .max = 138 },
197 .m1 = { .min = 16, .max = 23 },
198 .m2 = { .min = 5, .max = 11 },
199 .p = { .min = 5, .max = 80 },
200 .p1 = { .min = 1, .max = 8},
201 .p2 = { .dot_limit = 165000,
202 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800203 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700204};
205
206static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700207 .dot = { .min = 20000, .max = 115000 },
208 .vco = { .min = 1750000, .max = 3500000 },
209 .n = { .min = 1, .max = 3 },
210 .m = { .min = 104, .max = 138 },
211 .m1 = { .min = 17, .max = 23 },
212 .m2 = { .min = 5, .max = 11 },
213 .p = { .min = 28, .max = 112 },
214 .p1 = { .min = 2, .max = 8 },
215 .p2 = { .dot_limit = 0,
216 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800217 },
Ma Lingd4906092009-03-18 20:13:27 +0800218 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700219};
220
221static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700222 .dot = { .min = 80000, .max = 224000 },
223 .vco = { .min = 1750000, .max = 3500000 },
224 .n = { .min = 1, .max = 3 },
225 .m = { .min = 104, .max = 138 },
226 .m1 = { .min = 17, .max = 23 },
227 .m2 = { .min = 5, .max = 11 },
228 .p = { .min = 14, .max = 42 },
229 .p1 = { .min = 2, .max = 6 },
230 .p2 = { .dot_limit = 0,
231 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800232 },
Ma Lingd4906092009-03-18 20:13:27 +0800233 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700234};
235
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500236static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400237 .dot = { .min = 20000, .max = 400000},
238 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700239 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400240 .n = { .min = 3, .max = 6 },
241 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700242 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400243 .m1 = { .min = 0, .max = 0 },
244 .m2 = { .min = 0, .max = 254 },
245 .p = { .min = 5, .max = 80 },
246 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700247 .p2 = { .dot_limit = 200000,
248 .p2_slow = 10, .p2_fast = 5 },
Shaohua Li61157072009-04-03 15:24:43 +0800249 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700250};
251
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500252static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400253 .dot = { .min = 20000, .max = 400000 },
254 .vco = { .min = 1700000, .max = 3500000 },
255 .n = { .min = 3, .max = 6 },
256 .m = { .min = 2, .max = 256 },
257 .m1 = { .min = 0, .max = 0 },
258 .m2 = { .min = 0, .max = 254 },
259 .p = { .min = 7, .max = 112 },
260 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700261 .p2 = { .dot_limit = 112000,
262 .p2_slow = 14, .p2_fast = 14 },
Shaohua Li61157072009-04-03 15:24:43 +0800263 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700264};
265
Eric Anholt273e27c2011-03-30 13:01:10 -0700266/* Ironlake / Sandybridge
267 *
268 * We calculate clock using (register_value + 2) for N/M1/M2, so here
269 * the range value for them is (actual_value - 2).
270 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800271static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700272 .dot = { .min = 25000, .max = 350000 },
273 .vco = { .min = 1760000, .max = 3510000 },
274 .n = { .min = 1, .max = 5 },
275 .m = { .min = 79, .max = 127 },
276 .m1 = { .min = 12, .max = 22 },
277 .m2 = { .min = 5, .max = 9 },
278 .p = { .min = 5, .max = 80 },
279 .p1 = { .min = 1, .max = 8 },
280 .p2 = { .dot_limit = 225000,
281 .p2_slow = 10, .p2_fast = 5 },
Zhao Yakui45476682009-12-31 16:06:04 +0800282 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700283};
284
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800285static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700286 .dot = { .min = 25000, .max = 350000 },
287 .vco = { .min = 1760000, .max = 3510000 },
288 .n = { .min = 1, .max = 3 },
289 .m = { .min = 79, .max = 118 },
290 .m1 = { .min = 12, .max = 22 },
291 .m2 = { .min = 5, .max = 9 },
292 .p = { .min = 28, .max = 112 },
293 .p1 = { .min = 2, .max = 8 },
294 .p2 = { .dot_limit = 225000,
295 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800296 .find_pll = intel_g4x_find_best_PLL,
297};
298
299static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700300 .dot = { .min = 25000, .max = 350000 },
301 .vco = { .min = 1760000, .max = 3510000 },
302 .n = { .min = 1, .max = 3 },
303 .m = { .min = 79, .max = 127 },
304 .m1 = { .min = 12, .max = 22 },
305 .m2 = { .min = 5, .max = 9 },
306 .p = { .min = 14, .max = 56 },
307 .p1 = { .min = 2, .max = 8 },
308 .p2 = { .dot_limit = 225000,
309 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800310 .find_pll = intel_g4x_find_best_PLL,
311};
312
Eric Anholt273e27c2011-03-30 13:01:10 -0700313/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800314static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700315 .dot = { .min = 25000, .max = 350000 },
316 .vco = { .min = 1760000, .max = 3510000 },
317 .n = { .min = 1, .max = 2 },
318 .m = { .min = 79, .max = 126 },
319 .m1 = { .min = 12, .max = 22 },
320 .m2 = { .min = 5, .max = 9 },
321 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400322 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .p2 = { .dot_limit = 225000,
324 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800325 .find_pll = intel_g4x_find_best_PLL,
326};
327
328static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700329 .dot = { .min = 25000, .max = 350000 },
330 .vco = { .min = 1760000, .max = 3510000 },
331 .n = { .min = 1, .max = 3 },
332 .m = { .min = 79, .max = 126 },
333 .m1 = { .min = 12, .max = 22 },
334 .m2 = { .min = 5, .max = 9 },
335 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400336 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700337 .p2 = { .dot_limit = 225000,
338 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800339 .find_pll = intel_g4x_find_best_PLL,
340};
341
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700342static const intel_limit_t intel_limits_vlv_dac = {
343 .dot = { .min = 25000, .max = 270000 },
344 .vco = { .min = 4000000, .max = 6000000 },
345 .n = { .min = 1, .max = 7 },
346 .m = { .min = 22, .max = 450 }, /* guess */
347 .m1 = { .min = 2, .max = 3 },
348 .m2 = { .min = 11, .max = 156 },
349 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200350 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700351 .p2 = { .dot_limit = 270000,
352 .p2_slow = 2, .p2_fast = 20 },
353 .find_pll = intel_vlv_find_best_pll,
354};
355
356static const intel_limit_t intel_limits_vlv_hdmi = {
Daniel Vetter75e53982013-04-18 21:10:43 +0200357 .dot = { .min = 25000, .max = 270000 },
358 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700359 .n = { .min = 1, .max = 7 },
360 .m = { .min = 60, .max = 300 }, /* guess */
361 .m1 = { .min = 2, .max = 3 },
362 .m2 = { .min = 11, .max = 156 },
363 .p = { .min = 10, .max = 30 },
364 .p1 = { .min = 2, .max = 3 },
365 .p2 = { .dot_limit = 270000,
366 .p2_slow = 2, .p2_fast = 20 },
367 .find_pll = intel_vlv_find_best_pll,
368};
369
370static const intel_limit_t intel_limits_vlv_dp = {
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530371 .dot = { .min = 25000, .max = 270000 },
372 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700373 .n = { .min = 1, .max = 7 },
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530374 .m = { .min = 22, .max = 450 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700375 .m1 = { .min = 2, .max = 3 },
376 .m2 = { .min = 11, .max = 156 },
377 .p = { .min = 10, .max = 30 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200378 .p1 = { .min = 1, .max = 3 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700379 .p2 = { .dot_limit = 270000,
380 .p2_slow = 2, .p2_fast = 20 },
381 .find_pll = intel_vlv_find_best_pll,
382};
383
Jesse Barnes57f350b2012-03-28 13:39:25 -0700384u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
385{
Daniel Vetter09153002012-12-12 14:06:44 +0100386 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnes57f350b2012-03-28 13:39:25 -0700387
Jesse Barnes57f350b2012-03-28 13:39:25 -0700388 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
389 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100390 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700391 }
392
393 I915_WRITE(DPIO_REG, reg);
394 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
395 DPIO_BYTE);
396 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
397 DRM_ERROR("DPIO read wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100398 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700399 }
Jesse Barnes57f350b2012-03-28 13:39:25 -0700400
Daniel Vetter09153002012-12-12 14:06:44 +0100401 return I915_READ(DPIO_DATA);
Jesse Barnes57f350b2012-03-28 13:39:25 -0700402}
403
Pallavi Ge2fa6fb2013-04-18 14:44:28 -0700404void intel_dpio_write(struct drm_i915_private *dev_priv, int reg, u32 val)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700405{
Daniel Vetter09153002012-12-12 14:06:44 +0100406 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700407
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700408 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
409 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100410 return;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700411 }
412
413 I915_WRITE(DPIO_DATA, val);
414 I915_WRITE(DPIO_REG, reg);
415 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
416 DPIO_BYTE);
417 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
418 DRM_ERROR("DPIO write wait timed out\n");
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700419}
420
Chris Wilson1b894b52010-12-14 20:04:54 +0000421static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
422 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800423{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800424 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800425 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800426
427 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100428 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000429 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800430 limit = &intel_limits_ironlake_dual_lvds_100m;
431 else
432 limit = &intel_limits_ironlake_dual_lvds;
433 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000434 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800435 limit = &intel_limits_ironlake_single_lvds_100m;
436 else
437 limit = &intel_limits_ironlake_single_lvds;
438 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200439 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800440 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800441
442 return limit;
443}
444
Ma Ling044c7c42009-03-18 20:13:23 +0800445static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
446{
447 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800448 const intel_limit_t *limit;
449
450 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100451 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700452 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800453 else
Keith Packarde4b36692009-06-05 19:22:17 -0700454 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800455 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
456 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700457 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800458 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700459 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800460 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700461 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800462
463 return limit;
464}
465
Chris Wilson1b894b52010-12-14 20:04:54 +0000466static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800467{
468 struct drm_device *dev = crtc->dev;
469 const intel_limit_t *limit;
470
Eric Anholtbad720f2009-10-22 16:11:14 -0700471 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000472 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800473 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800474 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500475 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800476 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500477 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800478 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500479 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700480 } else if (IS_VALLEYVIEW(dev)) {
481 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
482 limit = &intel_limits_vlv_dac;
483 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
484 limit = &intel_limits_vlv_hdmi;
485 else
486 limit = &intel_limits_vlv_dp;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100487 } else if (!IS_GEN2(dev)) {
488 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
489 limit = &intel_limits_i9xx_lvds;
490 else
491 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800492 } else {
493 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700494 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800495 else
Keith Packarde4b36692009-06-05 19:22:17 -0700496 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800497 }
498 return limit;
499}
500
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500501/* m1 is reserved as 0 in Pineview, n is a ring counter */
502static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800503{
Shaohua Li21778322009-02-23 15:19:16 +0800504 clock->m = clock->m2 + 2;
505 clock->p = clock->p1 * clock->p2;
506 clock->vco = refclk * clock->m / clock->n;
507 clock->dot = clock->vco / clock->p;
508}
509
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200510static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
511{
512 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
513}
514
Shaohua Li21778322009-02-23 15:19:16 +0800515static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
516{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500517 if (IS_PINEVIEW(dev)) {
518 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800519 return;
520 }
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200521 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800522 clock->p = clock->p1 * clock->p2;
523 clock->vco = refclk * clock->m / (clock->n + 2);
524 clock->dot = clock->vco / clock->p;
525}
526
Jesse Barnes79e53942008-11-07 14:24:08 -0800527/**
528 * Returns whether any output on the specified pipe is of the specified type
529 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100530bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800531{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100532 struct drm_device *dev = crtc->dev;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100533 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800534
Daniel Vetter6c2b7c12012-07-05 09:50:24 +0200535 for_each_encoder_on_crtc(dev, crtc, encoder)
536 if (encoder->type == type)
Chris Wilson4ef69c72010-09-09 15:14:28 +0100537 return true;
538
539 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800540}
541
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800542#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800543/**
544 * Returns whether the given set of divisors are valid for a given refclk with
545 * the given connectors.
546 */
547
Chris Wilson1b894b52010-12-14 20:04:54 +0000548static bool intel_PLL_is_valid(struct drm_device *dev,
549 const intel_limit_t *limit,
550 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800551{
Jesse Barnes79e53942008-11-07 14:24:08 -0800552 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400553 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800554 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400555 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800556 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400557 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800558 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400559 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500560 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400561 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800562 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400563 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800564 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400565 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800566 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400567 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800568 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
569 * connector, etc., rather than just a single range.
570 */
571 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400572 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800573
574 return true;
575}
576
Ma Lingd4906092009-03-18 20:13:27 +0800577static bool
578intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800579 int target, int refclk, intel_clock_t *match_clock,
580 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800581
Jesse Barnes79e53942008-11-07 14:24:08 -0800582{
583 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800584 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800585 int err = target;
586
Daniel Vettera210b022012-11-26 17:22:08 +0100587 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800588 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100589 * For LVDS just rely on its current settings for dual-channel.
590 * We haven't figured out how to reliably set up different
591 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800592 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100593 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800594 clock.p2 = limit->p2.p2_fast;
595 else
596 clock.p2 = limit->p2.p2_slow;
597 } else {
598 if (target < limit->p2.dot_limit)
599 clock.p2 = limit->p2.p2_slow;
600 else
601 clock.p2 = limit->p2.p2_fast;
602 }
603
Akshay Joshi0206e352011-08-16 15:34:10 -0400604 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800605
Zhao Yakui42158662009-11-20 11:24:18 +0800606 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
607 clock.m1++) {
608 for (clock.m2 = limit->m2.min;
609 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500610 /* m1 is always 0 in Pineview */
611 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800612 break;
613 for (clock.n = limit->n.min;
614 clock.n <= limit->n.max; clock.n++) {
615 for (clock.p1 = limit->p1.min;
616 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800617 int this_err;
618
Shaohua Li21778322009-02-23 15:19:16 +0800619 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000620 if (!intel_PLL_is_valid(dev, limit,
621 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800622 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800623 if (match_clock &&
624 clock.p != match_clock->p)
625 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800626
627 this_err = abs(clock.dot - target);
628 if (this_err < err) {
629 *best_clock = clock;
630 err = this_err;
631 }
632 }
633 }
634 }
635 }
636
637 return (err != target);
638}
639
Ma Lingd4906092009-03-18 20:13:27 +0800640static bool
641intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800642 int target, int refclk, intel_clock_t *match_clock,
643 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800644{
645 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800646 intel_clock_t clock;
647 int max_n;
648 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400649 /* approximately equals target * 0.00585 */
650 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800651 found = false;
652
653 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800654 int lvds_reg;
655
Eric Anholtc619eed2010-01-28 16:45:52 -0800656 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800657 lvds_reg = PCH_LVDS;
658 else
659 lvds_reg = LVDS;
Daniel Vetter1974cad2012-11-26 17:22:09 +0100660 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800661 clock.p2 = limit->p2.p2_fast;
662 else
663 clock.p2 = limit->p2.p2_slow;
664 } else {
665 if (target < limit->p2.dot_limit)
666 clock.p2 = limit->p2.p2_slow;
667 else
668 clock.p2 = limit->p2.p2_fast;
669 }
670
671 memset(best_clock, 0, sizeof(*best_clock));
672 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200673 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800674 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200675 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800676 for (clock.m1 = limit->m1.max;
677 clock.m1 >= limit->m1.min; clock.m1--) {
678 for (clock.m2 = limit->m2.max;
679 clock.m2 >= limit->m2.min; clock.m2--) {
680 for (clock.p1 = limit->p1.max;
681 clock.p1 >= limit->p1.min; clock.p1--) {
682 int this_err;
683
Shaohua Li21778322009-02-23 15:19:16 +0800684 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000685 if (!intel_PLL_is_valid(dev, limit,
686 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800687 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800688 if (match_clock &&
689 clock.p != match_clock->p)
690 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000691
692 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800693 if (this_err < err_most) {
694 *best_clock = clock;
695 err_most = this_err;
696 max_n = clock.n;
697 found = true;
698 }
699 }
700 }
701 }
702 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800703 return found;
704}
Ma Lingd4906092009-03-18 20:13:27 +0800705
Zhenyu Wang2c072452009-06-05 15:38:42 +0800706static bool
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700707intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
708 int target, int refclk, intel_clock_t *match_clock,
709 intel_clock_t *best_clock)
710{
711 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
712 u32 m, n, fastclk;
713 u32 updrate, minupdate, fracbits, p;
714 unsigned long bestppm, ppm, absppm;
715 int dotclk, flag;
716
Alan Coxaf447bd2012-07-25 13:49:18 +0100717 flag = 0;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700718 dotclk = target * 1000;
719 bestppm = 1000000;
720 ppm = absppm = 0;
721 fastclk = dotclk / (2*100);
722 updrate = 0;
723 minupdate = 19200;
724 fracbits = 1;
725 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
726 bestm1 = bestm2 = bestp1 = bestp2 = 0;
727
728 /* based on hardware requirement, prefer smaller n to precision */
729 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
730 updrate = refclk / n;
731 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
732 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
733 if (p2 > 10)
734 p2 = p2 - 1;
735 p = p1 * p2;
736 /* based on hardware requirement, prefer bigger m1,m2 values */
737 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
738 m2 = (((2*(fastclk * p * n / m1 )) +
739 refclk) / (2*refclk));
740 m = m1 * m2;
741 vco = updrate * m;
742 if (vco >= limit->vco.min && vco < limit->vco.max) {
743 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
744 absppm = (ppm > 0) ? ppm : (-ppm);
745 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
746 bestppm = 0;
747 flag = 1;
748 }
749 if (absppm < bestppm - 10) {
750 bestppm = absppm;
751 flag = 1;
752 }
753 if (flag) {
754 bestn = n;
755 bestm1 = m1;
756 bestm2 = m2;
757 bestp1 = p1;
758 bestp2 = p2;
759 flag = 0;
760 }
761 }
762 }
763 }
764 }
765 }
766 best_clock->n = bestn;
767 best_clock->m1 = bestm1;
768 best_clock->m2 = bestm2;
769 best_clock->p1 = bestp1;
770 best_clock->p2 = bestp2;
771
772 return true;
773}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700774
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200775enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
776 enum pipe pipe)
777{
778 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
780
Daniel Vetter3b117c82013-04-17 20:15:07 +0200781 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200782}
783
Paulo Zanonia928d532012-05-04 17:18:15 -0300784static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
785{
786 struct drm_i915_private *dev_priv = dev->dev_private;
787 u32 frame, frame_reg = PIPEFRAME(pipe);
788
789 frame = I915_READ(frame_reg);
790
791 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
792 DRM_DEBUG_KMS("vblank wait timed out\n");
793}
794
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700795/**
796 * intel_wait_for_vblank - wait for vblank on a given pipe
797 * @dev: drm device
798 * @pipe: pipe to wait for
799 *
800 * Wait for vblank to occur on a given pipe. Needed for various bits of
801 * mode setting code.
802 */
803void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800804{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700805 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800806 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700807
Paulo Zanonia928d532012-05-04 17:18:15 -0300808 if (INTEL_INFO(dev)->gen >= 5) {
809 ironlake_wait_for_vblank(dev, pipe);
810 return;
811 }
812
Chris Wilson300387c2010-09-05 20:25:43 +0100813 /* Clear existing vblank status. Note this will clear any other
814 * sticky status fields as well.
815 *
816 * This races with i915_driver_irq_handler() with the result
817 * that either function could miss a vblank event. Here it is not
818 * fatal, as we will either wait upon the next vblank interrupt or
819 * timeout. Generally speaking intel_wait_for_vblank() is only
820 * called during modeset at which time the GPU should be idle and
821 * should *not* be performing page flips and thus not waiting on
822 * vblanks...
823 * Currently, the result of us stealing a vblank from the irq
824 * handler is that a single frame will be skipped during swapbuffers.
825 */
826 I915_WRITE(pipestat_reg,
827 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
828
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700829 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100830 if (wait_for(I915_READ(pipestat_reg) &
831 PIPE_VBLANK_INTERRUPT_STATUS,
832 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700833 DRM_DEBUG_KMS("vblank wait timed out\n");
834}
835
Keith Packardab7ad7f2010-10-03 00:33:06 -0700836/*
837 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700838 * @dev: drm device
839 * @pipe: pipe to wait for
840 *
841 * After disabling a pipe, we can't wait for vblank in the usual way,
842 * spinning on the vblank interrupt status bit, since we won't actually
843 * see an interrupt when the pipe is disabled.
844 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700845 * On Gen4 and above:
846 * wait for the pipe register state bit to turn off
847 *
848 * Otherwise:
849 * wait for the display line value to settle (it usually
850 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100851 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700852 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100853void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700854{
855 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200856 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
857 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700858
Keith Packardab7ad7f2010-10-03 00:33:06 -0700859 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200860 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700861
Keith Packardab7ad7f2010-10-03 00:33:06 -0700862 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100863 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
864 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200865 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700866 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300867 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100868 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700869 unsigned long timeout = jiffies + msecs_to_jiffies(100);
870
Paulo Zanoni837ba002012-05-04 17:18:14 -0300871 if (IS_GEN2(dev))
872 line_mask = DSL_LINEMASK_GEN2;
873 else
874 line_mask = DSL_LINEMASK_GEN3;
875
Keith Packardab7ad7f2010-10-03 00:33:06 -0700876 /* Wait for the display line to settle */
877 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300878 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700879 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300880 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700881 time_after(timeout, jiffies));
882 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +0200883 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700884 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800885}
886
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000887/*
888 * ibx_digital_port_connected - is the specified port connected?
889 * @dev_priv: i915 private structure
890 * @port: the port to test
891 *
892 * Returns true if @port is connected, false otherwise.
893 */
894bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
895 struct intel_digital_port *port)
896{
897 u32 bit;
898
Damien Lespiauc36346e2012-12-13 16:09:03 +0000899 if (HAS_PCH_IBX(dev_priv->dev)) {
900 switch(port->port) {
901 case PORT_B:
902 bit = SDE_PORTB_HOTPLUG;
903 break;
904 case PORT_C:
905 bit = SDE_PORTC_HOTPLUG;
906 break;
907 case PORT_D:
908 bit = SDE_PORTD_HOTPLUG;
909 break;
910 default:
911 return true;
912 }
913 } else {
914 switch(port->port) {
915 case PORT_B:
916 bit = SDE_PORTB_HOTPLUG_CPT;
917 break;
918 case PORT_C:
919 bit = SDE_PORTC_HOTPLUG_CPT;
920 break;
921 case PORT_D:
922 bit = SDE_PORTD_HOTPLUG_CPT;
923 break;
924 default:
925 return true;
926 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000927 }
928
929 return I915_READ(SDEISR) & bit;
930}
931
Jesse Barnesb24e7172011-01-04 15:09:30 -0800932static const char *state_string(bool enabled)
933{
934 return enabled ? "on" : "off";
935}
936
937/* Only for pre-ILK configs */
938static void assert_pll(struct drm_i915_private *dev_priv,
939 enum pipe pipe, bool state)
940{
941 int reg;
942 u32 val;
943 bool cur_state;
944
945 reg = DPLL(pipe);
946 val = I915_READ(reg);
947 cur_state = !!(val & DPLL_VCO_ENABLE);
948 WARN(cur_state != state,
949 "PLL state assertion failure (expected %s, current %s)\n",
950 state_string(state), state_string(cur_state));
951}
952#define assert_pll_enabled(d, p) assert_pll(d, p, true)
953#define assert_pll_disabled(d, p) assert_pll(d, p, false)
954
Jesse Barnes040484a2011-01-03 12:14:26 -0800955/* For ILK+ */
956static void assert_pch_pll(struct drm_i915_private *dev_priv,
Chris Wilson92b27b02012-05-20 18:10:50 +0100957 struct intel_pch_pll *pll,
958 struct intel_crtc *crtc,
959 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800960{
Jesse Barnes040484a2011-01-03 12:14:26 -0800961 u32 val;
962 bool cur_state;
963
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300964 if (HAS_PCH_LPT(dev_priv->dev)) {
965 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
966 return;
967 }
968
Chris Wilson92b27b02012-05-20 18:10:50 +0100969 if (WARN (!pll,
970 "asserting PCH PLL %s with no PLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100971 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100972
Chris Wilson92b27b02012-05-20 18:10:50 +0100973 val = I915_READ(pll->pll_reg);
974 cur_state = !!(val & DPLL_VCO_ENABLE);
975 WARN(cur_state != state,
976 "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
977 pll->pll_reg, state_string(state), state_string(cur_state), val);
978
979 /* Make sure the selected PLL is correctly attached to the transcoder */
980 if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
Jesse Barnesd3ccbe82011-10-12 09:27:42 -0700981 u32 pch_dpll;
982
983 pch_dpll = I915_READ(PCH_DPLL_SEL);
Chris Wilson92b27b02012-05-20 18:10:50 +0100984 cur_state = pll->pll_reg == _PCH_DPLL_B;
985 if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +0300986 "PLL[%d] not attached to this transcoder %c: %08x\n",
987 cur_state, pipe_name(crtc->pipe), pch_dpll)) {
Chris Wilson92b27b02012-05-20 18:10:50 +0100988 cur_state = !!(val >> (4*crtc->pipe + 3));
989 WARN(cur_state != state,
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +0300990 "PLL[%d] not %s on this transcoder %c: %08x\n",
Chris Wilson92b27b02012-05-20 18:10:50 +0100991 pll->pll_reg == _PCH_DPLL_B,
992 state_string(state),
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +0300993 pipe_name(crtc->pipe),
Chris Wilson92b27b02012-05-20 18:10:50 +0100994 val);
995 }
Jesse Barnesd3ccbe82011-10-12 09:27:42 -0700996 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800997}
Chris Wilson92b27b02012-05-20 18:10:50 +0100998#define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
999#define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
Jesse Barnes040484a2011-01-03 12:14:26 -08001000
1001static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1002 enum pipe pipe, bool state)
1003{
1004 int reg;
1005 u32 val;
1006 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001007 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1008 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001009
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001010 if (HAS_DDI(dev_priv->dev)) {
1011 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001012 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001013 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001014 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001015 } else {
1016 reg = FDI_TX_CTL(pipe);
1017 val = I915_READ(reg);
1018 cur_state = !!(val & FDI_TX_ENABLE);
1019 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001020 WARN(cur_state != state,
1021 "FDI TX state assertion failure (expected %s, current %s)\n",
1022 state_string(state), state_string(cur_state));
1023}
1024#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1025#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1026
1027static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1028 enum pipe pipe, bool state)
1029{
1030 int reg;
1031 u32 val;
1032 bool cur_state;
1033
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001034 reg = FDI_RX_CTL(pipe);
1035 val = I915_READ(reg);
1036 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001037 WARN(cur_state != state,
1038 "FDI RX state assertion failure (expected %s, current %s)\n",
1039 state_string(state), state_string(cur_state));
1040}
1041#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1042#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1043
1044static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1045 enum pipe pipe)
1046{
1047 int reg;
1048 u32 val;
1049
1050 /* ILK FDI PLL is always enabled */
1051 if (dev_priv->info->gen == 5)
1052 return;
1053
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001054 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001055 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001056 return;
1057
Jesse Barnes040484a2011-01-03 12:14:26 -08001058 reg = FDI_TX_CTL(pipe);
1059 val = I915_READ(reg);
1060 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1061}
1062
1063static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1064 enum pipe pipe)
1065{
1066 int reg;
1067 u32 val;
1068
1069 reg = FDI_RX_CTL(pipe);
1070 val = I915_READ(reg);
1071 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1072}
1073
Jesse Barnesea0760c2011-01-04 15:09:32 -08001074static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1075 enum pipe pipe)
1076{
1077 int pp_reg, lvds_reg;
1078 u32 val;
1079 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001080 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001081
1082 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1083 pp_reg = PCH_PP_CONTROL;
1084 lvds_reg = PCH_LVDS;
1085 } else {
1086 pp_reg = PP_CONTROL;
1087 lvds_reg = LVDS;
1088 }
1089
1090 val = I915_READ(pp_reg);
1091 if (!(val & PANEL_POWER_ON) ||
1092 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1093 locked = false;
1094
1095 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1096 panel_pipe = PIPE_B;
1097
1098 WARN(panel_pipe == pipe && locked,
1099 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001100 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001101}
1102
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001103void assert_pipe(struct drm_i915_private *dev_priv,
1104 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105{
1106 int reg;
1107 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001108 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001109 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1110 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001111
Daniel Vetter8e636782012-01-22 01:36:48 +01001112 /* if we need the pipe A quirk it must be always on */
1113 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1114 state = true;
1115
Paulo Zanoni15d199e2013-03-22 14:14:13 -03001116 if (!intel_using_power_well(dev_priv->dev) &&
1117 cpu_transcoder != TRANSCODER_EDP) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001118 cur_state = false;
1119 } else {
1120 reg = PIPECONF(cpu_transcoder);
1121 val = I915_READ(reg);
1122 cur_state = !!(val & PIPECONF_ENABLE);
1123 }
1124
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001125 WARN(cur_state != state,
1126 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001127 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001128}
1129
Chris Wilson931872f2012-01-16 23:01:13 +00001130static void assert_plane(struct drm_i915_private *dev_priv,
1131 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001132{
1133 int reg;
1134 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001135 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136
1137 reg = DSPCNTR(plane);
1138 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001139 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1140 WARN(cur_state != state,
1141 "plane %c assertion failure (expected %s, current %s)\n",
1142 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001143}
1144
Chris Wilson931872f2012-01-16 23:01:13 +00001145#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1146#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1147
Jesse Barnesb24e7172011-01-04 15:09:30 -08001148static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1149 enum pipe pipe)
1150{
1151 int reg, i;
1152 u32 val;
1153 int cur_pipe;
1154
Jesse Barnes19ec1352011-02-02 12:28:02 -08001155 /* Planes are fixed to pipes on ILK+ */
Jesse Barnesda6ecc52013-03-08 10:46:00 -08001156 if (HAS_PCH_SPLIT(dev_priv->dev) || IS_VALLEYVIEW(dev_priv->dev)) {
Adam Jackson28c057942011-10-07 14:38:42 -04001157 reg = DSPCNTR(pipe);
1158 val = I915_READ(reg);
1159 WARN((val & DISPLAY_PLANE_ENABLE),
1160 "plane %c assertion failure, should be disabled but not\n",
1161 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001162 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001163 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001164
Jesse Barnesb24e7172011-01-04 15:09:30 -08001165 /* Need to check both planes against the pipe */
1166 for (i = 0; i < 2; i++) {
1167 reg = DSPCNTR(i);
1168 val = I915_READ(reg);
1169 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1170 DISPPLANE_SEL_PIPE_SHIFT;
1171 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001172 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1173 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001174 }
1175}
1176
Jesse Barnes19332d72013-03-28 09:55:38 -07001177static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1178 enum pipe pipe)
1179{
1180 int reg, i;
1181 u32 val;
1182
1183 if (!IS_VALLEYVIEW(dev_priv->dev))
1184 return;
1185
1186 /* Need to check both planes against the pipe */
1187 for (i = 0; i < dev_priv->num_plane; i++) {
1188 reg = SPCNTR(pipe, i);
1189 val = I915_READ(reg);
1190 WARN((val & SP_ENABLE),
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001191 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1192 sprite_name(pipe, i), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001193 }
1194}
1195
Jesse Barnes92f25842011-01-04 15:09:34 -08001196static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1197{
1198 u32 val;
1199 bool enabled;
1200
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001201 if (HAS_PCH_LPT(dev_priv->dev)) {
1202 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1203 return;
1204 }
1205
Jesse Barnes92f25842011-01-04 15:09:34 -08001206 val = I915_READ(PCH_DREF_CONTROL);
1207 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1208 DREF_SUPERSPREAD_SOURCE_MASK));
1209 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1210}
1211
1212static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1213 enum pipe pipe)
1214{
1215 int reg;
1216 u32 val;
1217 bool enabled;
1218
1219 reg = TRANSCONF(pipe);
1220 val = I915_READ(reg);
1221 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001222 WARN(enabled,
1223 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1224 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001225}
1226
Keith Packard4e634382011-08-06 10:39:45 -07001227static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1228 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001229{
1230 if ((val & DP_PORT_EN) == 0)
1231 return false;
1232
1233 if (HAS_PCH_CPT(dev_priv->dev)) {
1234 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1235 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1236 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1237 return false;
1238 } else {
1239 if ((val & DP_PIPE_MASK) != (pipe << 30))
1240 return false;
1241 }
1242 return true;
1243}
1244
Keith Packard1519b992011-08-06 10:35:34 -07001245static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1246 enum pipe pipe, u32 val)
1247{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001248 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001249 return false;
1250
1251 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001252 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001253 return false;
1254 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001255 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001256 return false;
1257 }
1258 return true;
1259}
1260
1261static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1262 enum pipe pipe, u32 val)
1263{
1264 if ((val & LVDS_PORT_EN) == 0)
1265 return false;
1266
1267 if (HAS_PCH_CPT(dev_priv->dev)) {
1268 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1269 return false;
1270 } else {
1271 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1272 return false;
1273 }
1274 return true;
1275}
1276
1277static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1278 enum pipe pipe, u32 val)
1279{
1280 if ((val & ADPA_DAC_ENABLE) == 0)
1281 return false;
1282 if (HAS_PCH_CPT(dev_priv->dev)) {
1283 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1284 return false;
1285 } else {
1286 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1287 return false;
1288 }
1289 return true;
1290}
1291
Jesse Barnes291906f2011-02-02 12:28:03 -08001292static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001293 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001294{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001295 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001296 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001297 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001298 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001299
Daniel Vetter75c5da22012-09-10 21:58:29 +02001300 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1301 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001302 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001303}
1304
1305static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1306 enum pipe pipe, int reg)
1307{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001308 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001309 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001310 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001311 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001312
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001313 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001314 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001315 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001316}
1317
1318static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1319 enum pipe pipe)
1320{
1321 int reg;
1322 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001323
Keith Packardf0575e92011-07-25 22:12:43 -07001324 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1325 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1326 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001327
1328 reg = PCH_ADPA;
1329 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001330 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001331 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001332 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001333
1334 reg = PCH_LVDS;
1335 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001336 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001337 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001338 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001339
Paulo Zanonie2debe92013-02-18 19:00:27 -03001340 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1341 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1342 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001343}
1344
Jesse Barnesb24e7172011-01-04 15:09:30 -08001345/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001346 * intel_enable_pll - enable a PLL
1347 * @dev_priv: i915 private structure
1348 * @pipe: pipe PLL to enable
1349 *
1350 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1351 * make sure the PLL reg is writable first though, since the panel write
1352 * protect mechanism may be enabled.
1353 *
1354 * Note! This is for pre-ILK only.
Thomas Richter7434a252012-07-18 19:22:30 +02001355 *
1356 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001357 */
1358static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1359{
1360 int reg;
1361 u32 val;
1362
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001363 assert_pipe_disabled(dev_priv, pipe);
1364
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001365 /* No really, not for ILK+ */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001366 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001367
1368 /* PLL is protected by panel, make sure we can write it */
1369 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1370 assert_panel_unlocked(dev_priv, pipe);
1371
1372 reg = DPLL(pipe);
1373 val = I915_READ(reg);
1374 val |= DPLL_VCO_ENABLE;
1375
1376 /* We do this three times for luck */
1377 I915_WRITE(reg, val);
1378 POSTING_READ(reg);
1379 udelay(150); /* wait for warmup */
1380 I915_WRITE(reg, val);
1381 POSTING_READ(reg);
1382 udelay(150); /* wait for warmup */
1383 I915_WRITE(reg, val);
1384 POSTING_READ(reg);
1385 udelay(150); /* wait for warmup */
1386}
1387
1388/**
1389 * intel_disable_pll - disable a PLL
1390 * @dev_priv: i915 private structure
1391 * @pipe: pipe PLL to disable
1392 *
1393 * Disable the PLL for @pipe, making sure the pipe is off first.
1394 *
1395 * Note! This is for pre-ILK only.
1396 */
1397static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1398{
1399 int reg;
1400 u32 val;
1401
1402 /* Don't disable pipe A or pipe A PLLs if needed */
1403 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1404 return;
1405
1406 /* Make sure the pipe isn't still relying on us */
1407 assert_pipe_disabled(dev_priv, pipe);
1408
1409 reg = DPLL(pipe);
1410 val = I915_READ(reg);
1411 val &= ~DPLL_VCO_ENABLE;
1412 I915_WRITE(reg, val);
1413 POSTING_READ(reg);
1414}
1415
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001416/* SBI access */
1417static void
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001418intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value,
1419 enum intel_sbi_destination destination)
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001420{
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001421 u32 tmp;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001422
Daniel Vetter09153002012-12-12 14:06:44 +01001423 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001424
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001425 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001426 100)) {
1427 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001428 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001429 }
1430
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001431 I915_WRITE(SBI_ADDR, (reg << 16));
1432 I915_WRITE(SBI_DATA, value);
1433
1434 if (destination == SBI_ICLK)
1435 tmp = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRWR;
1436 else
1437 tmp = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IOWR;
1438 I915_WRITE(SBI_CTL_STAT, SBI_BUSY | tmp);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001439
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001440 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001441 100)) {
1442 DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001443 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001444 }
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001445}
1446
1447static u32
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001448intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg,
1449 enum intel_sbi_destination destination)
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001450{
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001451 u32 value = 0;
Daniel Vetter09153002012-12-12 14:06:44 +01001452 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001453
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001454 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001455 100)) {
1456 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001457 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001458 }
1459
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001460 I915_WRITE(SBI_ADDR, (reg << 16));
1461
1462 if (destination == SBI_ICLK)
1463 value = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRRD;
1464 else
1465 value = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IORD;
1466 I915_WRITE(SBI_CTL_STAT, value | SBI_BUSY);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001467
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001468 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001469 100)) {
1470 DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001471 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001472 }
1473
Daniel Vetter09153002012-12-12 14:06:44 +01001474 return I915_READ(SBI_DATA);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001475}
1476
Jesse Barnes89b667f2013-04-18 14:51:36 -07001477void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
1478{
1479 u32 port_mask;
1480
1481 if (!port)
1482 port_mask = DPLL_PORTB_READY_MASK;
1483 else
1484 port_mask = DPLL_PORTC_READY_MASK;
1485
1486 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1487 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1488 'B' + port, I915_READ(DPLL(0)));
1489}
1490
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001491/**
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001492 * ironlake_enable_pch_pll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001493 * @dev_priv: i915 private structure
1494 * @pipe: pipe PLL to enable
1495 *
1496 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1497 * drives the transcoder clock.
1498 */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001499static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001500{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001501 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Chris Wilson48da64a2012-05-13 20:16:12 +01001502 struct intel_pch_pll *pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001503 int reg;
1504 u32 val;
1505
Chris Wilson48da64a2012-05-13 20:16:12 +01001506 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001507 BUG_ON(dev_priv->info->gen < 5);
Chris Wilson48da64a2012-05-13 20:16:12 +01001508 pll = intel_crtc->pch_pll;
1509 if (pll == NULL)
1510 return;
1511
1512 if (WARN_ON(pll->refcount == 0))
1513 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001514
1515 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1516 pll->pll_reg, pll->active, pll->on,
1517 intel_crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001518
1519 /* PCH refclock must be enabled first */
1520 assert_pch_refclk_enabled(dev_priv);
1521
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001522 if (pll->active++ && pll->on) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001523 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001524 return;
1525 }
1526
1527 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1528
1529 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001530 val = I915_READ(reg);
1531 val |= DPLL_VCO_ENABLE;
1532 I915_WRITE(reg, val);
1533 POSTING_READ(reg);
1534 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001535
1536 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001537}
1538
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001539static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001540{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001541 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1542 struct intel_pch_pll *pll = intel_crtc->pch_pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001543 int reg;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001544 u32 val;
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001545
Jesse Barnes92f25842011-01-04 15:09:34 -08001546 /* PCH only available on ILK+ */
1547 BUG_ON(dev_priv->info->gen < 5);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001548 if (pll == NULL)
1549 return;
1550
Chris Wilson48da64a2012-05-13 20:16:12 +01001551 if (WARN_ON(pll->refcount == 0))
1552 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001553
1554 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1555 pll->pll_reg, pll->active, pll->on,
1556 intel_crtc->base.base.id);
1557
Chris Wilson48da64a2012-05-13 20:16:12 +01001558 if (WARN_ON(pll->active == 0)) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001559 assert_pch_pll_disabled(dev_priv, pll, NULL);
Chris Wilson48da64a2012-05-13 20:16:12 +01001560 return;
1561 }
1562
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001563 if (--pll->active) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001564 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001565 return;
1566 }
1567
1568 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
Jesse Barnes92f25842011-01-04 15:09:34 -08001569
1570 /* Make sure transcoder isn't still depending on us */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001571 assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001572
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001573 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001574 val = I915_READ(reg);
1575 val &= ~DPLL_VCO_ENABLE;
1576 I915_WRITE(reg, val);
1577 POSTING_READ(reg);
1578 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001579
1580 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001581}
1582
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001583static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1584 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001585{
Daniel Vetter23670b322012-11-01 09:15:30 +01001586 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001587 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetter23670b322012-11-01 09:15:30 +01001588 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001589
1590 /* PCH only available on ILK+ */
1591 BUG_ON(dev_priv->info->gen < 5);
1592
1593 /* Make sure PCH DPLL is enabled */
Chris Wilson92b27b02012-05-20 18:10:50 +01001594 assert_pch_pll_enabled(dev_priv,
1595 to_intel_crtc(crtc)->pch_pll,
1596 to_intel_crtc(crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001597
1598 /* FDI must be feeding us bits for PCH ports */
1599 assert_fdi_tx_enabled(dev_priv, pipe);
1600 assert_fdi_rx_enabled(dev_priv, pipe);
1601
Daniel Vetter23670b322012-11-01 09:15:30 +01001602 if (HAS_PCH_CPT(dev)) {
1603 /* Workaround: Set the timing override bit before enabling the
1604 * pch transcoder. */
1605 reg = TRANS_CHICKEN2(pipe);
1606 val = I915_READ(reg);
1607 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1608 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001609 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001610
Jesse Barnes040484a2011-01-03 12:14:26 -08001611 reg = TRANSCONF(pipe);
1612 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001613 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001614
1615 if (HAS_PCH_IBX(dev_priv->dev)) {
1616 /*
1617 * make the BPC in transcoder be consistent with
1618 * that in pipeconf reg.
1619 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001620 val &= ~PIPECONF_BPC_MASK;
1621 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001622 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001623
1624 val &= ~TRANS_INTERLACE_MASK;
1625 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001626 if (HAS_PCH_IBX(dev_priv->dev) &&
1627 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1628 val |= TRANS_LEGACY_INTERLACED_ILK;
1629 else
1630 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001631 else
1632 val |= TRANS_PROGRESSIVE;
1633
Jesse Barnes040484a2011-01-03 12:14:26 -08001634 I915_WRITE(reg, val | TRANS_ENABLE);
1635 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001636 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001637}
1638
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001639static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001640 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001641{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001642 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001643
1644 /* PCH only available on ILK+ */
1645 BUG_ON(dev_priv->info->gen < 5);
1646
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001647 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001648 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001649 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001650
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001651 /* Workaround: set timing override bit. */
1652 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001653 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001654 I915_WRITE(_TRANSA_CHICKEN2, val);
1655
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001656 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001657 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001658
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001659 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1660 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001661 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001662 else
1663 val |= TRANS_PROGRESSIVE;
1664
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001665 I915_WRITE(TRANSCONF(TRANSCODER_A), val);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001666 if (wait_for(I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE, 100))
1667 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001668}
1669
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001670static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1671 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001672{
Daniel Vetter23670b322012-11-01 09:15:30 +01001673 struct drm_device *dev = dev_priv->dev;
1674 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001675
1676 /* FDI relies on the transcoder */
1677 assert_fdi_tx_disabled(dev_priv, pipe);
1678 assert_fdi_rx_disabled(dev_priv, pipe);
1679
Jesse Barnes291906f2011-02-02 12:28:03 -08001680 /* Ports must be off as well */
1681 assert_pch_ports_disabled(dev_priv, pipe);
1682
Jesse Barnes040484a2011-01-03 12:14:26 -08001683 reg = TRANSCONF(pipe);
1684 val = I915_READ(reg);
1685 val &= ~TRANS_ENABLE;
1686 I915_WRITE(reg, val);
1687 /* wait for PCH transcoder off, transcoder state */
1688 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001689 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001690
1691 if (!HAS_PCH_IBX(dev)) {
1692 /* Workaround: Clear the timing override chicken bit again. */
1693 reg = TRANS_CHICKEN2(pipe);
1694 val = I915_READ(reg);
1695 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1696 I915_WRITE(reg, val);
1697 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001698}
1699
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001700static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001701{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001702 u32 val;
1703
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001704 val = I915_READ(_TRANSACONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001705 val &= ~TRANS_ENABLE;
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001706 I915_WRITE(_TRANSACONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001707 /* wait for PCH transcoder off, transcoder state */
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001708 if (wait_for((I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE) == 0, 50))
1709 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001710
1711 /* Workaround: clear timing override bit. */
1712 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001713 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001714 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001715}
1716
1717/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001718 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001719 * @dev_priv: i915 private structure
1720 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001721 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001722 *
1723 * Enable @pipe, making sure that various hardware specific requirements
1724 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1725 *
1726 * @pipe should be %PIPE_A or %PIPE_B.
1727 *
1728 * Will wait until the pipe is actually running (i.e. first vblank) before
1729 * returning.
1730 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001731static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1732 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001733{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001734 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1735 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001736 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001737 int reg;
1738 u32 val;
1739
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001740 assert_planes_disabled(dev_priv, pipe);
1741 assert_sprites_disabled(dev_priv, pipe);
1742
Paulo Zanoni681e5812012-12-06 11:12:38 -02001743 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001744 pch_transcoder = TRANSCODER_A;
1745 else
1746 pch_transcoder = pipe;
1747
Jesse Barnesb24e7172011-01-04 15:09:30 -08001748 /*
1749 * A pipe without a PLL won't actually be able to drive bits from
1750 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1751 * need the check.
1752 */
1753 if (!HAS_PCH_SPLIT(dev_priv->dev))
1754 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001755 else {
1756 if (pch_port) {
1757 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001758 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001759 assert_fdi_tx_pll_enabled(dev_priv,
1760 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001761 }
1762 /* FIXME: assert CPU port conditions for SNB+ */
1763 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001764
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001765 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001766 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001767 if (val & PIPECONF_ENABLE)
1768 return;
1769
1770 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001771 intel_wait_for_vblank(dev_priv->dev, pipe);
1772}
1773
1774/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001775 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001776 * @dev_priv: i915 private structure
1777 * @pipe: pipe to disable
1778 *
1779 * Disable @pipe, making sure that various hardware specific requirements
1780 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1781 *
1782 * @pipe should be %PIPE_A or %PIPE_B.
1783 *
1784 * Will wait until the pipe has shut down before returning.
1785 */
1786static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1787 enum pipe pipe)
1788{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001789 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1790 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001791 int reg;
1792 u32 val;
1793
1794 /*
1795 * Make sure planes won't keep trying to pump pixels to us,
1796 * or we might hang the display.
1797 */
1798 assert_planes_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001799 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001800
1801 /* Don't disable pipe A or pipe A PLLs if needed */
1802 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1803 return;
1804
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001805 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001806 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001807 if ((val & PIPECONF_ENABLE) == 0)
1808 return;
1809
1810 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001811 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1812}
1813
Keith Packardd74362c2011-07-28 14:47:14 -07001814/*
1815 * Plane regs are double buffered, going from enabled->disabled needs a
1816 * trigger in order to latch. The display address reg provides this.
1817 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001818void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001819 enum plane plane)
1820{
Damien Lespiau14f86142012-10-29 15:24:49 +00001821 if (dev_priv->info->gen >= 4)
1822 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1823 else
1824 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001825}
1826
Jesse Barnesb24e7172011-01-04 15:09:30 -08001827/**
1828 * intel_enable_plane - enable a display plane on a given pipe
1829 * @dev_priv: i915 private structure
1830 * @plane: plane to enable
1831 * @pipe: pipe being fed
1832 *
1833 * Enable @plane on @pipe, making sure that @pipe is running first.
1834 */
1835static void intel_enable_plane(struct drm_i915_private *dev_priv,
1836 enum plane plane, enum pipe pipe)
1837{
1838 int reg;
1839 u32 val;
1840
1841 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1842 assert_pipe_enabled(dev_priv, pipe);
1843
1844 reg = DSPCNTR(plane);
1845 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001846 if (val & DISPLAY_PLANE_ENABLE)
1847 return;
1848
1849 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001850 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001851 intel_wait_for_vblank(dev_priv->dev, pipe);
1852}
1853
Jesse Barnesb24e7172011-01-04 15:09:30 -08001854/**
1855 * intel_disable_plane - disable a display plane
1856 * @dev_priv: i915 private structure
1857 * @plane: plane to disable
1858 * @pipe: pipe consuming the data
1859 *
1860 * Disable @plane; should be an independent operation.
1861 */
1862static void intel_disable_plane(struct drm_i915_private *dev_priv,
1863 enum plane plane, enum pipe pipe)
1864{
1865 int reg;
1866 u32 val;
1867
1868 reg = DSPCNTR(plane);
1869 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001870 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1871 return;
1872
1873 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001874 intel_flush_display_plane(dev_priv, plane);
1875 intel_wait_for_vblank(dev_priv->dev, pipe);
1876}
1877
Chris Wilson693db182013-03-05 14:52:39 +00001878static bool need_vtd_wa(struct drm_device *dev)
1879{
1880#ifdef CONFIG_INTEL_IOMMU
1881 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1882 return true;
1883#endif
1884 return false;
1885}
1886
Chris Wilson127bd2a2010-07-23 23:32:05 +01001887int
Chris Wilson48b956c2010-09-14 12:50:34 +01001888intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001889 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001890 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001891{
Chris Wilsonce453d82011-02-21 14:43:56 +00001892 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001893 u32 alignment;
1894 int ret;
1895
Chris Wilson05394f32010-11-08 19:18:58 +00001896 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001897 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001898 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1899 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001900 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001901 alignment = 4 * 1024;
1902 else
1903 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001904 break;
1905 case I915_TILING_X:
1906 /* pin() will align the object as required by fence */
1907 alignment = 0;
1908 break;
1909 case I915_TILING_Y:
Daniel Vetter8bb6e952013-04-06 23:54:56 +02001910 /* Despite that we check this in framebuffer_init userspace can
1911 * screw us over and change the tiling after the fact. Only
1912 * pinned buffers can't change their tiling. */
1913 DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001914 return -EINVAL;
1915 default:
1916 BUG();
1917 }
1918
Chris Wilson693db182013-03-05 14:52:39 +00001919 /* Note that the w/a also requires 64 PTE of padding following the
1920 * bo. We currently fill all unused PTE with the shadow page and so
1921 * we should always have valid PTE following the scanout preventing
1922 * the VT-d warning.
1923 */
1924 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1925 alignment = 256 * 1024;
1926
Chris Wilsonce453d82011-02-21 14:43:56 +00001927 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001928 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001929 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001930 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001931
1932 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1933 * fence, whereas 965+ only requires a fence if using
1934 * framebuffer compression. For simplicity, we always install
1935 * a fence as the cost is not that onerous.
1936 */
Chris Wilson06d98132012-04-17 15:31:24 +01001937 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001938 if (ret)
1939 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001940
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001941 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001942
Chris Wilsonce453d82011-02-21 14:43:56 +00001943 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001944 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001945
1946err_unpin:
1947 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001948err_interruptible:
1949 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001950 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001951}
1952
Chris Wilson1690e1e2011-12-14 13:57:08 +01001953void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1954{
1955 i915_gem_object_unpin_fence(obj);
1956 i915_gem_object_unpin(obj);
1957}
1958
Daniel Vetterc2c75132012-07-05 12:17:30 +02001959/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1960 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00001961unsigned long intel_gen4_compute_page_offset(int *x, int *y,
1962 unsigned int tiling_mode,
1963 unsigned int cpp,
1964 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02001965{
Chris Wilsonbc752862013-02-21 20:04:31 +00001966 if (tiling_mode != I915_TILING_NONE) {
1967 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001968
Chris Wilsonbc752862013-02-21 20:04:31 +00001969 tile_rows = *y / 8;
1970 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02001971
Chris Wilsonbc752862013-02-21 20:04:31 +00001972 tiles = *x / (512/cpp);
1973 *x %= 512/cpp;
1974
1975 return tile_rows * pitch * 8 + tiles * 4096;
1976 } else {
1977 unsigned int offset;
1978
1979 offset = *y * pitch + *x * cpp;
1980 *y = 0;
1981 *x = (offset & 4095) / cpp;
1982 return offset & -4096;
1983 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02001984}
1985
Jesse Barnes17638cd2011-06-24 12:19:23 -07001986static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1987 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001988{
1989 struct drm_device *dev = crtc->dev;
1990 struct drm_i915_private *dev_priv = dev->dev_private;
1991 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1992 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001993 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001994 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02001995 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001996 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001997 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001998
1999 switch (plane) {
2000 case 0:
2001 case 1:
2002 break;
2003 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002004 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes81255562010-08-02 12:07:50 -07002005 return -EINVAL;
2006 }
2007
2008 intel_fb = to_intel_framebuffer(fb);
2009 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002010
Chris Wilson5eddb702010-09-11 13:48:45 +01002011 reg = DSPCNTR(plane);
2012 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002013 /* Mask out pixel format bits in case we change it */
2014 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002015 switch (fb->pixel_format) {
2016 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002017 dspcntr |= DISPPLANE_8BPP;
2018 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002019 case DRM_FORMAT_XRGB1555:
2020 case DRM_FORMAT_ARGB1555:
2021 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002022 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002023 case DRM_FORMAT_RGB565:
2024 dspcntr |= DISPPLANE_BGRX565;
2025 break;
2026 case DRM_FORMAT_XRGB8888:
2027 case DRM_FORMAT_ARGB8888:
2028 dspcntr |= DISPPLANE_BGRX888;
2029 break;
2030 case DRM_FORMAT_XBGR8888:
2031 case DRM_FORMAT_ABGR8888:
2032 dspcntr |= DISPPLANE_RGBX888;
2033 break;
2034 case DRM_FORMAT_XRGB2101010:
2035 case DRM_FORMAT_ARGB2101010:
2036 dspcntr |= DISPPLANE_BGRX101010;
2037 break;
2038 case DRM_FORMAT_XBGR2101010:
2039 case DRM_FORMAT_ABGR2101010:
2040 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002041 break;
2042 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002043 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002044 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002045
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002046 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002047 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002048 dspcntr |= DISPPLANE_TILED;
2049 else
2050 dspcntr &= ~DISPPLANE_TILED;
2051 }
2052
Chris Wilson5eddb702010-09-11 13:48:45 +01002053 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002054
Daniel Vettere506a0c2012-07-05 12:17:29 +02002055 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002056
Daniel Vetterc2c75132012-07-05 12:17:30 +02002057 if (INTEL_INFO(dev)->gen >= 4) {
2058 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002059 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2060 fb->bits_per_pixel / 8,
2061 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002062 linear_offset -= intel_crtc->dspaddr_offset;
2063 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002064 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002065 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002066
2067 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2068 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002069 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002070 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002071 I915_MODIFY_DISPBASE(DSPSURF(plane),
2072 obj->gtt_offset + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002073 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002074 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002075 } else
Daniel Vettere506a0c2012-07-05 12:17:29 +02002076 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002077 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002078
Jesse Barnes17638cd2011-06-24 12:19:23 -07002079 return 0;
2080}
2081
2082static int ironlake_update_plane(struct drm_crtc *crtc,
2083 struct drm_framebuffer *fb, int x, int y)
2084{
2085 struct drm_device *dev = crtc->dev;
2086 struct drm_i915_private *dev_priv = dev->dev_private;
2087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2088 struct intel_framebuffer *intel_fb;
2089 struct drm_i915_gem_object *obj;
2090 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002091 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002092 u32 dspcntr;
2093 u32 reg;
2094
2095 switch (plane) {
2096 case 0:
2097 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002098 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002099 break;
2100 default:
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002101 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
Jesse Barnes17638cd2011-06-24 12:19:23 -07002102 return -EINVAL;
2103 }
2104
2105 intel_fb = to_intel_framebuffer(fb);
2106 obj = intel_fb->obj;
2107
2108 reg = DSPCNTR(plane);
2109 dspcntr = I915_READ(reg);
2110 /* Mask out pixel format bits in case we change it */
2111 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002112 switch (fb->pixel_format) {
2113 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002114 dspcntr |= DISPPLANE_8BPP;
2115 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002116 case DRM_FORMAT_RGB565:
2117 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002118 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002119 case DRM_FORMAT_XRGB8888:
2120 case DRM_FORMAT_ARGB8888:
2121 dspcntr |= DISPPLANE_BGRX888;
2122 break;
2123 case DRM_FORMAT_XBGR8888:
2124 case DRM_FORMAT_ABGR8888:
2125 dspcntr |= DISPPLANE_RGBX888;
2126 break;
2127 case DRM_FORMAT_XRGB2101010:
2128 case DRM_FORMAT_ARGB2101010:
2129 dspcntr |= DISPPLANE_BGRX101010;
2130 break;
2131 case DRM_FORMAT_XBGR2101010:
2132 case DRM_FORMAT_ABGR2101010:
2133 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002134 break;
2135 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002136 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002137 }
2138
2139 if (obj->tiling_mode != I915_TILING_NONE)
2140 dspcntr |= DISPPLANE_TILED;
2141 else
2142 dspcntr &= ~DISPPLANE_TILED;
2143
2144 /* must disable */
2145 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2146
2147 I915_WRITE(reg, dspcntr);
2148
Daniel Vettere506a0c2012-07-05 12:17:29 +02002149 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002150 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002151 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2152 fb->bits_per_pixel / 8,
2153 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002154 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002155
Daniel Vettere506a0c2012-07-05 12:17:29 +02002156 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2157 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002158 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002159 I915_MODIFY_DISPBASE(DSPSURF(plane),
2160 obj->gtt_offset + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002161 if (IS_HASWELL(dev)) {
2162 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2163 } else {
2164 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2165 I915_WRITE(DSPLINOFF(plane), linear_offset);
2166 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002167 POSTING_READ(reg);
2168
2169 return 0;
2170}
2171
2172/* Assume fb object is pinned & idle & fenced and just update base pointers */
2173static int
2174intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2175 int x, int y, enum mode_set_atomic state)
2176{
2177 struct drm_device *dev = crtc->dev;
2178 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002179
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002180 if (dev_priv->display.disable_fbc)
2181 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002182 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002183
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002184 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002185}
2186
Ville Syrjälä96a02912013-02-18 19:08:49 +02002187void intel_display_handle_reset(struct drm_device *dev)
2188{
2189 struct drm_i915_private *dev_priv = dev->dev_private;
2190 struct drm_crtc *crtc;
2191
2192 /*
2193 * Flips in the rings have been nuked by the reset,
2194 * so complete all pending flips so that user space
2195 * will get its events and not get stuck.
2196 *
2197 * Also update the base address of all primary
2198 * planes to the the last fb to make sure we're
2199 * showing the correct fb after a reset.
2200 *
2201 * Need to make two loops over the crtcs so that we
2202 * don't try to grab a crtc mutex before the
2203 * pending_flip_queue really got woken up.
2204 */
2205
2206 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2208 enum plane plane = intel_crtc->plane;
2209
2210 intel_prepare_page_flip(dev, plane);
2211 intel_finish_page_flip_plane(dev, plane);
2212 }
2213
2214 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2216
2217 mutex_lock(&crtc->mutex);
2218 if (intel_crtc->active)
2219 dev_priv->display.update_plane(crtc, crtc->fb,
2220 crtc->x, crtc->y);
2221 mutex_unlock(&crtc->mutex);
2222 }
2223}
2224
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002225static int
Chris Wilson14667a42012-04-03 17:58:35 +01002226intel_finish_fb(struct drm_framebuffer *old_fb)
2227{
2228 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2229 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2230 bool was_interruptible = dev_priv->mm.interruptible;
2231 int ret;
2232
Chris Wilson14667a42012-04-03 17:58:35 +01002233 /* Big Hammer, we also need to ensure that any pending
2234 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2235 * current scanout is retired before unpinning the old
2236 * framebuffer.
2237 *
2238 * This should only fail upon a hung GPU, in which case we
2239 * can safely continue.
2240 */
2241 dev_priv->mm.interruptible = false;
2242 ret = i915_gem_object_finish_gpu(obj);
2243 dev_priv->mm.interruptible = was_interruptible;
2244
2245 return ret;
2246}
2247
Ville Syrjälä198598d2012-10-31 17:50:24 +02002248static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2249{
2250 struct drm_device *dev = crtc->dev;
2251 struct drm_i915_master_private *master_priv;
2252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2253
2254 if (!dev->primary->master)
2255 return;
2256
2257 master_priv = dev->primary->master->driver_priv;
2258 if (!master_priv->sarea_priv)
2259 return;
2260
2261 switch (intel_crtc->pipe) {
2262 case 0:
2263 master_priv->sarea_priv->pipeA_x = x;
2264 master_priv->sarea_priv->pipeA_y = y;
2265 break;
2266 case 1:
2267 master_priv->sarea_priv->pipeB_x = x;
2268 master_priv->sarea_priv->pipeB_y = y;
2269 break;
2270 default:
2271 break;
2272 }
2273}
2274
Chris Wilson14667a42012-04-03 17:58:35 +01002275static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002276intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002277 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002278{
2279 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002280 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002281 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002282 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002283 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002284
2285 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002286 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002287 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002288 return 0;
2289 }
2290
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002291 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002292 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2293 plane_name(intel_crtc->plane),
2294 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002295 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002296 }
2297
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002298 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002299 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002300 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002301 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002302 if (ret != 0) {
2303 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002304 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002305 return ret;
2306 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002307
Daniel Vetter94352cf2012-07-05 22:51:56 +02002308 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002309 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002310 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002311 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002312 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002313 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002314 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002315
Daniel Vetter94352cf2012-07-05 22:51:56 +02002316 old_fb = crtc->fb;
2317 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002318 crtc->x = x;
2319 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002320
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002321 if (old_fb) {
2322 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002323 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002324 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002325
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002326 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002327 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002328
Ville Syrjälä198598d2012-10-31 17:50:24 +02002329 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002330
2331 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002332}
2333
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002334static void intel_fdi_normal_train(struct drm_crtc *crtc)
2335{
2336 struct drm_device *dev = crtc->dev;
2337 struct drm_i915_private *dev_priv = dev->dev_private;
2338 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2339 int pipe = intel_crtc->pipe;
2340 u32 reg, temp;
2341
2342 /* enable normal train */
2343 reg = FDI_TX_CTL(pipe);
2344 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002345 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002346 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2347 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002348 } else {
2349 temp &= ~FDI_LINK_TRAIN_NONE;
2350 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002351 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002352 I915_WRITE(reg, temp);
2353
2354 reg = FDI_RX_CTL(pipe);
2355 temp = I915_READ(reg);
2356 if (HAS_PCH_CPT(dev)) {
2357 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2358 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2359 } else {
2360 temp &= ~FDI_LINK_TRAIN_NONE;
2361 temp |= FDI_LINK_TRAIN_NONE;
2362 }
2363 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2364
2365 /* wait one idle pattern time */
2366 POSTING_READ(reg);
2367 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002368
2369 /* IVB wants error correction enabled */
2370 if (IS_IVYBRIDGE(dev))
2371 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2372 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002373}
2374
Daniel Vetter01a415f2012-10-27 15:58:40 +02002375static void ivb_modeset_global_resources(struct drm_device *dev)
2376{
2377 struct drm_i915_private *dev_priv = dev->dev_private;
2378 struct intel_crtc *pipe_B_crtc =
2379 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2380 struct intel_crtc *pipe_C_crtc =
2381 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2382 uint32_t temp;
2383
2384 /* When everything is off disable fdi C so that we could enable fdi B
2385 * with all lanes. XXX: This misses the case where a pipe is not using
2386 * any pch resources and so doesn't need any fdi lanes. */
2387 if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
2388 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2389 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2390
2391 temp = I915_READ(SOUTH_CHICKEN1);
2392 temp &= ~FDI_BC_BIFURCATION_SELECT;
2393 DRM_DEBUG_KMS("disabling fdi C rx\n");
2394 I915_WRITE(SOUTH_CHICKEN1, temp);
2395 }
2396}
2397
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002398/* The FDI link training functions for ILK/Ibexpeak. */
2399static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2400{
2401 struct drm_device *dev = crtc->dev;
2402 struct drm_i915_private *dev_priv = dev->dev_private;
2403 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2404 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002405 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002406 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002407
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002408 /* FDI needs bits from pipe & plane first */
2409 assert_pipe_enabled(dev_priv, pipe);
2410 assert_plane_enabled(dev_priv, plane);
2411
Adam Jacksone1a44742010-06-25 15:32:14 -04002412 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2413 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002414 reg = FDI_RX_IMR(pipe);
2415 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002416 temp &= ~FDI_RX_SYMBOL_LOCK;
2417 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002418 I915_WRITE(reg, temp);
2419 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002420 udelay(150);
2421
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002422 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002423 reg = FDI_TX_CTL(pipe);
2424 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002425 temp &= ~(7 << 19);
2426 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002427 temp &= ~FDI_LINK_TRAIN_NONE;
2428 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002429 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002430
Chris Wilson5eddb702010-09-11 13:48:45 +01002431 reg = FDI_RX_CTL(pipe);
2432 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002433 temp &= ~FDI_LINK_TRAIN_NONE;
2434 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002435 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2436
2437 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002438 udelay(150);
2439
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002440 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002441 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2442 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2443 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002444
Chris Wilson5eddb702010-09-11 13:48:45 +01002445 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002446 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002447 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002448 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2449
2450 if ((temp & FDI_RX_BIT_LOCK)) {
2451 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002452 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002453 break;
2454 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002455 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002456 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002457 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002458
2459 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002460 reg = FDI_TX_CTL(pipe);
2461 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002462 temp &= ~FDI_LINK_TRAIN_NONE;
2463 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002464 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002465
Chris Wilson5eddb702010-09-11 13:48:45 +01002466 reg = FDI_RX_CTL(pipe);
2467 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002468 temp &= ~FDI_LINK_TRAIN_NONE;
2469 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002470 I915_WRITE(reg, temp);
2471
2472 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002473 udelay(150);
2474
Chris Wilson5eddb702010-09-11 13:48:45 +01002475 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002476 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002477 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002478 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2479
2480 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002481 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002482 DRM_DEBUG_KMS("FDI train 2 done.\n");
2483 break;
2484 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002485 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002486 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002487 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002488
2489 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002490
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002491}
2492
Akshay Joshi0206e352011-08-16 15:34:10 -04002493static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002494 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2495 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2496 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2497 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2498};
2499
2500/* The FDI link training functions for SNB/Cougarpoint. */
2501static void gen6_fdi_link_train(struct drm_crtc *crtc)
2502{
2503 struct drm_device *dev = crtc->dev;
2504 struct drm_i915_private *dev_priv = dev->dev_private;
2505 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2506 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002507 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002508
Adam Jacksone1a44742010-06-25 15:32:14 -04002509 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2510 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002511 reg = FDI_RX_IMR(pipe);
2512 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002513 temp &= ~FDI_RX_SYMBOL_LOCK;
2514 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002515 I915_WRITE(reg, temp);
2516
2517 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002518 udelay(150);
2519
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002520 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002521 reg = FDI_TX_CTL(pipe);
2522 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002523 temp &= ~(7 << 19);
2524 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002525 temp &= ~FDI_LINK_TRAIN_NONE;
2526 temp |= FDI_LINK_TRAIN_PATTERN_1;
2527 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2528 /* SNB-B */
2529 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002530 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002531
Daniel Vetterd74cf322012-10-26 10:58:13 +02002532 I915_WRITE(FDI_RX_MISC(pipe),
2533 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2534
Chris Wilson5eddb702010-09-11 13:48:45 +01002535 reg = FDI_RX_CTL(pipe);
2536 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002537 if (HAS_PCH_CPT(dev)) {
2538 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2539 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2540 } else {
2541 temp &= ~FDI_LINK_TRAIN_NONE;
2542 temp |= FDI_LINK_TRAIN_PATTERN_1;
2543 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002544 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2545
2546 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002547 udelay(150);
2548
Akshay Joshi0206e352011-08-16 15:34:10 -04002549 for (i = 0; i < 4; i++) {
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_VOL_EMP_MASK;
2553 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002554 I915_WRITE(reg, temp);
2555
2556 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002557 udelay(500);
2558
Sean Paulfa37d392012-03-02 12:53:39 -05002559 for (retry = 0; retry < 5; retry++) {
2560 reg = FDI_RX_IIR(pipe);
2561 temp = I915_READ(reg);
2562 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2563 if (temp & FDI_RX_BIT_LOCK) {
2564 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2565 DRM_DEBUG_KMS("FDI train 1 done.\n");
2566 break;
2567 }
2568 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002569 }
Sean Paulfa37d392012-03-02 12:53:39 -05002570 if (retry < 5)
2571 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002572 }
2573 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002574 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002575
2576 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002577 reg = FDI_TX_CTL(pipe);
2578 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002579 temp &= ~FDI_LINK_TRAIN_NONE;
2580 temp |= FDI_LINK_TRAIN_PATTERN_2;
2581 if (IS_GEN6(dev)) {
2582 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2583 /* SNB-B */
2584 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2585 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002586 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002587
Chris Wilson5eddb702010-09-11 13:48:45 +01002588 reg = FDI_RX_CTL(pipe);
2589 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002590 if (HAS_PCH_CPT(dev)) {
2591 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2592 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2593 } else {
2594 temp &= ~FDI_LINK_TRAIN_NONE;
2595 temp |= FDI_LINK_TRAIN_PATTERN_2;
2596 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002597 I915_WRITE(reg, temp);
2598
2599 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002600 udelay(150);
2601
Akshay Joshi0206e352011-08-16 15:34:10 -04002602 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002603 reg = FDI_TX_CTL(pipe);
2604 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002605 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2606 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002607 I915_WRITE(reg, temp);
2608
2609 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002610 udelay(500);
2611
Sean Paulfa37d392012-03-02 12:53:39 -05002612 for (retry = 0; retry < 5; retry++) {
2613 reg = FDI_RX_IIR(pipe);
2614 temp = I915_READ(reg);
2615 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2616 if (temp & FDI_RX_SYMBOL_LOCK) {
2617 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2618 DRM_DEBUG_KMS("FDI train 2 done.\n");
2619 break;
2620 }
2621 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002622 }
Sean Paulfa37d392012-03-02 12:53:39 -05002623 if (retry < 5)
2624 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002625 }
2626 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002627 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002628
2629 DRM_DEBUG_KMS("FDI train done.\n");
2630}
2631
Jesse Barnes357555c2011-04-28 15:09:55 -07002632/* Manual link training for Ivy Bridge A0 parts */
2633static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2634{
2635 struct drm_device *dev = crtc->dev;
2636 struct drm_i915_private *dev_priv = dev->dev_private;
2637 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2638 int pipe = intel_crtc->pipe;
2639 u32 reg, temp, i;
2640
2641 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2642 for train result */
2643 reg = FDI_RX_IMR(pipe);
2644 temp = I915_READ(reg);
2645 temp &= ~FDI_RX_SYMBOL_LOCK;
2646 temp &= ~FDI_RX_BIT_LOCK;
2647 I915_WRITE(reg, temp);
2648
2649 POSTING_READ(reg);
2650 udelay(150);
2651
Daniel Vetter01a415f2012-10-27 15:58:40 +02002652 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2653 I915_READ(FDI_RX_IIR(pipe)));
2654
Jesse Barnes357555c2011-04-28 15:09:55 -07002655 /* enable CPU FDI TX and PCH FDI RX */
2656 reg = FDI_TX_CTL(pipe);
2657 temp = I915_READ(reg);
2658 temp &= ~(7 << 19);
2659 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2660 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2661 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2662 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2663 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002664 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002665 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2666
Daniel Vetterd74cf322012-10-26 10:58:13 +02002667 I915_WRITE(FDI_RX_MISC(pipe),
2668 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2669
Jesse Barnes357555c2011-04-28 15:09:55 -07002670 reg = FDI_RX_CTL(pipe);
2671 temp = I915_READ(reg);
2672 temp &= ~FDI_LINK_TRAIN_AUTO;
2673 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2674 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002675 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002676 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2677
2678 POSTING_READ(reg);
2679 udelay(150);
2680
Akshay Joshi0206e352011-08-16 15:34:10 -04002681 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002682 reg = FDI_TX_CTL(pipe);
2683 temp = I915_READ(reg);
2684 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2685 temp |= snb_b_fdi_train_param[i];
2686 I915_WRITE(reg, temp);
2687
2688 POSTING_READ(reg);
2689 udelay(500);
2690
2691 reg = FDI_RX_IIR(pipe);
2692 temp = I915_READ(reg);
2693 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2694
2695 if (temp & FDI_RX_BIT_LOCK ||
2696 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2697 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002698 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002699 break;
2700 }
2701 }
2702 if (i == 4)
2703 DRM_ERROR("FDI train 1 fail!\n");
2704
2705 /* Train 2 */
2706 reg = FDI_TX_CTL(pipe);
2707 temp = I915_READ(reg);
2708 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2709 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2710 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2711 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2712 I915_WRITE(reg, temp);
2713
2714 reg = FDI_RX_CTL(pipe);
2715 temp = I915_READ(reg);
2716 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2717 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2718 I915_WRITE(reg, temp);
2719
2720 POSTING_READ(reg);
2721 udelay(150);
2722
Akshay Joshi0206e352011-08-16 15:34:10 -04002723 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002724 reg = FDI_TX_CTL(pipe);
2725 temp = I915_READ(reg);
2726 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2727 temp |= snb_b_fdi_train_param[i];
2728 I915_WRITE(reg, temp);
2729
2730 POSTING_READ(reg);
2731 udelay(500);
2732
2733 reg = FDI_RX_IIR(pipe);
2734 temp = I915_READ(reg);
2735 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2736
2737 if (temp & FDI_RX_SYMBOL_LOCK) {
2738 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002739 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002740 break;
2741 }
2742 }
2743 if (i == 4)
2744 DRM_ERROR("FDI train 2 fail!\n");
2745
2746 DRM_DEBUG_KMS("FDI train done.\n");
2747}
2748
Daniel Vetter88cefb62012-08-12 19:27:14 +02002749static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002750{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002751 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002752 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002753 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002754 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002755
Jesse Barnesc64e3112010-09-10 11:27:03 -07002756
Jesse Barnes0e23b992010-09-10 11:10:00 -07002757 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002758 reg = FDI_RX_CTL(pipe);
2759 temp = I915_READ(reg);
2760 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002761 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002762 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002763 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2764
2765 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002766 udelay(200);
2767
2768 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002769 temp = I915_READ(reg);
2770 I915_WRITE(reg, temp | FDI_PCDCLK);
2771
2772 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002773 udelay(200);
2774
Paulo Zanoni20749732012-11-23 15:30:38 -02002775 /* Enable CPU FDI TX PLL, always on for Ironlake */
2776 reg = FDI_TX_CTL(pipe);
2777 temp = I915_READ(reg);
2778 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2779 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002780
Paulo Zanoni20749732012-11-23 15:30:38 -02002781 POSTING_READ(reg);
2782 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002783 }
2784}
2785
Daniel Vetter88cefb62012-08-12 19:27:14 +02002786static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2787{
2788 struct drm_device *dev = intel_crtc->base.dev;
2789 struct drm_i915_private *dev_priv = dev->dev_private;
2790 int pipe = intel_crtc->pipe;
2791 u32 reg, temp;
2792
2793 /* Switch from PCDclk to Rawclk */
2794 reg = FDI_RX_CTL(pipe);
2795 temp = I915_READ(reg);
2796 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2797
2798 /* Disable CPU FDI TX PLL */
2799 reg = FDI_TX_CTL(pipe);
2800 temp = I915_READ(reg);
2801 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2802
2803 POSTING_READ(reg);
2804 udelay(100);
2805
2806 reg = FDI_RX_CTL(pipe);
2807 temp = I915_READ(reg);
2808 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2809
2810 /* Wait for the clocks to turn off. */
2811 POSTING_READ(reg);
2812 udelay(100);
2813}
2814
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002815static void ironlake_fdi_disable(struct drm_crtc *crtc)
2816{
2817 struct drm_device *dev = crtc->dev;
2818 struct drm_i915_private *dev_priv = dev->dev_private;
2819 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2820 int pipe = intel_crtc->pipe;
2821 u32 reg, temp;
2822
2823 /* disable CPU FDI tx and PCH FDI rx */
2824 reg = FDI_TX_CTL(pipe);
2825 temp = I915_READ(reg);
2826 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2827 POSTING_READ(reg);
2828
2829 reg = FDI_RX_CTL(pipe);
2830 temp = I915_READ(reg);
2831 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002832 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002833 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2834
2835 POSTING_READ(reg);
2836 udelay(100);
2837
2838 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002839 if (HAS_PCH_IBX(dev)) {
2840 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002841 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002842
2843 /* still set train pattern 1 */
2844 reg = FDI_TX_CTL(pipe);
2845 temp = I915_READ(reg);
2846 temp &= ~FDI_LINK_TRAIN_NONE;
2847 temp |= FDI_LINK_TRAIN_PATTERN_1;
2848 I915_WRITE(reg, temp);
2849
2850 reg = FDI_RX_CTL(pipe);
2851 temp = I915_READ(reg);
2852 if (HAS_PCH_CPT(dev)) {
2853 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2854 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2855 } else {
2856 temp &= ~FDI_LINK_TRAIN_NONE;
2857 temp |= FDI_LINK_TRAIN_PATTERN_1;
2858 }
2859 /* BPC in FDI rx is consistent with that in PIPECONF */
2860 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002861 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002862 I915_WRITE(reg, temp);
2863
2864 POSTING_READ(reg);
2865 udelay(100);
2866}
2867
Chris Wilson5bb61642012-09-27 21:25:58 +01002868static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2869{
2870 struct drm_device *dev = crtc->dev;
2871 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002872 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002873 unsigned long flags;
2874 bool pending;
2875
Ville Syrjälä10d83732013-01-29 18:13:34 +02002876 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2877 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002878 return false;
2879
2880 spin_lock_irqsave(&dev->event_lock, flags);
2881 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2882 spin_unlock_irqrestore(&dev->event_lock, flags);
2883
2884 return pending;
2885}
2886
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002887static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2888{
Chris Wilson0f911282012-04-17 10:05:38 +01002889 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002890 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002891
2892 if (crtc->fb == NULL)
2893 return;
2894
Daniel Vetter2c10d572012-12-20 21:24:07 +01002895 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2896
Chris Wilson5bb61642012-09-27 21:25:58 +01002897 wait_event(dev_priv->pending_flip_queue,
2898 !intel_crtc_has_pending_flip(crtc));
2899
Chris Wilson0f911282012-04-17 10:05:38 +01002900 mutex_lock(&dev->struct_mutex);
2901 intel_finish_fb(crtc->fb);
2902 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002903}
2904
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002905/* Program iCLKIP clock to the desired frequency */
2906static void lpt_program_iclkip(struct drm_crtc *crtc)
2907{
2908 struct drm_device *dev = crtc->dev;
2909 struct drm_i915_private *dev_priv = dev->dev_private;
2910 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2911 u32 temp;
2912
Daniel Vetter09153002012-12-12 14:06:44 +01002913 mutex_lock(&dev_priv->dpio_lock);
2914
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002915 /* It is necessary to ungate the pixclk gate prior to programming
2916 * the divisors, and gate it back when it is done.
2917 */
2918 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2919
2920 /* Disable SSCCTL */
2921 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002922 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2923 SBI_SSCCTL_DISABLE,
2924 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002925
2926 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
2927 if (crtc->mode.clock == 20000) {
2928 auxdiv = 1;
2929 divsel = 0x41;
2930 phaseinc = 0x20;
2931 } else {
2932 /* The iCLK virtual clock root frequency is in MHz,
2933 * but the crtc->mode.clock in in KHz. To get the divisors,
2934 * it is necessary to divide one by another, so we
2935 * convert the virtual clock precision to KHz here for higher
2936 * precision.
2937 */
2938 u32 iclk_virtual_root_freq = 172800 * 1000;
2939 u32 iclk_pi_range = 64;
2940 u32 desired_divisor, msb_divisor_value, pi_value;
2941
2942 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
2943 msb_divisor_value = desired_divisor / iclk_pi_range;
2944 pi_value = desired_divisor % iclk_pi_range;
2945
2946 auxdiv = 0;
2947 divsel = msb_divisor_value - 2;
2948 phaseinc = pi_value;
2949 }
2950
2951 /* This should not happen with any sane values */
2952 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2953 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2954 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
2955 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2956
2957 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2958 crtc->mode.clock,
2959 auxdiv,
2960 divsel,
2961 phasedir,
2962 phaseinc);
2963
2964 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002965 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002966 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2967 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2968 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2969 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2970 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2971 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002972 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002973
2974 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002975 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002976 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2977 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002978 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002979
2980 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002981 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002982 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002983 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002984
2985 /* Wait for initialization time */
2986 udelay(24);
2987
2988 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01002989
2990 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002991}
2992
Jesse Barnesf67a5592011-01-05 10:31:48 -08002993/*
2994 * Enable PCH resources required for PCH ports:
2995 * - PCH PLLs
2996 * - FDI training & RX/TX
2997 * - update transcoder timings
2998 * - DP transcoding bits
2999 * - transcoder
3000 */
3001static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003002{
3003 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003004 struct drm_i915_private *dev_priv = dev->dev_private;
3005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3006 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003007 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003008
Chris Wilsone7e164d2012-05-11 09:21:25 +01003009 assert_transcoder_disabled(dev_priv, pipe);
3010
Daniel Vettercd986ab2012-10-26 10:58:12 +02003011 /* Write the TU size bits before fdi link training, so that error
3012 * detection works. */
3013 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3014 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3015
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003016 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003017 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003018
Daniel Vetter572deb32012-10-27 18:46:14 +02003019 /* XXX: pch pll's can be enabled any time before we enable the PCH
3020 * transcoder, and we actually should do this to not upset any PCH
3021 * transcoder that already use the clock when we share it.
3022 *
3023 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
3024 * unconditionally resets the pll - we need that to have the right LVDS
3025 * enable sequence. */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02003026 ironlake_enable_pch_pll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01003027
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003028 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003029 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003030
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003031 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003032 switch (pipe) {
3033 default:
3034 case 0:
3035 temp |= TRANSA_DPLL_ENABLE;
3036 sel = TRANSA_DPLLB_SEL;
3037 break;
3038 case 1:
3039 temp |= TRANSB_DPLL_ENABLE;
3040 sel = TRANSB_DPLLB_SEL;
3041 break;
3042 case 2:
3043 temp |= TRANSC_DPLL_ENABLE;
3044 sel = TRANSC_DPLLB_SEL;
3045 break;
Jesse Barnesd64311a2011-10-12 15:01:33 -07003046 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003047 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3048 temp |= sel;
3049 else
3050 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003051 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003052 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003053
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003054 /* set transcoder timing, panel must allow it */
3055 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003056 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3057 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3058 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3059
3060 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3061 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3062 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003063 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003064
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003065 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003066
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003067 /* For PCH DP, enable TRANS_DP_CTL */
3068 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003069 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3070 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003071 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003072 reg = TRANS_DP_CTL(pipe);
3073 temp = I915_READ(reg);
3074 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003075 TRANS_DP_SYNC_MASK |
3076 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003077 temp |= (TRANS_DP_OUTPUT_ENABLE |
3078 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003079 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003080
3081 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003082 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003083 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003084 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003085
3086 switch (intel_trans_dp_port_sel(crtc)) {
3087 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003088 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003089 break;
3090 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003091 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003092 break;
3093 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003094 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003095 break;
3096 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003097 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003098 }
3099
Chris Wilson5eddb702010-09-11 13:48:45 +01003100 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003101 }
3102
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003103 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003104}
3105
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003106static void lpt_pch_enable(struct drm_crtc *crtc)
3107{
3108 struct drm_device *dev = crtc->dev;
3109 struct drm_i915_private *dev_priv = dev->dev_private;
3110 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003111 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003112
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003113 assert_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003114
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003115 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003116
Paulo Zanoni0540e482012-10-31 18:12:40 -02003117 /* Set transcoder timing. */
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003118 I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
3119 I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
3120 I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003121
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003122 I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
3123 I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
3124 I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
3125 I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003126
Paulo Zanoni937bb612012-10-31 18:12:47 -02003127 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003128}
3129
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003130static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3131{
3132 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3133
3134 if (pll == NULL)
3135 return;
3136
3137 if (pll->refcount == 0) {
3138 WARN(1, "bad PCH PLL refcount\n");
3139 return;
3140 }
3141
3142 --pll->refcount;
3143 intel_crtc->pch_pll = NULL;
3144}
3145
3146static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3147{
3148 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3149 struct intel_pch_pll *pll;
3150 int i;
3151
3152 pll = intel_crtc->pch_pll;
3153 if (pll) {
3154 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3155 intel_crtc->base.base.id, pll->pll_reg);
3156 goto prepare;
3157 }
3158
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003159 if (HAS_PCH_IBX(dev_priv->dev)) {
3160 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3161 i = intel_crtc->pipe;
3162 pll = &dev_priv->pch_plls[i];
3163
3164 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3165 intel_crtc->base.base.id, pll->pll_reg);
3166
3167 goto found;
3168 }
3169
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003170 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3171 pll = &dev_priv->pch_plls[i];
3172
3173 /* Only want to check enabled timings first */
3174 if (pll->refcount == 0)
3175 continue;
3176
3177 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3178 fp == I915_READ(pll->fp0_reg)) {
3179 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3180 intel_crtc->base.base.id,
3181 pll->pll_reg, pll->refcount, pll->active);
3182
3183 goto found;
3184 }
3185 }
3186
3187 /* Ok no matching timings, maybe there's a free one? */
3188 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3189 pll = &dev_priv->pch_plls[i];
3190 if (pll->refcount == 0) {
3191 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3192 intel_crtc->base.base.id, pll->pll_reg);
3193 goto found;
3194 }
3195 }
3196
3197 return NULL;
3198
3199found:
3200 intel_crtc->pch_pll = pll;
3201 pll->refcount++;
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003202 DRM_DEBUG_DRIVER("using pll %d for pipe %c\n", i, pipe_name(intel_crtc->pipe));
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003203prepare: /* separate function? */
3204 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003205
Chris Wilsone04c7352012-05-02 20:43:56 +01003206 /* Wait for the clocks to stabilize before rewriting the regs */
3207 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003208 POSTING_READ(pll->pll_reg);
3209 udelay(150);
Chris Wilsone04c7352012-05-02 20:43:56 +01003210
3211 I915_WRITE(pll->fp0_reg, fp);
3212 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003213 pll->on = false;
3214 return pll;
3215}
3216
Jesse Barnesd4270e52011-10-11 10:43:02 -07003217void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3218{
3219 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003220 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003221 u32 temp;
3222
3223 temp = I915_READ(dslreg);
3224 udelay(500);
3225 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003226 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003227 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003228 }
3229}
3230
Jesse Barnesf67a5592011-01-05 10:31:48 -08003231static void ironlake_crtc_enable(struct drm_crtc *crtc)
3232{
3233 struct drm_device *dev = crtc->dev;
3234 struct drm_i915_private *dev_priv = dev->dev_private;
3235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003236 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003237 int pipe = intel_crtc->pipe;
3238 int plane = intel_crtc->plane;
3239 u32 temp;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003240
Daniel Vetter08a48462012-07-02 11:43:47 +02003241 WARN_ON(!crtc->enabled);
3242
Jesse Barnesf67a5592011-01-05 10:31:48 -08003243 if (intel_crtc->active)
3244 return;
3245
3246 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003247
3248 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3249 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3250
Jesse Barnesf67a5592011-01-05 10:31:48 -08003251 intel_update_watermarks(dev);
3252
3253 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3254 temp = I915_READ(PCH_LVDS);
3255 if ((temp & LVDS_PORT_EN) == 0)
3256 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3257 }
3258
Jesse Barnesf67a5592011-01-05 10:31:48 -08003259
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003260 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003261 /* Note: FDI PLL enabling _must_ be done before we enable the
3262 * cpu pipes, hence this is separate from all the other fdi/pch
3263 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003264 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003265 } else {
3266 assert_fdi_tx_disabled(dev_priv, pipe);
3267 assert_fdi_rx_disabled(dev_priv, pipe);
3268 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003269
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003270 for_each_encoder_on_crtc(dev, crtc, encoder)
3271 if (encoder->pre_enable)
3272 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003273
3274 /* Enable panel fitting for LVDS */
3275 if (dev_priv->pch_pf_size &&
Jani Nikula547dc042012-11-02 11:24:03 +02003276 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3277 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnesf67a5592011-01-05 10:31:48 -08003278 /* Force use of hard-coded filter coefficients
3279 * as some pre-programmed values are broken,
3280 * e.g. x201.
3281 */
Paulo Zanoni13888d72012-11-20 13:27:41 -02003282 if (IS_IVYBRIDGE(dev))
3283 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3284 PF_PIPE_SEL_IVB(pipe));
3285 else
3286 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003287 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3288 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003289 }
3290
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003291 /*
3292 * On ILK+ LUT must be loaded before the pipe is running but with
3293 * clocks enabled
3294 */
3295 intel_crtc_load_lut(crtc);
3296
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003297 intel_enable_pipe(dev_priv, pipe,
3298 intel_crtc->config.has_pch_encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003299 intel_enable_plane(dev_priv, plane, pipe);
3300
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003301 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08003302 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003303
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003304 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003305 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003306 mutex_unlock(&dev->struct_mutex);
3307
Chris Wilson6b383a72010-09-13 13:54:26 +01003308 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003309
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003310 for_each_encoder_on_crtc(dev, crtc, encoder)
3311 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003312
3313 if (HAS_PCH_CPT(dev))
3314 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003315
3316 /*
3317 * There seems to be a race in PCH platform hw (at least on some
3318 * outputs) where an enabled pipe still completes any pageflip right
3319 * away (as if the pipe is off) instead of waiting for vblank. As soon
3320 * as the first vblank happend, everything works as expected. Hence just
3321 * wait for one vblank before returning to avoid strange things
3322 * happening.
3323 */
3324 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003325}
3326
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003327static void haswell_crtc_enable(struct drm_crtc *crtc)
3328{
3329 struct drm_device *dev = crtc->dev;
3330 struct drm_i915_private *dev_priv = dev->dev_private;
3331 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3332 struct intel_encoder *encoder;
3333 int pipe = intel_crtc->pipe;
3334 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003335
3336 WARN_ON(!crtc->enabled);
3337
3338 if (intel_crtc->active)
3339 return;
3340
3341 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003342
3343 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3344 if (intel_crtc->config.has_pch_encoder)
3345 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3346
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003347 intel_update_watermarks(dev);
3348
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003349 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02003350 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003351
3352 for_each_encoder_on_crtc(dev, crtc, encoder)
3353 if (encoder->pre_enable)
3354 encoder->pre_enable(encoder);
3355
Paulo Zanoni1f544382012-10-24 11:32:00 -02003356 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003357
Paulo Zanoni1f544382012-10-24 11:32:00 -02003358 /* Enable panel fitting for eDP */
Jani Nikula547dc042012-11-02 11:24:03 +02003359 if (dev_priv->pch_pf_size &&
3360 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003361 /* Force use of hard-coded filter coefficients
3362 * as some pre-programmed values are broken,
3363 * e.g. x201.
3364 */
Paulo Zanoni54075a72012-11-20 13:27:42 -02003365 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3366 PF_PIPE_SEL_IVB(pipe));
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003367 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3368 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3369 }
3370
3371 /*
3372 * On ILK+ LUT must be loaded before the pipe is running but with
3373 * clocks enabled
3374 */
3375 intel_crtc_load_lut(crtc);
3376
Paulo Zanoni1f544382012-10-24 11:32:00 -02003377 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00003378 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003379
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003380 intel_enable_pipe(dev_priv, pipe,
3381 intel_crtc->config.has_pch_encoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003382 intel_enable_plane(dev_priv, plane, pipe);
3383
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003384 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003385 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003386
3387 mutex_lock(&dev->struct_mutex);
3388 intel_update_fbc(dev);
3389 mutex_unlock(&dev->struct_mutex);
3390
3391 intel_crtc_update_cursor(crtc, true);
3392
3393 for_each_encoder_on_crtc(dev, crtc, encoder)
3394 encoder->enable(encoder);
3395
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003396 /*
3397 * There seems to be a race in PCH platform hw (at least on some
3398 * outputs) where an enabled pipe still completes any pageflip right
3399 * away (as if the pipe is off) instead of waiting for vblank. As soon
3400 * as the first vblank happend, everything works as expected. Hence just
3401 * wait for one vblank before returning to avoid strange things
3402 * happening.
3403 */
3404 intel_wait_for_vblank(dev, intel_crtc->pipe);
3405}
3406
Jesse Barnes6be4a602010-09-10 10:26:01 -07003407static void ironlake_crtc_disable(struct drm_crtc *crtc)
3408{
3409 struct drm_device *dev = crtc->dev;
3410 struct drm_i915_private *dev_priv = dev->dev_private;
3411 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003412 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003413 int pipe = intel_crtc->pipe;
3414 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003415 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003416
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003417
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003418 if (!intel_crtc->active)
3419 return;
3420
Daniel Vetterea9d7582012-07-10 10:42:52 +02003421 for_each_encoder_on_crtc(dev, crtc, encoder)
3422 encoder->disable(encoder);
3423
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003424 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003425 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003426 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003427
Jesse Barnesb24e7172011-01-04 15:09:30 -08003428 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003429
Chris Wilson973d04f2011-07-08 12:22:37 +01003430 if (dev_priv->cfb_plane == plane)
3431 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003432
Paulo Zanoni86642812013-04-12 17:57:57 -03003433 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003434 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003435
Jesse Barnes6be4a602010-09-10 10:26:01 -07003436 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003437 I915_WRITE(PF_CTL(pipe), 0);
3438 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003439
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003440 for_each_encoder_on_crtc(dev, crtc, encoder)
3441 if (encoder->post_disable)
3442 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003443
Chris Wilson5eddb702010-09-11 13:48:45 +01003444 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003445
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003446 ironlake_disable_pch_transcoder(dev_priv, pipe);
Paulo Zanoni86642812013-04-12 17:57:57 -03003447 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003448
3449 if (HAS_PCH_CPT(dev)) {
3450 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003451 reg = TRANS_DP_CTL(pipe);
3452 temp = I915_READ(reg);
3453 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003454 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003456
3457 /* disable DPLL_SEL */
3458 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003459 switch (pipe) {
3460 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003461 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003462 break;
3463 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003464 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003465 break;
3466 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003467 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003468 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003469 break;
3470 default:
3471 BUG(); /* wtf */
3472 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003473 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003474 }
3475
3476 /* disable PCH DPLL */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003477 intel_disable_pch_pll(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003478
Daniel Vetter88cefb62012-08-12 19:27:14 +02003479 ironlake_fdi_pll_disable(intel_crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +01003480
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003481 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003482 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003483
3484 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003485 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003486 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003487}
3488
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003489static void haswell_crtc_disable(struct drm_crtc *crtc)
3490{
3491 struct drm_device *dev = crtc->dev;
3492 struct drm_i915_private *dev_priv = dev->dev_private;
3493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3494 struct intel_encoder *encoder;
3495 int pipe = intel_crtc->pipe;
3496 int plane = intel_crtc->plane;
Daniel Vetter3b117c82013-04-17 20:15:07 +02003497 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003498
3499 if (!intel_crtc->active)
3500 return;
3501
3502 for_each_encoder_on_crtc(dev, crtc, encoder)
3503 encoder->disable(encoder);
3504
3505 intel_crtc_wait_for_pending_flips(crtc);
3506 drm_vblank_off(dev, pipe);
3507 intel_crtc_update_cursor(crtc, false);
3508
3509 intel_disable_plane(dev_priv, plane, pipe);
3510
3511 if (dev_priv->cfb_plane == plane)
3512 intel_disable_fbc(dev);
3513
Paulo Zanoni86642812013-04-12 17:57:57 -03003514 if (intel_crtc->config.has_pch_encoder)
3515 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003516 intel_disable_pipe(dev_priv, pipe);
3517
Paulo Zanoniad80a812012-10-24 16:06:19 -02003518 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003519
Paulo Zanonif7708f72013-03-22 14:16:38 -03003520 /* XXX: Once we have proper panel fitter state tracking implemented with
3521 * hardware state read/check support we should switch to only disable
3522 * the panel fitter when we know it's used. */
3523 if (intel_using_power_well(dev)) {
3524 I915_WRITE(PF_CTL(pipe), 0);
3525 I915_WRITE(PF_WIN_SZ(pipe), 0);
3526 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003527
Paulo Zanoni1f544382012-10-24 11:32:00 -02003528 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003529
3530 for_each_encoder_on_crtc(dev, crtc, encoder)
3531 if (encoder->post_disable)
3532 encoder->post_disable(encoder);
3533
Daniel Vetter88adfff2013-03-28 10:42:01 +01003534 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003535 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03003536 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003537 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003538 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003539
3540 intel_crtc->active = false;
3541 intel_update_watermarks(dev);
3542
3543 mutex_lock(&dev->struct_mutex);
3544 intel_update_fbc(dev);
3545 mutex_unlock(&dev->struct_mutex);
3546}
3547
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003548static void ironlake_crtc_off(struct drm_crtc *crtc)
3549{
3550 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3551 intel_put_pch_pll(intel_crtc);
3552}
3553
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003554static void haswell_crtc_off(struct drm_crtc *crtc)
3555{
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3557
3558 /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
3559 * start using it. */
Daniel Vetter3b117c82013-04-17 20:15:07 +02003560 intel_crtc->config.cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003561
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003562 intel_ddi_put_crtc_pll(crtc);
3563}
3564
Daniel Vetter02e792f2009-09-15 22:57:34 +02003565static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3566{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003567 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003568 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003569 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003570
Chris Wilson23f09ce2010-08-12 13:53:37 +01003571 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003572 dev_priv->mm.interruptible = false;
3573 (void) intel_overlay_switch_off(intel_crtc->overlay);
3574 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003575 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003576 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003577
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003578 /* Let userspace switch the overlay on again. In most cases userspace
3579 * has to recompute where to put it anyway.
3580 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003581}
3582
Egbert Eich61bc95c2013-03-04 09:24:38 -05003583/**
3584 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3585 * cursor plane briefly if not already running after enabling the display
3586 * plane.
3587 * This workaround avoids occasional blank screens when self refresh is
3588 * enabled.
3589 */
3590static void
3591g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3592{
3593 u32 cntl = I915_READ(CURCNTR(pipe));
3594
3595 if ((cntl & CURSOR_MODE) == 0) {
3596 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3597
3598 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3599 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3600 intel_wait_for_vblank(dev_priv->dev, pipe);
3601 I915_WRITE(CURCNTR(pipe), cntl);
3602 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3603 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3604 }
3605}
3606
Jesse Barnes89b667f2013-04-18 14:51:36 -07003607static void valleyview_crtc_enable(struct drm_crtc *crtc)
3608{
3609 struct drm_device *dev = crtc->dev;
3610 struct drm_i915_private *dev_priv = dev->dev_private;
3611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3612 struct intel_encoder *encoder;
3613 int pipe = intel_crtc->pipe;
3614 int plane = intel_crtc->plane;
3615
3616 WARN_ON(!crtc->enabled);
3617
3618 if (intel_crtc->active)
3619 return;
3620
3621 intel_crtc->active = true;
3622 intel_update_watermarks(dev);
3623
3624 mutex_lock(&dev_priv->dpio_lock);
3625
3626 for_each_encoder_on_crtc(dev, crtc, encoder)
3627 if (encoder->pre_pll_enable)
3628 encoder->pre_pll_enable(encoder);
3629
3630 intel_enable_pll(dev_priv, pipe);
3631
3632 for_each_encoder_on_crtc(dev, crtc, encoder)
3633 if (encoder->pre_enable)
3634 encoder->pre_enable(encoder);
3635
3636 /* VLV wants encoder enabling _before_ the pipe is up. */
3637 for_each_encoder_on_crtc(dev, crtc, encoder)
3638 encoder->enable(encoder);
3639
3640 intel_enable_pipe(dev_priv, pipe, false);
3641 intel_enable_plane(dev_priv, plane, pipe);
3642
3643 intel_crtc_load_lut(crtc);
3644 intel_update_fbc(dev);
3645
3646 /* Give the overlay scaler a chance to enable if it's on this pipe */
3647 intel_crtc_dpms_overlay(intel_crtc, true);
3648 intel_crtc_update_cursor(crtc, true);
3649
3650 mutex_unlock(&dev_priv->dpio_lock);
3651}
3652
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003653static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003654{
3655 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003656 struct drm_i915_private *dev_priv = dev->dev_private;
3657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003658 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003659 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003660 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003661
Daniel Vetter08a48462012-07-02 11:43:47 +02003662 WARN_ON(!crtc->enabled);
3663
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003664 if (intel_crtc->active)
3665 return;
3666
3667 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003668 intel_update_watermarks(dev);
3669
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003670 intel_enable_pll(dev_priv, pipe);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003671
3672 for_each_encoder_on_crtc(dev, crtc, encoder)
3673 if (encoder->pre_enable)
3674 encoder->pre_enable(encoder);
3675
Jesse Barnes040484a2011-01-03 12:14:26 -08003676 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003677 intel_enable_plane(dev_priv, plane, pipe);
Egbert Eich61bc95c2013-03-04 09:24:38 -05003678 if (IS_G4X(dev))
3679 g4x_fixup_plane(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003680
3681 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003682 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003683
3684 /* Give the overlay scaler a chance to enable if it's on this pipe */
3685 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003686 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003687
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003688 for_each_encoder_on_crtc(dev, crtc, encoder)
3689 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003690}
3691
Daniel Vetter87476d62013-04-11 16:29:06 +02003692static void i9xx_pfit_disable(struct intel_crtc *crtc)
3693{
3694 struct drm_device *dev = crtc->base.dev;
3695 struct drm_i915_private *dev_priv = dev->dev_private;
3696 enum pipe pipe;
3697 uint32_t pctl = I915_READ(PFIT_CONTROL);
3698
3699 assert_pipe_disabled(dev_priv, crtc->pipe);
3700
3701 if (INTEL_INFO(dev)->gen >= 4)
3702 pipe = (pctl & PFIT_PIPE_MASK) >> PFIT_PIPE_SHIFT;
3703 else
3704 pipe = PIPE_B;
3705
3706 if (pipe == crtc->pipe) {
3707 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n", pctl);
3708 I915_WRITE(PFIT_CONTROL, 0);
3709 }
3710}
3711
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003712static void i9xx_crtc_disable(struct drm_crtc *crtc)
3713{
3714 struct drm_device *dev = crtc->dev;
3715 struct drm_i915_private *dev_priv = dev->dev_private;
3716 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003717 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003718 int pipe = intel_crtc->pipe;
3719 int plane = intel_crtc->plane;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003720
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003721 if (!intel_crtc->active)
3722 return;
3723
Daniel Vetterea9d7582012-07-10 10:42:52 +02003724 for_each_encoder_on_crtc(dev, crtc, encoder)
3725 encoder->disable(encoder);
3726
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003727 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003728 intel_crtc_wait_for_pending_flips(crtc);
3729 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003730 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003731 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003732
Chris Wilson973d04f2011-07-08 12:22:37 +01003733 if (dev_priv->cfb_plane == plane)
3734 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003735
Jesse Barnesb24e7172011-01-04 15:09:30 -08003736 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003737 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003738
Daniel Vetter87476d62013-04-11 16:29:06 +02003739 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003740
Jesse Barnes89b667f2013-04-18 14:51:36 -07003741 for_each_encoder_on_crtc(dev, crtc, encoder)
3742 if (encoder->post_disable)
3743 encoder->post_disable(encoder);
3744
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003745 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003746
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003747 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003748 intel_update_fbc(dev);
3749 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003750}
3751
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003752static void i9xx_crtc_off(struct drm_crtc *crtc)
3753{
3754}
3755
Daniel Vetter976f8a22012-07-08 22:34:21 +02003756static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3757 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003758{
3759 struct drm_device *dev = crtc->dev;
3760 struct drm_i915_master_private *master_priv;
3761 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3762 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003763
3764 if (!dev->primary->master)
3765 return;
3766
3767 master_priv = dev->primary->master->driver_priv;
3768 if (!master_priv->sarea_priv)
3769 return;
3770
Jesse Barnes79e53942008-11-07 14:24:08 -08003771 switch (pipe) {
3772 case 0:
3773 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3774 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3775 break;
3776 case 1:
3777 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3778 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3779 break;
3780 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003781 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003782 break;
3783 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003784}
3785
Daniel Vetter976f8a22012-07-08 22:34:21 +02003786/**
3787 * Sets the power management mode of the pipe and plane.
3788 */
3789void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003790{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003791 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003792 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003793 struct intel_encoder *intel_encoder;
3794 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003795
Daniel Vetter976f8a22012-07-08 22:34:21 +02003796 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3797 enable |= intel_encoder->connectors_active;
3798
3799 if (enable)
3800 dev_priv->display.crtc_enable(crtc);
3801 else
3802 dev_priv->display.crtc_disable(crtc);
3803
3804 intel_crtc_update_sarea(crtc, enable);
3805}
3806
Daniel Vetter976f8a22012-07-08 22:34:21 +02003807static void intel_crtc_disable(struct drm_crtc *crtc)
3808{
3809 struct drm_device *dev = crtc->dev;
3810 struct drm_connector *connector;
3811 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003812 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003813
3814 /* crtc should still be enabled when we disable it. */
3815 WARN_ON(!crtc->enabled);
3816
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003817 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003818 dev_priv->display.crtc_disable(crtc);
3819 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003820 dev_priv->display.off(crtc);
3821
Chris Wilson931872f2012-01-16 23:01:13 +00003822 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3823 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003824
3825 if (crtc->fb) {
3826 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003827 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003828 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003829 crtc->fb = NULL;
3830 }
3831
3832 /* Update computed state. */
3833 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3834 if (!connector->encoder || !connector->encoder->crtc)
3835 continue;
3836
3837 if (connector->encoder->crtc != crtc)
3838 continue;
3839
3840 connector->dpms = DRM_MODE_DPMS_OFF;
3841 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003842 }
3843}
3844
Daniel Vettera261b242012-07-26 19:21:47 +02003845void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003846{
Daniel Vettera261b242012-07-26 19:21:47 +02003847 struct drm_crtc *crtc;
3848
3849 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3850 if (crtc->enabled)
3851 intel_crtc_disable(crtc);
3852 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003853}
3854
Chris Wilsonea5b2132010-08-04 13:50:23 +01003855void intel_encoder_destroy(struct drm_encoder *encoder)
3856{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003857 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003858
Chris Wilsonea5b2132010-08-04 13:50:23 +01003859 drm_encoder_cleanup(encoder);
3860 kfree(intel_encoder);
3861}
3862
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003863/* Simple dpms helper for encodres with just one connector, no cloning and only
3864 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3865 * state of the entire output pipe. */
3866void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3867{
3868 if (mode == DRM_MODE_DPMS_ON) {
3869 encoder->connectors_active = true;
3870
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003871 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003872 } else {
3873 encoder->connectors_active = false;
3874
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003875 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003876 }
3877}
3878
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003879/* Cross check the actual hw state with our own modeset state tracking (and it's
3880 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003881static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003882{
3883 if (connector->get_hw_state(connector)) {
3884 struct intel_encoder *encoder = connector->encoder;
3885 struct drm_crtc *crtc;
3886 bool encoder_enabled;
3887 enum pipe pipe;
3888
3889 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3890 connector->base.base.id,
3891 drm_get_connector_name(&connector->base));
3892
3893 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3894 "wrong connector dpms state\n");
3895 WARN(connector->base.encoder != &encoder->base,
3896 "active connector not linked to encoder\n");
3897 WARN(!encoder->connectors_active,
3898 "encoder->connectors_active not set\n");
3899
3900 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3901 WARN(!encoder_enabled, "encoder not enabled\n");
3902 if (WARN_ON(!encoder->base.crtc))
3903 return;
3904
3905 crtc = encoder->base.crtc;
3906
3907 WARN(!crtc->enabled, "crtc not enabled\n");
3908 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3909 WARN(pipe != to_intel_crtc(crtc)->pipe,
3910 "encoder active on the wrong pipe\n");
3911 }
3912}
3913
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003914/* Even simpler default implementation, if there's really no special case to
3915 * consider. */
3916void intel_connector_dpms(struct drm_connector *connector, int mode)
3917{
3918 struct intel_encoder *encoder = intel_attached_encoder(connector);
3919
3920 /* All the simple cases only support two dpms states. */
3921 if (mode != DRM_MODE_DPMS_ON)
3922 mode = DRM_MODE_DPMS_OFF;
3923
3924 if (mode == connector->dpms)
3925 return;
3926
3927 connector->dpms = mode;
3928
3929 /* Only need to change hw state when actually enabled */
3930 if (encoder->base.crtc)
3931 intel_encoder_dpms(encoder, mode);
3932 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003933 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003934
Daniel Vetterb9805142012-08-31 17:37:33 +02003935 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003936}
3937
Daniel Vetterf0947c32012-07-02 13:10:34 +02003938/* Simple connector->get_hw_state implementation for encoders that support only
3939 * one connector and no cloning and hence the encoder state determines the state
3940 * of the connector. */
3941bool intel_connector_get_hw_state(struct intel_connector *connector)
3942{
Daniel Vetter24929352012-07-02 20:28:59 +02003943 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003944 struct intel_encoder *encoder = connector->encoder;
3945
3946 return encoder->get_hw_state(encoder, &pipe);
3947}
3948
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01003949static bool intel_crtc_compute_config(struct drm_crtc *crtc,
3950 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08003951{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003952 struct drm_device *dev = crtc->dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01003953 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01003954
Eric Anholtbad720f2009-10-22 16:11:14 -07003955 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003956 /* FDI link clock is fixed at 2.7G */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01003957 if (pipe_config->requested_mode.clock * 3
3958 > IRONLAKE_FDI_FREQ * 4)
Jesse Barnes2377b742010-07-07 14:06:43 -07003959 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003960 }
Chris Wilson89749352010-09-12 18:25:19 +01003961
Daniel Vetterf9bef082012-04-15 19:53:19 +02003962 /* All interlaced capable intel hw wants timings in frames. Note though
3963 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3964 * timings, so we need to be careful not to clobber these.*/
Daniel Vetter7ae89232013-03-27 00:44:52 +01003965 if (!pipe_config->timings_set)
Daniel Vetterf9bef082012-04-15 19:53:19 +02003966 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003967
Chris Wilson44f46b422012-06-21 13:19:59 +03003968 /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
3969 * with a hsync front porch of 0.
3970 */
3971 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
3972 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
3973 return false;
3974
Daniel Vetterbd080ee2013-04-17 20:01:39 +02003975 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01003976 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02003977 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01003978 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
3979 * for lvds. */
3980 pipe_config->pipe_bpp = 8*3;
3981 }
3982
Jesse Barnes79e53942008-11-07 14:24:08 -08003983 return true;
3984}
3985
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07003986static int valleyview_get_display_clock_speed(struct drm_device *dev)
3987{
3988 return 400000; /* FIXME */
3989}
3990
Jesse Barnese70236a2009-09-21 10:42:27 -07003991static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003992{
Jesse Barnese70236a2009-09-21 10:42:27 -07003993 return 400000;
3994}
Jesse Barnes79e53942008-11-07 14:24:08 -08003995
Jesse Barnese70236a2009-09-21 10:42:27 -07003996static int i915_get_display_clock_speed(struct drm_device *dev)
3997{
3998 return 333000;
3999}
Jesse Barnes79e53942008-11-07 14:24:08 -08004000
Jesse Barnese70236a2009-09-21 10:42:27 -07004001static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4002{
4003 return 200000;
4004}
Jesse Barnes79e53942008-11-07 14:24:08 -08004005
Jesse Barnese70236a2009-09-21 10:42:27 -07004006static int i915gm_get_display_clock_speed(struct drm_device *dev)
4007{
4008 u16 gcfgc = 0;
4009
4010 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4011
4012 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08004013 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07004014 else {
4015 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4016 case GC_DISPLAY_CLOCK_333_MHZ:
4017 return 333000;
4018 default:
4019 case GC_DISPLAY_CLOCK_190_200_MHZ:
4020 return 190000;
4021 }
4022 }
4023}
Jesse Barnes79e53942008-11-07 14:24:08 -08004024
Jesse Barnese70236a2009-09-21 10:42:27 -07004025static int i865_get_display_clock_speed(struct drm_device *dev)
4026{
4027 return 266000;
4028}
4029
4030static int i855_get_display_clock_speed(struct drm_device *dev)
4031{
4032 u16 hpllcc = 0;
4033 /* Assume that the hardware is in the high speed state. This
4034 * should be the default.
4035 */
4036 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4037 case GC_CLOCK_133_200:
4038 case GC_CLOCK_100_200:
4039 return 200000;
4040 case GC_CLOCK_166_250:
4041 return 250000;
4042 case GC_CLOCK_100_133:
4043 return 133000;
4044 }
4045
4046 /* Shouldn't happen */
4047 return 0;
4048}
4049
4050static int i830_get_display_clock_speed(struct drm_device *dev)
4051{
4052 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004053}
4054
Zhenyu Wang2c072452009-06-05 15:38:42 +08004055static void
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004056intel_reduce_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004057{
4058 while (*num > 0xffffff || *den > 0xffffff) {
4059 *num >>= 1;
4060 *den >>= 1;
4061 }
4062}
4063
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004064void
4065intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4066 int pixel_clock, int link_clock,
4067 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004068{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004069 m_n->tu = 64;
Chris Wilson22ed1112010-12-04 01:01:29 +00004070 m_n->gmch_m = bits_per_pixel * pixel_clock;
4071 m_n->gmch_n = link_clock * nlanes * 8;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004072 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
Chris Wilson22ed1112010-12-04 01:01:29 +00004073 m_n->link_m = pixel_clock;
4074 m_n->link_n = link_clock;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004075 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004076}
4077
Chris Wilsona7615032011-01-12 17:04:08 +00004078static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4079{
Keith Packard72bbe58c2011-09-26 16:09:45 -07004080 if (i915_panel_use_ssc >= 0)
4081 return i915_panel_use_ssc != 0;
4082 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004083 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004084}
4085
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004086static int vlv_get_refclk(struct drm_crtc *crtc)
4087{
4088 struct drm_device *dev = crtc->dev;
4089 struct drm_i915_private *dev_priv = dev->dev_private;
4090 int refclk = 27000; /* for DP & HDMI */
4091
4092 return 100000; /* only one validated so far */
4093
4094 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4095 refclk = 96000;
4096 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4097 if (intel_panel_use_ssc(dev_priv))
4098 refclk = 100000;
4099 else
4100 refclk = 96000;
4101 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4102 refclk = 100000;
4103 }
4104
4105 return refclk;
4106}
4107
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004108static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4109{
4110 struct drm_device *dev = crtc->dev;
4111 struct drm_i915_private *dev_priv = dev->dev_private;
4112 int refclk;
4113
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004114 if (IS_VALLEYVIEW(dev)) {
4115 refclk = vlv_get_refclk(crtc);
4116 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004117 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4118 refclk = dev_priv->lvds_ssc_freq * 1000;
4119 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4120 refclk / 1000);
4121 } else if (!IS_GEN2(dev)) {
4122 refclk = 96000;
4123 } else {
4124 refclk = 48000;
4125 }
4126
4127 return refclk;
4128}
4129
Daniel Vetterf47709a2013-03-28 10:42:02 +01004130static void i9xx_adjust_sdvo_tv_clock(struct intel_crtc *crtc)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004131{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004132 unsigned dotclock = crtc->config.adjusted_mode.clock;
4133 struct dpll *clock = &crtc->config.dpll;
4134
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004135 /* SDVO TV has fixed PLL values depend on its clock range,
4136 this mirrors vbios setting. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01004137 if (dotclock >= 100000 && dotclock < 140500) {
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004138 clock->p1 = 2;
4139 clock->p2 = 10;
4140 clock->n = 3;
4141 clock->m1 = 16;
4142 clock->m2 = 8;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004143 } else if (dotclock >= 140500 && dotclock <= 200000) {
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004144 clock->p1 = 1;
4145 clock->p2 = 10;
4146 clock->n = 6;
4147 clock->m1 = 12;
4148 clock->m2 = 8;
4149 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004150
4151 crtc->config.clock_set = true;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004152}
4153
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004154static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
4155{
4156 return (1 << dpll->n) << 16 | dpll->m1 << 8 | dpll->m2;
4157}
4158
4159static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
4160{
4161 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
4162}
4163
Daniel Vetterf47709a2013-03-28 10:42:02 +01004164static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08004165 intel_clock_t *reduced_clock)
4166{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004167 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004168 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004169 int pipe = crtc->pipe;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004170 u32 fp, fp2 = 0;
4171
4172 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004173 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004174 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004175 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004176 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004177 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004178 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02004179 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08004180 }
4181
4182 I915_WRITE(FP0(pipe), fp);
4183
Daniel Vetterf47709a2013-03-28 10:42:02 +01004184 crtc->lowfreq_avail = false;
4185 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jesse Barnesa7516a02011-12-15 12:30:37 -08004186 reduced_clock && i915_powersave) {
4187 I915_WRITE(FP1(pipe), fp2);
Daniel Vetterf47709a2013-03-28 10:42:02 +01004188 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004189 } else {
4190 I915_WRITE(FP1(pipe), fp);
4191 }
4192}
4193
Jesse Barnes89b667f2013-04-18 14:51:36 -07004194static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
4195{
4196 u32 reg_val;
4197
4198 /*
4199 * PLLB opamp always calibrates to max value of 0x3f, force enable it
4200 * and set it to a reasonable value instead.
4201 */
4202 reg_val = intel_dpio_read(dev_priv, DPIO_IREF(1));
4203 reg_val &= 0xffffff00;
4204 reg_val |= 0x00000030;
4205 intel_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4206
4207 reg_val = intel_dpio_read(dev_priv, DPIO_CALIBRATION);
4208 reg_val &= 0x8cffffff;
4209 reg_val = 0x8c000000;
4210 intel_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4211
4212 reg_val = intel_dpio_read(dev_priv, DPIO_IREF(1));
4213 reg_val &= 0xffffff00;
4214 intel_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
4215
4216 reg_val = intel_dpio_read(dev_priv, DPIO_CALIBRATION);
4217 reg_val &= 0x00ffffff;
4218 reg_val |= 0xb0000000;
4219 intel_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
4220}
4221
Daniel Vetter03afc4a2013-04-02 23:42:31 +02004222static void intel_dp_set_m_n(struct intel_crtc *crtc)
4223{
4224 if (crtc->config.has_pch_encoder)
4225 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4226 else
4227 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
4228}
4229
Daniel Vetterf47709a2013-03-28 10:42:02 +01004230static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004231{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004232 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004233 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004234 struct drm_display_mode *adjusted_mode =
4235 &crtc->config.adjusted_mode;
4236 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004237 int pipe = crtc->pipe;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004238 u32 dpll, mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004239 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004240 bool is_hdmi;
4241 u32 coreclk, reg_val, temp;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004242
Daniel Vetter09153002012-12-12 14:06:44 +01004243 mutex_lock(&dev_priv->dpio_lock);
4244
Jesse Barnes89b667f2013-04-18 14:51:36 -07004245 is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004246
Daniel Vetterf47709a2013-03-28 10:42:02 +01004247 bestn = crtc->config.dpll.n;
4248 bestm1 = crtc->config.dpll.m1;
4249 bestm2 = crtc->config.dpll.m2;
4250 bestp1 = crtc->config.dpll.p1;
4251 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004252
Jesse Barnes89b667f2013-04-18 14:51:36 -07004253 /* See eDP HDMI DPIO driver vbios notes doc */
4254
4255 /* PLL B needs special handling */
4256 if (pipe)
4257 vlv_pllb_recal_opamp(dev_priv);
4258
4259 /* Set up Tx target for periodic Rcomp update */
4260 intel_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
4261
4262 /* Disable target IRef on PLL */
4263 reg_val = intel_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
4264 reg_val &= 0x00ffffff;
4265 intel_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
4266
4267 /* Disable fast lock */
4268 intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
4269
4270 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004271 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4272 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4273 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004274 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004275 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI) ||
4276 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4277 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4278 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
4279 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4280
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004281 mdiv |= DPIO_ENABLE_CALIBRATION;
4282 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4283
Jesse Barnes89b667f2013-04-18 14:51:36 -07004284 /* Set HBR and RBR LPF coefficients */
4285 if (adjusted_mode->clock == 162000 ||
4286 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
4287 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
4288 0x005f0021);
4289 else
4290 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
4291 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004292
Jesse Barnes89b667f2013-04-18 14:51:36 -07004293 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
4294 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
4295 /* Use SSC source */
4296 if (!pipe)
4297 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4298 0x0df40000);
4299 else
4300 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4301 0x0df70000);
4302 } else { /* HDMI or VGA */
4303 /* Use bend source */
4304 if (!pipe)
4305 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4306 0x0df70000);
4307 else
4308 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe),
4309 0x0df40000);
4310 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004311
Jesse Barnes89b667f2013-04-18 14:51:36 -07004312 coreclk = intel_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
4313 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
4314 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
4315 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
4316 coreclk |= 0x01000000;
4317 intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
4318
4319 intel_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
4320
4321 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
4322 if (encoder->pre_pll_enable)
4323 encoder->pre_pll_enable(encoder);
4324
4325 /* Enable DPIO clock input */
4326 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
4327 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
4328 if (pipe)
4329 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004330
4331 dpll |= DPLL_VCO_ENABLE;
4332 I915_WRITE(DPLL(pipe), dpll);
4333 POSTING_READ(DPLL(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07004334 udelay(150);
4335
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004336 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4337 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4338
Jesse Barnes89b667f2013-04-18 14:51:36 -07004339 if (is_hdmi) {
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004340 temp = 0;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004341 if (crtc->config.pixel_multiplier > 1) {
4342 temp = (crtc->config.pixel_multiplier - 1)
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004343 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4344 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004345
Jesse Barnes89b667f2013-04-18 14:51:36 -07004346 I915_WRITE(DPLL_MD(pipe), temp);
4347 POSTING_READ(DPLL_MD(pipe));
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304348 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004349
Jesse Barnes89b667f2013-04-18 14:51:36 -07004350 if (crtc->config.has_dp_encoder)
4351 intel_dp_set_m_n(crtc);
Daniel Vetter09153002012-12-12 14:06:44 +01004352
4353 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004354}
4355
Daniel Vetterf47709a2013-03-28 10:42:02 +01004356static void i9xx_update_pll(struct intel_crtc *crtc,
4357 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004358 int num_connectors)
4359{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004360 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004361 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterdafd2262012-11-26 17:22:07 +01004362 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004363 int pipe = crtc->pipe;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004364 u32 dpll;
4365 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004366 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004367
Daniel Vetterf47709a2013-03-28 10:42:02 +01004368 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304369
Daniel Vetterf47709a2013-03-28 10:42:02 +01004370 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
4371 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004372
4373 dpll = DPLL_VGA_MODE_DIS;
4374
Daniel Vetterf47709a2013-03-28 10:42:02 +01004375 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004376 dpll |= DPLLB_MODE_LVDS;
4377 else
4378 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004379
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004380 if (is_sdvo) {
Daniel Vetterf47709a2013-03-28 10:42:02 +01004381 if ((crtc->config.pixel_multiplier > 1) &&
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004382 (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))) {
Daniel Vetterf47709a2013-03-28 10:42:02 +01004383 dpll |= (crtc->config.pixel_multiplier - 1)
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004384 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004385 }
4386 dpll |= DPLL_DVO_HIGH_SPEED;
4387 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004388 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004389 dpll |= DPLL_DVO_HIGH_SPEED;
4390
4391 /* compute bitmask from p1 value */
4392 if (IS_PINEVIEW(dev))
4393 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4394 else {
4395 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4396 if (IS_G4X(dev) && reduced_clock)
4397 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4398 }
4399 switch (clock->p2) {
4400 case 5:
4401 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4402 break;
4403 case 7:
4404 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4405 break;
4406 case 10:
4407 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4408 break;
4409 case 14:
4410 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4411 break;
4412 }
4413 if (INTEL_INFO(dev)->gen >= 4)
4414 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4415
Daniel Vetterf47709a2013-03-28 10:42:02 +01004416 if (is_sdvo && intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_TVOUT))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004417 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004418 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_TVOUT))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004419 /* XXX: just matching BIOS for now */
4420 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4421 dpll |= 3;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004422 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004423 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4424 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4425 else
4426 dpll |= PLL_REF_INPUT_DREFCLK;
4427
4428 dpll |= DPLL_VCO_ENABLE;
4429 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4430 POSTING_READ(DPLL(pipe));
4431 udelay(150);
4432
Daniel Vetterf47709a2013-03-28 10:42:02 +01004433 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetterdafd2262012-11-26 17:22:07 +01004434 if (encoder->pre_pll_enable)
4435 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004436
Daniel Vetterf47709a2013-03-28 10:42:02 +01004437 if (crtc->config.has_dp_encoder)
4438 intel_dp_set_m_n(crtc);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004439
4440 I915_WRITE(DPLL(pipe), dpll);
4441
4442 /* Wait for the clocks to stabilize. */
4443 POSTING_READ(DPLL(pipe));
4444 udelay(150);
4445
4446 if (INTEL_INFO(dev)->gen >= 4) {
4447 u32 temp = 0;
4448 if (is_sdvo) {
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004449 temp = 0;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004450 if (crtc->config.pixel_multiplier > 1) {
4451 temp = (crtc->config.pixel_multiplier - 1)
Daniel Vetter6cc5f342013-03-27 00:44:53 +01004452 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4453 }
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004454 }
4455 I915_WRITE(DPLL_MD(pipe), temp);
4456 } else {
4457 /* The pixel multiplier can only be updated once the
4458 * DPLL is enabled and the clocks are stable.
4459 *
4460 * So write it again.
4461 */
4462 I915_WRITE(DPLL(pipe), dpll);
4463 }
4464}
4465
Daniel Vetterf47709a2013-03-28 10:42:02 +01004466static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004467 struct drm_display_mode *adjusted_mode,
Daniel Vetterf47709a2013-03-28 10:42:02 +01004468 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004469 int num_connectors)
4470{
Daniel Vetterf47709a2013-03-28 10:42:02 +01004471 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004472 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterdafd2262012-11-26 17:22:07 +01004473 struct intel_encoder *encoder;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004474 int pipe = crtc->pipe;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004475 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01004476 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004477
Daniel Vetterf47709a2013-03-28 10:42:02 +01004478 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304479
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004480 dpll = DPLL_VGA_MODE_DIS;
4481
Daniel Vetterf47709a2013-03-28 10:42:02 +01004482 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004483 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4484 } else {
4485 if (clock->p1 == 2)
4486 dpll |= PLL_P1_DIVIDE_BY_TWO;
4487 else
4488 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4489 if (clock->p2 == 4)
4490 dpll |= PLL_P2_DIVIDE_BY_4;
4491 }
4492
Daniel Vetterf47709a2013-03-28 10:42:02 +01004493 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004494 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4495 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4496 else
4497 dpll |= PLL_REF_INPUT_DREFCLK;
4498
4499 dpll |= DPLL_VCO_ENABLE;
4500 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4501 POSTING_READ(DPLL(pipe));
4502 udelay(150);
4503
Daniel Vetterf47709a2013-03-28 10:42:02 +01004504 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetterdafd2262012-11-26 17:22:07 +01004505 if (encoder->pre_pll_enable)
4506 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004507
Daniel Vetter5b5896e2012-09-11 12:37:55 +02004508 I915_WRITE(DPLL(pipe), dpll);
4509
4510 /* Wait for the clocks to stabilize. */
4511 POSTING_READ(DPLL(pipe));
4512 udelay(150);
4513
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004514 /* The pixel multiplier can only be updated once the
4515 * DPLL is enabled and the clocks are stable.
4516 *
4517 * So write it again.
4518 */
4519 I915_WRITE(DPLL(pipe), dpll);
4520}
4521
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004522static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
4523 struct drm_display_mode *mode,
4524 struct drm_display_mode *adjusted_mode)
4525{
4526 struct drm_device *dev = intel_crtc->base.dev;
4527 struct drm_i915_private *dev_priv = dev->dev_private;
4528 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004529 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004530 uint32_t vsyncshift;
4531
4532 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4533 /* the chip adds 2 halflines automatically */
4534 adjusted_mode->crtc_vtotal -= 1;
4535 adjusted_mode->crtc_vblank_end -= 1;
4536 vsyncshift = adjusted_mode->crtc_hsync_start
4537 - adjusted_mode->crtc_htotal / 2;
4538 } else {
4539 vsyncshift = 0;
4540 }
4541
4542 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004543 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004544
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004545 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004546 (adjusted_mode->crtc_hdisplay - 1) |
4547 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004548 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004549 (adjusted_mode->crtc_hblank_start - 1) |
4550 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004551 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004552 (adjusted_mode->crtc_hsync_start - 1) |
4553 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4554
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004555 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004556 (adjusted_mode->crtc_vdisplay - 1) |
4557 ((adjusted_mode->crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004558 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004559 (adjusted_mode->crtc_vblank_start - 1) |
4560 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004561 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004562 (adjusted_mode->crtc_vsync_start - 1) |
4563 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4564
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004565 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4566 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4567 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4568 * bits. */
4569 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4570 (pipe == PIPE_B || pipe == PIPE_C))
4571 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4572
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004573 /* pipesrc controls the size that is scaled from, which should
4574 * always be the user's requested size.
4575 */
4576 I915_WRITE(PIPESRC(pipe),
4577 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4578}
4579
Daniel Vetter84b046f2013-02-19 18:48:54 +01004580static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
4581{
4582 struct drm_device *dev = intel_crtc->base.dev;
4583 struct drm_i915_private *dev_priv = dev->dev_private;
4584 uint32_t pipeconf;
4585
4586 pipeconf = I915_READ(PIPECONF(intel_crtc->pipe));
4587
4588 if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4589 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4590 * core speed.
4591 *
4592 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4593 * pipe == 0 check?
4594 */
4595 if (intel_crtc->config.requested_mode.clock >
4596 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4597 pipeconf |= PIPECONF_DOUBLE_WIDE;
4598 else
4599 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4600 }
4601
4602 /* default to 8bpc */
4603 pipeconf &= ~(PIPECONF_BPC_MASK | PIPECONF_DITHER_EN);
4604 if (intel_crtc->config.has_dp_encoder) {
4605 if (intel_crtc->config.dither) {
4606 pipeconf |= PIPECONF_6BPC |
4607 PIPECONF_DITHER_EN |
4608 PIPECONF_DITHER_TYPE_SP;
4609 }
4610 }
4611
4612 if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(&intel_crtc->base,
4613 INTEL_OUTPUT_EDP)) {
4614 if (intel_crtc->config.dither) {
4615 pipeconf |= PIPECONF_6BPC |
4616 PIPECONF_ENABLE |
4617 I965_PIPECONF_ACTIVE;
4618 }
4619 }
4620
4621 if (HAS_PIPE_CXSR(dev)) {
4622 if (intel_crtc->lowfreq_avail) {
4623 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4624 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4625 } else {
4626 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4627 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4628 }
4629 }
4630
4631 pipeconf &= ~PIPECONF_INTERLACE_MASK;
4632 if (!IS_GEN2(dev) &&
4633 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4634 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4635 else
4636 pipeconf |= PIPECONF_PROGRESSIVE;
4637
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03004638 if (IS_VALLEYVIEW(dev)) {
4639 if (intel_crtc->config.limited_color_range)
4640 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
4641 else
4642 pipeconf &= ~PIPECONF_COLOR_RANGE_SELECT;
4643 }
4644
Daniel Vetter84b046f2013-02-19 18:48:54 +01004645 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
4646 POSTING_READ(PIPECONF(intel_crtc->pipe));
4647}
4648
Eric Anholtf564048e2011-03-30 13:01:02 -07004649static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07004650 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004651 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004652{
4653 struct drm_device *dev = crtc->dev;
4654 struct drm_i915_private *dev_priv = dev->dev_private;
4655 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01004656 struct drm_display_mode *adjusted_mode =
4657 &intel_crtc->config.adjusted_mode;
4658 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08004659 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004660 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004661 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004662 intel_clock_t clock, reduced_clock;
Daniel Vetter84b046f2013-02-19 18:48:54 +01004663 u32 dspcntr;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004664 bool ok, has_reduced_clock = false, is_sdvo = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01004665 bool is_lvds = false, is_tv = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004666 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004667 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004668 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004669
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004670 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004671 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004672 case INTEL_OUTPUT_LVDS:
4673 is_lvds = true;
4674 break;
4675 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08004676 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08004677 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01004678 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004679 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004680 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004681 case INTEL_OUTPUT_TVOUT:
4682 is_tv = true;
4683 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004684 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004685
Eric Anholtc751ce42010-03-25 11:48:48 -07004686 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004687 }
4688
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004689 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004690
Ma Lingd4906092009-03-18 20:13:27 +08004691 /*
4692 * Returns a set of divisors for the desired target clock with the given
4693 * refclk, or FALSE. The returned values represent the clock equation:
4694 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4695 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004696 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08004697 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4698 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004699 if (!ok) {
4700 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004701 return -EINVAL;
4702 }
4703
4704 /* Ensure that the cursor is valid for the new mode before changing... */
4705 intel_crtc_update_cursor(crtc, true);
4706
4707 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004708 /*
4709 * Ensure we match the reduced clock's P to the target clock.
4710 * If the clocks don't match, we can't switch the display clock
4711 * by using the FP0/FP1. In such case we will disable the LVDS
4712 * downclock feature.
4713 */
Eric Anholtf564048e2011-03-30 13:01:02 -07004714 has_reduced_clock = limit->find_pll(limit, crtc,
4715 dev_priv->lvds_downclock,
4716 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08004717 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004718 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004719 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01004720 /* Compat-code for transition, will disappear. */
4721 if (!intel_crtc->config.clock_set) {
4722 intel_crtc->config.dpll.n = clock.n;
4723 intel_crtc->config.dpll.m1 = clock.m1;
4724 intel_crtc->config.dpll.m2 = clock.m2;
4725 intel_crtc->config.dpll.p1 = clock.p1;
4726 intel_crtc->config.dpll.p2 = clock.p2;
4727 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004728
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004729 if (is_sdvo && is_tv)
Daniel Vetterf47709a2013-03-28 10:42:02 +01004730 i9xx_adjust_sdvo_tv_clock(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004731
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004732 if (IS_GEN2(dev))
Daniel Vetterf47709a2013-03-28 10:42:02 +01004733 i8xx_update_pll(intel_crtc, adjusted_mode,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304734 has_reduced_clock ? &reduced_clock : NULL,
4735 num_connectors);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004736 else if (IS_VALLEYVIEW(dev))
Daniel Vetterf47709a2013-03-28 10:42:02 +01004737 vlv_update_pll(intel_crtc);
Eric Anholtf564048e2011-03-30 13:01:02 -07004738 else
Daniel Vetterf47709a2013-03-28 10:42:02 +01004739 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004740 has_reduced_clock ? &reduced_clock : NULL,
Jesse Barnes89b667f2013-04-18 14:51:36 -07004741 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004742
Eric Anholtf564048e2011-03-30 13:01:02 -07004743 /* Set up the display plane register */
4744 dspcntr = DISPPLANE_GAMMA_ENABLE;
4745
Jesse Barnesda6ecc52013-03-08 10:46:00 -08004746 if (!IS_VALLEYVIEW(dev)) {
4747 if (pipe == 0)
4748 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4749 else
4750 dspcntr |= DISPPLANE_SEL_PIPE_B;
4751 }
Eric Anholtf564048e2011-03-30 13:01:02 -07004752
Ville Syrjälä2582a852013-04-17 17:48:47 +03004753 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe_name(pipe));
Eric Anholtf564048e2011-03-30 13:01:02 -07004754 drm_mode_debug_printmodeline(mode);
4755
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004756 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Eric Anholtf564048e2011-03-30 13:01:02 -07004757
4758 /* pipesrc and dspsize control the size that is scaled from,
4759 * which should always be the user's requested size.
4760 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004761 I915_WRITE(DSPSIZE(plane),
4762 ((mode->vdisplay - 1) << 16) |
4763 (mode->hdisplay - 1));
4764 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004765
Daniel Vetter84b046f2013-02-19 18:48:54 +01004766 i9xx_set_pipeconf(intel_crtc);
4767
Eric Anholtf564048e2011-03-30 13:01:02 -07004768 I915_WRITE(DSPCNTR(plane), dspcntr);
4769 POSTING_READ(DSPCNTR(plane));
4770
Daniel Vetter94352cf2012-07-05 22:51:56 +02004771 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004772
4773 intel_update_watermarks(dev);
4774
Eric Anholtf564048e2011-03-30 13:01:02 -07004775 return ret;
4776}
4777
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01004778static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4779 struct intel_crtc_config *pipe_config)
4780{
4781 struct drm_device *dev = crtc->base.dev;
4782 struct drm_i915_private *dev_priv = dev->dev_private;
4783 uint32_t tmp;
4784
4785 tmp = I915_READ(PIPECONF(crtc->pipe));
4786 if (!(tmp & PIPECONF_ENABLE))
4787 return false;
4788
4789 return true;
4790}
4791
Paulo Zanonidde86e22012-12-01 12:04:25 -02004792static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07004793{
4794 struct drm_i915_private *dev_priv = dev->dev_private;
4795 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004796 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004797 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004798 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004799 bool has_cpu_edp = false;
4800 bool has_pch_edp = false;
4801 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07004802 bool has_ck505 = false;
4803 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004804
4805 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07004806 list_for_each_entry(encoder, &mode_config->encoder_list,
4807 base.head) {
4808 switch (encoder->type) {
4809 case INTEL_OUTPUT_LVDS:
4810 has_panel = true;
4811 has_lvds = true;
4812 break;
4813 case INTEL_OUTPUT_EDP:
4814 has_panel = true;
4815 if (intel_encoder_is_pch_edp(&encoder->base))
4816 has_pch_edp = true;
4817 else
4818 has_cpu_edp = true;
4819 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004820 }
4821 }
4822
Keith Packard99eb6a02011-09-26 14:29:12 -07004823 if (HAS_PCH_IBX(dev)) {
4824 has_ck505 = dev_priv->display_clock_mode;
4825 can_ssc = has_ck505;
4826 } else {
4827 has_ck505 = false;
4828 can_ssc = true;
4829 }
4830
4831 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4832 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4833 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004834
4835 /* Ironlake: try to setup display ref clock before DPLL
4836 * enabling. This is only under driver's control after
4837 * PCH B stepping, previous chipset stepping should be
4838 * ignoring this setting.
4839 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004840 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004841
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004842 /* As we must carefully and slowly disable/enable each source in turn,
4843 * compute the final state we want first and check if we need to
4844 * make any changes at all.
4845 */
4846 final = val;
4847 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07004848 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004849 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07004850 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004851 final |= DREF_NONSPREAD_SOURCE_ENABLE;
4852
4853 final &= ~DREF_SSC_SOURCE_MASK;
4854 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4855 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004856
Keith Packard199e5d72011-09-22 12:01:57 -07004857 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004858 final |= DREF_SSC_SOURCE_ENABLE;
4859
4860 if (intel_panel_use_ssc(dev_priv) && can_ssc)
4861 final |= DREF_SSC1_ENABLE;
4862
4863 if (has_cpu_edp) {
4864 if (intel_panel_use_ssc(dev_priv) && can_ssc)
4865 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4866 else
4867 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
4868 } else
4869 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4870 } else {
4871 final |= DREF_SSC_SOURCE_DISABLE;
4872 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4873 }
4874
4875 if (final == val)
4876 return;
4877
4878 /* Always enable nonspread source */
4879 val &= ~DREF_NONSPREAD_SOURCE_MASK;
4880
4881 if (has_ck505)
4882 val |= DREF_NONSPREAD_CK505_ENABLE;
4883 else
4884 val |= DREF_NONSPREAD_SOURCE_ENABLE;
4885
4886 if (has_panel) {
4887 val &= ~DREF_SSC_SOURCE_MASK;
4888 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004889
Keith Packard199e5d72011-09-22 12:01:57 -07004890 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07004891 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004892 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004893 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02004894 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004895 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004896
4897 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004898 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07004899 POSTING_READ(PCH_DREF_CONTROL);
4900 udelay(200);
4901
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004902 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004903
4904 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07004905 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07004906 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004907 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004908 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004909 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07004910 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004911 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004912 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004913 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004914
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004915 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07004916 POSTING_READ(PCH_DREF_CONTROL);
4917 udelay(200);
4918 } else {
4919 DRM_DEBUG_KMS("Disabling SSC entirely\n");
4920
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004921 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07004922
4923 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004924 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004925
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004926 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07004927 POSTING_READ(PCH_DREF_CONTROL);
4928 udelay(200);
4929
4930 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004931 val &= ~DREF_SSC_SOURCE_MASK;
4932 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004933
4934 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004935 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004936
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004937 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004938 POSTING_READ(PCH_DREF_CONTROL);
4939 udelay(200);
4940 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07004941
4942 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004943}
4944
Paulo Zanonidde86e22012-12-01 12:04:25 -02004945/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
4946static void lpt_init_pch_refclk(struct drm_device *dev)
4947{
4948 struct drm_i915_private *dev_priv = dev->dev_private;
4949 struct drm_mode_config *mode_config = &dev->mode_config;
4950 struct intel_encoder *encoder;
4951 bool has_vga = false;
4952 bool is_sdv = false;
4953 u32 tmp;
4954
4955 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
4956 switch (encoder->type) {
4957 case INTEL_OUTPUT_ANALOG:
4958 has_vga = true;
4959 break;
4960 }
4961 }
4962
4963 if (!has_vga)
4964 return;
4965
Daniel Vetterc00db242013-01-22 15:33:27 +01004966 mutex_lock(&dev_priv->dpio_lock);
4967
Paulo Zanonidde86e22012-12-01 12:04:25 -02004968 /* XXX: Rip out SDV support once Haswell ships for real. */
4969 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
4970 is_sdv = true;
4971
4972 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4973 tmp &= ~SBI_SSCCTL_DISABLE;
4974 tmp |= SBI_SSCCTL_PATHALT;
4975 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4976
4977 udelay(24);
4978
4979 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4980 tmp &= ~SBI_SSCCTL_PATHALT;
4981 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4982
4983 if (!is_sdv) {
4984 tmp = I915_READ(SOUTH_CHICKEN2);
4985 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
4986 I915_WRITE(SOUTH_CHICKEN2, tmp);
4987
4988 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
4989 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
4990 DRM_ERROR("FDI mPHY reset assert timeout\n");
4991
4992 tmp = I915_READ(SOUTH_CHICKEN2);
4993 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
4994 I915_WRITE(SOUTH_CHICKEN2, tmp);
4995
4996 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
4997 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
4998 100))
4999 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
5000 }
5001
5002 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5003 tmp &= ~(0xFF << 24);
5004 tmp |= (0x12 << 24);
5005 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5006
Paulo Zanonidde86e22012-12-01 12:04:25 -02005007 if (is_sdv) {
5008 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
5009 tmp |= 0x7FFF;
5010 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
5011 }
5012
5013 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5014 tmp |= (1 << 11);
5015 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5016
5017 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5018 tmp |= (1 << 11);
5019 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5020
5021 if (is_sdv) {
5022 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5023 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5024 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5025
5026 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5027 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5028 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5029
5030 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5031 tmp |= (0x3F << 8);
5032 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5033
5034 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5035 tmp |= (0x3F << 8);
5036 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5037 }
5038
5039 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5040 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5041 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5042
5043 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5044 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5045 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5046
5047 if (!is_sdv) {
5048 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5049 tmp &= ~(7 << 13);
5050 tmp |= (5 << 13);
5051 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5052
5053 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5054 tmp &= ~(7 << 13);
5055 tmp |= (5 << 13);
5056 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5057 }
5058
5059 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5060 tmp &= ~0xFF;
5061 tmp |= 0x1C;
5062 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5063
5064 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5065 tmp &= ~0xFF;
5066 tmp |= 0x1C;
5067 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5068
5069 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5070 tmp &= ~(0xFF << 16);
5071 tmp |= (0x1C << 16);
5072 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5073
5074 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5075 tmp &= ~(0xFF << 16);
5076 tmp |= (0x1C << 16);
5077 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5078
5079 if (!is_sdv) {
5080 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5081 tmp |= (1 << 27);
5082 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5083
5084 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5085 tmp |= (1 << 27);
5086 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5087
5088 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5089 tmp &= ~(0xF << 28);
5090 tmp |= (4 << 28);
5091 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5092
5093 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5094 tmp &= ~(0xF << 28);
5095 tmp |= (4 << 28);
5096 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5097 }
5098
5099 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5100 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5101 tmp |= SBI_DBUFF0_ENABLE;
5102 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005103
5104 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005105}
5106
5107/*
5108 * Initialize reference clocks when the driver loads
5109 */
5110void intel_init_pch_refclk(struct drm_device *dev)
5111{
5112 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5113 ironlake_init_pch_refclk(dev);
5114 else if (HAS_PCH_LPT(dev))
5115 lpt_init_pch_refclk(dev);
5116}
5117
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005118static int ironlake_get_refclk(struct drm_crtc *crtc)
5119{
5120 struct drm_device *dev = crtc->dev;
5121 struct drm_i915_private *dev_priv = dev->dev_private;
5122 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005123 struct intel_encoder *edp_encoder = NULL;
5124 int num_connectors = 0;
5125 bool is_lvds = false;
5126
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005127 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005128 switch (encoder->type) {
5129 case INTEL_OUTPUT_LVDS:
5130 is_lvds = true;
5131 break;
5132 case INTEL_OUTPUT_EDP:
5133 edp_encoder = encoder;
5134 break;
5135 }
5136 num_connectors++;
5137 }
5138
5139 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5140 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5141 dev_priv->lvds_ssc_freq);
5142 return dev_priv->lvds_ssc_freq * 1000;
5143 }
5144
5145 return 120000;
5146}
5147
Paulo Zanonic8203562012-09-12 10:06:29 -03005148static void ironlake_set_pipeconf(struct drm_crtc *crtc,
5149 struct drm_display_mode *adjusted_mode,
5150 bool dither)
5151{
5152 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5154 int pipe = intel_crtc->pipe;
5155 uint32_t val;
5156
5157 val = I915_READ(PIPECONF(pipe));
5158
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005159 val &= ~PIPECONF_BPC_MASK;
Daniel Vetter965e0c42013-03-27 00:44:57 +01005160 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03005161 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005162 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005163 break;
5164 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005165 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005166 break;
5167 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005168 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005169 break;
5170 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005171 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005172 break;
5173 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005174 /* Case prevented by intel_choose_pipe_bpp_dither. */
5175 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005176 }
5177
5178 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5179 if (dither)
5180 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5181
5182 val &= ~PIPECONF_INTERLACE_MASK;
5183 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5184 val |= PIPECONF_INTERLACED_ILK;
5185 else
5186 val |= PIPECONF_PROGRESSIVE;
5187
Daniel Vetter50f3b012013-03-27 00:44:56 +01005188 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005189 val |= PIPECONF_COLOR_RANGE_SELECT;
5190 else
5191 val &= ~PIPECONF_COLOR_RANGE_SELECT;
5192
Paulo Zanonic8203562012-09-12 10:06:29 -03005193 I915_WRITE(PIPECONF(pipe), val);
5194 POSTING_READ(PIPECONF(pipe));
5195}
5196
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005197/*
5198 * Set up the pipe CSC unit.
5199 *
5200 * Currently only full range RGB to limited range RGB conversion
5201 * is supported, but eventually this should handle various
5202 * RGB<->YCbCr scenarios as well.
5203 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01005204static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005205{
5206 struct drm_device *dev = crtc->dev;
5207 struct drm_i915_private *dev_priv = dev->dev_private;
5208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5209 int pipe = intel_crtc->pipe;
5210 uint16_t coeff = 0x7800; /* 1.0 */
5211
5212 /*
5213 * TODO: Check what kind of values actually come out of the pipe
5214 * with these coeff/postoff values and adjust to get the best
5215 * accuracy. Perhaps we even need to take the bpc value into
5216 * consideration.
5217 */
5218
Daniel Vetter50f3b012013-03-27 00:44:56 +01005219 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005220 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5221
5222 /*
5223 * GY/GU and RY/RU should be the other way around according
5224 * to BSpec, but reality doesn't agree. Just set them up in
5225 * a way that results in the correct picture.
5226 */
5227 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5228 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5229
5230 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5231 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5232
5233 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5234 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5235
5236 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5237 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5238 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5239
5240 if (INTEL_INFO(dev)->gen > 6) {
5241 uint16_t postoff = 0;
5242
Daniel Vetter50f3b012013-03-27 00:44:56 +01005243 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005244 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5245
5246 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5247 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5248 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5249
5250 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5251 } else {
5252 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5253
Daniel Vetter50f3b012013-03-27 00:44:56 +01005254 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005255 mode |= CSC_BLACK_SCREEN_OFFSET;
5256
5257 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5258 }
5259}
5260
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005261static void haswell_set_pipeconf(struct drm_crtc *crtc,
5262 struct drm_display_mode *adjusted_mode,
5263 bool dither)
5264{
5265 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5266 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02005267 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005268 uint32_t val;
5269
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005270 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005271
5272 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5273 if (dither)
5274 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5275
5276 val &= ~PIPECONF_INTERLACE_MASK_HSW;
5277 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5278 val |= PIPECONF_INTERLACED_ILK;
5279 else
5280 val |= PIPECONF_PROGRESSIVE;
5281
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005282 I915_WRITE(PIPECONF(cpu_transcoder), val);
5283 POSTING_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005284}
5285
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005286static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5287 struct drm_display_mode *adjusted_mode,
5288 intel_clock_t *clock,
5289 bool *has_reduced_clock,
5290 intel_clock_t *reduced_clock)
5291{
5292 struct drm_device *dev = crtc->dev;
5293 struct drm_i915_private *dev_priv = dev->dev_private;
5294 struct intel_encoder *intel_encoder;
5295 int refclk;
5296 const intel_limit_t *limit;
5297 bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
5298
5299 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5300 switch (intel_encoder->type) {
5301 case INTEL_OUTPUT_LVDS:
5302 is_lvds = true;
5303 break;
5304 case INTEL_OUTPUT_SDVO:
5305 case INTEL_OUTPUT_HDMI:
5306 is_sdvo = true;
5307 if (intel_encoder->needs_tv_clock)
5308 is_tv = true;
5309 break;
5310 case INTEL_OUTPUT_TVOUT:
5311 is_tv = true;
5312 break;
5313 }
5314 }
5315
5316 refclk = ironlake_get_refclk(crtc);
5317
5318 /*
5319 * Returns a set of divisors for the desired target clock with the given
5320 * refclk, or FALSE. The returned values represent the clock equation:
5321 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5322 */
5323 limit = intel_limit(crtc, refclk);
5324 ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5325 clock);
5326 if (!ret)
5327 return false;
5328
5329 if (is_lvds && dev_priv->lvds_downclock_avail) {
5330 /*
5331 * Ensure we match the reduced clock's P to the target clock.
5332 * If the clocks don't match, we can't switch the display clock
5333 * by using the FP0/FP1. In such case we will disable the LVDS
5334 * downclock feature.
5335 */
5336 *has_reduced_clock = limit->find_pll(limit, crtc,
5337 dev_priv->lvds_downclock,
5338 refclk,
5339 clock,
5340 reduced_clock);
5341 }
5342
5343 if (is_sdvo && is_tv)
Daniel Vetterf47709a2013-03-28 10:42:02 +01005344 i9xx_adjust_sdvo_tv_clock(to_intel_crtc(crtc));
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005345
5346 return true;
5347}
5348
Daniel Vetter01a415f2012-10-27 15:58:40 +02005349static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5350{
5351 struct drm_i915_private *dev_priv = dev->dev_private;
5352 uint32_t temp;
5353
5354 temp = I915_READ(SOUTH_CHICKEN1);
5355 if (temp & FDI_BC_BIFURCATION_SELECT)
5356 return;
5357
5358 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5359 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5360
5361 temp |= FDI_BC_BIFURCATION_SELECT;
5362 DRM_DEBUG_KMS("enabling fdi C rx\n");
5363 I915_WRITE(SOUTH_CHICKEN1, temp);
5364 POSTING_READ(SOUTH_CHICKEN1);
5365}
5366
5367static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
5368{
5369 struct drm_device *dev = intel_crtc->base.dev;
5370 struct drm_i915_private *dev_priv = dev->dev_private;
5371 struct intel_crtc *pipe_B_crtc =
5372 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5373
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005374 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5375 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
Daniel Vetter01a415f2012-10-27 15:58:40 +02005376 if (intel_crtc->fdi_lanes > 4) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005377 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5378 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
Daniel Vetter01a415f2012-10-27 15:58:40 +02005379 /* Clamp lanes to avoid programming the hw with bogus values. */
5380 intel_crtc->fdi_lanes = 4;
5381
5382 return false;
5383 }
5384
Ben Widawsky7eb552a2013-03-13 14:05:41 -07005385 if (INTEL_INFO(dev)->num_pipes == 2)
Daniel Vetter01a415f2012-10-27 15:58:40 +02005386 return true;
5387
5388 switch (intel_crtc->pipe) {
5389 case PIPE_A:
5390 return true;
5391 case PIPE_B:
5392 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5393 intel_crtc->fdi_lanes > 2) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005394 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5395 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
Daniel Vetter01a415f2012-10-27 15:58:40 +02005396 /* Clamp lanes to avoid programming the hw with bogus values. */
5397 intel_crtc->fdi_lanes = 2;
5398
5399 return false;
5400 }
5401
5402 if (intel_crtc->fdi_lanes > 2)
5403 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5404 else
5405 cpt_enable_fdi_bc_bifurcation(dev);
5406
5407 return true;
5408 case PIPE_C:
5409 if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
5410 if (intel_crtc->fdi_lanes > 2) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005411 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5412 pipe_name(intel_crtc->pipe), intel_crtc->fdi_lanes);
Daniel Vetter01a415f2012-10-27 15:58:40 +02005413 /* Clamp lanes to avoid programming the hw with bogus values. */
5414 intel_crtc->fdi_lanes = 2;
5415
5416 return false;
5417 }
5418 } else {
5419 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5420 return false;
5421 }
5422
5423 cpt_enable_fdi_bc_bifurcation(dev);
5424
5425 return true;
5426 default:
5427 BUG();
5428 }
5429}
5430
Paulo Zanonid4b19312012-11-29 11:29:32 -02005431int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5432{
5433 /*
5434 * Account for spread spectrum to avoid
5435 * oversubscribing the link. Max center spread
5436 * is 2.5%; use 5% for safety's sake.
5437 */
5438 u32 bps = target_clock * bpp * 21 / 20;
5439 return bps / (link_bw * 8) + 1;
5440}
5441
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005442void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5443 struct intel_link_m_n *m_n)
5444{
5445 struct drm_device *dev = crtc->base.dev;
5446 struct drm_i915_private *dev_priv = dev->dev_private;
5447 int pipe = crtc->pipe;
5448
5449 I915_WRITE(TRANSDATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5450 I915_WRITE(TRANSDATA_N1(pipe), m_n->gmch_n);
5451 I915_WRITE(TRANSDPLINK_M1(pipe), m_n->link_m);
5452 I915_WRITE(TRANSDPLINK_N1(pipe), m_n->link_n);
5453}
5454
5455void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5456 struct intel_link_m_n *m_n)
5457{
5458 struct drm_device *dev = crtc->base.dev;
5459 struct drm_i915_private *dev_priv = dev->dev_private;
5460 int pipe = crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005461 enum transcoder transcoder = crtc->config.cpu_transcoder;
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005462
5463 if (INTEL_INFO(dev)->gen >= 5) {
5464 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5465 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5466 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5467 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5468 } else {
5469 I915_WRITE(PIPE_GMCH_DATA_M(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5470 I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n->gmch_n);
5471 I915_WRITE(PIPE_DP_LINK_M(pipe), m_n->link_m);
5472 I915_WRITE(PIPE_DP_LINK_N(pipe), m_n->link_n);
5473 }
5474}
5475
5476static void ironlake_fdi_set_m_n(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08005477{
5478 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08005479 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005480 struct drm_display_mode *adjusted_mode =
5481 &intel_crtc->config.adjusted_mode;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005482 struct intel_link_m_n m_n = {0};
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005483 int target_clock, lane, link_bw;
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005484
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005485 /* FDI is a binary signal running at ~2.7GHz, encoding
5486 * each output octet as 10 bits. The actual frequency
5487 * is stored as a divider into a 100MHz clock, and the
5488 * mode pixel clock is stored in units of 1KHz.
5489 * Hence the bw of each lane in terms of the mode signal
5490 * is:
5491 */
5492 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005493
Daniel Vetterdf92b1e2013-03-28 10:41:58 +01005494 if (intel_crtc->config.pixel_target_clock)
5495 target_clock = intel_crtc->config.pixel_target_clock;
Daniel Vetter94bf2ce2012-06-04 18:39:19 +02005496 else
5497 target_clock = adjusted_mode->clock;
5498
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005499 lane = ironlake_get_lanes_required(target_clock, link_bw,
5500 intel_crtc->config.pipe_bpp);
Eric Anholt8febb292011-03-30 13:01:07 -07005501
5502 intel_crtc->fdi_lanes = lane;
5503
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005504 if (intel_crtc->config.pixel_multiplier > 1)
5505 link_bw *= intel_crtc->config.pixel_multiplier;
Daniel Vetter965e0c42013-03-27 00:44:57 +01005506 intel_link_compute_m_n(intel_crtc->config.pipe_bpp, lane, target_clock,
5507 link_bw, &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07005508
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005509 intel_cpu_transcoder_set_m_n(intel_crtc, &m_n);
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005510}
5511
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005512static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
5513{
5514 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
5515}
5516
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005517static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005518 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005519 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005520{
5521 struct drm_crtc *crtc = &intel_crtc->base;
5522 struct drm_device *dev = crtc->dev;
5523 struct drm_i915_private *dev_priv = dev->dev_private;
5524 struct intel_encoder *intel_encoder;
5525 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005526 int factor, num_connectors = 0;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005527 bool is_lvds = false, is_sdvo = false, is_tv = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005528
5529 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5530 switch (intel_encoder->type) {
5531 case INTEL_OUTPUT_LVDS:
5532 is_lvds = true;
5533 break;
5534 case INTEL_OUTPUT_SDVO:
5535 case INTEL_OUTPUT_HDMI:
5536 is_sdvo = true;
5537 if (intel_encoder->needs_tv_clock)
5538 is_tv = true;
5539 break;
5540 case INTEL_OUTPUT_TVOUT:
5541 is_tv = true;
5542 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005543 }
5544
5545 num_connectors++;
5546 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005547
Chris Wilsonc1858122010-12-03 21:35:48 +00005548 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005549 factor = 21;
5550 if (is_lvds) {
5551 if ((intel_panel_use_ssc(dev_priv) &&
5552 dev_priv->lvds_ssc_freq == 100) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02005553 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07005554 factor = 25;
5555 } else if (is_sdvo && is_tv)
5556 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005557
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005558 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02005559 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005560
Daniel Vetter9a7c7892013-04-04 22:20:34 +02005561 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
5562 *fp2 |= FP_CB_TUNE;
5563
Chris Wilson5eddb702010-09-11 13:48:45 +01005564 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005565
Eric Anholta07d6782011-03-30 13:01:08 -07005566 if (is_lvds)
5567 dpll |= DPLLB_MODE_LVDS;
5568 else
5569 dpll |= DPLLB_MODE_DAC_SERIAL;
5570 if (is_sdvo) {
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005571 if (intel_crtc->config.pixel_multiplier > 1) {
5572 dpll |= (intel_crtc->config.pixel_multiplier - 1)
5573 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08005574 }
Eric Anholta07d6782011-03-30 13:01:08 -07005575 dpll |= DPLL_DVO_HIGH_SPEED;
5576 }
Daniel Vetter8b470472013-03-28 10:41:59 +01005577 if (intel_crtc->config.has_dp_encoder &&
5578 intel_crtc->config.has_pch_encoder)
Eric Anholta07d6782011-03-30 13:01:08 -07005579 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005580
Eric Anholta07d6782011-03-30 13:01:08 -07005581 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005582 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005583 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005584 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005585
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005586 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005587 case 5:
5588 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5589 break;
5590 case 7:
5591 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5592 break;
5593 case 10:
5594 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5595 break;
5596 case 14:
5597 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5598 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005599 }
5600
5601 if (is_sdvo && is_tv)
5602 dpll |= PLL_REF_INPUT_TVCLKINBC;
5603 else if (is_tv)
5604 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005605 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08005606 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00005607 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005608 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005609 else
5610 dpll |= PLL_REF_INPUT_DREFCLK;
5611
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005612 return dpll;
5613}
5614
Jesse Barnes79e53942008-11-07 14:24:08 -08005615static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08005616 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005617 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005618{
5619 struct drm_device *dev = crtc->dev;
5620 struct drm_i915_private *dev_priv = dev->dev_private;
5621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005622 struct drm_display_mode *adjusted_mode =
5623 &intel_crtc->config.adjusted_mode;
5624 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08005625 int pipe = intel_crtc->pipe;
5626 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005627 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005628 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005629 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005630 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01005631 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005632 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005633 int ret;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005634 bool dither, fdi_config_ok;
Jesse Barnes79e53942008-11-07 14:24:08 -08005635
5636 for_each_encoder_on_crtc(dev, crtc, encoder) {
5637 switch (encoder->type) {
5638 case INTEL_OUTPUT_LVDS:
5639 is_lvds = true;
5640 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005641 }
5642
5643 num_connectors++;
5644 }
5645
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005646 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5647 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5648
Daniel Vetter3b117c82013-04-17 20:15:07 +02005649 intel_crtc->config.cpu_transcoder = pipe;
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005650
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005651 ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
5652 &has_reduced_clock, &reduced_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005653 if (!ok) {
5654 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5655 return -EINVAL;
5656 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01005657 /* Compat-code for transition, will disappear. */
5658 if (!intel_crtc->config.clock_set) {
5659 intel_crtc->config.dpll.n = clock.n;
5660 intel_crtc->config.dpll.m1 = clock.m1;
5661 intel_crtc->config.dpll.m2 = clock.m2;
5662 intel_crtc->config.dpll.p1 = clock.p1;
5663 intel_crtc->config.dpll.p2 = clock.p2;
5664 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005665
5666 /* Ensure that the cursor is valid for the new mode before changing... */
5667 intel_crtc_update_cursor(crtc, true);
5668
Jesse Barnes79e53942008-11-07 14:24:08 -08005669 /* determine panel color depth */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01005670 dither = intel_crtc->config.dither;
Paulo Zanonic8203562012-09-12 10:06:29 -03005671 if (is_lvds && dev_priv->lvds_dither)
5672 dither = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005673
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005674 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08005675 drm_mode_debug_printmodeline(mode);
5676
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005677 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01005678 if (intel_crtc->config.has_pch_encoder) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005679 struct intel_pch_pll *pll;
Chris Wilson5eddb702010-09-11 13:48:45 +01005680
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005681 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005682 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005683 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005684
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005685 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02005686 &fp, &reduced_clock,
5687 has_reduced_clock ? &fp2 : NULL);
5688
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005689 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5690 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005691 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
5692 pipe_name(pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07005693 return -EINVAL;
5694 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005695 } else
5696 intel_put_pch_pll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005697
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005698 if (intel_crtc->config.has_dp_encoder)
5699 intel_dp_set_m_n(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005700
Daniel Vetterdafd2262012-11-26 17:22:07 +01005701 for_each_encoder_on_crtc(dev, crtc, encoder)
5702 if (encoder->pre_pll_enable)
5703 encoder->pre_pll_enable(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08005704
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005705 if (intel_crtc->pch_pll) {
5706 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005707
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005708 /* Wait for the clocks to stabilize. */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005709 POSTING_READ(intel_crtc->pch_pll->pll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005710 udelay(150);
5711
Eric Anholt8febb292011-03-30 13:01:07 -07005712 /* The pixel multiplier can only be updated once the
5713 * DPLL is enabled and the clocks are stable.
5714 *
5715 * So write it again.
5716 */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005717 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08005718 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005719
Chris Wilson5eddb702010-09-11 13:48:45 +01005720 intel_crtc->lowfreq_avail = false;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005721 if (intel_crtc->pch_pll) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07005722 if (is_lvds && has_reduced_clock && i915_powersave) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005723 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005724 intel_crtc->lowfreq_avail = true;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005725 } else {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005726 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07005727 }
5728 }
5729
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005730 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005731
Daniel Vetter01a415f2012-10-27 15:58:40 +02005732 /* Note, this also computes intel_crtc->fdi_lanes which is used below in
5733 * ironlake_check_fdi_lanes. */
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005734 intel_crtc->fdi_lanes = 0;
5735 if (intel_crtc->config.has_pch_encoder)
5736 ironlake_fdi_set_m_n(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01005737
Daniel Vetter01a415f2012-10-27 15:58:40 +02005738 fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005739
Paulo Zanonic8203562012-09-12 10:06:29 -03005740 ironlake_set_pipeconf(crtc, adjusted_mode, dither);
Jesse Barnes79e53942008-11-07 14:24:08 -08005741
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005742 /* Set up the display plane register */
5743 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005744 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005745
Daniel Vetter94352cf2012-07-05 22:51:56 +02005746 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005747
5748 intel_update_watermarks(dev);
5749
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03005750 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5751
Daniel Vetter01a415f2012-10-27 15:58:40 +02005752 return fdi_config_ok ? ret : -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005753}
5754
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005755static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5756 struct intel_crtc_config *pipe_config)
5757{
5758 struct drm_device *dev = crtc->base.dev;
5759 struct drm_i915_private *dev_priv = dev->dev_private;
5760 uint32_t tmp;
5761
5762 tmp = I915_READ(PIPECONF(crtc->pipe));
5763 if (!(tmp & PIPECONF_ENABLE))
5764 return false;
5765
Daniel Vetter88adfff2013-03-28 10:42:01 +01005766 if (I915_READ(TRANSCONF(crtc->pipe)) & TRANS_ENABLE)
5767 pipe_config->has_pch_encoder = true;
5768
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005769 return true;
5770}
5771
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005772static void haswell_modeset_global_resources(struct drm_device *dev)
5773{
5774 struct drm_i915_private *dev_priv = dev->dev_private;
5775 bool enable = false;
5776 struct intel_crtc *crtc;
5777 struct intel_encoder *encoder;
5778
5779 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
5780 if (crtc->pipe != PIPE_A && crtc->base.enabled)
5781 enable = true;
5782 /* XXX: Should check for edp transcoder here, but thanks to init
5783 * sequence that's not yet available. Just in case desktop eDP
5784 * on PORT D is possible on haswell, too. */
5785 }
5786
5787 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
5788 base.head) {
5789 if (encoder->type != INTEL_OUTPUT_EDP &&
5790 encoder->connectors_active)
5791 enable = true;
5792 }
5793
5794 /* Even the eDP panel fitter is outside the always-on well. */
5795 if (dev_priv->pch_pf_size)
5796 enable = true;
5797
5798 intel_set_power_well(dev, enable);
5799}
5800
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005801static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005802 int x, int y,
5803 struct drm_framebuffer *fb)
5804{
5805 struct drm_device *dev = crtc->dev;
5806 struct drm_i915_private *dev_priv = dev->dev_private;
5807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005808 struct drm_display_mode *adjusted_mode =
5809 &intel_crtc->config.adjusted_mode;
5810 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005811 int pipe = intel_crtc->pipe;
5812 int plane = intel_crtc->plane;
5813 int num_connectors = 0;
Daniel Vetter8b470472013-03-28 10:41:59 +01005814 bool is_cpu_edp = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005815 struct intel_encoder *encoder;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005816 int ret;
5817 bool dither;
5818
5819 for_each_encoder_on_crtc(dev, crtc, encoder) {
5820 switch (encoder->type) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005821 case INTEL_OUTPUT_EDP:
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005822 if (!intel_encoder_is_pch_edp(&encoder->base))
5823 is_cpu_edp = true;
5824 break;
5825 }
5826
5827 num_connectors++;
5828 }
5829
Daniel Vetterbba21812013-03-22 10:53:40 +01005830 if (is_cpu_edp)
Daniel Vetter3b117c82013-04-17 20:15:07 +02005831 intel_crtc->config.cpu_transcoder = TRANSCODER_EDP;
Daniel Vetterbba21812013-03-22 10:53:40 +01005832 else
Daniel Vetter3b117c82013-04-17 20:15:07 +02005833 intel_crtc->config.cpu_transcoder = pipe;
Daniel Vetterbba21812013-03-22 10:53:40 +01005834
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005835 /* We are not sure yet this won't happen. */
5836 WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
5837 INTEL_PCH_TYPE(dev));
5838
5839 WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
5840 num_connectors, pipe_name(pipe));
5841
Daniel Vetter3b117c82013-04-17 20:15:07 +02005842 WARN_ON(I915_READ(PIPECONF(intel_crtc->config.cpu_transcoder)) &
Paulo Zanoni1ce42922012-10-05 12:06:01 -03005843 (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
5844
5845 WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
5846
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005847 if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
5848 return -EINVAL;
5849
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005850 /* Ensure that the cursor is valid for the new mode before changing... */
5851 intel_crtc_update_cursor(crtc, true);
5852
5853 /* determine panel color depth */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01005854 dither = intel_crtc->config.dither;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005855
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03005856 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe_name(pipe));
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005857 drm_mode_debug_printmodeline(mode);
5858
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005859 if (intel_crtc->config.has_dp_encoder)
5860 intel_dp_set_m_n(intel_crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005861
5862 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005863
5864 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5865
Daniel Vetter6cf86a52013-04-02 23:38:10 +02005866 if (intel_crtc->config.has_pch_encoder)
5867 ironlake_fdi_set_m_n(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005868
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005869 haswell_set_pipeconf(crtc, adjusted_mode, dither);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005870
Daniel Vetter50f3b012013-03-27 00:44:56 +01005871 intel_set_pipe_csc(crtc);
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005872
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005873 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005874 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005875 POSTING_READ(DSPCNTR(plane));
5876
5877 ret = intel_pipe_set_base(crtc, x, y, fb);
5878
5879 intel_update_watermarks(dev);
5880
5881 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5882
Jesse Barnes79e53942008-11-07 14:24:08 -08005883 return ret;
5884}
5885
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005886static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5887 struct intel_crtc_config *pipe_config)
5888{
5889 struct drm_device *dev = crtc->base.dev;
5890 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2bfce952013-04-18 16:35:40 -03005891 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005892 uint32_t tmp;
5893
Paulo Zanoni2bfce952013-04-18 16:35:40 -03005894 if (!intel_using_power_well(dev_priv->dev) &&
5895 cpu_transcoder != TRANSCODER_EDP)
5896 return false;
5897
5898 tmp = I915_READ(PIPECONF(cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005899 if (!(tmp & PIPECONF_ENABLE))
5900 return false;
5901
Daniel Vetter88adfff2013-03-28 10:42:01 +01005902 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03005903 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01005904 * DDI E. So just check whether this pipe is wired to DDI E and whether
5905 * the PCH transcoder is on.
5906 */
Paulo Zanonif196e6b2013-04-18 16:35:41 -03005907 tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01005908 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
5909 I915_READ(TRANSCONF(PIPE_A)) & TRANS_ENABLE)
5910 pipe_config->has_pch_encoder = true;
5911
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01005912 return true;
5913}
5914
Eric Anholtf564048e2011-03-30 13:01:02 -07005915static int intel_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005916 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005917 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07005918{
5919 struct drm_device *dev = crtc->dev;
5920 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01005921 struct drm_encoder_helper_funcs *encoder_funcs;
5922 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07005923 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005924 struct drm_display_mode *adjusted_mode =
5925 &intel_crtc->config.adjusted_mode;
5926 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
Eric Anholt0b701d22011-03-30 13:01:03 -07005927 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07005928 int ret;
5929
Eric Anholt0b701d22011-03-30 13:01:03 -07005930 drm_vblank_pre_modeset(dev, pipe);
5931
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005932 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
5933
Jesse Barnes79e53942008-11-07 14:24:08 -08005934 drm_vblank_post_modeset(dev, pipe);
5935
Daniel Vetter9256aa12012-10-31 19:26:13 +01005936 if (ret != 0)
5937 return ret;
5938
5939 for_each_encoder_on_crtc(dev, crtc, encoder) {
5940 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
5941 encoder->base.base.id,
5942 drm_get_encoder_name(&encoder->base),
5943 mode->base.id, mode->name);
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005944 if (encoder->mode_set) {
5945 encoder->mode_set(encoder);
5946 } else {
5947 encoder_funcs = encoder->base.helper_private;
5948 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
5949 }
Daniel Vetter9256aa12012-10-31 19:26:13 +01005950 }
5951
5952 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005953}
5954
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005955static bool intel_eld_uptodate(struct drm_connector *connector,
5956 int reg_eldv, uint32_t bits_eldv,
5957 int reg_elda, uint32_t bits_elda,
5958 int reg_edid)
5959{
5960 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5961 uint8_t *eld = connector->eld;
5962 uint32_t i;
5963
5964 i = I915_READ(reg_eldv);
5965 i &= bits_eldv;
5966
5967 if (!eld[0])
5968 return !i;
5969
5970 if (!i)
5971 return false;
5972
5973 i = I915_READ(reg_elda);
5974 i &= ~bits_elda;
5975 I915_WRITE(reg_elda, i);
5976
5977 for (i = 0; i < eld[2]; i++)
5978 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
5979 return false;
5980
5981 return true;
5982}
5983
Wu Fengguange0dac652011-09-05 14:25:34 +08005984static void g4x_write_eld(struct drm_connector *connector,
5985 struct drm_crtc *crtc)
5986{
5987 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5988 uint8_t *eld = connector->eld;
5989 uint32_t eldv;
5990 uint32_t len;
5991 uint32_t i;
5992
5993 i = I915_READ(G4X_AUD_VID_DID);
5994
5995 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
5996 eldv = G4X_ELDV_DEVCL_DEVBLC;
5997 else
5998 eldv = G4X_ELDV_DEVCTG;
5999
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006000 if (intel_eld_uptodate(connector,
6001 G4X_AUD_CNTL_ST, eldv,
6002 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6003 G4X_HDMIW_HDMIEDID))
6004 return;
6005
Wu Fengguange0dac652011-09-05 14:25:34 +08006006 i = I915_READ(G4X_AUD_CNTL_ST);
6007 i &= ~(eldv | G4X_ELD_ADDR);
6008 len = (i >> 9) & 0x1f; /* ELD buffer size */
6009 I915_WRITE(G4X_AUD_CNTL_ST, i);
6010
6011 if (!eld[0])
6012 return;
6013
6014 len = min_t(uint8_t, eld[2], len);
6015 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6016 for (i = 0; i < len; i++)
6017 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6018
6019 i = I915_READ(G4X_AUD_CNTL_ST);
6020 i |= eldv;
6021 I915_WRITE(G4X_AUD_CNTL_ST, i);
6022}
6023
Wang Xingchao83358c852012-08-16 22:43:37 +08006024static void haswell_write_eld(struct drm_connector *connector,
6025 struct drm_crtc *crtc)
6026{
6027 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6028 uint8_t *eld = connector->eld;
6029 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08006031 uint32_t eldv;
6032 uint32_t i;
6033 int len;
6034 int pipe = to_intel_crtc(crtc)->pipe;
6035 int tmp;
6036
6037 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
6038 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
6039 int aud_config = HSW_AUD_CFG(pipe);
6040 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
6041
6042
6043 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
6044
6045 /* Audio output enable */
6046 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
6047 tmp = I915_READ(aud_cntrl_st2);
6048 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
6049 I915_WRITE(aud_cntrl_st2, tmp);
6050
6051 /* Wait for 1 vertical blank */
6052 intel_wait_for_vblank(dev, pipe);
6053
6054 /* Set ELD valid state */
6055 tmp = I915_READ(aud_cntrl_st2);
6056 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
6057 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
6058 I915_WRITE(aud_cntrl_st2, tmp);
6059 tmp = I915_READ(aud_cntrl_st2);
6060 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
6061
6062 /* Enable HDMI mode */
6063 tmp = I915_READ(aud_config);
6064 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
6065 /* clear N_programing_enable and N_value_index */
6066 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
6067 I915_WRITE(aud_config, tmp);
6068
6069 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
6070
6071 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006072 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006073
6074 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6075 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6076 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6077 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6078 } else
6079 I915_WRITE(aud_config, 0);
6080
6081 if (intel_eld_uptodate(connector,
6082 aud_cntrl_st2, eldv,
6083 aud_cntl_st, IBX_ELD_ADDRESS,
6084 hdmiw_hdmiedid))
6085 return;
6086
6087 i = I915_READ(aud_cntrl_st2);
6088 i &= ~eldv;
6089 I915_WRITE(aud_cntrl_st2, i);
6090
6091 if (!eld[0])
6092 return;
6093
6094 i = I915_READ(aud_cntl_st);
6095 i &= ~IBX_ELD_ADDRESS;
6096 I915_WRITE(aud_cntl_st, i);
6097 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6098 DRM_DEBUG_DRIVER("port num:%d\n", i);
6099
6100 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6101 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6102 for (i = 0; i < len; i++)
6103 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6104
6105 i = I915_READ(aud_cntrl_st2);
6106 i |= eldv;
6107 I915_WRITE(aud_cntrl_st2, i);
6108
6109}
6110
Wu Fengguange0dac652011-09-05 14:25:34 +08006111static void ironlake_write_eld(struct drm_connector *connector,
6112 struct drm_crtc *crtc)
6113{
6114 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6115 uint8_t *eld = connector->eld;
6116 uint32_t eldv;
6117 uint32_t i;
6118 int len;
6119 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006120 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006121 int aud_cntl_st;
6122 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006123 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006124
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006125 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006126 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6127 aud_config = IBX_AUD_CFG(pipe);
6128 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006129 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006130 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006131 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6132 aud_config = CPT_AUD_CFG(pipe);
6133 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006134 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006135 }
6136
Wang Xingchao9b138a82012-08-09 16:52:18 +08006137 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006138
6139 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006140 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006141 if (!i) {
6142 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6143 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006144 eldv = IBX_ELD_VALIDB;
6145 eldv |= IBX_ELD_VALIDB << 4;
6146 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006147 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03006148 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006149 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006150 }
6151
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006152 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 */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006155 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6156 } else
6157 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006158
6159 if (intel_eld_uptodate(connector,
6160 aud_cntrl_st2, eldv,
6161 aud_cntl_st, IBX_ELD_ADDRESS,
6162 hdmiw_hdmiedid))
6163 return;
6164
Wu Fengguange0dac652011-09-05 14:25:34 +08006165 i = I915_READ(aud_cntrl_st2);
6166 i &= ~eldv;
6167 I915_WRITE(aud_cntrl_st2, i);
6168
6169 if (!eld[0])
6170 return;
6171
Wu Fengguange0dac652011-09-05 14:25:34 +08006172 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006173 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006174 I915_WRITE(aud_cntl_st, i);
6175
6176 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6177 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6178 for (i = 0; i < len; i++)
6179 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6180
6181 i = I915_READ(aud_cntrl_st2);
6182 i |= eldv;
6183 I915_WRITE(aud_cntrl_st2, i);
6184}
6185
6186void intel_write_eld(struct drm_encoder *encoder,
6187 struct drm_display_mode *mode)
6188{
6189 struct drm_crtc *crtc = encoder->crtc;
6190 struct drm_connector *connector;
6191 struct drm_device *dev = encoder->dev;
6192 struct drm_i915_private *dev_priv = dev->dev_private;
6193
6194 connector = drm_select_eld(encoder, mode);
6195 if (!connector)
6196 return;
6197
6198 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6199 connector->base.id,
6200 drm_get_connector_name(connector),
6201 connector->encoder->base.id,
6202 drm_get_encoder_name(connector->encoder));
6203
6204 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6205
6206 if (dev_priv->display.write_eld)
6207 dev_priv->display.write_eld(connector, crtc);
6208}
6209
Jesse Barnes79e53942008-11-07 14:24:08 -08006210/** Loads the palette/gamma unit for the CRTC with the prepared values */
6211void intel_crtc_load_lut(struct drm_crtc *crtc)
6212{
6213 struct drm_device *dev = crtc->dev;
6214 struct drm_i915_private *dev_priv = dev->dev_private;
6215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006216 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006217 int i;
6218
6219 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006220 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006221 return;
6222
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006223 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006224 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006225 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006226
Jesse Barnes79e53942008-11-07 14:24:08 -08006227 for (i = 0; i < 256; i++) {
6228 I915_WRITE(palreg + 4 * i,
6229 (intel_crtc->lut_r[i] << 16) |
6230 (intel_crtc->lut_g[i] << 8) |
6231 intel_crtc->lut_b[i]);
6232 }
6233}
6234
Chris Wilson560b85b2010-08-07 11:01:38 +01006235static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6236{
6237 struct drm_device *dev = crtc->dev;
6238 struct drm_i915_private *dev_priv = dev->dev_private;
6239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6240 bool visible = base != 0;
6241 u32 cntl;
6242
6243 if (intel_crtc->cursor_visible == visible)
6244 return;
6245
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006246 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006247 if (visible) {
6248 /* On these chipsets we can only modify the base whilst
6249 * the cursor is disabled.
6250 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006251 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006252
6253 cntl &= ~(CURSOR_FORMAT_MASK);
6254 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6255 cntl |= CURSOR_ENABLE |
6256 CURSOR_GAMMA_ENABLE |
6257 CURSOR_FORMAT_ARGB;
6258 } else
6259 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006260 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006261
6262 intel_crtc->cursor_visible = visible;
6263}
6264
6265static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6266{
6267 struct drm_device *dev = crtc->dev;
6268 struct drm_i915_private *dev_priv = dev->dev_private;
6269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6270 int pipe = intel_crtc->pipe;
6271 bool visible = base != 0;
6272
6273 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006274 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006275 if (base) {
6276 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6277 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6278 cntl |= pipe << 28; /* Connect to correct pipe */
6279 } else {
6280 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6281 cntl |= CURSOR_MODE_DISABLE;
6282 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006283 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006284
6285 intel_crtc->cursor_visible = visible;
6286 }
6287 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006288 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006289}
6290
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006291static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6292{
6293 struct drm_device *dev = crtc->dev;
6294 struct drm_i915_private *dev_priv = dev->dev_private;
6295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6296 int pipe = intel_crtc->pipe;
6297 bool visible = base != 0;
6298
6299 if (intel_crtc->cursor_visible != visible) {
6300 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6301 if (base) {
6302 cntl &= ~CURSOR_MODE;
6303 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6304 } else {
6305 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6306 cntl |= CURSOR_MODE_DISABLE;
6307 }
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006308 if (IS_HASWELL(dev))
6309 cntl |= CURSOR_PIPE_CSC_ENABLE;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006310 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6311
6312 intel_crtc->cursor_visible = visible;
6313 }
6314 /* and commit changes on next vblank */
6315 I915_WRITE(CURBASE_IVB(pipe), base);
6316}
6317
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006318/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006319static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6320 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006321{
6322 struct drm_device *dev = crtc->dev;
6323 struct drm_i915_private *dev_priv = dev->dev_private;
6324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6325 int pipe = intel_crtc->pipe;
6326 int x = intel_crtc->cursor_x;
6327 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006328 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006329 bool visible;
6330
6331 pos = 0;
6332
Chris Wilson6b383a72010-09-13 13:54:26 +01006333 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006334 base = intel_crtc->cursor_addr;
6335 if (x > (int) crtc->fb->width)
6336 base = 0;
6337
6338 if (y > (int) crtc->fb->height)
6339 base = 0;
6340 } else
6341 base = 0;
6342
6343 if (x < 0) {
6344 if (x + intel_crtc->cursor_width < 0)
6345 base = 0;
6346
6347 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6348 x = -x;
6349 }
6350 pos |= x << CURSOR_X_SHIFT;
6351
6352 if (y < 0) {
6353 if (y + intel_crtc->cursor_height < 0)
6354 base = 0;
6355
6356 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6357 y = -y;
6358 }
6359 pos |= y << CURSOR_Y_SHIFT;
6360
6361 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006362 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006363 return;
6364
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006365 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006366 I915_WRITE(CURPOS_IVB(pipe), pos);
6367 ivb_update_cursor(crtc, base);
6368 } else {
6369 I915_WRITE(CURPOS(pipe), pos);
6370 if (IS_845G(dev) || IS_I865G(dev))
6371 i845_update_cursor(crtc, base);
6372 else
6373 i9xx_update_cursor(crtc, base);
6374 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006375}
6376
Jesse Barnes79e53942008-11-07 14:24:08 -08006377static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006378 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006379 uint32_t handle,
6380 uint32_t width, uint32_t height)
6381{
6382 struct drm_device *dev = crtc->dev;
6383 struct drm_i915_private *dev_priv = dev->dev_private;
6384 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006385 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006386 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006387 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006388
Jesse Barnes79e53942008-11-07 14:24:08 -08006389 /* if we want to turn off the cursor ignore width and height */
6390 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006391 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006392 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006393 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006394 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006395 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006396 }
6397
6398 /* Currently we only support 64x64 cursors */
6399 if (width != 64 || height != 64) {
6400 DRM_ERROR("we currently only support 64x64 cursors\n");
6401 return -EINVAL;
6402 }
6403
Chris Wilson05394f32010-11-08 19:18:58 +00006404 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006405 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006406 return -ENOENT;
6407
Chris Wilson05394f32010-11-08 19:18:58 +00006408 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006409 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006410 ret = -ENOMEM;
6411 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006412 }
6413
Dave Airlie71acb5e2008-12-30 20:31:46 +10006414 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006415 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006416 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00006417 unsigned alignment;
6418
Chris Wilsond9e86c02010-11-10 16:40:20 +00006419 if (obj->tiling_mode) {
6420 DRM_ERROR("cursor cannot be tiled\n");
6421 ret = -EINVAL;
6422 goto fail_locked;
6423 }
6424
Chris Wilson693db182013-03-05 14:52:39 +00006425 /* Note that the w/a also requires 2 PTE of padding following
6426 * the bo. We currently fill all unused PTE with the shadow
6427 * page and so we should always have valid PTE following the
6428 * cursor preventing the VT-d warning.
6429 */
6430 alignment = 0;
6431 if (need_vtd_wa(dev))
6432 alignment = 64*1024;
6433
6434 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006435 if (ret) {
6436 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006437 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006438 }
6439
Chris Wilsond9e86c02010-11-10 16:40:20 +00006440 ret = i915_gem_object_put_fence(obj);
6441 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006442 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006443 goto fail_unpin;
6444 }
6445
Chris Wilson05394f32010-11-08 19:18:58 +00006446 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006447 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006448 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006449 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006450 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6451 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006452 if (ret) {
6453 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006454 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006455 }
Chris Wilson05394f32010-11-08 19:18:58 +00006456 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006457 }
6458
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006459 if (IS_GEN2(dev))
Jesse Barnes14b603912009-05-20 16:47:08 -04006460 I915_WRITE(CURSIZE, (height << 12) | width);
6461
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006462 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006463 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006464 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006465 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006466 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6467 } else
6468 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006469 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006470 }
Jesse Barnes80824002009-09-10 15:28:06 -07006471
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006472 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006473
6474 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006475 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006476 intel_crtc->cursor_width = width;
6477 intel_crtc->cursor_height = height;
6478
Chris Wilson6b383a72010-09-13 13:54:26 +01006479 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006480
Jesse Barnes79e53942008-11-07 14:24:08 -08006481 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006482fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006483 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006484fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006485 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006486fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006487 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006488 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006489}
6490
6491static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6492{
Jesse Barnes79e53942008-11-07 14:24:08 -08006493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006494
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006495 intel_crtc->cursor_x = x;
6496 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006497
Chris Wilson6b383a72010-09-13 13:54:26 +01006498 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006499
6500 return 0;
6501}
6502
6503/** Sets the color ramps on behalf of RandR */
6504void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6505 u16 blue, int regno)
6506{
6507 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6508
6509 intel_crtc->lut_r[regno] = red >> 8;
6510 intel_crtc->lut_g[regno] = green >> 8;
6511 intel_crtc->lut_b[regno] = blue >> 8;
6512}
6513
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006514void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6515 u16 *blue, int regno)
6516{
6517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6518
6519 *red = intel_crtc->lut_r[regno] << 8;
6520 *green = intel_crtc->lut_g[regno] << 8;
6521 *blue = intel_crtc->lut_b[regno] << 8;
6522}
6523
Jesse Barnes79e53942008-11-07 14:24:08 -08006524static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006525 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006526{
James Simmons72034252010-08-03 01:33:19 +01006527 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006529
James Simmons72034252010-08-03 01:33:19 +01006530 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006531 intel_crtc->lut_r[i] = red[i] >> 8;
6532 intel_crtc->lut_g[i] = green[i] >> 8;
6533 intel_crtc->lut_b[i] = blue[i] >> 8;
6534 }
6535
6536 intel_crtc_load_lut(crtc);
6537}
6538
Jesse Barnes79e53942008-11-07 14:24:08 -08006539/* VESA 640x480x72Hz mode to set on the pipe */
6540static struct drm_display_mode load_detect_mode = {
6541 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6542 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6543};
6544
Chris Wilsond2dff872011-04-19 08:36:26 +01006545static struct drm_framebuffer *
6546intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006547 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006548 struct drm_i915_gem_object *obj)
6549{
6550 struct intel_framebuffer *intel_fb;
6551 int ret;
6552
6553 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6554 if (!intel_fb) {
6555 drm_gem_object_unreference_unlocked(&obj->base);
6556 return ERR_PTR(-ENOMEM);
6557 }
6558
6559 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6560 if (ret) {
6561 drm_gem_object_unreference_unlocked(&obj->base);
6562 kfree(intel_fb);
6563 return ERR_PTR(ret);
6564 }
6565
6566 return &intel_fb->base;
6567}
6568
6569static u32
6570intel_framebuffer_pitch_for_width(int width, int bpp)
6571{
6572 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6573 return ALIGN(pitch, 64);
6574}
6575
6576static u32
6577intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6578{
6579 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6580 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6581}
6582
6583static struct drm_framebuffer *
6584intel_framebuffer_create_for_mode(struct drm_device *dev,
6585 struct drm_display_mode *mode,
6586 int depth, int bpp)
6587{
6588 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006589 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006590
6591 obj = i915_gem_alloc_object(dev,
6592 intel_framebuffer_size_for_mode(mode, bpp));
6593 if (obj == NULL)
6594 return ERR_PTR(-ENOMEM);
6595
6596 mode_cmd.width = mode->hdisplay;
6597 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006598 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6599 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006600 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006601
6602 return intel_framebuffer_create(dev, &mode_cmd, obj);
6603}
6604
6605static struct drm_framebuffer *
6606mode_fits_in_fbdev(struct drm_device *dev,
6607 struct drm_display_mode *mode)
6608{
6609 struct drm_i915_private *dev_priv = dev->dev_private;
6610 struct drm_i915_gem_object *obj;
6611 struct drm_framebuffer *fb;
6612
6613 if (dev_priv->fbdev == NULL)
6614 return NULL;
6615
6616 obj = dev_priv->fbdev->ifb.obj;
6617 if (obj == NULL)
6618 return NULL;
6619
6620 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006621 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6622 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006623 return NULL;
6624
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006625 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006626 return NULL;
6627
6628 return fb;
6629}
6630
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006631bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006632 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006633 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006634{
6635 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006636 struct intel_encoder *intel_encoder =
6637 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006638 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006639 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006640 struct drm_crtc *crtc = NULL;
6641 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006642 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006643 int i = -1;
6644
Chris Wilsond2dff872011-04-19 08:36:26 +01006645 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6646 connector->base.id, drm_get_connector_name(connector),
6647 encoder->base.id, drm_get_encoder_name(encoder));
6648
Jesse Barnes79e53942008-11-07 14:24:08 -08006649 /*
6650 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006651 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006652 * - if the connector already has an assigned crtc, use it (but make
6653 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006654 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006655 * - try to find the first unused crtc that can drive this connector,
6656 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006657 */
6658
6659 /* See if we already have a CRTC for this connector */
6660 if (encoder->crtc) {
6661 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006662
Daniel Vetter7b240562012-12-12 00:35:33 +01006663 mutex_lock(&crtc->mutex);
6664
Daniel Vetter24218aa2012-08-12 19:27:11 +02006665 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006666 old->load_detect_temp = false;
6667
6668 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006669 if (connector->dpms != DRM_MODE_DPMS_ON)
6670 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006671
Chris Wilson71731882011-04-19 23:10:58 +01006672 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006673 }
6674
6675 /* Find an unused one (if possible) */
6676 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6677 i++;
6678 if (!(encoder->possible_crtcs & (1 << i)))
6679 continue;
6680 if (!possible_crtc->enabled) {
6681 crtc = possible_crtc;
6682 break;
6683 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006684 }
6685
6686 /*
6687 * If we didn't find an unused CRTC, don't use any.
6688 */
6689 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006690 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6691 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006692 }
6693
Daniel Vetter7b240562012-12-12 00:35:33 +01006694 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02006695 intel_encoder->new_crtc = to_intel_crtc(crtc);
6696 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006697
6698 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006699 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006700 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006701 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006702
Chris Wilson64927112011-04-20 07:25:26 +01006703 if (!mode)
6704 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006705
Chris Wilsond2dff872011-04-19 08:36:26 +01006706 /* We need a framebuffer large enough to accommodate all accesses
6707 * that the plane may generate whilst we perform load detection.
6708 * We can not rely on the fbcon either being present (we get called
6709 * during its initialisation to detect all boot displays, or it may
6710 * not even exist) or that it is large enough to satisfy the
6711 * requested mode.
6712 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006713 fb = mode_fits_in_fbdev(dev, mode);
6714 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006715 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006716 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6717 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006718 } else
6719 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006720 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006721 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01006722 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006723 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006724 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006725
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006726 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006727 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006728 if (old->release_fb)
6729 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01006730 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006731 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006732 }
Chris Wilson71731882011-04-19 23:10:58 +01006733
Jesse Barnes79e53942008-11-07 14:24:08 -08006734 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006735 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006736 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006737}
6738
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006739void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006740 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006741{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006742 struct intel_encoder *intel_encoder =
6743 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006744 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01006745 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08006746
Chris Wilsond2dff872011-04-19 08:36:26 +01006747 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6748 connector->base.id, drm_get_connector_name(connector),
6749 encoder->base.id, drm_get_encoder_name(encoder));
6750
Chris Wilson8261b192011-04-19 23:18:09 +01006751 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006752 to_intel_connector(connector)->new_encoder = NULL;
6753 intel_encoder->new_crtc = NULL;
6754 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006755
Daniel Vetter36206362012-12-10 20:42:17 +01006756 if (old->release_fb) {
6757 drm_framebuffer_unregister_private(old->release_fb);
6758 drm_framebuffer_unreference(old->release_fb);
6759 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006760
Daniel Vetter67c96402013-01-23 16:25:09 +00006761 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01006762 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006763 }
6764
Eric Anholtc751ce42010-03-25 11:48:48 -07006765 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006766 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6767 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01006768
6769 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08006770}
6771
6772/* Returns the clock of the currently programmed mode of the given pipe. */
6773static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6774{
6775 struct drm_i915_private *dev_priv = dev->dev_private;
6776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6777 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006778 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006779 u32 fp;
6780 intel_clock_t clock;
6781
6782 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006783 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006784 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006785 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006786
6787 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006788 if (IS_PINEVIEW(dev)) {
6789 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6790 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006791 } else {
6792 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6793 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6794 }
6795
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006796 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006797 if (IS_PINEVIEW(dev))
6798 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6799 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006800 else
6801 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006802 DPLL_FPA01_P1_POST_DIV_SHIFT);
6803
6804 switch (dpll & DPLL_MODE_MASK) {
6805 case DPLLB_MODE_DAC_SERIAL:
6806 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6807 5 : 10;
6808 break;
6809 case DPLLB_MODE_LVDS:
6810 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6811 7 : 14;
6812 break;
6813 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006814 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006815 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6816 return 0;
6817 }
6818
6819 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08006820 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006821 } else {
6822 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6823
6824 if (is_lvds) {
6825 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6826 DPLL_FPA01_P1_POST_DIV_SHIFT);
6827 clock.p2 = 14;
6828
6829 if ((dpll & PLL_REF_INPUT_MASK) ==
6830 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6831 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08006832 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006833 } else
Shaohua Li21778322009-02-23 15:19:16 +08006834 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006835 } else {
6836 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6837 clock.p1 = 2;
6838 else {
6839 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6840 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6841 }
6842 if (dpll & PLL_P2_DIVIDE_BY_4)
6843 clock.p2 = 4;
6844 else
6845 clock.p2 = 2;
6846
Shaohua Li21778322009-02-23 15:19:16 +08006847 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006848 }
6849 }
6850
6851 /* XXX: It would be nice to validate the clocks, but we can't reuse
6852 * i830PllIsValid() because it relies on the xf86_config connector
6853 * configuration being accurate, which it isn't necessarily.
6854 */
6855
6856 return clock.dot;
6857}
6858
6859/** Returns the currently programmed mode of the given pipe. */
6860struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6861 struct drm_crtc *crtc)
6862{
Jesse Barnes548f2452011-02-17 10:40:53 -08006863 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006864 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02006865 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006866 struct drm_display_mode *mode;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006867 int htot = I915_READ(HTOTAL(cpu_transcoder));
6868 int hsync = I915_READ(HSYNC(cpu_transcoder));
6869 int vtot = I915_READ(VTOTAL(cpu_transcoder));
6870 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08006871
6872 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6873 if (!mode)
6874 return NULL;
6875
6876 mode->clock = intel_crtc_clock_get(dev, crtc);
6877 mode->hdisplay = (htot & 0xffff) + 1;
6878 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6879 mode->hsync_start = (hsync & 0xffff) + 1;
6880 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6881 mode->vdisplay = (vtot & 0xffff) + 1;
6882 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6883 mode->vsync_start = (vsync & 0xffff) + 1;
6884 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6885
6886 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006887
6888 return mode;
6889}
6890
Daniel Vetter3dec0092010-08-20 21:40:52 +02006891static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07006892{
6893 struct drm_device *dev = crtc->dev;
6894 drm_i915_private_t *dev_priv = dev->dev_private;
6895 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6896 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006897 int dpll_reg = DPLL(pipe);
6898 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07006899
Eric Anholtbad720f2009-10-22 16:11:14 -07006900 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006901 return;
6902
6903 if (!dev_priv->lvds_downclock_avail)
6904 return;
6905
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006906 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006907 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08006908 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006909
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006910 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006911
6912 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6913 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006914 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006915
Jesse Barnes652c3932009-08-17 13:31:43 -07006916 dpll = I915_READ(dpll_reg);
6917 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08006918 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006919 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006920}
6921
6922static void intel_decrease_pllclock(struct drm_crtc *crtc)
6923{
6924 struct drm_device *dev = crtc->dev;
6925 drm_i915_private_t *dev_priv = dev->dev_private;
6926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006927
Eric Anholtbad720f2009-10-22 16:11:14 -07006928 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006929 return;
6930
6931 if (!dev_priv->lvds_downclock_avail)
6932 return;
6933
6934 /*
6935 * Since this is called by a timer, we should never get here in
6936 * the manual case.
6937 */
6938 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01006939 int pipe = intel_crtc->pipe;
6940 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02006941 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01006942
Zhao Yakui44d98a62009-10-09 11:39:40 +08006943 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006944
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006945 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006946
Chris Wilson074b5e12012-05-02 12:07:06 +01006947 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006948 dpll |= DISPLAY_RATE_SELECT_FPA1;
6949 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006950 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006951 dpll = I915_READ(dpll_reg);
6952 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08006953 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006954 }
6955
6956}
6957
Chris Wilsonf047e392012-07-21 12:31:41 +01006958void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006959{
Chris Wilsonf047e392012-07-21 12:31:41 +01006960 i915_update_gfx_val(dev->dev_private);
6961}
6962
6963void intel_mark_idle(struct drm_device *dev)
6964{
Chris Wilson725a5b52013-01-08 11:02:57 +00006965 struct drm_crtc *crtc;
6966
6967 if (!i915_powersave)
6968 return;
6969
6970 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6971 if (!crtc->fb)
6972 continue;
6973
6974 intel_decrease_pllclock(crtc);
6975 }
Chris Wilsonf047e392012-07-21 12:31:41 +01006976}
6977
6978void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
6979{
6980 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07006981 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006982
6983 if (!i915_powersave)
6984 return;
6985
Jesse Barnes652c3932009-08-17 13:31:43 -07006986 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006987 if (!crtc->fb)
6988 continue;
6989
Chris Wilsonf047e392012-07-21 12:31:41 +01006990 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6991 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006992 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006993}
6994
Jesse Barnes79e53942008-11-07 14:24:08 -08006995static void intel_crtc_destroy(struct drm_crtc *crtc)
6996{
6997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006998 struct drm_device *dev = crtc->dev;
6999 struct intel_unpin_work *work;
7000 unsigned long flags;
7001
7002 spin_lock_irqsave(&dev->event_lock, flags);
7003 work = intel_crtc->unpin_work;
7004 intel_crtc->unpin_work = NULL;
7005 spin_unlock_irqrestore(&dev->event_lock, flags);
7006
7007 if (work) {
7008 cancel_work_sync(&work->work);
7009 kfree(work);
7010 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007011
7012 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007013
Jesse Barnes79e53942008-11-07 14:24:08 -08007014 kfree(intel_crtc);
7015}
7016
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007017static void intel_unpin_work_fn(struct work_struct *__work)
7018{
7019 struct intel_unpin_work *work =
7020 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007021 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007022
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007023 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007024 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007025 drm_gem_object_unreference(&work->pending_flip_obj->base);
7026 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007027
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007028 intel_update_fbc(dev);
7029 mutex_unlock(&dev->struct_mutex);
7030
7031 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
7032 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
7033
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007034 kfree(work);
7035}
7036
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007037static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007038 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007039{
7040 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7042 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007043 unsigned long flags;
7044
7045 /* Ignore early vblank irqs */
7046 if (intel_crtc == NULL)
7047 return;
7048
7049 spin_lock_irqsave(&dev->event_lock, flags);
7050 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00007051
7052 /* Ensure we don't miss a work->pending update ... */
7053 smp_rmb();
7054
7055 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007056 spin_unlock_irqrestore(&dev->event_lock, flags);
7057 return;
7058 }
7059
Chris Wilsone7d841c2012-12-03 11:36:30 +00007060 /* and that the unpin work is consistent wrt ->pending. */
7061 smp_rmb();
7062
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007063 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007064
Rob Clark45a066e2012-10-08 14:50:40 -05007065 if (work->event)
7066 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007067
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007068 drm_vblank_put(dev, intel_crtc->pipe);
7069
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007070 spin_unlock_irqrestore(&dev->event_lock, flags);
7071
Daniel Vetter2c10d572012-12-20 21:24:07 +01007072 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007073
7074 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007075
7076 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007077}
7078
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007079void intel_finish_page_flip(struct drm_device *dev, int pipe)
7080{
7081 drm_i915_private_t *dev_priv = dev->dev_private;
7082 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7083
Mario Kleiner49b14a52010-12-09 07:00:07 +01007084 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007085}
7086
7087void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7088{
7089 drm_i915_private_t *dev_priv = dev->dev_private;
7090 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7091
Mario Kleiner49b14a52010-12-09 07:00:07 +01007092 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007093}
7094
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007095void intel_prepare_page_flip(struct drm_device *dev, int plane)
7096{
7097 drm_i915_private_t *dev_priv = dev->dev_private;
7098 struct intel_crtc *intel_crtc =
7099 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7100 unsigned long flags;
7101
Chris Wilsone7d841c2012-12-03 11:36:30 +00007102 /* NB: An MMIO update of the plane base pointer will also
7103 * generate a page-flip completion irq, i.e. every modeset
7104 * is also accompanied by a spurious intel_prepare_page_flip().
7105 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007106 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007107 if (intel_crtc->unpin_work)
7108 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007109 spin_unlock_irqrestore(&dev->event_lock, flags);
7110}
7111
Chris Wilsone7d841c2012-12-03 11:36:30 +00007112inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7113{
7114 /* Ensure that the work item is consistent when activating it ... */
7115 smp_wmb();
7116 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7117 /* and that it is marked active as soon as the irq could fire. */
7118 smp_wmb();
7119}
7120
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007121static int intel_gen2_queue_flip(struct drm_device *dev,
7122 struct drm_crtc *crtc,
7123 struct drm_framebuffer *fb,
7124 struct drm_i915_gem_object *obj)
7125{
7126 struct drm_i915_private *dev_priv = dev->dev_private;
7127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007128 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007129 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007130 int ret;
7131
Daniel Vetter6d90c952012-04-26 23:28:05 +02007132 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007133 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007134 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007135
Daniel Vetter6d90c952012-04-26 23:28:05 +02007136 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007137 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007138 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007139
7140 /* Can't queue multiple flips, so wait for the previous
7141 * one to finish before executing the next.
7142 */
7143 if (intel_crtc->plane)
7144 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7145 else
7146 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007147 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7148 intel_ring_emit(ring, MI_NOOP);
7149 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7150 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7151 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007152 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007153 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007154
7155 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007156 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007157 return 0;
7158
7159err_unpin:
7160 intel_unpin_fb_obj(obj);
7161err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007162 return ret;
7163}
7164
7165static int intel_gen3_queue_flip(struct drm_device *dev,
7166 struct drm_crtc *crtc,
7167 struct drm_framebuffer *fb,
7168 struct drm_i915_gem_object *obj)
7169{
7170 struct drm_i915_private *dev_priv = dev->dev_private;
7171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007172 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007173 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007174 int ret;
7175
Daniel Vetter6d90c952012-04-26 23:28:05 +02007176 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007177 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007178 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007179
Daniel Vetter6d90c952012-04-26 23:28:05 +02007180 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007181 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007182 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007183
7184 if (intel_crtc->plane)
7185 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7186 else
7187 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007188 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7189 intel_ring_emit(ring, MI_NOOP);
7190 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7191 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7192 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007193 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007194 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007195
Chris Wilsone7d841c2012-12-03 11:36:30 +00007196 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007197 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007198 return 0;
7199
7200err_unpin:
7201 intel_unpin_fb_obj(obj);
7202err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007203 return ret;
7204}
7205
7206static int intel_gen4_queue_flip(struct drm_device *dev,
7207 struct drm_crtc *crtc,
7208 struct drm_framebuffer *fb,
7209 struct drm_i915_gem_object *obj)
7210{
7211 struct drm_i915_private *dev_priv = dev->dev_private;
7212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7213 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007214 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007215 int ret;
7216
Daniel Vetter6d90c952012-04-26 23:28:05 +02007217 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007218 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007219 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007220
Daniel Vetter6d90c952012-04-26 23:28:05 +02007221 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007222 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007223 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007224
7225 /* i965+ uses the linear or tiled offsets from the
7226 * Display Registers (which do not change across a page-flip)
7227 * so we need only reprogram the base address.
7228 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007229 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7230 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7231 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007232 intel_ring_emit(ring,
7233 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
7234 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007235
7236 /* XXX Enabling the panel-fitter across page-flip is so far
7237 * untested on non-native modes, so ignore it for now.
7238 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7239 */
7240 pf = 0;
7241 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007242 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007243
7244 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007245 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007246 return 0;
7247
7248err_unpin:
7249 intel_unpin_fb_obj(obj);
7250err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007251 return ret;
7252}
7253
7254static int intel_gen6_queue_flip(struct drm_device *dev,
7255 struct drm_crtc *crtc,
7256 struct drm_framebuffer *fb,
7257 struct drm_i915_gem_object *obj)
7258{
7259 struct drm_i915_private *dev_priv = dev->dev_private;
7260 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007261 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007262 uint32_t pf, pipesrc;
7263 int ret;
7264
Daniel Vetter6d90c952012-04-26 23:28:05 +02007265 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007266 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007267 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007268
Daniel Vetter6d90c952012-04-26 23:28:05 +02007269 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007270 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007271 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007272
Daniel Vetter6d90c952012-04-26 23:28:05 +02007273 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7274 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7275 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007276 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007277
Chris Wilson99d9acd2012-04-17 20:37:00 +01007278 /* Contrary to the suggestions in the documentation,
7279 * "Enable Panel Fitter" does not seem to be required when page
7280 * flipping with a non-native mode, and worse causes a normal
7281 * modeset to fail.
7282 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7283 */
7284 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007285 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007286 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007287
7288 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007289 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007290 return 0;
7291
7292err_unpin:
7293 intel_unpin_fb_obj(obj);
7294err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007295 return ret;
7296}
7297
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007298/*
7299 * On gen7 we currently use the blit ring because (in early silicon at least)
7300 * the render ring doesn't give us interrpts for page flip completion, which
7301 * means clients will hang after the first flip is queued. Fortunately the
7302 * blit ring generates interrupts properly, so use it instead.
7303 */
7304static int intel_gen7_queue_flip(struct drm_device *dev,
7305 struct drm_crtc *crtc,
7306 struct drm_framebuffer *fb,
7307 struct drm_i915_gem_object *obj)
7308{
7309 struct drm_i915_private *dev_priv = dev->dev_private;
7310 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7311 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007312 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007313 int ret;
7314
7315 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7316 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007317 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007318
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007319 switch(intel_crtc->plane) {
7320 case PLANE_A:
7321 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7322 break;
7323 case PLANE_B:
7324 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7325 break;
7326 case PLANE_C:
7327 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7328 break;
7329 default:
7330 WARN_ONCE(1, "unknown plane in flip command\n");
7331 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007332 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007333 }
7334
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007335 ret = intel_ring_begin(ring, 4);
7336 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007337 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007338
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007339 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007340 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Daniel Vetterc2c75132012-07-05 12:17:30 +02007341 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007342 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007343
7344 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007345 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007346 return 0;
7347
7348err_unpin:
7349 intel_unpin_fb_obj(obj);
7350err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007351 return ret;
7352}
7353
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007354static int intel_default_queue_flip(struct drm_device *dev,
7355 struct drm_crtc *crtc,
7356 struct drm_framebuffer *fb,
7357 struct drm_i915_gem_object *obj)
7358{
7359 return -ENODEV;
7360}
7361
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007362static int intel_crtc_page_flip(struct drm_crtc *crtc,
7363 struct drm_framebuffer *fb,
7364 struct drm_pending_vblank_event *event)
7365{
7366 struct drm_device *dev = crtc->dev;
7367 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007368 struct drm_framebuffer *old_fb = crtc->fb;
7369 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007370 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7371 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007372 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007373 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007374
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007375 /* Can't change pixel format via MI display flips. */
7376 if (fb->pixel_format != crtc->fb->pixel_format)
7377 return -EINVAL;
7378
7379 /*
7380 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7381 * Note that pitch changes could also affect these register.
7382 */
7383 if (INTEL_INFO(dev)->gen > 3 &&
7384 (fb->offsets[0] != crtc->fb->offsets[0] ||
7385 fb->pitches[0] != crtc->fb->pitches[0]))
7386 return -EINVAL;
7387
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007388 work = kzalloc(sizeof *work, GFP_KERNEL);
7389 if (work == NULL)
7390 return -ENOMEM;
7391
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007392 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007393 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007394 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007395 INIT_WORK(&work->work, intel_unpin_work_fn);
7396
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007397 ret = drm_vblank_get(dev, intel_crtc->pipe);
7398 if (ret)
7399 goto free_work;
7400
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007401 /* We borrow the event spin lock for protecting unpin_work */
7402 spin_lock_irqsave(&dev->event_lock, flags);
7403 if (intel_crtc->unpin_work) {
7404 spin_unlock_irqrestore(&dev->event_lock, flags);
7405 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007406 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007407
7408 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007409 return -EBUSY;
7410 }
7411 intel_crtc->unpin_work = work;
7412 spin_unlock_irqrestore(&dev->event_lock, flags);
7413
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007414 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7415 flush_workqueue(dev_priv->wq);
7416
Chris Wilson79158102012-05-23 11:13:58 +01007417 ret = i915_mutex_lock_interruptible(dev);
7418 if (ret)
7419 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007420
Jesse Barnes75dfca82010-02-10 15:09:44 -08007421 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007422 drm_gem_object_reference(&work->old_fb_obj->base);
7423 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007424
7425 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007426
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007427 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007428
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007429 work->enable_stall_check = true;
7430
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007431 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02007432 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007433
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007434 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7435 if (ret)
7436 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007437
Chris Wilson7782de32011-07-08 12:22:41 +01007438 intel_disable_fbc(dev);
Chris Wilsonf047e392012-07-21 12:31:41 +01007439 intel_mark_fb_busy(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007440 mutex_unlock(&dev->struct_mutex);
7441
Jesse Barnese5510fa2010-07-01 16:48:37 -07007442 trace_i915_flip_request(intel_crtc->plane, obj);
7443
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007444 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007445
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007446cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007447 atomic_dec(&intel_crtc->unpin_work_count);
Ville Syrjälä4a35f832013-02-22 16:53:38 +02007448 crtc->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007449 drm_gem_object_unreference(&work->old_fb_obj->base);
7450 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007451 mutex_unlock(&dev->struct_mutex);
7452
Chris Wilson79158102012-05-23 11:13:58 +01007453cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007454 spin_lock_irqsave(&dev->event_lock, flags);
7455 intel_crtc->unpin_work = NULL;
7456 spin_unlock_irqrestore(&dev->event_lock, flags);
7457
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007458 drm_vblank_put(dev, intel_crtc->pipe);
7459free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007460 kfree(work);
7461
7462 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007463}
7464
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007465static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007466 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7467 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007468};
7469
Daniel Vetter6ed0f792012-07-08 19:41:43 +02007470bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
7471{
7472 struct intel_encoder *other_encoder;
7473 struct drm_crtc *crtc = &encoder->new_crtc->base;
7474
7475 if (WARN_ON(!crtc))
7476 return false;
7477
7478 list_for_each_entry(other_encoder,
7479 &crtc->dev->mode_config.encoder_list,
7480 base.head) {
7481
7482 if (&other_encoder->new_crtc->base != crtc ||
7483 encoder == other_encoder)
7484 continue;
7485 else
7486 return true;
7487 }
7488
7489 return false;
7490}
7491
Daniel Vetter50f56112012-07-02 09:35:43 +02007492static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7493 struct drm_crtc *crtc)
7494{
7495 struct drm_device *dev;
7496 struct drm_crtc *tmp;
7497 int crtc_mask = 1;
7498
7499 WARN(!crtc, "checking null crtc?\n");
7500
7501 dev = crtc->dev;
7502
7503 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7504 if (tmp == crtc)
7505 break;
7506 crtc_mask <<= 1;
7507 }
7508
7509 if (encoder->possible_crtcs & crtc_mask)
7510 return true;
7511 return false;
7512}
7513
Daniel Vetter9a935852012-07-05 22:34:27 +02007514/**
7515 * intel_modeset_update_staged_output_state
7516 *
7517 * Updates the staged output configuration state, e.g. after we've read out the
7518 * current hw state.
7519 */
7520static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7521{
7522 struct intel_encoder *encoder;
7523 struct intel_connector *connector;
7524
7525 list_for_each_entry(connector, &dev->mode_config.connector_list,
7526 base.head) {
7527 connector->new_encoder =
7528 to_intel_encoder(connector->base.encoder);
7529 }
7530
7531 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7532 base.head) {
7533 encoder->new_crtc =
7534 to_intel_crtc(encoder->base.crtc);
7535 }
7536}
7537
7538/**
7539 * intel_modeset_commit_output_state
7540 *
7541 * This function copies the stage display pipe configuration to the real one.
7542 */
7543static void intel_modeset_commit_output_state(struct drm_device *dev)
7544{
7545 struct intel_encoder *encoder;
7546 struct intel_connector *connector;
7547
7548 list_for_each_entry(connector, &dev->mode_config.connector_list,
7549 base.head) {
7550 connector->base.encoder = &connector->new_encoder->base;
7551 }
7552
7553 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7554 base.head) {
7555 encoder->base.crtc = &encoder->new_crtc->base;
7556 }
7557}
7558
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007559static int
7560pipe_config_set_bpp(struct drm_crtc *crtc,
7561 struct drm_framebuffer *fb,
7562 struct intel_crtc_config *pipe_config)
7563{
7564 struct drm_device *dev = crtc->dev;
7565 struct drm_connector *connector;
7566 int bpp;
7567
Daniel Vetterd42264b2013-03-28 16:38:08 +01007568 switch (fb->pixel_format) {
7569 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007570 bpp = 8*3; /* since we go through a colormap */
7571 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007572 case DRM_FORMAT_XRGB1555:
7573 case DRM_FORMAT_ARGB1555:
7574 /* checked in intel_framebuffer_init already */
7575 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
7576 return -EINVAL;
7577 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007578 bpp = 6*3; /* min is 18bpp */
7579 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007580 case DRM_FORMAT_XBGR8888:
7581 case DRM_FORMAT_ABGR8888:
7582 /* checked in intel_framebuffer_init already */
7583 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
7584 return -EINVAL;
7585 case DRM_FORMAT_XRGB8888:
7586 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007587 bpp = 8*3;
7588 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01007589 case DRM_FORMAT_XRGB2101010:
7590 case DRM_FORMAT_ARGB2101010:
7591 case DRM_FORMAT_XBGR2101010:
7592 case DRM_FORMAT_ABGR2101010:
7593 /* checked in intel_framebuffer_init already */
7594 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01007595 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007596 bpp = 10*3;
7597 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01007598 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007599 default:
7600 DRM_DEBUG_KMS("unsupported depth\n");
7601 return -EINVAL;
7602 }
7603
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007604 pipe_config->pipe_bpp = bpp;
7605
7606 /* Clamp display bpp to EDID value */
7607 list_for_each_entry(connector, &dev->mode_config.connector_list,
7608 head) {
7609 if (connector->encoder && connector->encoder->crtc != crtc)
7610 continue;
7611
7612 /* Don't use an invalid EDID bpc value */
7613 if (connector->display_info.bpc &&
7614 connector->display_info.bpc * 3 < bpp) {
7615 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
7616 bpp, connector->display_info.bpc*3);
7617 pipe_config->pipe_bpp = connector->display_info.bpc*3;
7618 }
Daniel Vetter996a22392013-04-19 11:24:34 +02007619
7620 /* Clamp bpp to 8 on screens without EDID 1.4 */
7621 if (connector->display_info.bpc == 0 && bpp > 24) {
7622 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
7623 bpp);
7624 pipe_config->pipe_bpp = 24;
7625 }
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007626 }
7627
7628 return bpp;
7629}
7630
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007631static struct intel_crtc_config *
7632intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007633 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007634 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02007635{
7636 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02007637 struct drm_encoder_helper_funcs *encoder_funcs;
7638 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007639 struct intel_crtc_config *pipe_config;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007640 int plane_bpp;
Daniel Vetter7758a112012-07-08 19:40:39 +02007641
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007642 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
7643 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02007644 return ERR_PTR(-ENOMEM);
7645
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007646 drm_mode_copy(&pipe_config->adjusted_mode, mode);
7647 drm_mode_copy(&pipe_config->requested_mode, mode);
7648
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007649 plane_bpp = pipe_config_set_bpp(crtc, fb, pipe_config);
7650 if (plane_bpp < 0)
7651 goto fail;
7652
Daniel Vetter7758a112012-07-08 19:40:39 +02007653 /* Pass our mode to the connectors and the CRTC to give them a chance to
7654 * adjust it according to limitations or connector properties, and also
7655 * a chance to reject the mode entirely.
7656 */
7657 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7658 base.head) {
7659
7660 if (&encoder->new_crtc->base != crtc)
7661 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01007662
7663 if (encoder->compute_config) {
7664 if (!(encoder->compute_config(encoder, pipe_config))) {
7665 DRM_DEBUG_KMS("Encoder config failure\n");
7666 goto fail;
7667 }
7668
7669 continue;
7670 }
7671
Daniel Vetter7758a112012-07-08 19:40:39 +02007672 encoder_funcs = encoder->base.helper_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007673 if (!(encoder_funcs->mode_fixup(&encoder->base,
7674 &pipe_config->requested_mode,
7675 &pipe_config->adjusted_mode))) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007676 DRM_DEBUG_KMS("Encoder fixup failed\n");
7677 goto fail;
7678 }
7679 }
7680
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007681 if (!(intel_crtc_compute_config(crtc, pipe_config))) {
Daniel Vetter7758a112012-07-08 19:40:39 +02007682 DRM_DEBUG_KMS("CRTC fixup failed\n");
7683 goto fail;
7684 }
7685 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
7686
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01007687 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
7688 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
7689 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7690
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007691 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02007692fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007693 kfree(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +02007694 return ERR_PTR(-EINVAL);
7695}
7696
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007697/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7698 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7699static void
7700intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7701 unsigned *prepare_pipes, unsigned *disable_pipes)
7702{
7703 struct intel_crtc *intel_crtc;
7704 struct drm_device *dev = crtc->dev;
7705 struct intel_encoder *encoder;
7706 struct intel_connector *connector;
7707 struct drm_crtc *tmp_crtc;
7708
7709 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7710
7711 /* Check which crtcs have changed outputs connected to them, these need
7712 * to be part of the prepare_pipes mask. We don't (yet) support global
7713 * modeset across multiple crtcs, so modeset_pipes will only have one
7714 * bit set at most. */
7715 list_for_each_entry(connector, &dev->mode_config.connector_list,
7716 base.head) {
7717 if (connector->base.encoder == &connector->new_encoder->base)
7718 continue;
7719
7720 if (connector->base.encoder) {
7721 tmp_crtc = connector->base.encoder->crtc;
7722
7723 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7724 }
7725
7726 if (connector->new_encoder)
7727 *prepare_pipes |=
7728 1 << connector->new_encoder->new_crtc->pipe;
7729 }
7730
7731 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7732 base.head) {
7733 if (encoder->base.crtc == &encoder->new_crtc->base)
7734 continue;
7735
7736 if (encoder->base.crtc) {
7737 tmp_crtc = encoder->base.crtc;
7738
7739 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7740 }
7741
7742 if (encoder->new_crtc)
7743 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7744 }
7745
7746 /* Check for any pipes that will be fully disabled ... */
7747 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7748 base.head) {
7749 bool used = false;
7750
7751 /* Don't try to disable disabled crtcs. */
7752 if (!intel_crtc->base.enabled)
7753 continue;
7754
7755 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7756 base.head) {
7757 if (encoder->new_crtc == intel_crtc)
7758 used = true;
7759 }
7760
7761 if (!used)
7762 *disable_pipes |= 1 << intel_crtc->pipe;
7763 }
7764
7765
7766 /* set_mode is also used to update properties on life display pipes. */
7767 intel_crtc = to_intel_crtc(crtc);
7768 if (crtc->enabled)
7769 *prepare_pipes |= 1 << intel_crtc->pipe;
7770
Daniel Vetterb6c51642013-04-12 18:48:43 +02007771 /*
7772 * For simplicity do a full modeset on any pipe where the output routing
7773 * changed. We could be more clever, but that would require us to be
7774 * more careful with calling the relevant encoder->mode_set functions.
7775 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007776 if (*prepare_pipes)
7777 *modeset_pipes = *prepare_pipes;
7778
7779 /* ... and mask these out. */
7780 *modeset_pipes &= ~(*disable_pipes);
7781 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02007782
7783 /*
7784 * HACK: We don't (yet) fully support global modesets. intel_set_config
7785 * obies this rule, but the modeset restore mode of
7786 * intel_modeset_setup_hw_state does not.
7787 */
7788 *modeset_pipes &= 1 << intel_crtc->pipe;
7789 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02007790
7791 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7792 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007793}
7794
Daniel Vetterea9d7582012-07-10 10:42:52 +02007795static bool intel_crtc_in_use(struct drm_crtc *crtc)
7796{
7797 struct drm_encoder *encoder;
7798 struct drm_device *dev = crtc->dev;
7799
7800 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
7801 if (encoder->crtc == crtc)
7802 return true;
7803
7804 return false;
7805}
7806
7807static void
7808intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
7809{
7810 struct intel_encoder *intel_encoder;
7811 struct intel_crtc *intel_crtc;
7812 struct drm_connector *connector;
7813
7814 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
7815 base.head) {
7816 if (!intel_encoder->base.crtc)
7817 continue;
7818
7819 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
7820
7821 if (prepare_pipes & (1 << intel_crtc->pipe))
7822 intel_encoder->connectors_active = false;
7823 }
7824
7825 intel_modeset_commit_output_state(dev);
7826
7827 /* Update computed state. */
7828 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7829 base.head) {
7830 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
7831 }
7832
7833 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7834 if (!connector->encoder || !connector->encoder->crtc)
7835 continue;
7836
7837 intel_crtc = to_intel_crtc(connector->encoder->crtc);
7838
7839 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02007840 struct drm_property *dpms_property =
7841 dev->mode_config.dpms_property;
7842
Daniel Vetterea9d7582012-07-10 10:42:52 +02007843 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05007844 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02007845 dpms_property,
7846 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02007847
7848 intel_encoder = to_intel_encoder(connector->encoder);
7849 intel_encoder->connectors_active = true;
7850 }
7851 }
7852
7853}
7854
Daniel Vetter25c5b262012-07-08 22:08:04 +02007855#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
7856 list_for_each_entry((intel_crtc), \
7857 &(dev)->mode_config.crtc_list, \
7858 base.head) \
7859 if (mask & (1 <<(intel_crtc)->pipe)) \
7860
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007861static bool
7862intel_pipe_config_compare(struct intel_crtc_config *current_config,
7863 struct intel_crtc_config *pipe_config)
7864{
Daniel Vetter88adfff2013-03-28 10:42:01 +01007865 if (current_config->has_pch_encoder != pipe_config->has_pch_encoder) {
7866 DRM_ERROR("mismatch in has_pch_encoder "
7867 "(expected %i, found %i)\n",
7868 current_config->has_pch_encoder,
7869 pipe_config->has_pch_encoder);
7870 return false;
7871 }
7872
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007873 return true;
7874}
7875
Daniel Vetterb9805142012-08-31 17:37:33 +02007876void
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007877intel_modeset_check_state(struct drm_device *dev)
7878{
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007879 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007880 struct intel_crtc *crtc;
7881 struct intel_encoder *encoder;
7882 struct intel_connector *connector;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007883 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007884
7885 list_for_each_entry(connector, &dev->mode_config.connector_list,
7886 base.head) {
7887 /* This also checks the encoder/connector hw state with the
7888 * ->get_hw_state callbacks. */
7889 intel_connector_check_state(connector);
7890
7891 WARN(&connector->new_encoder->base != connector->base.encoder,
7892 "connector's staged encoder doesn't match current encoder\n");
7893 }
7894
7895 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7896 base.head) {
7897 bool enabled = false;
7898 bool active = false;
7899 enum pipe pipe, tracked_pipe;
7900
7901 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
7902 encoder->base.base.id,
7903 drm_get_encoder_name(&encoder->base));
7904
7905 WARN(&encoder->new_crtc->base != encoder->base.crtc,
7906 "encoder's stage crtc doesn't match current crtc\n");
7907 WARN(encoder->connectors_active && !encoder->base.crtc,
7908 "encoder's active_connectors set, but no crtc\n");
7909
7910 list_for_each_entry(connector, &dev->mode_config.connector_list,
7911 base.head) {
7912 if (connector->base.encoder != &encoder->base)
7913 continue;
7914 enabled = true;
7915 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
7916 active = true;
7917 }
7918 WARN(!!encoder->base.crtc != enabled,
7919 "encoder's enabled state mismatch "
7920 "(expected %i, found %i)\n",
7921 !!encoder->base.crtc, enabled);
7922 WARN(active && !encoder->base.crtc,
7923 "active encoder with no crtc\n");
7924
7925 WARN(encoder->connectors_active != active,
7926 "encoder's computed active state doesn't match tracked active state "
7927 "(expected %i, found %i)\n", active, encoder->connectors_active);
7928
7929 active = encoder->get_hw_state(encoder, &pipe);
7930 WARN(active != encoder->connectors_active,
7931 "encoder's hw state doesn't match sw tracking "
7932 "(expected %i, found %i)\n",
7933 encoder->connectors_active, active);
7934
7935 if (!encoder->base.crtc)
7936 continue;
7937
7938 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
7939 WARN(active && pipe != tracked_pipe,
7940 "active encoder's pipe doesn't match"
7941 "(expected %i, found %i)\n",
7942 tracked_pipe, pipe);
7943
7944 }
7945
7946 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
7947 base.head) {
7948 bool enabled = false;
7949 bool active = false;
7950
7951 DRM_DEBUG_KMS("[CRTC:%d]\n",
7952 crtc->base.base.id);
7953
7954 WARN(crtc->active && !crtc->base.enabled,
7955 "active crtc, but not enabled in sw tracking\n");
7956
7957 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7958 base.head) {
7959 if (encoder->base.crtc != &crtc->base)
7960 continue;
7961 enabled = true;
7962 if (encoder->connectors_active)
7963 active = true;
7964 }
7965 WARN(active != crtc->active,
7966 "crtc's computed active state doesn't match tracked active state "
7967 "(expected %i, found %i)\n", active, crtc->active);
7968 WARN(enabled != crtc->base.enabled,
7969 "crtc's computed enabled state doesn't match tracked enabled state "
7970 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
7971
Daniel Vetter88adfff2013-03-28 10:42:01 +01007972 memset(&pipe_config, 0, sizeof(pipe_config));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007973 active = dev_priv->display.get_pipe_config(crtc,
7974 &pipe_config);
7975 WARN(crtc->active != active,
7976 "crtc active state doesn't match with hw state "
7977 "(expected %i, found %i)\n", crtc->active, active);
7978
7979 WARN(active &&
7980 !intel_pipe_config_compare(&crtc->config, &pipe_config),
7981 "pipe state doesn't match!\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007982 }
7983}
7984
Daniel Vetterf30da182013-04-11 20:22:50 +02007985static int __intel_set_mode(struct drm_crtc *crtc,
7986 struct drm_display_mode *mode,
7987 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02007988{
7989 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02007990 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01007991 struct drm_display_mode *saved_mode, *saved_hwmode;
7992 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007993 struct intel_crtc *intel_crtc;
7994 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007995 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02007996
Tim Gardner3ac18232012-12-07 07:54:26 -07007997 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007998 if (!saved_mode)
7999 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07008000 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02008001
Daniel Vettere2e1ed42012-07-08 21:14:38 +02008002 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02008003 &prepare_pipes, &disable_pipes);
8004
Tim Gardner3ac18232012-12-07 07:54:26 -07008005 *saved_hwmode = crtc->hwmode;
8006 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008007
Daniel Vetter25c5b262012-07-08 22:08:04 +02008008 /* Hack: Because we don't (yet) support global modeset on multiple
8009 * crtcs, we don't keep track of the new mode for more than one crtc.
8010 * Hence simply check whether any bit is set in modeset_pipes in all the
8011 * pieces of code that are not yet converted to deal with mutliple crtcs
8012 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008013 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01008014 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008015 if (IS_ERR(pipe_config)) {
8016 ret = PTR_ERR(pipe_config);
8017 pipe_config = NULL;
8018
Tim Gardner3ac18232012-12-07 07:54:26 -07008019 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008020 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008021 }
8022
Daniel Vetter460da9162013-03-27 00:44:51 +01008023 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
8024 intel_crtc_disable(&intel_crtc->base);
8025
Daniel Vetterea9d7582012-07-10 10:42:52 +02008026 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
8027 if (intel_crtc->base.enabled)
8028 dev_priv->display.crtc_disable(&intel_crtc->base);
8029 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008030
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02008031 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
8032 * to set it here already despite that we pass it down the callchain.
8033 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008034 if (modeset_pipes) {
Daniel Vetter3b117c82013-04-17 20:15:07 +02008035 enum transcoder tmp = to_intel_crtc(crtc)->config.cpu_transcoder;
Daniel Vetter25c5b262012-07-08 22:08:04 +02008036 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008037 /* mode_set/enable/disable functions rely on a correct pipe
8038 * config. */
8039 to_intel_crtc(crtc)->config = *pipe_config;
Daniel Vetter3b117c82013-04-17 20:15:07 +02008040 to_intel_crtc(crtc)->config.cpu_transcoder = tmp;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008041 }
Daniel Vetter7758a112012-07-08 19:40:39 +02008042
Daniel Vetterea9d7582012-07-10 10:42:52 +02008043 /* Only after disabling all output pipelines that will be changed can we
8044 * update the the output configuration. */
8045 intel_modeset_update_state(dev, prepare_pipes);
8046
Daniel Vetter47fab732012-10-26 10:58:18 +02008047 if (dev_priv->display.modeset_global_resources)
8048 dev_priv->display.modeset_global_resources(dev);
8049
Daniel Vettera6778b32012-07-02 09:56:42 +02008050 /* Set up the DPLL and any encoders state that needs to adjust or depend
8051 * on the DPLL.
8052 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008053 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008054 ret = intel_crtc_mode_set(&intel_crtc->base,
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008055 x, y, fb);
8056 if (ret)
8057 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02008058 }
8059
8060 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02008061 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
8062 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02008063
Daniel Vetter25c5b262012-07-08 22:08:04 +02008064 if (modeset_pipes) {
8065 /* Store real post-adjustment hardware mode. */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008066 crtc->hwmode = pipe_config->adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008067
Daniel Vetter25c5b262012-07-08 22:08:04 +02008068 /* Calculate and store various constants which
8069 * are later needed by vblank and swap-completion
8070 * timestamping. They are derived from true hwmode.
8071 */
8072 drm_calc_timestamping_constants(crtc);
8073 }
Daniel Vettera6778b32012-07-02 09:56:42 +02008074
8075 /* FIXME: add subpixel order */
8076done:
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008077 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07008078 crtc->hwmode = *saved_hwmode;
8079 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02008080 }
8081
Tim Gardner3ac18232012-12-07 07:54:26 -07008082out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01008083 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -07008084 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02008085 return ret;
8086}
8087
Daniel Vetterf30da182013-04-11 20:22:50 +02008088int intel_set_mode(struct drm_crtc *crtc,
8089 struct drm_display_mode *mode,
8090 int x, int y, struct drm_framebuffer *fb)
8091{
8092 int ret;
8093
8094 ret = __intel_set_mode(crtc, mode, x, y, fb);
8095
8096 if (ret == 0)
8097 intel_modeset_check_state(crtc->dev);
8098
8099 return ret;
8100}
8101
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008102void intel_crtc_restore_mode(struct drm_crtc *crtc)
8103{
8104 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
8105}
8106
Daniel Vetter25c5b262012-07-08 22:08:04 +02008107#undef for_each_intel_crtc_masked
8108
Daniel Vetterd9e55602012-07-04 22:16:09 +02008109static void intel_set_config_free(struct intel_set_config *config)
8110{
8111 if (!config)
8112 return;
8113
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008114 kfree(config->save_connector_encoders);
8115 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02008116 kfree(config);
8117}
8118
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008119static int intel_set_config_save_state(struct drm_device *dev,
8120 struct intel_set_config *config)
8121{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008122 struct drm_encoder *encoder;
8123 struct drm_connector *connector;
8124 int count;
8125
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008126 config->save_encoder_crtcs =
8127 kcalloc(dev->mode_config.num_encoder,
8128 sizeof(struct drm_crtc *), GFP_KERNEL);
8129 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008130 return -ENOMEM;
8131
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008132 config->save_connector_encoders =
8133 kcalloc(dev->mode_config.num_connector,
8134 sizeof(struct drm_encoder *), GFP_KERNEL);
8135 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008136 return -ENOMEM;
8137
8138 /* Copy data. Note that driver private data is not affected.
8139 * Should anything bad happen only the expected state is
8140 * restored, not the drivers personal bookkeeping.
8141 */
8142 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008143 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008144 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008145 }
8146
8147 count = 0;
8148 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02008149 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008150 }
8151
8152 return 0;
8153}
8154
8155static void intel_set_config_restore_state(struct drm_device *dev,
8156 struct intel_set_config *config)
8157{
Daniel Vetter9a935852012-07-05 22:34:27 +02008158 struct intel_encoder *encoder;
8159 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008160 int count;
8161
8162 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008163 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8164 encoder->new_crtc =
8165 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008166 }
8167
8168 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008169 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
8170 connector->new_encoder =
8171 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008172 }
8173}
8174
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008175static void
8176intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8177 struct intel_set_config *config)
8178{
8179
8180 /* We should be able to check here if the fb has the same properties
8181 * and then just flip_or_move it */
8182 if (set->crtc->fb != set->fb) {
8183 /* If we have no fb then treat it as a full mode set */
8184 if (set->crtc->fb == NULL) {
8185 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
8186 config->mode_changed = true;
8187 } else if (set->fb == NULL) {
8188 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +01008189 } else if (set->fb->pixel_format !=
8190 set->crtc->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008191 config->mode_changed = true;
8192 } else
8193 config->fb_changed = true;
8194 }
8195
Daniel Vetter835c5872012-07-10 18:11:08 +02008196 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008197 config->fb_changed = true;
8198
8199 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
8200 DRM_DEBUG_KMS("modes are different, full mode set\n");
8201 drm_mode_debug_printmodeline(&set->crtc->mode);
8202 drm_mode_debug_printmodeline(set->mode);
8203 config->mode_changed = true;
8204 }
8205}
8206
Daniel Vetter2e431052012-07-04 22:42:15 +02008207static int
Daniel Vetter9a935852012-07-05 22:34:27 +02008208intel_modeset_stage_output_state(struct drm_device *dev,
8209 struct drm_mode_set *set,
8210 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02008211{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008212 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008213 struct intel_connector *connector;
8214 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02008215 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02008216
Damien Lespiau9abdda72013-02-13 13:29:23 +00008217 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02008218 * of connectors. For paranoia, double-check this. */
8219 WARN_ON(!set->fb && (set->num_connectors != 0));
8220 WARN_ON(set->fb && (set->num_connectors == 0));
8221
Daniel Vetter50f56112012-07-02 09:35:43 +02008222 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008223 list_for_each_entry(connector, &dev->mode_config.connector_list,
8224 base.head) {
8225 /* Otherwise traverse passed in connector list and get encoders
8226 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008227 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008228 if (set->connectors[ro] == &connector->base) {
8229 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02008230 break;
8231 }
8232 }
8233
Daniel Vetter9a935852012-07-05 22:34:27 +02008234 /* If we disable the crtc, disable all its connectors. Also, if
8235 * the connector is on the changing crtc but not on the new
8236 * connector list, disable it. */
8237 if ((!set->fb || ro == set->num_connectors) &&
8238 connector->base.encoder &&
8239 connector->base.encoder->crtc == set->crtc) {
8240 connector->new_encoder = NULL;
8241
8242 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8243 connector->base.base.id,
8244 drm_get_connector_name(&connector->base));
8245 }
8246
8247
8248 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008249 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008250 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008251 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008252 }
8253 /* connector->new_encoder is now updated for all connectors. */
8254
8255 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008256 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008257 list_for_each_entry(connector, &dev->mode_config.connector_list,
8258 base.head) {
8259 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02008260 continue;
8261
Daniel Vetter9a935852012-07-05 22:34:27 +02008262 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02008263
8264 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008265 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02008266 new_crtc = set->crtc;
8267 }
8268
8269 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02008270 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8271 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008272 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02008273 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008274 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8275
8276 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8277 connector->base.base.id,
8278 drm_get_connector_name(&connector->base),
8279 new_crtc->base.id);
8280 }
8281
8282 /* Check for any encoders that needs to be disabled. */
8283 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8284 base.head) {
8285 list_for_each_entry(connector,
8286 &dev->mode_config.connector_list,
8287 base.head) {
8288 if (connector->new_encoder == encoder) {
8289 WARN_ON(!connector->new_encoder->new_crtc);
8290
8291 goto next_encoder;
8292 }
8293 }
8294 encoder->new_crtc = NULL;
8295next_encoder:
8296 /* Only now check for crtc changes so we don't miss encoders
8297 * that will be disabled. */
8298 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008299 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008300 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008301 }
8302 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008303 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008304
Daniel Vetter2e431052012-07-04 22:42:15 +02008305 return 0;
8306}
8307
8308static int intel_crtc_set_config(struct drm_mode_set *set)
8309{
8310 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02008311 struct drm_mode_set save_set;
8312 struct intel_set_config *config;
8313 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02008314
Daniel Vetter8d3e3752012-07-05 16:09:09 +02008315 BUG_ON(!set);
8316 BUG_ON(!set->crtc);
8317 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02008318
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01008319 /* Enforce sane interface api - has been abused by the fb helper. */
8320 BUG_ON(!set->mode && set->fb);
8321 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +02008322
Daniel Vetter2e431052012-07-04 22:42:15 +02008323 if (set->fb) {
8324 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8325 set->crtc->base.id, set->fb->base.id,
8326 (int)set->num_connectors, set->x, set->y);
8327 } else {
8328 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02008329 }
8330
8331 dev = set->crtc->dev;
8332
8333 ret = -ENOMEM;
8334 config = kzalloc(sizeof(*config), GFP_KERNEL);
8335 if (!config)
8336 goto out_config;
8337
8338 ret = intel_set_config_save_state(dev, config);
8339 if (ret)
8340 goto out_config;
8341
8342 save_set.crtc = set->crtc;
8343 save_set.mode = &set->crtc->mode;
8344 save_set.x = set->crtc->x;
8345 save_set.y = set->crtc->y;
8346 save_set.fb = set->crtc->fb;
8347
8348 /* Compute whether we need a full modeset, only an fb base update or no
8349 * change at all. In the future we might also check whether only the
8350 * mode changed, e.g. for LVDS where we only change the panel fitter in
8351 * such cases. */
8352 intel_set_config_compute_mode_changes(set, config);
8353
Daniel Vetter9a935852012-07-05 22:34:27 +02008354 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02008355 if (ret)
8356 goto fail;
8357
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008358 if (config->mode_changed) {
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008359 if (set->mode) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008360 DRM_DEBUG_KMS("attempting to set mode from"
8361 " userspace\n");
8362 drm_mode_debug_printmodeline(set->mode);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008363 }
8364
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008365 ret = intel_set_mode(set->crtc, set->mode,
8366 set->x, set->y, set->fb);
8367 if (ret) {
8368 DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
8369 set->crtc->base.id, ret);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008370 goto fail;
8371 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008372 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02008373 intel_crtc_wait_for_pending_flips(set->crtc);
8374
Daniel Vetter4f660f42012-07-02 09:47:37 +02008375 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008376 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008377 }
8378
Daniel Vetterd9e55602012-07-04 22:16:09 +02008379 intel_set_config_free(config);
8380
Daniel Vetter50f56112012-07-02 09:35:43 +02008381 return 0;
8382
8383fail:
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008384 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008385
8386 /* Try to restore the config */
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008387 if (config->mode_changed &&
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008388 intel_set_mode(save_set.crtc, save_set.mode,
8389 save_set.x, save_set.y, save_set.fb))
Daniel Vetter50f56112012-07-02 09:35:43 +02008390 DRM_ERROR("failed to restore config after modeset failure\n");
8391
Daniel Vetterd9e55602012-07-04 22:16:09 +02008392out_config:
8393 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008394 return ret;
8395}
8396
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008397static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008398 .cursor_set = intel_crtc_cursor_set,
8399 .cursor_move = intel_crtc_cursor_move,
8400 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008401 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008402 .destroy = intel_crtc_destroy,
8403 .page_flip = intel_crtc_page_flip,
8404};
8405
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008406static void intel_cpu_pll_init(struct drm_device *dev)
8407{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008408 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008409 intel_ddi_pll_init(dev);
8410}
8411
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008412static void intel_pch_pll_init(struct drm_device *dev)
8413{
8414 drm_i915_private_t *dev_priv = dev->dev_private;
8415 int i;
8416
8417 if (dev_priv->num_pch_pll == 0) {
8418 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
8419 return;
8420 }
8421
8422 for (i = 0; i < dev_priv->num_pch_pll; i++) {
8423 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
8424 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
8425 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
8426 }
8427}
8428
Hannes Ederb358d0a2008-12-18 21:18:47 +01008429static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08008430{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008431 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008432 struct intel_crtc *intel_crtc;
8433 int i;
8434
8435 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8436 if (intel_crtc == NULL)
8437 return;
8438
8439 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8440
8441 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08008442 for (i = 0; i < 256; i++) {
8443 intel_crtc->lut_r[i] = i;
8444 intel_crtc->lut_g[i] = i;
8445 intel_crtc->lut_b[i] = i;
8446 }
8447
Jesse Barnes80824002009-09-10 15:28:06 -07008448 /* Swap pipes & planes for FBC on pre-965 */
8449 intel_crtc->pipe = pipe;
8450 intel_crtc->plane = pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02008451 intel_crtc->config.cpu_transcoder = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01008452 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008453 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01008454 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07008455 }
8456
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008457 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8458 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8459 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8460 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8461
Jesse Barnes79e53942008-11-07 14:24:08 -08008462 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08008463}
8464
Carl Worth08d7b3d2009-04-29 14:43:54 -07008465int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00008466 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07008467{
Carl Worth08d7b3d2009-04-29 14:43:54 -07008468 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02008469 struct drm_mode_object *drmmode_obj;
8470 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008471
Daniel Vetter1cff8f62012-04-24 09:55:08 +02008472 if (!drm_core_check_feature(dev, DRIVER_MODESET))
8473 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008474
Daniel Vetterc05422d2009-08-11 16:05:30 +02008475 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8476 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07008477
Daniel Vetterc05422d2009-08-11 16:05:30 +02008478 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07008479 DRM_ERROR("no such CRTC id\n");
8480 return -EINVAL;
8481 }
8482
Daniel Vetterc05422d2009-08-11 16:05:30 +02008483 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8484 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008485
Daniel Vetterc05422d2009-08-11 16:05:30 +02008486 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008487}
8488
Daniel Vetter66a92782012-07-12 20:08:18 +02008489static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008490{
Daniel Vetter66a92782012-07-12 20:08:18 +02008491 struct drm_device *dev = encoder->base.dev;
8492 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008493 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008494 int entry = 0;
8495
Daniel Vetter66a92782012-07-12 20:08:18 +02008496 list_for_each_entry(source_encoder,
8497 &dev->mode_config.encoder_list, base.head) {
8498
8499 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008500 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02008501
8502 /* Intel hw has only one MUX where enocoders could be cloned. */
8503 if (encoder->cloneable && source_encoder->cloneable)
8504 index_mask |= (1 << entry);
8505
Jesse Barnes79e53942008-11-07 14:24:08 -08008506 entry++;
8507 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01008508
Jesse Barnes79e53942008-11-07 14:24:08 -08008509 return index_mask;
8510}
8511
Chris Wilson4d302442010-12-14 19:21:29 +00008512static bool has_edp_a(struct drm_device *dev)
8513{
8514 struct drm_i915_private *dev_priv = dev->dev_private;
8515
8516 if (!IS_MOBILE(dev))
8517 return false;
8518
8519 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8520 return false;
8521
8522 if (IS_GEN5(dev) &&
8523 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8524 return false;
8525
8526 return true;
8527}
8528
Jesse Barnes79e53942008-11-07 14:24:08 -08008529static void intel_setup_outputs(struct drm_device *dev)
8530{
Eric Anholt725e30a2009-01-22 13:01:02 -08008531 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008532 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008533 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008534 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08008535
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008536 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00008537 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8538 /* disable the panel fitter on everything but LVDS */
8539 I915_WRITE(PFIT_CONTROL, 0);
8540 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008541
Paulo Zanonic40c0f52013-04-12 18:16:53 -03008542 if (!IS_ULT(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02008543 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008544
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008545 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03008546 int found;
8547
8548 /* Haswell uses DDI functions to detect digital outputs */
8549 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
8550 /* DDI A only supports eDP */
8551 if (found)
8552 intel_ddi_init(dev, PORT_A);
8553
8554 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
8555 * register */
8556 found = I915_READ(SFUSE_STRAP);
8557
8558 if (found & SFUSE_STRAP_DDIB_DETECTED)
8559 intel_ddi_init(dev, PORT_B);
8560 if (found & SFUSE_STRAP_DDIC_DETECTED)
8561 intel_ddi_init(dev, PORT_C);
8562 if (found & SFUSE_STRAP_DDID_DETECTED)
8563 intel_ddi_init(dev, PORT_D);
8564 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008565 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02008566 dpd_is_edp = intel_dpd_is_edp(dev);
8567
8568 if (has_edp_a(dev))
8569 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008570
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008571 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08008572 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01008573 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008574 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -03008575 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008576 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008577 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008578 }
8579
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008580 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03008581 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008582
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008583 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -03008584 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008585
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008586 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008587 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008588
Daniel Vetter270b3042012-10-27 15:52:05 +02008589 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008590 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008591 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05308592 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008593 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
8594 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05308595
Paulo Zanonidc0fa712013-02-19 16:21:46 -03008596 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
Paulo Zanonie2debe92013-02-18 19:00:27 -03008597 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
8598 PORT_B);
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008599 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
8600 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008601 }
Zhenyu Wang103a1962009-11-27 11:44:36 +08008602 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008603 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008604
Paulo Zanonie2debe92013-02-18 19:00:27 -03008605 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008606 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008607 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008608 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8609 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008610 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008611 }
Ma Ling27185ae2009-08-24 13:50:23 +08008612
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008613 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8614 DRM_DEBUG_KMS("probing DP_B\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008615 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008616 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008617 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008618
8619 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008620
Paulo Zanonie2debe92013-02-18 19:00:27 -03008621 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008622 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008623 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008624 }
Ma Ling27185ae2009-08-24 13:50:23 +08008625
Paulo Zanonie2debe92013-02-18 19:00:27 -03008626 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008627
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008628 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8629 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -03008630 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008631 }
8632 if (SUPPORTS_INTEGRATED_DP(dev)) {
8633 DRM_DEBUG_KMS("probing DP_C\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008634 intel_dp_init(dev, DP_C, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008635 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008636 }
Ma Ling27185ae2009-08-24 13:50:23 +08008637
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008638 if (SUPPORTS_INTEGRATED_DP(dev) &&
8639 (I915_READ(DP_D) & DP_DETECTED)) {
8640 DRM_DEBUG_KMS("probing DP_D\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008641 intel_dp_init(dev, DP_D, PORT_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008642 }
Eric Anholtbad720f2009-10-22 16:11:14 -07008643 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008644 intel_dvo_init(dev);
8645
Zhenyu Wang103a1962009-11-27 11:44:36 +08008646 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008647 intel_tv_init(dev);
8648
Chris Wilson4ef69c72010-09-09 15:14:28 +01008649 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8650 encoder->base.possible_crtcs = encoder->crtc_mask;
8651 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02008652 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08008653 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008654
Paulo Zanonidde86e22012-12-01 12:04:25 -02008655 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02008656
8657 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008658}
8659
8660static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8661{
8662 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008663
8664 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008665 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008666
8667 kfree(intel_fb);
8668}
8669
8670static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008671 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008672 unsigned int *handle)
8673{
8674 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008675 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008676
Chris Wilson05394f32010-11-08 19:18:58 +00008677 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008678}
8679
8680static const struct drm_framebuffer_funcs intel_fb_funcs = {
8681 .destroy = intel_user_framebuffer_destroy,
8682 .create_handle = intel_user_framebuffer_create_handle,
8683};
8684
Dave Airlie38651672010-03-30 05:34:13 +00008685int intel_framebuffer_init(struct drm_device *dev,
8686 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008687 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008688 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008689{
Jesse Barnes79e53942008-11-07 14:24:08 -08008690 int ret;
8691
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008692 if (obj->tiling_mode == I915_TILING_Y) {
8693 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01008694 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008695 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008696
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008697 if (mode_cmd->pitches[0] & 63) {
8698 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
8699 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01008700 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008701 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008702
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008703 /* FIXME <= Gen4 stride limits are bit unclear */
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008704 if (mode_cmd->pitches[0] > 32768) {
8705 DRM_DEBUG("pitch (%d) must be at less than 32768\n",
8706 mode_cmd->pitches[0]);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008707 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008708 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008709
8710 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008711 mode_cmd->pitches[0] != obj->stride) {
8712 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
8713 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008714 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008715 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008716
Ville Syrjälä57779d02012-10-31 17:50:14 +02008717 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008718 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02008719 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008720 case DRM_FORMAT_RGB565:
8721 case DRM_FORMAT_XRGB8888:
8722 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008723 break;
8724 case DRM_FORMAT_XRGB1555:
8725 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008726 if (INTEL_INFO(dev)->gen > 3) {
8727 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008728 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008729 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02008730 break;
8731 case DRM_FORMAT_XBGR8888:
8732 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008733 case DRM_FORMAT_XRGB2101010:
8734 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008735 case DRM_FORMAT_XBGR2101010:
8736 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008737 if (INTEL_INFO(dev)->gen < 4) {
8738 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008739 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008740 }
Jesse Barnesb5626742011-06-24 12:19:27 -07008741 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02008742 case DRM_FORMAT_YUYV:
8743 case DRM_FORMAT_UYVY:
8744 case DRM_FORMAT_YVYU:
8745 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008746 if (INTEL_INFO(dev)->gen < 5) {
8747 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008748 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008749 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008750 break;
8751 default:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008752 DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01008753 return -EINVAL;
8754 }
8755
Ville Syrjälä90f9a332012-10-31 17:50:19 +02008756 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
8757 if (mode_cmd->offsets[0] != 0)
8758 return -EINVAL;
8759
Daniel Vetterc7d73f62012-12-13 23:38:38 +01008760 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
8761 intel_fb->obj = obj;
8762
Jesse Barnes79e53942008-11-07 14:24:08 -08008763 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8764 if (ret) {
8765 DRM_ERROR("framebuffer init failed %d\n", ret);
8766 return ret;
8767 }
8768
Jesse Barnes79e53942008-11-07 14:24:08 -08008769 return 0;
8770}
8771
Jesse Barnes79e53942008-11-07 14:24:08 -08008772static struct drm_framebuffer *
8773intel_user_framebuffer_create(struct drm_device *dev,
8774 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008775 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08008776{
Chris Wilson05394f32010-11-08 19:18:58 +00008777 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008778
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008779 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8780 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00008781 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01008782 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08008783
Chris Wilsond2dff872011-04-19 08:36:26 +01008784 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08008785}
8786
Jesse Barnes79e53942008-11-07 14:24:08 -08008787static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08008788 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00008789 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08008790};
8791
Jesse Barnese70236a2009-09-21 10:42:27 -07008792/* Set up chip specific display functions */
8793static void intel_init_display(struct drm_device *dev)
8794{
8795 struct drm_i915_private *dev_priv = dev->dev_private;
8796
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008797 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008798 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008799 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02008800 dev_priv->display.crtc_enable = haswell_crtc_enable;
8801 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008802 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008803 dev_priv->display.update_plane = ironlake_update_plane;
8804 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008805 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -07008806 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008807 dev_priv->display.crtc_enable = ironlake_crtc_enable;
8808 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008809 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008810 dev_priv->display.update_plane = ironlake_update_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -07008811 } else if (IS_VALLEYVIEW(dev)) {
8812 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
8813 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
8814 dev_priv->display.crtc_enable = valleyview_crtc_enable;
8815 dev_priv->display.crtc_disable = i9xx_crtc_disable;
8816 dev_priv->display.off = i9xx_crtc_off;
8817 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008818 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008819 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Eric Anholtf564048e2011-03-30 13:01:02 -07008820 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008821 dev_priv->display.crtc_enable = i9xx_crtc_enable;
8822 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008823 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008824 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008825 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008826
Jesse Barnese70236a2009-09-21 10:42:27 -07008827 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07008828 if (IS_VALLEYVIEW(dev))
8829 dev_priv->display.get_display_clock_speed =
8830 valleyview_get_display_clock_speed;
8831 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07008832 dev_priv->display.get_display_clock_speed =
8833 i945_get_display_clock_speed;
8834 else if (IS_I915G(dev))
8835 dev_priv->display.get_display_clock_speed =
8836 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008837 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008838 dev_priv->display.get_display_clock_speed =
8839 i9xx_misc_get_display_clock_speed;
8840 else if (IS_I915GM(dev))
8841 dev_priv->display.get_display_clock_speed =
8842 i915gm_get_display_clock_speed;
8843 else if (IS_I865G(dev))
8844 dev_priv->display.get_display_clock_speed =
8845 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02008846 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008847 dev_priv->display.get_display_clock_speed =
8848 i855_get_display_clock_speed;
8849 else /* 852, 830 */
8850 dev_priv->display.get_display_clock_speed =
8851 i830_get_display_clock_speed;
8852
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008853 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01008854 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008855 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008856 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08008857 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008858 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008859 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07008860 } else if (IS_IVYBRIDGE(dev)) {
8861 /* FIXME: detect B0+ stepping and use auto training */
8862 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008863 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02008864 dev_priv->display.modeset_global_resources =
8865 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03008866 } else if (IS_HASWELL(dev)) {
8867 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08008868 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02008869 dev_priv->display.modeset_global_resources =
8870 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02008871 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07008872 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08008873 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07008874 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008875
8876 /* Default just returns -ENODEV to indicate unsupported */
8877 dev_priv->display.queue_flip = intel_default_queue_flip;
8878
8879 switch (INTEL_INFO(dev)->gen) {
8880 case 2:
8881 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8882 break;
8883
8884 case 3:
8885 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8886 break;
8887
8888 case 4:
8889 case 5:
8890 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8891 break;
8892
8893 case 6:
8894 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8895 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008896 case 7:
8897 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8898 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008899 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008900}
8901
Jesse Barnesb690e962010-07-19 13:53:12 -07008902/*
8903 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8904 * resume, or other times. This quirk makes sure that's the case for
8905 * affected systems.
8906 */
Akshay Joshi0206e352011-08-16 15:34:10 -04008907static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07008908{
8909 struct drm_i915_private *dev_priv = dev->dev_private;
8910
8911 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008912 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008913}
8914
Keith Packard435793d2011-07-12 14:56:22 -07008915/*
8916 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8917 */
8918static void quirk_ssc_force_disable(struct drm_device *dev)
8919{
8920 struct drm_i915_private *dev_priv = dev->dev_private;
8921 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008922 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07008923}
8924
Carsten Emde4dca20e2012-03-15 15:56:26 +01008925/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01008926 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
8927 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01008928 */
8929static void quirk_invert_brightness(struct drm_device *dev)
8930{
8931 struct drm_i915_private *dev_priv = dev->dev_private;
8932 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008933 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008934}
8935
8936struct intel_quirk {
8937 int device;
8938 int subsystem_vendor;
8939 int subsystem_device;
8940 void (*hook)(struct drm_device *dev);
8941};
8942
Egbert Eich5f85f172012-10-14 15:46:38 +02008943/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
8944struct intel_dmi_quirk {
8945 void (*hook)(struct drm_device *dev);
8946 const struct dmi_system_id (*dmi_id_list)[];
8947};
8948
8949static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
8950{
8951 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
8952 return 1;
8953}
8954
8955static const struct intel_dmi_quirk intel_dmi_quirks[] = {
8956 {
8957 .dmi_id_list = &(const struct dmi_system_id[]) {
8958 {
8959 .callback = intel_dmi_reverse_brightness,
8960 .ident = "NCR Corporation",
8961 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
8962 DMI_MATCH(DMI_PRODUCT_NAME, ""),
8963 },
8964 },
8965 { } /* terminating entry */
8966 },
8967 .hook = quirk_invert_brightness,
8968 },
8969};
8970
Ben Widawskyc43b5632012-04-16 14:07:40 -07008971static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07008972 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04008973 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07008974
Jesse Barnesb690e962010-07-19 13:53:12 -07008975 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
8976 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
8977
Jesse Barnesb690e962010-07-19 13:53:12 -07008978 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
8979 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
8980
Daniel Vetterccd0d362012-10-10 23:13:59 +02008981 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07008982 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02008983 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07008984
8985 /* Lenovo U160 cannot use SSC on LVDS */
8986 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02008987
8988 /* Sony Vaio Y cannot use SSC on LVDS */
8989 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01008990
8991 /* Acer Aspire 5734Z must invert backlight brightness */
8992 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02008993
8994 /* Acer/eMachines G725 */
8995 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02008996
8997 /* Acer/eMachines e725 */
8998 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02008999
9000 /* Acer/Packard Bell NCL20 */
9001 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Daniel Vetterac4199e2013-02-15 18:35:30 +01009002
9003 /* Acer Aspire 4736Z */
9004 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07009005};
9006
9007static void intel_init_quirks(struct drm_device *dev)
9008{
9009 struct pci_dev *d = dev->pdev;
9010 int i;
9011
9012 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9013 struct intel_quirk *q = &intel_quirks[i];
9014
9015 if (d->device == q->device &&
9016 (d->subsystem_vendor == q->subsystem_vendor ||
9017 q->subsystem_vendor == PCI_ANY_ID) &&
9018 (d->subsystem_device == q->subsystem_device ||
9019 q->subsystem_device == PCI_ANY_ID))
9020 q->hook(dev);
9021 }
Egbert Eich5f85f172012-10-14 15:46:38 +02009022 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
9023 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
9024 intel_dmi_quirks[i].hook(dev);
9025 }
Jesse Barnesb690e962010-07-19 13:53:12 -07009026}
9027
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009028/* Disable the VGA plane that we never use */
9029static void i915_disable_vga(struct drm_device *dev)
9030{
9031 struct drm_i915_private *dev_priv = dev->dev_private;
9032 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009033 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009034
9035 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07009036 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009037 sr1 = inb(VGA_SR_DATA);
9038 outb(sr1 | 1<<5, VGA_SR_DATA);
9039 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9040 udelay(300);
9041
9042 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9043 POSTING_READ(vga_reg);
9044}
9045
Daniel Vetterf8175862012-04-10 15:50:11 +02009046void intel_modeset_init_hw(struct drm_device *dev)
9047{
Paulo Zanonifa42e232013-01-25 16:59:11 -02009048 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03009049
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03009050 intel_prepare_ddi(dev);
9051
Daniel Vetterf8175862012-04-10 15:50:11 +02009052 intel_init_clock_gating(dev);
9053
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009054 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009055 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02009056 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02009057}
9058
Jesse Barnes79e53942008-11-07 14:24:08 -08009059void intel_modeset_init(struct drm_device *dev)
9060{
Jesse Barnes652c3932009-08-17 13:31:43 -07009061 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009062 int i, j, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009063
9064 drm_mode_config_init(dev);
9065
9066 dev->mode_config.min_width = 0;
9067 dev->mode_config.min_height = 0;
9068
Dave Airlie019d96c2011-09-29 16:20:42 +01009069 dev->mode_config.preferred_depth = 24;
9070 dev->mode_config.prefer_shadow = 1;
9071
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02009072 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08009073
Jesse Barnesb690e962010-07-19 13:53:12 -07009074 intel_init_quirks(dev);
9075
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03009076 intel_init_pm(dev);
9077
Ben Widawskye3c74752013-04-05 13:12:39 -07009078 if (INTEL_INFO(dev)->num_pipes == 0)
9079 return;
9080
Jesse Barnese70236a2009-09-21 10:42:27 -07009081 intel_init_display(dev);
9082
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009083 if (IS_GEN2(dev)) {
9084 dev->mode_config.max_width = 2048;
9085 dev->mode_config.max_height = 2048;
9086 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009087 dev->mode_config.max_width = 4096;
9088 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009089 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009090 dev->mode_config.max_width = 8192;
9091 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009092 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08009093 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009094
Zhao Yakui28c97732009-10-09 11:39:41 +08009095 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009096 INTEL_INFO(dev)->num_pipes,
9097 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009098
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009099 for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009100 intel_crtc_init(dev, i);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009101 for (j = 0; j < dev_priv->num_plane; j++) {
9102 ret = intel_plane_init(dev, i, j);
9103 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +03009104 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
9105 pipe_name(i), sprite_name(i, j), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07009106 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009107 }
9108
Paulo Zanoni79f689a2012-10-05 12:05:52 -03009109 intel_cpu_pll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009110 intel_pch_pll_init(dev);
9111
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009112 /* Just disable it once at startup */
9113 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009114 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00009115
9116 /* Just in case the BIOS is doing something questionable. */
9117 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009118}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009119
Daniel Vetter24929352012-07-02 20:28:59 +02009120static void
9121intel_connector_break_all_links(struct intel_connector *connector)
9122{
9123 connector->base.dpms = DRM_MODE_DPMS_OFF;
9124 connector->base.encoder = NULL;
9125 connector->encoder->connectors_active = false;
9126 connector->encoder->base.crtc = NULL;
9127}
9128
Daniel Vetter7fad7982012-07-04 17:51:47 +02009129static void intel_enable_pipe_a(struct drm_device *dev)
9130{
9131 struct intel_connector *connector;
9132 struct drm_connector *crt = NULL;
9133 struct intel_load_detect_pipe load_detect_temp;
9134
9135 /* We can't just switch on the pipe A, we need to set things up with a
9136 * proper mode and output configuration. As a gross hack, enable pipe A
9137 * by enabling the load detect pipe once. */
9138 list_for_each_entry(connector,
9139 &dev->mode_config.connector_list,
9140 base.head) {
9141 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
9142 crt = &connector->base;
9143 break;
9144 }
9145 }
9146
9147 if (!crt)
9148 return;
9149
9150 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
9151 intel_release_load_detect_pipe(crt, &load_detect_temp);
9152
9153
9154}
9155
Daniel Vetterfa555832012-10-10 23:14:00 +02009156static bool
9157intel_check_plane_mapping(struct intel_crtc *crtc)
9158{
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009159 struct drm_device *dev = crtc->base.dev;
9160 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009161 u32 reg, val;
9162
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009163 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +02009164 return true;
9165
9166 reg = DSPCNTR(!crtc->plane);
9167 val = I915_READ(reg);
9168
9169 if ((val & DISPLAY_PLANE_ENABLE) &&
9170 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
9171 return false;
9172
9173 return true;
9174}
9175
Daniel Vetter24929352012-07-02 20:28:59 +02009176static void intel_sanitize_crtc(struct intel_crtc *crtc)
9177{
9178 struct drm_device *dev = crtc->base.dev;
9179 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02009180 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02009181
Daniel Vetter24929352012-07-02 20:28:59 +02009182 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +02009183 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02009184 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
9185
9186 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02009187 * disable the crtc (and hence change the state) if it is wrong. Note
9188 * that gen4+ has a fixed plane -> pipe mapping. */
9189 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02009190 struct intel_connector *connector;
9191 bool plane;
9192
Daniel Vetter24929352012-07-02 20:28:59 +02009193 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
9194 crtc->base.base.id);
9195
9196 /* Pipe has the wrong plane attached and the plane is active.
9197 * Temporarily change the plane mapping and disable everything
9198 * ... */
9199 plane = crtc->plane;
9200 crtc->plane = !plane;
9201 dev_priv->display.crtc_disable(&crtc->base);
9202 crtc->plane = plane;
9203
9204 /* ... and break all links. */
9205 list_for_each_entry(connector, &dev->mode_config.connector_list,
9206 base.head) {
9207 if (connector->encoder->base.crtc != &crtc->base)
9208 continue;
9209
9210 intel_connector_break_all_links(connector);
9211 }
9212
9213 WARN_ON(crtc->active);
9214 crtc->base.enabled = false;
9215 }
Daniel Vetter24929352012-07-02 20:28:59 +02009216
Daniel Vetter7fad7982012-07-04 17:51:47 +02009217 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9218 crtc->pipe == PIPE_A && !crtc->active) {
9219 /* BIOS forgot to enable pipe A, this mostly happens after
9220 * resume. Force-enable the pipe to fix this, the update_dpms
9221 * call below we restore the pipe to the right state, but leave
9222 * the required bits on. */
9223 intel_enable_pipe_a(dev);
9224 }
9225
Daniel Vetter24929352012-07-02 20:28:59 +02009226 /* Adjust the state of the output pipe according to whether we
9227 * have active connectors/encoders. */
9228 intel_crtc_update_dpms(&crtc->base);
9229
9230 if (crtc->active != crtc->base.enabled) {
9231 struct intel_encoder *encoder;
9232
9233 /* This can happen either due to bugs in the get_hw_state
9234 * functions or because the pipe is force-enabled due to the
9235 * pipe A quirk. */
9236 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9237 crtc->base.base.id,
9238 crtc->base.enabled ? "enabled" : "disabled",
9239 crtc->active ? "enabled" : "disabled");
9240
9241 crtc->base.enabled = crtc->active;
9242
9243 /* Because we only establish the connector -> encoder ->
9244 * crtc links if something is active, this means the
9245 * crtc is now deactivated. Break the links. connector
9246 * -> encoder links are only establish when things are
9247 * actually up, hence no need to break them. */
9248 WARN_ON(crtc->active);
9249
9250 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9251 WARN_ON(encoder->connectors_active);
9252 encoder->base.crtc = NULL;
9253 }
9254 }
9255}
9256
9257static void intel_sanitize_encoder(struct intel_encoder *encoder)
9258{
9259 struct intel_connector *connector;
9260 struct drm_device *dev = encoder->base.dev;
9261
9262 /* We need to check both for a crtc link (meaning that the
9263 * encoder is active and trying to read from a pipe) and the
9264 * pipe itself being active. */
9265 bool has_active_crtc = encoder->base.crtc &&
9266 to_intel_crtc(encoder->base.crtc)->active;
9267
9268 if (encoder->connectors_active && !has_active_crtc) {
9269 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9270 encoder->base.base.id,
9271 drm_get_encoder_name(&encoder->base));
9272
9273 /* Connector is active, but has no active pipe. This is
9274 * fallout from our resume register restoring. Disable
9275 * the encoder manually again. */
9276 if (encoder->base.crtc) {
9277 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9278 encoder->base.base.id,
9279 drm_get_encoder_name(&encoder->base));
9280 encoder->disable(encoder);
9281 }
9282
9283 /* Inconsistent output/port/pipe state happens presumably due to
9284 * a bug in one of the get_hw_state functions. Or someplace else
9285 * in our code, like the register restore mess on resume. Clamp
9286 * things to off as a safer default. */
9287 list_for_each_entry(connector,
9288 &dev->mode_config.connector_list,
9289 base.head) {
9290 if (connector->encoder != encoder)
9291 continue;
9292
9293 intel_connector_break_all_links(connector);
9294 }
9295 }
9296 /* Enabled encoders without active connectors will be fixed in
9297 * the crtc fixup. */
9298}
9299
Daniel Vetter44cec742013-01-25 17:53:21 +01009300void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009301{
9302 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009303 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009304
9305 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9306 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +02009307 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009308 }
9309}
9310
Daniel Vetter24929352012-07-02 20:28:59 +02009311/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
9312 * and i915 state tracking structures. */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009313void intel_modeset_setup_hw_state(struct drm_device *dev,
9314 bool force_restore)
Daniel Vetter24929352012-07-02 20:28:59 +02009315{
9316 struct drm_i915_private *dev_priv = dev->dev_private;
9317 enum pipe pipe;
9318 u32 tmp;
Jesse Barnesb5644d02013-03-26 13:25:27 -07009319 struct drm_plane *plane;
Daniel Vetter24929352012-07-02 20:28:59 +02009320 struct intel_crtc *crtc;
9321 struct intel_encoder *encoder;
9322 struct intel_connector *connector;
9323
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009324 if (HAS_DDI(dev)) {
Paulo Zanonie28d54c2012-10-24 16:09:25 -02009325 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9326
9327 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9328 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9329 case TRANS_DDI_EDP_INPUT_A_ON:
9330 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9331 pipe = PIPE_A;
9332 break;
9333 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9334 pipe = PIPE_B;
9335 break;
9336 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9337 pipe = PIPE_C;
9338 break;
Damien Lespiauaaa148e2013-03-07 15:30:26 +00009339 default:
9340 /* A bogus value has been programmed, disable
9341 * the transcoder */
9342 WARN(1, "Bogus eDP source %08x\n", tmp);
9343 intel_ddi_disable_transcoder_func(dev_priv,
9344 TRANSCODER_EDP);
9345 goto setup_pipes;
Paulo Zanonie28d54c2012-10-24 16:09:25 -02009346 }
9347
9348 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Daniel Vetter3b117c82013-04-17 20:15:07 +02009349 crtc->config.cpu_transcoder = TRANSCODER_EDP;
Paulo Zanonie28d54c2012-10-24 16:09:25 -02009350
9351 DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
9352 pipe_name(pipe));
9353 }
9354 }
9355
Damien Lespiauaaa148e2013-03-07 15:30:26 +00009356setup_pipes:
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009357 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9358 base.head) {
Daniel Vetter3b117c82013-04-17 20:15:07 +02009359 enum transcoder tmp = crtc->config.cpu_transcoder;
Daniel Vetter88adfff2013-03-28 10:42:01 +01009360 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +02009361 crtc->config.cpu_transcoder = tmp;
9362
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009363 crtc->active = dev_priv->display.get_pipe_config(crtc,
9364 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +02009365
9366 crtc->base.enabled = crtc->active;
9367
9368 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9369 crtc->base.base.id,
9370 crtc->active ? "enabled" : "disabled");
9371 }
9372
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009373 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009374 intel_ddi_setup_hw_pll_state(dev);
9375
Daniel Vetter24929352012-07-02 20:28:59 +02009376 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9377 base.head) {
9378 pipe = 0;
9379
9380 if (encoder->get_hw_state(encoder, &pipe)) {
9381 encoder->base.crtc =
9382 dev_priv->pipe_to_crtc_mapping[pipe];
9383 } else {
9384 encoder->base.crtc = NULL;
9385 }
9386
9387 encoder->connectors_active = false;
9388 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9389 encoder->base.base.id,
9390 drm_get_encoder_name(&encoder->base),
9391 encoder->base.crtc ? "enabled" : "disabled",
9392 pipe);
9393 }
9394
9395 list_for_each_entry(connector, &dev->mode_config.connector_list,
9396 base.head) {
9397 if (connector->get_hw_state(connector)) {
9398 connector->base.dpms = DRM_MODE_DPMS_ON;
9399 connector->encoder->connectors_active = true;
9400 connector->base.encoder = &connector->encoder->base;
9401 } else {
9402 connector->base.dpms = DRM_MODE_DPMS_OFF;
9403 connector->base.encoder = NULL;
9404 }
9405 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9406 connector->base.base.id,
9407 drm_get_connector_name(&connector->base),
9408 connector->base.encoder ? "enabled" : "disabled");
9409 }
9410
9411 /* HW state is read out, now we need to sanitize this mess. */
9412 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9413 base.head) {
9414 intel_sanitize_encoder(encoder);
9415 }
9416
9417 for_each_pipe(pipe) {
9418 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9419 intel_sanitize_crtc(crtc);
9420 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009421
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009422 if (force_restore) {
Daniel Vetterf30da182013-04-11 20:22:50 +02009423 /*
9424 * We need to use raw interfaces for restoring state to avoid
9425 * checking (bogus) intermediate states.
9426 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009427 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -07009428 struct drm_crtc *crtc =
9429 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +02009430
9431 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
9432 crtc->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009433 }
Jesse Barnesb5644d02013-03-26 13:25:27 -07009434 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
9435 intel_plane_restore(plane);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009436
9437 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009438 } else {
9439 intel_modeset_update_staged_output_state(dev);
9440 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009441
9442 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +02009443
9444 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009445}
9446
9447void intel_modeset_gem_init(struct drm_device *dev)
9448{
Chris Wilson1833b132012-05-09 11:56:28 +01009449 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02009450
9451 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +02009452
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009453 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08009454}
9455
9456void intel_modeset_cleanup(struct drm_device *dev)
9457{
Jesse Barnes652c3932009-08-17 13:31:43 -07009458 struct drm_i915_private *dev_priv = dev->dev_private;
9459 struct drm_crtc *crtc;
9460 struct intel_crtc *intel_crtc;
9461
Daniel Vetterfd0c0642013-04-24 11:13:35 +02009462 /*
9463 * Interrupts and polling as the first thing to avoid creating havoc.
9464 * Too much stuff here (turning of rps, connectors, ...) would
9465 * experience fancy races otherwise.
9466 */
9467 drm_irq_uninstall(dev);
9468 cancel_work_sync(&dev_priv->hotplug_work);
9469 /*
9470 * Due to the hpd irq storm handling the hotplug work can re-arm the
9471 * poll handlers. Hence disable polling after hpd handling is shut down.
9472 */
Keith Packardf87ea762010-10-03 19:36:26 -07009473 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +02009474
Jesse Barnes652c3932009-08-17 13:31:43 -07009475 mutex_lock(&dev->struct_mutex);
9476
Jesse Barnes723bfd72010-10-07 16:01:13 -07009477 intel_unregister_dsm_handler();
9478
Jesse Barnes652c3932009-08-17 13:31:43 -07009479 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9480 /* Skip inactive CRTCs */
9481 if (!crtc->fb)
9482 continue;
9483
9484 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02009485 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009486 }
9487
Chris Wilson973d04f2011-07-08 12:22:37 +01009488 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07009489
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009490 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00009491
Daniel Vetter930ebb42012-06-29 23:32:16 +02009492 ironlake_teardown_rc6(dev);
9493
Kristian Høgsberg69341a52009-11-11 12:19:17 -05009494 mutex_unlock(&dev->struct_mutex);
9495
Chris Wilson1630fe72011-07-08 12:22:42 +01009496 /* flush any delayed tasks or pending work */
9497 flush_scheduled_work();
9498
Jani Nikuladc652f92013-04-12 15:18:38 +03009499 /* destroy backlight, if any, before the connectors */
9500 intel_panel_destroy_backlight(dev);
9501
Jesse Barnes79e53942008-11-07 14:24:08 -08009502 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +01009503
9504 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009505}
9506
Dave Airlie28d52042009-09-21 14:33:58 +10009507/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08009508 * Return which encoder is currently attached for connector.
9509 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01009510struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08009511{
Chris Wilsondf0e9242010-09-09 16:20:55 +01009512 return &intel_attached_encoder(connector)->base;
9513}
Jesse Barnes79e53942008-11-07 14:24:08 -08009514
Chris Wilsondf0e9242010-09-09 16:20:55 +01009515void intel_connector_attach_encoder(struct intel_connector *connector,
9516 struct intel_encoder *encoder)
9517{
9518 connector->encoder = encoder;
9519 drm_mode_connector_attach_encoder(&connector->base,
9520 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009521}
Dave Airlie28d52042009-09-21 14:33:58 +10009522
9523/*
9524 * set vga decode state - true == enable VGA decode
9525 */
9526int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9527{
9528 struct drm_i915_private *dev_priv = dev->dev_private;
9529 u16 gmch_ctrl;
9530
9531 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9532 if (state)
9533 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9534 else
9535 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9536 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9537 return 0;
9538}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009539
9540#ifdef CONFIG_DEBUG_FS
9541#include <linux/seq_file.h>
9542
9543struct intel_display_error_state {
9544 struct intel_cursor_error_state {
9545 u32 control;
9546 u32 position;
9547 u32 base;
9548 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +01009549 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009550
9551 struct intel_pipe_error_state {
9552 u32 conf;
9553 u32 source;
9554
9555 u32 htotal;
9556 u32 hblank;
9557 u32 hsync;
9558 u32 vtotal;
9559 u32 vblank;
9560 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +01009561 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009562
9563 struct intel_plane_error_state {
9564 u32 control;
9565 u32 stride;
9566 u32 size;
9567 u32 pos;
9568 u32 addr;
9569 u32 surface;
9570 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +01009571 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009572};
9573
9574struct intel_display_error_state *
9575intel_display_capture_error_state(struct drm_device *dev)
9576{
Akshay Joshi0206e352011-08-16 15:34:10 -04009577 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009578 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009579 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009580 int i;
9581
9582 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9583 if (error == NULL)
9584 return NULL;
9585
Damien Lespiau52331302012-08-15 19:23:25 +01009586 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009587 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
9588
Paulo Zanonia18c4c32013-03-06 20:03:12 -03009589 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
9590 error->cursor[i].control = I915_READ(CURCNTR(i));
9591 error->cursor[i].position = I915_READ(CURPOS(i));
9592 error->cursor[i].base = I915_READ(CURBASE(i));
9593 } else {
9594 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
9595 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
9596 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
9597 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009598
9599 error->plane[i].control = I915_READ(DSPCNTR(i));
9600 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009601 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -03009602 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009603 error->plane[i].pos = I915_READ(DSPPOS(i));
9604 }
Paulo Zanonica291362013-03-06 20:03:14 -03009605 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
9606 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009607 if (INTEL_INFO(dev)->gen >= 4) {
9608 error->plane[i].surface = I915_READ(DSPSURF(i));
9609 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9610 }
9611
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009612 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009613 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009614 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
9615 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
9616 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
9617 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
9618 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
9619 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009620 }
9621
9622 return error;
9623}
9624
9625void
9626intel_display_print_error_state(struct seq_file *m,
9627 struct drm_device *dev,
9628 struct intel_display_error_state *error)
9629{
9630 int i;
9631
Ben Widawsky7eb552a2013-03-13 14:05:41 -07009632 seq_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Damien Lespiau52331302012-08-15 19:23:25 +01009633 for_each_pipe(i) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009634 seq_printf(m, "Pipe [%d]:\n", i);
9635 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9636 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
9637 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9638 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9639 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9640 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9641 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9642 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9643
9644 seq_printf(m, "Plane [%d]:\n", i);
9645 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9646 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009647 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -03009648 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
Paulo Zanoni80ca3782013-03-22 14:20:57 -03009649 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9650 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -03009651 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Paulo Zanonica291362013-03-06 20:03:14 -03009652 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009653 if (INTEL_INFO(dev)->gen >= 4) {
9654 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9655 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9656 }
9657
9658 seq_printf(m, "Cursor [%d]:\n", i);
9659 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9660 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9661 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9662 }
9663}
9664#endif